2018-03-30 01:56:13 +03:00
|
|
|
#include "command.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "references.hh"
|
2019-12-18 18:39:02 +02:00
|
|
|
#include "common-args.hh"
|
|
|
|
#include "json.hh"
|
2018-03-30 01:56:13 +03:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2019-12-18 18:39:02 +02:00
|
|
|
struct CmdMakeContentAddressable : StorePathsCommand, MixJSON
|
2018-03-30 01:56:13 +03:00
|
|
|
{
|
|
|
|
CmdMakeContentAddressable()
|
|
|
|
{
|
|
|
|
realiseMode = Build;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
2019-10-21 18:58:17 +03:00
|
|
|
return "rewrite a path or closure to content-addressable form";
|
2018-03-30 01:56:13 +03:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:58:17 +03: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"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2019-12-05 20:11:09 +02:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2018-03-30 01:56:13 +03:00
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
auto paths = store->topoSortPaths(storePathsToSet(storePaths));
|
2018-03-30 01:56:13 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
std::reverse(paths.begin(), paths.end());
|
2018-03-30 01:56:13 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
std::map<StorePath, StorePath> remappings;
|
2018-03-30 01:56:13 +03:00
|
|
|
|
2019-12-18 18:39:02 +02:00
|
|
|
auto jsonRoot = json ? std::make_unique<JSONObject>(std::cout) : nullptr;
|
|
|
|
auto jsonRewrites = json ? std::make_unique<JSONObject>(jsonRoot->object("rewrites")) : nullptr;
|
|
|
|
|
2018-03-30 01:56:13 +03:00
|
|
|
for (auto & path : paths) {
|
2019-12-05 20:11:09 +02:00
|
|
|
auto pathS = store->printStorePath(path);
|
2018-03-30 01:56:13 +03:00
|
|
|
auto oldInfo = store->queryPathInfo(path);
|
2019-12-05 20:11:09 +02:00
|
|
|
auto oldHashPart = storePathToHash(pathS);
|
2018-03-30 01:56:13 +03:00
|
|
|
|
|
|
|
StringSink sink;
|
|
|
|
store->narFromPath(path, sink);
|
|
|
|
|
|
|
|
StringMap rewrites;
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
StorePathSet references;
|
|
|
|
bool hasSelfReference = false;
|
2018-03-30 01:56:13 +03:00
|
|
|
for (auto & ref : oldInfo->references) {
|
|
|
|
if (ref == path)
|
2019-12-05 20:11:09 +02:00
|
|
|
hasSelfReference = true;
|
2018-03-30 01:56:13 +03:00
|
|
|
else {
|
2019-12-05 20:11:09 +02:00
|
|
|
auto i = remappings.find(ref);
|
|
|
|
auto replacement = i != remappings.end() ? i->second.clone() : ref.clone();
|
2018-03-30 01:56:13 +03:00
|
|
|
// FIXME: warn about unremapped paths?
|
|
|
|
if (replacement != ref)
|
2019-12-05 20:11:09 +02:00
|
|
|
rewrites.insert_or_assign(store->printStorePath(ref), store->printStorePath(replacement));
|
|
|
|
references.insert(std::move(replacement));
|
2018-03-30 01:56:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*sink.s = rewriteStrings(*sink.s, rewrites);
|
|
|
|
|
|
|
|
HashModuloSink hashModuloSink(htSHA256, oldHashPart);
|
|
|
|
hashModuloSink((unsigned char *) sink.s->data(), sink.s->size());
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
auto narHash = hashModuloSink.finish().first;
|
|
|
|
|
|
|
|
ValidPathInfo info(store->makeFixedOutputPath(true, narHash, path.name(), references, hasSelfReference));
|
|
|
|
info.references = std::move(references);
|
|
|
|
if (hasSelfReference) info.references.insert(info.path.clone());
|
|
|
|
info.narHash = narHash;
|
2018-03-30 01:56:13 +03:00
|
|
|
info.narSize = sink.s->size();
|
|
|
|
info.ca = makeFixedOutputCA(true, info.narHash);
|
|
|
|
|
2019-12-18 18:39:02 +02:00
|
|
|
if (!json)
|
|
|
|
printError("rewrote '%s' to '%s'", pathS, store->printStorePath(info.path));
|
2018-03-30 01:56:13 +03:00
|
|
|
|
|
|
|
auto source = sinkToSource([&](Sink & nextSink) {
|
2019-12-05 20:11:09 +02:00
|
|
|
RewritingSink rsink2(oldHashPart, storePathToHash(store->printStorePath(info.path)), nextSink);
|
2018-03-30 01:56:13 +03:00
|
|
|
rsink2((unsigned char *) sink.s->data(), sink.s->size());
|
|
|
|
rsink2.flush();
|
|
|
|
});
|
|
|
|
|
|
|
|
store->addToStore(info, *source);
|
|
|
|
|
2019-12-18 18:39:02 +02:00
|
|
|
if (json)
|
|
|
|
jsonRewrites->attr(store->printStorePath(path), store->printStorePath(info.path));
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
remappings.insert_or_assign(std::move(path), std::move(info.path));
|
2018-03-30 01:56:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 17:01:35 +03:00
|
|
|
static auto r1 = registerCommand<CmdMakeContentAddressable>("make-content-addressable");
|