aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/util.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2020-05-06 11:33:55 +0200
committerGitHub <noreply@github.com>2020-05-06 11:33:55 +0200
commit74a1bfdcab03d3c6ecb9353f4bae0a0c550ddb98 (patch)
treeaefac7fd096274bd795cec43f222e93e4a26376e /src/libutil/util.cc
parent272c4ba36dab76b01eef19bd30e56310d033b4dd (diff)
parent2e5be2a7495ac0b204454c74664e590a38d039d3 (diff)
Merge pull request #3546 from guibou/nix_readfile_on_0_sized_files
builtins.readFile: do not truncate content
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r--src/libutil/util.cc13
1 files changed, 5 insertions, 8 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 615a7656c..71db92d77 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -316,19 +316,16 @@ string readFile(int fd)
if (fstat(fd, &st) == -1)
throw SysError("statting file");
- std::vector<unsigned char> buf(st.st_size);
- readFull(fd, buf.data(), st.st_size);
-
- return string((char *) buf.data(), st.st_size);
+ return drainFD(fd, true, st.st_size);
}
-string readFile(const Path & path, bool drain)
+string readFile(const Path & path)
{
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd)
throw SysError(format("opening file '%1%'") % path);
- return drain ? drainFD(fd.get()) : readFile(fd.get());
+ return readFile(fd.get());
}
@@ -665,9 +662,9 @@ void writeFull(int fd, const string & s, bool allowInterrupts)
}
-string drainFD(int fd, bool block)
+string drainFD(int fd, bool block, const size_t reserveSize)
{
- StringSink sink;
+ StringSink sink(reserveSize);
drainFD(fd, sink, block);
return std::move(*sink.s);
}