2018-03-29 22:56:13 +00:00
|
|
|
#include "command.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "references.hh"
|
2019-12-18 16:39:02 +00:00
|
|
|
#include "common-args.hh"
|
|
|
|
#include "json.hh"
|
2018-03-29 22:56:13 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2019-12-18 16:39:02 +00:00
|
|
|
struct CmdMakeContentAddressable : StorePathsCommand, MixJSON
|
2018-03-29 22:56:13 +00:00
|
|
|
{
|
|
|
|
CmdMakeContentAddressable()
|
|
|
|
{
|
|
|
|
realiseMode = Build;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
2019-10-21 15:58:17 +00:00
|
|
|
return "rewrite a path or closure to content-addressable form";
|
2018-03-29 22:56:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:58:17 +00:00
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
|
|
|
"To create a content-addressable representation of GNU Hello (but not its dependencies):",
|
|
|
|
"nix make-content-addressable nixpkgs.hello"
|
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To compute a content-addressable representation of the current NixOS system closure:",
|
|
|
|
"nix make-content-addressable -r /run/current-system"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-05-05 13:18:23 +00:00
|
|
|
|
|
|
|
Category category() override { return catUtility; }
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2018-03-29 22:56:13 +00:00
|
|
|
{
|
2020-06-16 20:20:18 +00:00
|
|
|
auto paths = store->topoSortPaths(StorePathSet(storePaths.begin(), storePaths.end()));
|
2018-03-29 22:56:13 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
std::reverse(paths.begin(), paths.end());
|
2018-03-29 22:56:13 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
std::map<StorePath, StorePath> remappings;
|
2018-03-29 22:56:13 +00:00
|
|
|
|
2019-12-18 16:39:02 +00:00
|
|
|
auto jsonRoot = json ? std::make_unique<JSONObject>(std::cout) : nullptr;
|
|
|
|
auto jsonRewrites = json ? std::make_unique<JSONObject>(jsonRoot->object("rewrites")) : nullptr;
|
|
|
|
|
2018-03-29 22:56:13 +00:00
|
|
|
for (auto & path : paths) {
|
2019-12-05 18:11:09 +00:00
|
|
|
auto pathS = store->printStorePath(path);
|
2018-03-29 22:56:13 +00:00
|
|
|
auto oldInfo = store->queryPathInfo(path);
|
2020-06-16 12:16:39 +00:00
|
|
|
std::string oldHashPart(path.hashPart());
|
2018-03-29 22:56:13 +00:00
|
|
|
|
|
|
|
StringSink sink;
|
|
|
|
store->narFromPath(path, sink);
|
|
|
|
|
|
|
|
StringMap rewrites;
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet references;
|
|
|
|
bool hasSelfReference = false;
|
2018-03-29 22:56:13 +00:00
|
|
|
for (auto & ref : oldInfo->references) {
|
|
|
|
if (ref == path)
|
2019-12-05 18:11:09 +00:00
|
|
|
hasSelfReference = true;
|
2018-03-29 22:56:13 +00:00
|
|
|
else {
|
2019-12-05 18:11:09 +00:00
|
|
|
auto i = remappings.find(ref);
|
2020-06-16 20:20:18 +00:00
|
|
|
auto replacement = i != remappings.end() ? i->second : ref;
|
2018-03-29 22:56:13 +00:00
|
|
|
// FIXME: warn about unremapped paths?
|
|
|
|
if (replacement != ref)
|
2019-12-05 18:11:09 +00:00
|
|
|
rewrites.insert_or_assign(store->printStorePath(ref), store->printStorePath(replacement));
|
|
|
|
references.insert(std::move(replacement));
|
2018-03-29 22:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*sink.s = rewriteStrings(*sink.s, rewrites);
|
|
|
|
|
|
|
|
HashModuloSink hashModuloSink(htSHA256, oldHashPart);
|
|
|
|
hashModuloSink((unsigned char *) sink.s->data(), sink.s->size());
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto narHash = hashModuloSink.finish().first;
|
|
|
|
|
2020-03-29 05:04:55 +00:00
|
|
|
ValidPathInfo info(store->makeFixedOutputPath(FileIngestionMethod::Recursive, narHash, path.name(), references, hasSelfReference));
|
2019-12-05 18:11:09 +00:00
|
|
|
info.references = std::move(references);
|
2020-06-16 20:20:18 +00:00
|
|
|
if (hasSelfReference) info.references.insert(info.path);
|
2019-12-05 18:11:09 +00:00
|
|
|
info.narHash = narHash;
|
2018-03-29 22:56:13 +00:00
|
|
|
info.narSize = sink.s->size();
|
2020-06-19 15:18:19 +00:00
|
|
|
info.ca = FixedOutputHash {
|
2020-06-19 17:42:56 +00:00
|
|
|
.method = FileIngestionMethod::Recursive,
|
2020-06-23 17:03:10 +00:00
|
|
|
.hash = *info.narHash,
|
2020-06-02 00:37:43 +00:00
|
|
|
};
|
2018-03-29 22:56:13 +00:00
|
|
|
|
2019-12-18 16:39:02 +00:00
|
|
|
if (!json)
|
2020-05-13 15:52:36 +00:00
|
|
|
printInfo("rewrote '%s' to '%s'", pathS, store->printStorePath(info.path));
|
2018-03-29 22:56:13 +00:00
|
|
|
|
|
|
|
auto source = sinkToSource([&](Sink & nextSink) {
|
2020-06-16 12:16:39 +00:00
|
|
|
RewritingSink rsink2(oldHashPart, std::string(info.path.hashPart()), nextSink);
|
2018-03-29 22:56:13 +00:00
|
|
|
rsink2((unsigned char *) sink.s->data(), sink.s->size());
|
|
|
|
rsink2.flush();
|
|
|
|
});
|
|
|
|
|
|
|
|
store->addToStore(info, *source);
|
|
|
|
|
2019-12-18 16:39:02 +00:00
|
|
|
if (json)
|
|
|
|
jsonRewrites->attr(store->printStorePath(path), store->printStorePath(info.path));
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
remappings.insert_or_assign(std::move(path), std::move(info.path));
|
2018-03-29 22:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 14:01:35 +00:00
|
|
|
static auto r1 = registerCommand<CmdMakeContentAddressable>("make-content-addressable");
|