libutil: convert readFileSource to a generator

Change-Id: I5f92b15fd367d46eb047d74ab6e317b4f51a46d3
This commit is contained in:
eldritch horrors 2024-03-24 00:44:57 +01:00
parent 06220a71c1
commit f4f6d1d8e2
10 changed files with 17 additions and 24 deletions

View file

@ -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;

View file

@ -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;
}
},

View file

@ -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);

View file

@ -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;
}
},

View file

@ -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(

View file

@ -289,17 +289,12 @@ std::string readFile(const Path & path)
}
box_ptr<Source> readFileSource(const Path & path)
Generator<Bytes> 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<FileSource>(std::move(fd));
co_yield drainFDSource(fd.get());
}

View file

@ -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<Source> readFileSource(const Path & path);
Generator<Bytes> readFileSource(const Path & path);
/**
* Write a string to a file.

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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);