diff --git a/src/libfetchers/filtering-source-accessor.cc b/src/libfetchers/filtering-source-accessor.cc index ceb6457d473d..c1000af3a4c8 100644 --- a/src/libfetchers/filtering-source-accessor.cc +++ b/src/libfetchers/filtering-source-accessor.cc @@ -1,6 +1,8 @@ #include "nix/fetchers/filtering-source-accessor.hh" #include "nix/util/sync.hh" +#include "nix/util/util.hh" +#include #include namespace nix { @@ -108,13 +110,19 @@ ref AllowListSourceAccessor::create( return make_ref(next, allowedPrefixes, allowedPaths, std::move(makeNotAllowedError)); } +CachingFilteringSourceAccessor::CachingFilteringSourceAccessor( + const SourcePath & src, MakeNotAllowedError && makeNotAllowedError) + : FilteringSourceAccessor(src, std::move(makeNotAllowedError)) + , cache(make_ref>()) +{ +} + bool CachingFilteringSourceAccessor::isAllowed(const CanonPath & path) { - auto i = cache.find(path); - if (i != cache.end()) - return i->second; + if (auto allowed = getConcurrent(*cache, path)) + return *allowed; auto res = isAllowedUncached(path); - cache.emplace(path, res); + cache->emplace(path, res); return res; } diff --git a/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh b/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh index c5c1ce282b30..5ea218d5b655 100644 --- a/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh +++ b/src/libfetchers/include/nix/fetchers/filtering-source-accessor.hh @@ -5,6 +5,8 @@ #include #include +#include + namespace nix { /** @@ -95,9 +97,9 @@ struct AllowListSourceAccessor : public FilteringSourceAccessor */ struct CachingFilteringSourceAccessor : FilteringSourceAccessor { - std::map cache; + const ref> cache; - using FilteringSourceAccessor::FilteringSourceAccessor; + CachingFilteringSourceAccessor(const SourcePath & src, MakeNotAllowedError && makeNotAllowedError); bool isAllowed(const CanonPath & path) override;