Skip to content

Commit

Permalink
Normalize the hash-rewriting process when building derivations
Browse files Browse the repository at this point in the history
  • Loading branch information
thufschmitt committed Mar 17, 2023
1 parent c9d77b0 commit a5c970a
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/libstore/build/local-derivation-goal.cc
Expand Up @@ -2361,14 +2361,14 @@ DrvOutputs LocalDerivationGoal::registerOutputs()
continue;
auto references = *referencesOpt;

auto rewriteOutput = [&]() {
auto rewriteOutput = [&](const StringMap & rewrites) {
/* Apply hash rewriting if necessary. */
if (!outputRewrites.empty()) {
if (!rewrites.empty()) {
debug("rewriting hashes in '%1%'; cross fingers", actualPath);

/* FIXME: Is this actually streaming? */
auto source = sinkToSource([&](Sink & nextSink) {
RewritingSink rsink(outputRewrites, nextSink);
RewritingSink rsink(rewrites, nextSink);
dumpPath(actualPath, rsink);
rsink.flush();
});
Expand Down Expand Up @@ -2423,7 +2423,7 @@ DrvOutputs LocalDerivationGoal::registerOutputs()
"since recursive hashing is not enabled (outputHashMode=flat)",
actualPath);
}
rewriteOutput();
rewriteOutput(outputRewrites);
/* FIXME optimize and deduplicate with addToStore */
std::string oldHashPart { scratchPath->hashPart() };
HashModuloSink caSink { outputHash.hashType, oldHashPart };
Expand All @@ -2445,16 +2445,14 @@ DrvOutputs LocalDerivationGoal::registerOutputs()
refs.second,
refs.first);
if (*scratchPath != finalPath) {
// Also rewrite the output path
auto source = sinkToSource([&](Sink & nextSink) {
RewritingSink rsink2(oldHashPart, std::string(finalPath.hashPart()), nextSink);
dumpPath(actualPath, rsink2);
rsink2.flush();
});
Path tmpPath = actualPath + ".tmp";
restorePath(tmpPath, *source);
deletePath(actualPath);
movePath(tmpPath, actualPath);
// If the path has some self-references, we need to rewrite
// them.
// (note that this doesn't invalidate the ca hash we calculated
// above because it's computed *modulo the self-references*, so
// it already takes this rewrite into account).
rewriteOutput(
StringMap{{oldHashPart,
std::string(finalPath.hashPart())}});
}

HashResult narHashAndSize = hashPath(htSHA256, actualPath);
Expand Down Expand Up @@ -2487,7 +2485,7 @@ DrvOutputs LocalDerivationGoal::registerOutputs()
outputRewrites.insert_or_assign(
std::string { scratchPath->hashPart() },
std::string { requiredFinalPath.hashPart() });
rewriteOutput();
rewriteOutput(outputRewrites);
auto narHashAndSize = hashPath(htSHA256, actualPath);
ValidPathInfo newInfo0 { requiredFinalPath, narHashAndSize.first };
newInfo0.narSize = narHashAndSize.second;
Expand Down

0 comments on commit a5c970a

Please sign in to comment.