Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libfetchers/fetch-to-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ std::pair<StorePath, Hash> fetchToStore2(
auto hash = Hash::parseSRI(fetchers::getStrAttr(*res, "hash"));
auto storePath =
store.makeFixedOutputPathFromCA(name, ContentAddressWithReferences::fromParts(method, hash, {}));
if (mode == FetchMode::DryRun || store.isValidPath(storePath)) {
if (mode == FetchMode::DryRun || store.maybeQueryPathInfo(storePath)) {
debug(
"source path '%s' cache hit in '%s' (hash '%s')",
path,
Expand Down
13 changes: 11 additions & 2 deletions src/libstore/include/nix/store/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ public:
StorePath followLinksToStorePath(std::string_view path) const;

/**
* Check whether a path is valid.
* Check whether a path is valid. NOTE: this function does not
* generally cache whether a path is valid. You may want to use
* `maybeQueryPathInfo()`, which does cache.
*/
bool isValidPath(const StorePath & path);

Expand Down Expand Up @@ -322,10 +324,17 @@ public:

/**
* Query information about a valid path. It is permitted to omit
* the name part of the store path.
* the name part of the store path. Throws an exception if the
* path is not valid.
*/
ref<const ValidPathInfo> queryPathInfo(const StorePath & path);

/**
* Like `queryPathInfo()`, but returns `nullptr` if the path is
* not valid.
*/
std::shared_ptr<const ValidPathInfo> maybeQueryPathInfo(const StorePath & path);

/**
* Asynchronous version of queryPathInfo().
*/
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/local-fs-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct LocalStoreAccessor : PosixSourceAccessor
void requireStoreObject(const CanonPath & path)
{
auto [storePath, rest] = store->toStorePath(store->storeDir + path.abs());
if (requireValidPath && !store->isValidPath(storePath))
if (requireValidPath && !store->maybeQueryPathInfo(storePath))
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
}

Expand Down
17 changes: 17 additions & 0 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,23 @@ ref<const ValidPathInfo> Store::queryPathInfo(const StorePath & storePath)
return promise.get_future().get();
}

std::shared_ptr<const ValidPathInfo> Store::maybeQueryPathInfo(const StorePath & storePath)
{
std::promise<std::shared_ptr<const ValidPathInfo>> promise;

queryPathInfo(storePath, {[&](std::future<ref<const ValidPathInfo>> result) {
try {
promise.set_value(result.get());
} catch (InvalidPath &) {
promise.set_value(nullptr);
} catch (...) {
promise.set_exception(std::current_exception());
}
}});

return promise.get_future().get();
}

static bool goodStorePath(const StorePath & expected, const StorePath & actual)
{
return expected.hashPart() == actual.hashPart()
Expand Down
Loading