libutil: return a source from readFile

don't consume a sink, return a source instead. the only reason to not do
this is a very slight reduction in dynamic allocations, but since we are
going to *at least* do disk io that will not be a lot of overhead anyway

Change-Id: Iae2f879ec64c3c3ac1d5310eeb6a85e696d4614a
This commit is contained in:
eldritch horrors 2024-05-03 22:53:24 +02:00
parent 67f778670c
commit 11f4a5bc7e
10 changed files with 19 additions and 13 deletions

View file

@ -385,7 +385,7 @@ StorePath BinaryCacheStore::addToStore(
if (method == FileIngestionMethod::Recursive) { if (method == FileIngestionMethod::Recursive) {
dumpPath(srcPath, sink, filter); dumpPath(srcPath, sink, filter);
} else { } else {
readFile(srcPath, sink); readFileSource(srcPath)->drainInto(sink);
} }
auto h = sink.finish().first; auto h = sink.finish().first;

View file

@ -2486,7 +2486,7 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs()
HashModuloSink caSink { outputHash.hashType, oldHashPart }; HashModuloSink caSink { outputHash.hashType, oldHashPart };
std::visit(overloaded { std::visit(overloaded {
[&](const TextIngestionMethod &) { [&](const TextIngestionMethod &) {
readFile(actualPath, caSink); readFileSource(actualPath)->drainInto(caSink);
}, },
[&](const FileIngestionMethod & m2) { [&](const FileIngestionMethod & m2) {
switch (m2) { switch (m2) {
@ -2494,7 +2494,7 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs()
dumpPath(actualPath, caSink); dumpPath(actualPath, caSink);
break; break;
case FileIngestionMethod::Flat: case FileIngestionMethod::Flat:
readFile(actualPath, caSink); readFileSource(actualPath)->drainInto(caSink);
break; break;
} }
}, },

View file

@ -71,7 +71,7 @@ protected:
void getFile(const std::string & path, Sink & sink) override void getFile(const std::string & path, Sink & sink) override
{ {
try { try {
readFile(binaryCacheDir + "/" + path, sink); readFileSource(binaryCacheDir + "/" + path)->drainInto(sink);
} catch (SysError & e) { } catch (SysError & e) {
if (e.errNo == ENOENT) if (e.errNo == ENOENT)
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path); 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) ); HashModuloSink caSink ( hashType, std::string(pathHash) );
std::visit(overloaded { std::visit(overloaded {
[&](const TextIngestionMethod &) { [&](const TextIngestionMethod &) {
readFile(path, caSink); readFileSource(path)->drainInto(caSink);
}, },
[&](const FileIngestionMethod & m2) { [&](const FileIngestionMethod & m2) {
switch (m2) { switch (m2) {
@ -1898,7 +1898,7 @@ ContentAddress LocalStore::hashCAPath(
dumpPath(path, caSink); dumpPath(path, caSink);
break; break;
case FileIngestionMethod::Flat: case FileIngestionMethod::Flat:
readFile(path, caSink); readFileSource(path)->drainInto(caSink);
break; break;
} }
}, },

View file

@ -279,7 +279,7 @@ StorePath Store::addToStore(
if (method == FileIngestionMethod::Recursive) if (method == FileIngestionMethod::Recursive)
dumpPath(srcPath, sink, filter); dumpPath(srcPath, sink, filter);
else else
readFile(srcPath, sink); readFileSource(srcPath)->drainInto(sink);
}); });
return addToStoreFromDump(*source, name, method, hashAlgo, repair, references); return addToStoreFromDump(*source, name, method, hashAlgo, repair, references);
} }

View file

@ -289,12 +289,17 @@ std::string readFile(const Path & path)
} }
void readFile(const Path & path, Sink & sink) box_ptr<Source> readFileSource(const Path & path)
{ {
AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)}; AutoCloseFD fd{open(path.c_str(), O_RDONLY | O_CLOEXEC)};
if (!fd) if (!fd)
throw SysError("opening file '%s'", path); throw SysError("opening file '%s'", path);
drainFD(fd.get(), sink);
struct FileSource : FdSource {
AutoCloseFD fd;
explicit FileSource(AutoCloseFD fd) : FdSource(fd.get()), fd(std::move(fd)) {}
};
return make_box_ptr<FileSource>(std::move(fd));
} }

View file

@ -5,6 +5,7 @@
* Utiltities for working with the file sytem and file paths. * Utiltities for working with the file sytem and file paths.
*/ */
#include "box_ptr.hh"
#include "types.hh" #include "types.hh"
#include "file-descriptor.hh" #include "file-descriptor.hh"
@ -142,7 +143,7 @@ unsigned char getFileType(const Path & path);
* Read the contents of a file into a string. * Read the contents of a file into a string.
*/ */
std::string readFile(const Path & path); std::string readFile(const Path & path);
void readFile(const Path & path, Sink & sink); box_ptr<Source> readFileSource(const Path & path);
/** /**
* Write a string to a file. * 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) Hash hashFile(HashType ht, const Path & path)
{ {
HashSink sink(ht); HashSink sink(ht);
readFile(path, sink); readFileSource(path)->drainInto(sink);
return sink.finish().first; return sink.finish().first;
} }

View file

@ -37,7 +37,7 @@ struct CmdAddToStore : MixDryRun, StoreCommand
Hash hash = narHash; Hash hash = narHash;
if (ingestionMethod == FileIngestionMethod::Flat) { if (ingestionMethod == FileIngestionMethod::Flat) {
HashSink hsink(htSHA256); HashSink hsink(htSHA256);
readFile(path, hsink); readFileSource(path)->drainInto(hsink);
hash = hsink.finish().first; hash = hsink.finish().first;
} }

View file

@ -85,7 +85,7 @@ struct CmdHashBase : Command
switch (mode) { switch (mode) {
case FileIngestionMethod::Flat: case FileIngestionMethod::Flat:
readFile(path, *hashSink); readFileSource(path)->drainInto(*hashSink);
break; break;
case FileIngestionMethod::Recursive: case FileIngestionMethod::Recursive:
dumpPath(path, *hashSink); dumpPath(path, *hashSink);