aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/local-store.cc
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-07-15 21:05:29 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-07-15 21:05:29 +0000
commitaff69e229cb584c852980738d7d46cdbdd9aaf59 (patch)
treea34960be129ea70e6da9fc2e77a6652a172d2854 /src/libstore/local-store.cc
parent592851fb67cd15807109d6f65fb81f6af89af966 (diff)
parent650c2c655810c375296b52997e2f85298c7c566a (diff)
Merge remote-tracking branch 'obsidian/fix-add-to-star-narhash' into from-dump-stream
Diffstat (limited to 'src/libstore/local-store.cc')
-rw-r--r--src/libstore/local-store.cc43
1 files changed, 21 insertions, 22 deletions
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc
index dac7a50c4..b9fae6089 100644
--- a/src/libstore/local-store.cc
+++ b/src/libstore/local-store.cc
@@ -1066,15 +1066,8 @@ StorePath LocalStore::addToStoreCommon(
const string & name, FileIngestionMethod method, HashType hashAlgo, RepairFlag repair,
std::function<void(Sink &, size_t &)> demux)
{
- /* For computing the NAR hash. */
- auto sha256Sink = std::make_unique<HashSink>(htSHA256);
-
- /* For computing the store path. In recursive SHA-256 mode, this
- is the same as the NAR hash, so no need to do it again. */
- std::unique_ptr<HashSink> hashSink =
- method == FileIngestionMethod::Recursive && hashAlgo == htSHA256
- ? nullptr
- : std::make_unique<HashSink>(hashAlgo);
+ /* For computing the store path. */
+ auto hashSink = std::make_unique<HashSink>(hashAlgo);
/* Read the source path into memory, but only if it's up to
narBufferSize bytes. If it's larger, write it to a temporary
@@ -1083,21 +1076,20 @@ StorePath LocalStore::addToStoreCommon(
temporary path. Otherwise, we move it to the destination store
path. */
bool inMemory = true;
- std::string nar;
+ std::string dump;
auto source = sinkToSource([&](Sink & sink, size_t & wanted) {
LambdaSink sink2([&](const unsigned char * buf, size_t len) {
- (*sha256Sink)(buf, len);
- if (hashSink) (*hashSink)(buf, len);
+ (*hashSink)(buf, len);
if (inMemory) {
- if (nar.size() + len > settings.narBufferSize) {
+ if (dump.size() + len > settings.narBufferSize) {
inMemory = false;
sink << 1;
- sink((const unsigned char *) nar.data(), nar.size());
- nar.clear();
+ sink((const unsigned char *) dump.data(), dump.size());
+ dump.clear();
} else {
- nar.append((const char *) buf, len);
+ dump.append((const char *) buf, len);
}
}
@@ -1129,9 +1121,7 @@ StorePath LocalStore::addToStoreCommon(
/* The NAR fits in memory, so we didn't do restorePath(). */
}
- auto sha256 = sha256Sink->finish();
-
- Hash hash = hashSink ? hashSink->finish().first : sha256.first;
+ auto [hash, size] = hashSink->finish();
auto dstPath = makeFixedOutputPath(method, hash, name);
@@ -1154,7 +1144,7 @@ StorePath LocalStore::addToStoreCommon(
if (inMemory) {
/* Restore from the NAR in memory. */
- StringSource source(nar);
+ StringSource source(dump);
if (method == FileIngestionMethod::Recursive)
restorePath(realPath, source);
else
@@ -1165,13 +1155,22 @@ StorePath LocalStore::addToStoreCommon(
throw Error("renaming '%s' to '%s'", tempPath, realPath);
}
+ /* For computing the nar hash. In recursive SHA-256 mode, this
+ is the same as the store hash, so no need to do it again. */
+ auto narHash = std::pair { hash, size };
+ if (method != FileIngestionMethod::Recursive || hashAlgo != htSHA256) {
+ HashSink narSink { htSHA256 };
+ dumpPath(realPath, narSink);
+ narHash = narSink.finish();
+ }
+
canonicalisePathMetaData(realPath, -1); // FIXME: merge into restorePath
optimisePath(realPath);
ValidPathInfo info(dstPath);
- info.narHash = sha256.first;
- info.narSize = sha256.second;
+ info.narHash = narHash.first;
+ info.narSize = narHash.second;
info.ca = FixedOutputHash { .method = method, .hash = hash };
registerValidPath(info);
}