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()
|
|
|
|
{
|
2020-07-15 21:05:42 +03:00
|
|
|
realiseMode = Realise::Outputs;
|
2018-03-30 01:56:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-05-05 16:18:23 +03:00
|
|
|
|
|
|
|
Category category() override { return catUtility; }
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2018-03-30 01:56:13 +03:00
|
|
|
{
|
2020-06-16 23:20:18 +03:00
|
|
|
auto paths = store->topoSortPaths(StorePathSet(storePaths.begin(), storePaths.end()));
|
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);
|
2020-06-16 15:16:39 +03:00
|
|
|
std::string oldHashPart(path.hashPart());
|
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);
|
2020-06-16 23:20:18 +03:00
|
|
|
auto replacement = i != remappings.end() ? i->second : ref;
|
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;
|
|
|
|
|
2020-03-29 08:04:55 +03:00
|
|
|
ValidPathInfo info(store->makeFixedOutputPath(FileIngestionMethod::Recursive, narHash, path.name(), references, hasSelfReference));
|
2019-12-05 20:11:09 +02:00
|
|
|
info.references = std::move(references);
|
2020-06-16 23:20:18 +03:00
|
|
|
if (hasSelfReference) info.references.insert(info.path);
|
2019-12-05 20:11:09 +02:00
|
|
|
info.narHash = narHash;
|
2018-03-30 01:56:13 +03:00
|
|
|
info.narSize = sink.s->size();
|
2020-06-19 18:18:19 +03:00
|
|
|
info.ca = FixedOutputHash {
|
2020-06-19 20:42:56 +03:00
|
|
|
.method = FileIngestionMethod::Recursive,
|
|
|
|
.hash = info.narHash,
|
2020-06-02 03:37:43 +03:00
|
|
|
};
|
2018-03-30 01:56:13 +03:00
|
|
|
|
2019-12-18 18:39:02 +02:00
|
|
|
if (!json)
|
2020-05-13 18:52:36 +03:00
|
|
|
printInfo("rewrote '%s' to '%s'", pathS, store->printStorePath(info.path));
|
2018-03-30 01:56:13 +03:00
|
|
|
|
|
|
|
auto source = sinkToSource([&](Sink & nextSink) {
|
2020-06-16 15:16:39 +03:00
|
|
|
RewritingSink rsink2(oldHashPart, std::string(info.path.hashPart()), 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");
|