From f4f6d1d8e24a092422fcec601e21683179b735c9 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sun, 24 Mar 2024 00:44:57 +0100 Subject: [PATCH] libutil: convert readFileSource to a generator Change-Id: I5f92b15fd367d46eb047d74ab6e317b4f51a46d3 --- src/libstore/binary-cache-store.cc | 2 +- src/libstore/build/local-derivation-goal.cc | 4 ++-- src/libstore/local-binary-cache-store.cc | 2 +- src/libstore/local-store.cc | 4 ++-- src/libstore/store-api.cc | 12 +++++------- src/libutil/file-system.cc | 9 ++------- src/libutil/file-system.hh | 2 +- src/libutil/hash.cc | 2 +- src/nix/add-to-store.cc | 2 +- src/nix/hash.cc | 2 +- 10 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc index 85267023d..2519a2830 100644 --- a/src/libstore/binary-cache-store.cc +++ b/src/libstore/binary-cache-store.cc @@ -385,7 +385,7 @@ StorePath BinaryCacheStore::addToStore( if (method == FileIngestionMethod::Recursive) { sink << dumpPath(srcPath, filter); } else { - readFileSource(srcPath)->drainInto(sink); + sink << readFileSource(srcPath); } auto h = sink.finish().first; diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index 347f91f54..b2a301b00 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -2219,7 +2219,7 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs() HashModuloSink caSink { outputHash.hashType, oldHashPart }; std::visit(overloaded { [&](const TextIngestionMethod &) { - readFileSource(actualPath)->drainInto(caSink); + caSink << readFileSource(actualPath); }, [&](const FileIngestionMethod & m2) { switch (m2) { @@ -2227,7 +2227,7 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs() caSink << dumpPath(actualPath); break; case FileIngestionMethod::Flat: - readFileSource(actualPath)->drainInto(caSink); + caSink << readFileSource(actualPath); break; } }, diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc index 5f6730476..5ba5bd802 100644 --- a/src/libstore/local-binary-cache-store.cc +++ b/src/libstore/local-binary-cache-store.cc @@ -71,7 +71,7 @@ protected: void getFile(const std::string & path, Sink & sink) override { try { - readFileSource(binaryCacheDir + "/" + path)->drainInto(sink); + sink << readFileSource(binaryCacheDir + "/" + path); } catch (SysError & e) { if (e.errNo == ENOENT) throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path); diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index ce5af0082..98e4d7efe 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -1890,7 +1890,7 @@ ContentAddress LocalStore::hashCAPath( HashModuloSink caSink ( hashType, std::string(pathHash) ); std::visit(overloaded { [&](const TextIngestionMethod &) { - readFileSource(path)->drainInto(caSink); + caSink << readFileSource(path); }, [&](const FileIngestionMethod & m2) { switch (m2) { @@ -1898,7 +1898,7 @@ ContentAddress LocalStore::hashCAPath( caSink << dumpPath(path); break; case FileIngestionMethod::Flat: - readFileSource(path)->drainInto(caSink); + caSink << readFileSource(path); break; } }, diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 2dad1f5f6..683da1129 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -275,13 +275,11 @@ StorePath Store::addToStore( const StorePathSet & references) { Path srcPath(absPath(_srcPath)); - auto source = sinkToSource([&](Sink & sink) { - if (method == FileIngestionMethod::Recursive) - sink << dumpPath(srcPath, filter); - else - readFileSource(srcPath)->drainInto(sink); - }); - return addToStoreFromDump(*source, name, method, hashAlgo, repair, references); + auto source = GeneratorSource{ + method == FileIngestionMethod::Recursive ? dumpPath(srcPath, filter).decay() + : readFileSource(srcPath) + }; + return addToStoreFromDump(source, name, method, hashAlgo, repair, references); } void Store::addMultipleToStore( diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 871707468..13c2b27eb 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -289,17 +289,12 @@ std::string readFile(const Path & path) } -box_ptr readFileSource(const Path & path) +Generator readFileSource(const Path & path) { AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)}; if (!fd) throw SysError("opening file '%s'", path); - - struct FileSource : FdSource { - AutoCloseFD fd; - explicit FileSource(AutoCloseFD fd) : FdSource(fd.get()), fd(std::move(fd)) {} - }; - return make_box_ptr(std::move(fd)); + co_yield drainFDSource(fd.get()); } diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh index 17f5da062..636c13f13 100644 --- a/src/libutil/file-system.hh +++ b/src/libutil/file-system.hh @@ -143,7 +143,7 @@ unsigned char getFileType(const Path & path); * Read the contents of a file into a string. */ std::string readFile(const Path & path); -box_ptr readFileSource(const Path & path); +Generator readFileSource(const Path & path); /** * Write a string to a file. diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index c0ad7f5fa..a762dc940 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -324,7 +324,7 @@ Hash hashString(HashType ht, std::string_view s) Hash hashFile(HashType ht, const Path & path) { HashSink sink(ht); - readFileSource(path)->drainInto(sink); + sink << readFileSource(path); return sink.finish().first; } diff --git a/src/nix/add-to-store.cc b/src/nix/add-to-store.cc index f7e3212be..7e16e12c3 100644 --- a/src/nix/add-to-store.cc +++ b/src/nix/add-to-store.cc @@ -37,7 +37,7 @@ struct CmdAddToStore : MixDryRun, StoreCommand Hash hash = narHash; if (ingestionMethod == FileIngestionMethod::Flat) { HashSink hsink(htSHA256); - readFileSource(path)->drainInto(hsink); + hsink << readFileSource(path); hash = hsink.finish().first; } diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 8d4bc7826..d9079d551 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -85,7 +85,7 @@ struct CmdHashBase : Command switch (mode) { case FileIngestionMethod::Flat: - readFileSource(path)->drainInto(*hashSink); + *hashSink << readFileSource(path); break; case FileIngestionMethod::Recursive: *hashSink << dumpPath(path);