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