Skip to content

Commit

Permalink
Make the Nar hash non modulo
Browse files Browse the repository at this point in the history
It makes much more sense to have the Nar hash be a plain straight hash
rather than a hash modulo
  • Loading branch information
thufschmitt committed May 25, 2021
1 parent af4ff64 commit 79ae9e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
38 changes: 20 additions & 18 deletions src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2354,32 +2354,19 @@ void LocalDerivationGoal::registerOutputs()
}
auto got = caSink.finish().first;
auto refs = rewriteRefs();
HashModuloSink narSink { htSHA256, oldHashPart };
dumpPath(actualPath, narSink);
auto narHashAndSize = narSink.finish();
ValidPathInfo newInfo0 {
worker.store.makeFixedOutputPath(

auto finalPath = worker.store.makeFixedOutputPath(
outputHash.method,
got,
outputPathName(drv->name, outputName),
refs.second,
refs.first),
narHashAndSize.first,
};
newInfo0.narSize = narHashAndSize.second;
newInfo0.ca = FixedOutputHash {
.method = outputHash.method,
.hash = got,
};
newInfo0.references = refs.second;
if (refs.first)
newInfo0.references.insert(newInfo0.path);
if (scratchPath != newInfo0.path) {
refs.first);
if (scratchPath != finalPath) {
// Also rewrite the output path
auto source = sinkToSource([&](Sink & nextSink) {
StringSink sink;
dumpPath(actualPath, sink);
RewritingSink rsink2(oldHashPart, std::string(newInfo0.path.hashPart()), nextSink);
RewritingSink rsink2(oldHashPart, std::string(finalPath.hashPart()), nextSink);
rsink2(*sink.s);
rsink2.flush();
});
Expand All @@ -2389,6 +2376,21 @@ void LocalDerivationGoal::registerOutputs()
movePath(tmpPath, actualPath);
}

HashResult narHashAndSize = hashPath(htSHA256, actualPath);
ValidPathInfo newInfo0 {
finalPath,
narHashAndSize.first,
};

newInfo0.narSize = narHashAndSize.second;
newInfo0.ca = FixedOutputHash {
.method = outputHash.method,
.hash = got,
};
newInfo0.references = refs.second;
if (refs.first)
newInfo0.references.insert(newInfo0.path);

assert(newInfo0.ca);
return newInfo0;
};
Expand Down
10 changes: 3 additions & 7 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1152,17 +1152,13 @@ void LocalStore::addToStore(const ValidPathInfo & info, Source & source,

/* While restoring the path from the NAR, compute the hash
of the NAR. */
std::unique_ptr<AbstractHashSink> hashSink;
if (!info.ca.has_value() || !info.references.count(info.path))
hashSink = std::make_unique<HashSink>(htSHA256);
else
hashSink = std::make_unique<HashModuloSink>(htSHA256, std::string(info.path.hashPart()));
HashSink hashSink(htSHA256);

TeeSource wrapperSource { source, *hashSink };
TeeSource wrapperSource { source, hashSink };

restorePath(realPath, wrapperSource);

auto hashResult = hashSink->finish();
auto hashResult = hashSink.finish();

if (hashResult.first != info.narHash)
throw Error("hash mismatch importing path '%s';\n specified: %s\n got: %s",
Expand Down

0 comments on commit 79ae9e4

Please sign in to comment.