From b2905dc08e87bfb9b3d5f238ba731d958d9b0cbd Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 16 Jun 2025 16:05:08 +0200 Subject: [PATCH] fetchToStore(): Address a FIXME --- src/libfetchers/fetch-to-store.cc | 51 +++++++++++++++++++------------ 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/libfetchers/fetch-to-store.cc b/src/libfetchers/fetch-to-store.cc index 31de2b1e1..877e49c14 100644 --- a/src/libfetchers/fetch-to-store.cc +++ b/src/libfetchers/fetch-to-store.cc @@ -64,26 +64,37 @@ std::pair fetchToStore2( auto filter2 = filter ? *filter : defaultPathFilter; - if (mode == FetchMode::DryRun) { - auto [storePath, hash] = store.computeStorePath( - name, path, method, HashAlgorithm::SHA256, {}, filter2); - debug("hashed '%s' to '%s'", path, store.printStorePath(storePath)); - if (cacheKey) - fetchers::getCache()->upsert(*cacheKey, {{"hash", hash.to_string(HashFormat::SRI, true)}}); - return {storePath, hash}; - } else { - auto storePath = store.addToStore( - name, path, method, HashAlgorithm::SHA256, {}, filter2, repair); - debug("copied '%s' to '%s'", path, store.printStorePath(storePath)); - // FIXME: this is the wrong hash when method != - // ContentAddressMethod::Raw::NixArchive. Doesn't matter at - // the moment since the only place where that's the case - // doesn't use the hash. - auto hash = store.queryPathInfo(storePath)->narHash; - if (cacheKey) - fetchers::getCache()->upsert(*cacheKey, {{"hash", hash.to_string(HashFormat::SRI, true)}}); - return {storePath, hash}; - } + auto [storePath, hash] = + mode == FetchMode::DryRun + ? ({ + auto [storePath, hash] = store.computeStorePath( + name, path, method, HashAlgorithm::SHA256, {}, filter2); + debug("hashed '%s' to '%s' (hash '%s')", path, store.printStorePath(storePath), hash.to_string(HashFormat::SRI, true)); + std::make_pair(storePath, hash); + }) + : ({ + // FIXME: ideally addToStore() would return the hash + // right away (like computeStorePath()). + auto storePath = store.addToStore( + name, path, method, HashAlgorithm::SHA256, {}, filter2, repair); + auto info = store.queryPathInfo(storePath); + assert(info->references.empty()); + auto hash = + method == ContentAddressMethod::Raw::NixArchive + ? info->narHash + : ({ + if (!info->ca || info->ca->method != method) + throw Error("path '%s' lacks a CA field", store.printStorePath(storePath)); + info->ca->hash; + }); + debug("copied '%s' to '%s' (hash '%s')", path, store.printStorePath(storePath), hash.to_string(HashFormat::SRI, true)); + std::make_pair(storePath, hash); + }); + + if (cacheKey) + fetchers::getCache()->upsert(*cacheKey, {{"hash", hash.to_string(HashFormat::SRI, true)}}); + + return {storePath, hash}; } }