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()
|
|
|
|
{
|
2020-07-15 18:05:42 +00:00
|
|
|
realiseMode = Realise::Outputs;
|
2018-03-29 22:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
2020-12-10 16:36:59 +00:00
|
|
|
return "rewrite a path or closure to content-addressed form";
|
2018-03-29 22:56:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 16:36:59 +00:00
|
|
|
std::string doc() override
|
2019-10-21 15:58:17 +00:00
|
|
|
{
|
2020-12-10 16:36:59 +00:00
|
|
|
return
|
|
|
|
#include "make-content-addressable.md"
|
|
|
|
;
|
2019-10-21 15:58:17 +00:00
|
|
|
}
|
2020-05-05 13:18:23 +00:00
|
|
|
|
2021-09-27 08:53: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:20:05 +00:00
|
|
|
sink.s = rewriteStrings(sink.s, rewrites);
|
2018-03-29 22:56:13 +00:00
|
|
|
|
|
|
|
HashModuloSink hashModuloSink(htSHA256, oldHashPart);
|
2022-01-17 21:20:05 +00:00
|
|
|
hashModuloSink(sink.s);
|
2018-03-29 22:56:13 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto narHash = hashModuloSink.finish().first;
|
|
|
|
|
2020-08-06 18:31:48 +00:00
|
|
|
ValidPathInfo info {
|
|
|
|
store->makeFixedOutputPath(FileIngestionMethod::Recursive, narHash, path.name(), references, hasSelfReference),
|
|
|
|
narHash,
|
|
|
|
};
|
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);
|
2022-01-17 21:20:05 +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-08-05 18:42:48 +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-12-10 16:11:39 +00:00
|
|
|
notice("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);
|
2022-01-17 21:20:05 +00:00
|
|
|
rsink2(sink.s);
|
2018-03-29 22:56:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-24 18:38:56 +00:00
|
|
|
static auto rCmdMakeContentAddressable = registerCommand2<CmdMakeContentAddressable>({"store", "make-content-addressable"});
|