Return narHash when available

This commit is contained in:
Eelco Dolstra 2022-06-01 14:20:38 +02:00
parent b01ee2a93d
commit 6ab3b86cf5
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
3 changed files with 17 additions and 5 deletions

View file

@ -144,13 +144,20 @@ std::pair<Tree, Input> Input::fetch(ref<Store> store) const
.storePath = storePath,
};
auto narHash = store->queryPathInfo(tree.storePath)->narHash;
checkLocked(*store, storePath, input);
return {std::move(tree), input};
}
void Input::checkLocked(Store & store, const StorePath & storePath, Input & input) const
{
auto narHash = store.queryPathInfo(storePath)->narHash;
input.attrs.insert_or_assign("narHash", narHash.to_string(SRI, true));
if (auto prevNarHash = getNarHash()) {
if (narHash != *prevNarHash)
throw Error((unsigned int) 102, "NAR hash mismatch in input '%s' (%s), expected '%s', got '%s'",
to_string(), tree.actualPath, prevNarHash->to_string(SRI, true), narHash.to_string(SRI, true));
to_string(), store.printStorePath(storePath), prevNarHash->to_string(SRI, true), narHash.to_string(SRI, true));
}
if (auto prevLastModified = getLastModified()) {
@ -168,8 +175,6 @@ std::pair<Tree, Input> Input::fetch(ref<Store> store) const
input.locked = true;
assert(input.hasAllInfo());
return {std::move(tree), input};
}
std::pair<ref<InputAccessor>, Input> Input::lazyFetch(ref<Store> store) const
@ -328,6 +333,8 @@ std::pair<ref<InputAccessor>, Input> InputScheme::lazyFetch(ref<Store> store, co
{
auto [storePath, input2] = fetch(store, input);
input.checkLocked(*store, storePath, input2);
return {makeFSInputAccessor(CanonPath(store->toRealPath(storePath))), input2};
}

View file

@ -101,6 +101,10 @@ public:
std::optional<Hash> getRev() const;
std::optional<uint64_t> getRevCount() const;
std::optional<time_t> getLastModified() const;
private:
void checkLocked(Store & store, const StorePath & storePath, Input & input) const;
};
/* The InputScheme represents a type of fetcher. Each fetcher

View file

@ -223,7 +223,8 @@ struct CurlInputScheme : InputScheme
ParsedURL toURL(const Input & input) override
{
auto url = parseURL(getStrAttr(input.attrs, "url"));
// NAR hashes are preferred over file hashes since tar/zip files // don't have a canonical representation.
// NAR hashes are preferred over file hashes since tar/zip
// files don't have a canonical representation.
if (auto narHash = input.getNarHash())
url.query.insert_or_assign("narHash", narHash->to_string(SRI, true));
return url;