From 81c2e0ac8e76ddb3fd3c8e2ce59929853614b1b6 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Wed, 11 Sep 2024 00:27:39 -0700 Subject: archive: rename ParseSink to NARParseVisitor - Rename the listener to not be called a "sink". If it were a "sink" it would be eating bytes and conform with any of the Nix sink stuff (maybe FileHandle should be a Sink itself! but that's a later CL's problem). This is a parser listener. - Move the RetrieveRegularNARSink thing into store-api.cc, which is its only usage, and fix it to actually do what it is stated to do: crash if its invariants are violated. It's, of course, used to erm, unpack single-file NAR files, generated via a horrible contraption of sources and sinks that looks like a plumbing blueprint. Refactoring that is a future task. - Add a description of the invariants of NARParseVisitor in preparation of refactoring it. Change-Id: Ifca1d74d2947204a1f66349772e54dad0743e944 --- src/libstore/store-api.cc | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'src/libstore/store-api.cc') diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 6d9fec41b..1619e5062 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -379,6 +379,37 @@ void Store::addMultipleToStore( } } +namespace { +/** + * If the NAR archive contains a single file at top-level, then save + * the contents of the file to `s`. Otherwise assert. + */ +struct RetrieveRegularNARVisitor : NARParseVisitor +{ + Sink & sink; + + RetrieveRegularNARVisitor(Sink & sink) : sink(sink) { } + + void createRegularFile(const Path & path) override + { + } + + void receiveContents(std::string_view data) override + { + sink(data); + } + + void createDirectory(const Path & path) override + { + assert(false && "RetrieveRegularNARVisitor::createDirectory must not be called"); + } + + void createSymlink(const Path & path, const std::string & target) override + { + assert(false && "RetrieveRegularNARVisitor::createSymlink must not be called"); + } +}; +} /* The aim of this function is to compute in one pass the correct ValidPathInfo for @@ -413,7 +444,7 @@ ValidPathInfo Store::addToStoreSlow(std::string_view name, const Path & srcPath, /* Note that fileSink and unusualHashTee must be mutually exclusive, since they both write to caHashSink. Note that that requisite is currently true because the former is only used in the flat case. */ - RetrieveRegularNARSink fileSink { caHashSink }; + RetrieveRegularNARVisitor fileSink { caHashSink }; TeeSink unusualHashTee { narHashSink, caHashSink }; auto & narSink = method == FileIngestionMethod::Recursive && hashAlgo != HashType::SHA256 @@ -429,7 +460,7 @@ ValidPathInfo Store::addToStoreSlow(std::string_view name, const Path & srcPath, information to narSink. */ TeeSource tapped { fileSource, narSink }; - ParseSink blank; + NARParseVisitor blank; auto & parseSink = method == FileIngestionMethod::Flat ? fileSink : blank; -- cgit v1.2.3