2022-03-22 22:14:58 +02: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 22:47:50 +02:00
|
|
|
StorePathSet closure;
|
|
|
|
srcStore.computeFSClosure(storePaths, closure);
|
2022-03-22 22:14:58 +02:00
|
|
|
|
2022-03-22 22:47:50 +02:00
|
|
|
auto paths = srcStore.topoSortPaths(closure);
|
2022-03-22 22:14:58 +02: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;
|
|
|
|
srcStore.narFromPath(path, sink);
|
|
|
|
|
|
|
|
StringMap rewrites;
|
|
|
|
|
2023-01-06 22:36:05 +02:00
|
|
|
StoreReferences refs;
|
2023-01-14 23:38:43 +02:00
|
|
|
for (auto & ref : oldInfo->references) {
|
|
|
|
if (ref == path)
|
|
|
|
refs.self = true;
|
2023-01-30 16:59:55 +02:00
|
|
|
else {
|
|
|
|
auto i = remappings.find(ref);
|
|
|
|
auto replacement = i != remappings.end() ? i->second : ref;
|
|
|
|
// FIXME: warn about unremapped paths?
|
2023-02-01 17:04:28 +02:00
|
|
|
if (replacement != ref)
|
2023-01-30 16:59:55 +02:00
|
|
|
rewrites.insert_or_assign(srcStore.printStorePath(ref), srcStore.printStorePath(replacement));
|
2023-02-01 17:04:28 +02:00
|
|
|
refs.others.insert(std::move(replacement));
|
2022-03-22 22:14:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sink.s = rewriteStrings(sink.s, rewrites);
|
|
|
|
|
2023-11-28 15:20:27 +02:00
|
|
|
HashModuloSink hashModuloSink(HashAlgorithm::SHA256, oldHashPart);
|
2022-03-22 22:14:58 +02:00
|
|
|
hashModuloSink(sink.s);
|
|
|
|
|
2022-03-22 22:47:50 +02:00
|
|
|
auto narModuloHash = hashModuloSink.finish().first;
|
2022-03-22 22:14:58 +02:00
|
|
|
|
2022-03-25 21:25:08 +02:00
|
|
|
ValidPathInfo info {
|
|
|
|
dstStore,
|
2023-01-23 19:58:11 +02:00
|
|
|
path.name(),
|
|
|
|
FixedOutputInfo {
|
2024-05-17 00:46:43 +03:00
|
|
|
.method = FileIngestionMethod::NixArchive,
|
2023-07-06 01:53:44 +03:00
|
|
|
.hash = narModuloHash,
|
2023-02-28 18:57:20 +02:00
|
|
|
.references = std::move(refs),
|
2022-03-25 21:25:08 +02:00
|
|
|
},
|
|
|
|
Hash::dummy,
|
|
|
|
};
|
2022-03-22 22:47:50 +02:00
|
|
|
|
2022-03-25 21:25:08 +02:00
|
|
|
printInfo("rewriting '%s' to '%s'", pathS, dstStore.printStorePath(info.path));
|
2022-03-22 22:47:50 +02:00
|
|
|
|
|
|
|
StringSink sink2;
|
2022-03-25 21:25:08 +02:00
|
|
|
RewritingSink rsink2(oldHashPart, std::string(info.path.hashPart()), sink2);
|
2022-03-22 22:47:50 +02:00
|
|
|
rsink2(sink.s);
|
|
|
|
rsink2.flush();
|
|
|
|
|
2023-11-28 15:20:27 +02:00
|
|
|
info.narHash = hashString(HashAlgorithm::SHA256, sink2.s);
|
2022-03-22 22:14:58 +02:00
|
|
|
info.narSize = sink.s.size();
|
|
|
|
|
2022-03-22 22:47:50 +02:00
|
|
|
StringSource source(sink2.s);
|
|
|
|
dstStore.addToStore(info, source);
|
2022-03-22 22:14:58 +02:00
|
|
|
|
|
|
|
remappings.insert_or_assign(std::move(path), std::move(info.path));
|
|
|
|
}
|
|
|
|
|
|
|
|
return remappings;
|
|
|
|
}
|
|
|
|
|
2023-06-05 12:37:41 +03: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 22:14:58 +02:00
|
|
|
}
|