diff --git a/src/libfetchers/fetch-to-store.cc b/src/libfetchers/fetch-to-store.cc index 9a861a11d..618f32cae 100644 --- a/src/libfetchers/fetch-to-store.cc +++ b/src/libfetchers/fetch-to-store.cc @@ -31,10 +31,14 @@ StorePath fetchToStore( // a `PosixSourceAccessor` pointing to a store path. std::optional cacheKey; - std::optional fingerprint; - if (!filter && (fingerprint = path.accessor->getFingerprint(path.path))) { - cacheKey = makeFetchToStoreCacheKey(std::string{name}, *fingerprint, method, path.path.abs()); + auto [subpath, fingerprint] = + filter + ? std::pair>{path.path, std::nullopt} + : path.accessor->getFingerprint(path.path); + + if (fingerprint) { + cacheKey = makeFetchToStoreCacheKey(std::string{name}, *fingerprint, method, subpath.abs()); if (auto res = fetchers::getCache()->lookupStorePath(*cacheKey, store, mode == FetchMode::DryRun)) { debug("store path cache hit for '%s'", path); return res->storePath; diff --git a/src/libfetchers/fetchers.cc b/src/libfetchers/fetchers.cc index 7ab1f567a..5764f310d 100644 --- a/src/libfetchers/fetchers.cc +++ b/src/libfetchers/fetchers.cc @@ -338,8 +338,7 @@ std::pair, Input> Input::getAccessorUnchecked(ref sto auto accessor = make_ref(makeStorePathAccessor(store, storePath)); - if (auto fingerprint = getFingerprint(store)) - accessor->setFingerprint(*fingerprint); + accessor->fingerprint = getFingerprint(store); // FIXME: ideally we would use the `showPath()` of the // "real" accessor for this fetcher type. @@ -353,10 +352,8 @@ std::pair, Input> Input::getAccessorUnchecked(ref sto auto [accessor, result] = scheme->getAccessor(store, *this); - assert(!accessor->getFingerprint(CanonPath::root)); - - if (auto fingerprint = result.getFingerprint(store)) - accessor->setFingerprint(*fingerprint); + assert(!accessor->fingerprint); + accessor->fingerprint = result.getFingerprint(store); return {accessor, std::move(result)}; } diff --git a/src/libfetchers/filtering-source-accessor.cc b/src/libfetchers/filtering-source-accessor.cc index 12e4a688b..c339cdbdb 100644 --- a/src/libfetchers/filtering-source-accessor.cc +++ b/src/libfetchers/filtering-source-accessor.cc @@ -58,16 +58,13 @@ std::string FilteringSourceAccessor::showPath(const CanonPath & path) return displayPrefix + next->showPath(prefix / path) + displaySuffix; } -std::optional FilteringSourceAccessor::getFingerprint(const CanonPath & path) +std::pair> FilteringSourceAccessor::getFingerprint(const CanonPath & path) { + if (fingerprint) + return {path, fingerprint}; return next->getFingerprint(prefix / path); } -void FilteringSourceAccessor::setFingerprint(std::string fingerprint) -{ - next->setFingerprint(std::move(fingerprint)); -} - void FilteringSourceAccessor::checkAccess(const CanonPath & path) { if (!isAllowed(path)) diff --git a/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh b/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh index 391cd371b..e0228ad9b 100644 --- a/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh +++ b/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh @@ -50,9 +50,7 @@ struct FilteringSourceAccessor : SourceAccessor std::string showPath(const CanonPath & path) override; - std::optional getFingerprint(const CanonPath & path) override; - - void setFingerprint(std::string fingerprint) override; + std::pair> getFingerprint(const CanonPath & path) override; /** * Call `makeNotAllowedError` to throw a `RestrictedPathError` diff --git a/src/libutil/include/nix/util/forwarding-source-accessor.hh b/src/libutil/include/nix/util/forwarding-source-accessor.hh index cfa5ff9b8..bdba2addc 100644 --- a/src/libutil/include/nix/util/forwarding-source-accessor.hh +++ b/src/libutil/include/nix/util/forwarding-source-accessor.hh @@ -52,16 +52,6 @@ struct ForwardingSourceAccessor : SourceAccessor { return next->getPhysicalPath(path); } - - std::optional getFingerprint(const CanonPath & path) override - { - return next->getFingerprint(path); - } - - void setFingerprint(std::string fingerprint) override - { - next->setFingerprint(std::move(fingerprint)); - } }; } diff --git a/src/libutil/include/nix/util/source-accessor.hh b/src/libutil/include/nix/util/source-accessor.hh index 560e1fda0..4084b3bdc 100644 --- a/src/libutil/include/nix/util/source-accessor.hh +++ b/src/libutil/include/nix/util/source-accessor.hh @@ -177,28 +177,32 @@ struct SourceAccessor : std::enable_shared_from_this SymlinkResolution mode = SymlinkResolution::Full); /** - * Return a string that uniquely represents the contents of this - * accessor. This is used for caching lookups (see - * `fetchToStore()`). - * - * Fingerprints are generally for the entire accessor, but this - * method takes a `path` argument to support accessors like - * `MountedSourceAccessor` that combine multiple underlying - * accessors. A fingerprint should only be returned if it uniquely - * represents everything under `path`. + * A string that uniquely represents the contents of this + * accessor. This is used for caching lookups (see `fetchToStore()`). */ - virtual std::optional getFingerprint(const CanonPath & path) - { - return _fingerprint; - } + std::optional fingerprint; - virtual void setFingerprint(std::string fingerprint) + /** + * Return the fingerprint for `path`. This is usually the + * fingerprint of the current accessor, but for composite + * accessors (like `MountedSourceAccessor`), we want to return the + * fingerprint of the "inner" accessor if the current one lacks a + * fingerprint. + * + * So this method is intended to return the most-outer accessor + * that has a fingerprint for `path`. It also returns the path that `path` + * corresponds to in that accessor. + * + * For example: in a `MountedSourceAccessor` that has + * `/nix/store/foo` mounted, + * `getFingerprint("/nix/store/foo/bar")` will return the path + * `/bar` and the fingerprint of the `/nix/store/foo` accessor. + */ + virtual std::pair> getFingerprint(const CanonPath & path) { - _fingerprint = std::move(fingerprint); + return {path, fingerprint}; } - std::optional _fingerprint; - /** * Return the maximum last-modified time of the files in this * tree, if available. diff --git a/src/libutil/mounted-source-accessor.cc b/src/libutil/mounted-source-accessor.cc index 9292291c1..ed62fd2a3 100644 --- a/src/libutil/mounted-source-accessor.cc +++ b/src/libutil/mounted-source-accessor.cc @@ -91,12 +91,11 @@ struct MountedSourceAccessorImpl : MountedSourceAccessor return nullptr; } - std::optional getFingerprint(const CanonPath & path) override + std::pair> getFingerprint(const CanonPath & path) override { + if (fingerprint) + return {path, fingerprint}; auto [accessor, subpath] = resolve(path); - // FIXME: check that there are no mounts underneath the mount - // point of `accessor`, since that would invalidate the - // fingerprint. (However we don't have such at the moment.) return accessor->getFingerprint(subpath); } };