aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/file-system.cc9
-rw-r--r--src/libutil/file-system.hh3
-rw-r--r--src/libutil/hash.cc2
3 files changed, 10 insertions, 4 deletions
diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc
index d573b22b4..f51f3c092 100644
--- a/src/libutil/file-system.cc
+++ b/src/libutil/file-system.cc
@@ -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)};
if (!fd)
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));
}
diff --git a/src/libutil/file-system.hh b/src/libutil/file-system.hh
index 6c1923d55..64d884227 100644
--- a/src/libutil/file-system.hh
+++ b/src/libutil/file-system.hh
@@ -5,6 +5,7 @@
* Utiltities for working with the file sytem and file paths.
*/
+#include "box_ptr.hh"
#include "types.hh"
#include "file-descriptor.hh"
@@ -142,7 +143,7 @@ unsigned char getFileType(const Path & path);
* Read the contents of a file into a string.
*/
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.
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 006b5000c..822fa150e 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);
- readFile(path, sink);
+ readFileSource(path)->drainInto(sink);
return sink.finish().first;
}