1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-24 11:19:35 +01:00

nix/prefetch: Be honest about when path name is derived from URL

Only add the message to trace when name is really derived from URL.
This commit is contained in:
Sergei Zimmerman 2025-11-24 00:25:48 +03:00
parent 2594e417b5
commit 28fac9fe4d
No known key found for this signature in database

View file

@ -59,7 +59,7 @@ std::string resolveMirrorUrl(EvalState & state, const std::string & url)
std::tuple<StorePath, Hash> prefetchFile( std::tuple<StorePath, Hash> prefetchFile(
ref<Store> store, ref<Store> store,
const VerbatimURL & url, const VerbatimURL & url,
std::optional<std::string> name, std::optional<std::string> maybeName,
HashAlgorithm hashAlgo, HashAlgorithm hashAlgo,
std::optional<Hash> expectedHash, std::optional<Hash> expectedHash,
bool unpack, bool unpack,
@ -68,16 +68,21 @@ std::tuple<StorePath, Hash> prefetchFile(
ContentAddressMethod method = ContentAddressMethod method =
unpack || executable ? ContentAddressMethod::Raw::NixArchive : ContentAddressMethod::Raw::Flat; unpack || executable ? ContentAddressMethod::Raw::NixArchive : ContentAddressMethod::Raw::Flat;
/* Figure out a name in the Nix store. */ std::string name = maybeName
if (!name) { .or_else([&]() {
name = url.lastPathSegment(); /* Figure out a name in the Nix store. */
if (!name || name->empty()) auto derivedFromUrl = url.lastPathSegment();
throw Error("cannot figure out file name for '%s'", url.to_string()); if (!derivedFromUrl || derivedFromUrl->empty())
} throw Error("cannot figure out file name for '%s'", url.to_string());
return derivedFromUrl;
})
.value();
try { try {
checkName(*name); checkName(name);
} catch (BadStorePathName & e) { } catch (BadStorePathName & e) {
e.addTrace({}, "file name '%s' was extracted from URL '%s'", *name, url.to_string()); if (!maybeName)
e.addTrace({}, "file name '%s' was extracted from URL '%s'", name, url.to_string());
throw; throw;
} }
@ -89,7 +94,7 @@ std::tuple<StorePath, Hash> prefetchFile(
if (expectedHash) { if (expectedHash) {
hashAlgo = expectedHash->algo; hashAlgo = expectedHash->algo;
storePath = storePath =
store->makeFixedOutputPathFromCA(*name, ContentAddressWithReferences::fromParts(method, *expectedHash, {})); store->makeFixedOutputPathFromCA(name, ContentAddressWithReferences::fromParts(method, *expectedHash, {}));
if (store->isValidPath(*storePath)) if (store->isValidPath(*storePath))
hash = expectedHash; hash = expectedHash;
else else
@ -138,7 +143,7 @@ std::tuple<StorePath, Hash> prefetchFile(
Activity act(*logger, lvlChatty, actUnknown, fmt("adding '%s' to the store", url.to_string())); Activity act(*logger, lvlChatty, actUnknown, fmt("adding '%s' to the store", url.to_string()));
auto info = store->addToStoreSlow(*name, makeFSSourceAccessor(tmpFile), method, hashAlgo, {}, expectedHash); auto info = store->addToStoreSlow(name, makeFSSourceAccessor(tmpFile), method, hashAlgo, {}, expectedHash);
storePath = info.path; storePath = info.path;
assert(info.ca); assert(info.ca);
hash = info.ca->hash; hash = info.ca->hash;