diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index 848669ae8..b5a47c6ce 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -147,7 +147,7 @@ ref BinaryCacheStore::addToStoreCommon( write the compressed NAR to disk), into a HashSink (to get the NAR hash), and into a NarAccessor (to get the NAR listing). */ HashSink fileHashSink{HashAlgorithm::SHA256}; - std::shared_ptr narAccessor; + std::shared_ptr narAccessor; HashSink narHashSink{HashAlgorithm::SHA256}; { FdSink fileSink(fdTemp.get()); @@ -205,7 +205,7 @@ ref BinaryCacheStore::addToStoreCommon( if (config.writeNARListing) { nlohmann::json j = { {"version", 1}, - {"root", listNarDeep(*narAccessor, CanonPath::root)}, + {"root", narAccessor->getListing()}, }; upsertFile(std::string(info.path.hashPart()) + ".ls", j.dump(), "application/json"); diff --git a/src/libstore/remote-fs-accessor.cc b/src/libstore/remote-fs-accessor.cc index 2f768871a..7297723e1 100644 --- a/src/libstore/remote-fs-accessor.cc +++ b/src/libstore/remote-fs-accessor.cc @@ -84,7 +84,7 @@ std::shared_ptr RemoteFSAccessor::accessObject(const StorePath & auto narAccessor = makeNarAccessor(std::move(nar)); try { - nlohmann::json j = listNarDeep(*narAccessor, CanonPath::root); + nlohmann::json j = narAccessor->getListing(); writeFile(listingFile, j.dump()); } catch (...) { ignoreExceptionExceptInterrupt(); diff --git a/src/libutil/include/nix/util/nar-accessor.hh b/src/libutil/include/nix/util/nar-accessor.hh index 388dc1440..fa20497a8 100644 --- a/src/libutil/include/nix/util/nar-accessor.hh +++ b/src/libutil/include/nix/util/nar-accessor.hh @@ -10,13 +10,24 @@ namespace nix { struct Source; +/** + * A SourceAccessor for NAR files that provides access to the listing structure. + */ +struct NarAccessor : SourceAccessor +{ + /** + * Get the NAR listing structure. + */ + virtual const NarListing & getListing() const = 0; +}; + /** * Return an object that provides access to the contents of a NAR * file. */ -ref makeNarAccessor(std::string && nar); +ref makeNarAccessor(std::string && nar); -ref makeNarAccessor(Source & source); +ref makeNarAccessor(Source & source); /** * Create a NAR accessor from a NAR listing (in the format produced by @@ -33,12 +44,12 @@ GetNarBytes seekableGetNarBytes(const std::filesystem::path & path); GetNarBytes seekableGetNarBytes(Descriptor fd); -ref makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes); +ref makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes); /** * Creates a NAR accessor from a given stream and a GetNarBytes getter. * @param source Consumed eagerly. References to it are not persisted in the resulting SourceAccessor. */ -ref makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes); +ref makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes); } // namespace nix diff --git a/src/libutil/nar-accessor.cc b/src/libutil/nar-accessor.cc index a31c67ab2..10b222f73 100644 --- a/src/libutil/nar-accessor.cc +++ b/src/libutil/nar-accessor.cc @@ -4,13 +4,18 @@ namespace nix { -struct NarAccessor : public SourceAccessor +struct NarAccessorImpl : NarAccessor { NarListing root; GetNarBytes getNarBytes; - NarAccessor(std::string && nar) + const NarListing & getListing() const override + { + return root; + } + + NarAccessorImpl(std::string && nar) : root{[&nar]() { StringSource source(nar); return parseNarListing(source); @@ -20,18 +25,18 @@ struct NarAccessor : public SourceAccessor { } - NarAccessor(Source & source) + NarAccessorImpl(Source & source) : root{parseNarListing(source)} { } - NarAccessor(Source & source, GetNarBytes getNarBytes) + NarAccessorImpl(Source & source, GetNarBytes getNarBytes) : root{parseNarListing(source)} , getNarBytes{std::move(getNarBytes)} { } - NarAccessor(NarListing && listing, GetNarBytes getNarBytes) + NarAccessorImpl(NarListing && listing, GetNarBytes getNarBytes) : root{std::move(listing)} , getNarBytes{std::move(getNarBytes)} { @@ -127,24 +132,24 @@ struct NarAccessor : public SourceAccessor } }; -ref makeNarAccessor(std::string && nar) +ref makeNarAccessor(std::string && nar) { - return make_ref(std::move(nar)); + return make_ref(std::move(nar)); } -ref makeNarAccessor(Source & source) +ref makeNarAccessor(Source & source) { - return make_ref(source); + return make_ref(source); } -ref makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes) +ref makeLazyNarAccessor(NarListing listing, GetNarBytes getNarBytes) { - return make_ref(std::move(listing), getNarBytes); + return make_ref(std::move(listing), getNarBytes); } -ref makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes) +ref makeLazyNarAccessor(Source & source, GetNarBytes getNarBytes) { - return make_ref(source, getNarBytes); + return make_ref(source, getNarBytes); } GetNarBytes seekableGetNarBytes(const std::filesystem::path & path)