2022-03-22 20:14:58 +00:00
|
|
|
#include "make-content-addressed.hh"
|
|
|
|
#include "references.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
std::map<StorePath, StorePath> makeContentAddressed(
|
|
|
|
Store & srcStore,
|
|
|
|
Store & dstStore,
|
|
|
|
const StorePathSet & storePaths)
|
|
|
|
{
|
2022-03-22 20:47:50 +00:00
|
|
|
StorePathSet closure;
|
|
|
|
srcStore.computeFSClosure(storePaths, closure);
|
2022-03-22 20:14:58 +00:00
|
|
|
|
2022-03-22 20:47:50 +00:00
|
|
|
auto paths = srcStore.topoSortPaths(closure);
|
2022-03-22 20:14:58 +00:00
|
|
|
|
|
|
|
std::reverse(paths.begin(), paths.end());
|
|
|
|
|
|
|
|
std::map<StorePath, StorePath> remappings;
|
|
|
|
|
|
|
|
for (auto & path : paths) {
|
|
|
|
auto pathS = srcStore.printStorePath(path);
|
|
|
|
auto oldInfo = srcStore.queryPathInfo(path);
|
|
|
|
std::string oldHashPart(path.hashPart());
|
|
|
|
|
|
|
|
StringSink sink;
|
2024-05-16 15:58:27 +00:00
|
|
|
sink << srcStore.narFromPath(path);
|
2022-03-22 20:14:58 +00:00
|
|
|
|
|
|
|
StringMap rewrites;
|
|
|
|
|
2023-01-06 20:36:05 +00:00
|
|
|
StoreReferences refs;
|
2023-01-14 21:38:43 +00:00
|
|
|
for (auto & ref : oldInfo->references) {
|
|
|
|
if (ref == path)
|
|
|
|
refs.self = true;
|
2023-01-30 14:59:55 +00:00
|
|
|
else {
|
|
|
|
auto i = remappings.find(ref);
|
|
|
|
auto replacement = i != remappings.end() ? i->second : ref;
|
|
|
|
// FIXME: warn about unremapped paths?
|
2023-02-01 15:04:28 +00:00
|
|
|
if (replacement != ref)
|
2023-01-30 14:59:55 +00:00
|
|
|
rewrites.insert_or_assign(srcStore.printStorePath(ref), srcStore.printStorePath(replacement));
|
2023-02-01 15:04:28 +00:00
|
|
|
refs.others.insert(std::move(replacement));
|
2022-03-22 20:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sink.s = rewriteStrings(sink.s, rewrites);
|
|
|
|
|
2024-05-01 18:10:00 +00:00
|
|
|
auto narModuloHash = [&] {
|
|
|
|
StringSource source{sink.s};
|
|
|
|
return computeHashModulo(htSHA256, oldHashPart, source).first;
|
|
|
|
}();
|
2022-03-22 20:14:58 +00:00
|
|
|
|
2022-03-25 19:25:08 +00:00
|
|
|
ValidPathInfo info {
|
|
|
|
dstStore,
|
2023-01-23 17:58:11 +00:00
|
|
|
path.name(),
|
|
|
|
FixedOutputInfo {
|
2023-07-05 22:53:44 +00:00
|
|
|
.method = FileIngestionMethod::Recursive,
|
|
|
|
.hash = narModuloHash,
|
2023-02-28 16:57:20 +00:00
|
|
|
.references = std::move(refs),
|
2022-03-25 19:25:08 +00:00
|
|
|
},
|
|
|
|
Hash::dummy,
|
|
|
|
};
|
2022-03-22 20:47:50 +00:00
|
|
|
|
2022-03-25 19:25:08 +00:00
|
|
|
printInfo("rewriting '%s' to '%s'", pathS, dstStore.printStorePath(info.path));
|
2022-03-22 20:47:50 +00:00
|
|
|
|
2024-05-01 23:25:46 +00:00
|
|
|
const auto rewritten = rewriteStrings(sink.s, {{oldHashPart, std::string(info.path.hashPart())}});
|
2022-03-22 20:47:50 +00:00
|
|
|
|
2024-05-01 23:25:46 +00:00
|
|
|
info.narHash = hashString(htSHA256, rewritten);
|
2022-03-22 20:14:58 +00:00
|
|
|
info.narSize = sink.s.size();
|
|
|
|
|
2024-05-01 23:25:46 +00:00
|
|
|
StringSource source(rewritten);
|
2022-03-22 20:47:50 +00:00
|
|
|
dstStore.addToStore(info, source);
|
2022-03-22 20:14:58 +00:00
|
|
|
|
|
|
|
remappings.insert_or_assign(std::move(path), std::move(info.path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return remappings;
|
|
|
|
}
|
|
|
|
|
2023-06-05 09:37:41 +00:00
|
|
|
StorePath makeContentAddressed(
|
|
|
|
Store & srcStore,
|
|
|
|
Store & dstStore,
|
|
|
|
const StorePath & fromPath)
|
|
|
|
{
|
|
|
|
auto remappings = makeContentAddressed(srcStore, dstStore, StorePathSet { fromPath });
|
|
|
|
auto i = remappings.find(fromPath);
|
|
|
|
assert(i != remappings.end());
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
2022-03-22 20:14:58 +00:00
|
|
|
}
|