forked from lix-project/lix
Fix localhost::addToStore(...Path...)
We were calculating the nar hash wrong when the file ingestion method was flat. I don't think there's anything we can do in that case but dump the file again, so that's what I do. As an optomization, we again could reuse the original dump for just the recursive and non-sha256 case, but I rather do that after this fix, and after my other PRs which deduplicate this code.
This commit is contained in:
parent
926c3a6664
commit
8173e7bfef
|
@ -1097,15 +1097,8 @@ StorePath LocalStore::addToStore(const string & name, const Path & _srcPath,
|
||||||
{
|
{
|
||||||
Path srcPath(absPath(_srcPath));
|
Path srcPath(absPath(_srcPath));
|
||||||
|
|
||||||
/* For computing the NAR hash. */
|
/* For computing the store path. */
|
||||||
auto sha256Sink = std::make_unique<HashSink>(htSHA256);
|
auto hashSink = std::make_unique<HashSink>(hashAlgo);
|
||||||
|
|
||||||
/* 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);
|
|
||||||
|
|
||||||
/* Read the source path into memory, but only if it's up to
|
/* Read the source path into memory, but only if it's up to
|
||||||
narBufferSize bytes. If it's larger, write it to a temporary
|
narBufferSize bytes. If it's larger, write it to a temporary
|
||||||
|
@ -1114,13 +1107,12 @@ StorePath LocalStore::addToStore(const string & name, const Path & _srcPath,
|
||||||
temporary path. Otherwise, we move it to the destination store
|
temporary path. Otherwise, we move it to the destination store
|
||||||
path. */
|
path. */
|
||||||
bool inMemory = true;
|
bool inMemory = true;
|
||||||
std::string nar;
|
std::string nar; // TODO rename from "nar" to "dump"
|
||||||
|
|
||||||
auto source = sinkToSource([&](Sink & sink) {
|
auto source = sinkToSource([&](Sink & sink) {
|
||||||
|
|
||||||
LambdaSink sink2([&](const unsigned char * buf, size_t len) {
|
LambdaSink sink2([&](const unsigned char * buf, size_t len) {
|
||||||
(*sha256Sink)(buf, len);
|
(*hashSink)(buf, len);
|
||||||
if (hashSink) (*hashSink)(buf, len);
|
|
||||||
|
|
||||||
if (inMemory) {
|
if (inMemory) {
|
||||||
if (nar.size() + len > settings.narBufferSize) {
|
if (nar.size() + len > settings.narBufferSize) {
|
||||||
|
@ -1165,9 +1157,7 @@ StorePath LocalStore::addToStore(const string & name, const Path & _srcPath,
|
||||||
/* The NAR fits in memory, so we didn't do restorePath(). */
|
/* The NAR fits in memory, so we didn't do restorePath(). */
|
||||||
}
|
}
|
||||||
|
|
||||||
auto sha256 = sha256Sink->finish();
|
auto [hash, size] = hashSink->finish();
|
||||||
|
|
||||||
Hash hash = hashSink ? hashSink->finish().first : sha256.first;
|
|
||||||
|
|
||||||
auto dstPath = makeFixedOutputPath(method, hash, name);
|
auto dstPath = makeFixedOutputPath(method, hash, name);
|
||||||
|
|
||||||
|
@ -1201,13 +1191,22 @@ StorePath LocalStore::addToStore(const string & name, const Path & _srcPath,
|
||||||
throw Error("renaming '%s' to '%s'", tempPath, realPath);
|
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
|
canonicalisePathMetaData(realPath, -1); // FIXME: merge into restorePath
|
||||||
|
|
||||||
optimisePath(realPath);
|
optimisePath(realPath);
|
||||||
|
|
||||||
ValidPathInfo info(dstPath);
|
ValidPathInfo info(dstPath);
|
||||||
info.narHash = sha256.first;
|
info.narHash = narHash.first;
|
||||||
info.narSize = sha256.second;
|
info.narSize = narHash.second;
|
||||||
info.ca = FixedOutputHash { .method = method, .hash = hash };
|
info.ca = FixedOutputHash { .method = method, .hash = hash };
|
||||||
registerValidPath(info);
|
registerValidPath(info);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue