diff options
author | Fabricio Voznika <fvoznika@google.com> | 2020-07-13 12:22:01 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-07-13 12:23:18 -0700 |
commit | b7e8ce93de54a0f897832877255bed7005b08f14 (patch) | |
tree | 952130480d405e704c7bbf8b33af9ea3e292a037 /test/util/test_util.h | |
parent | 43c209f48e0aa9024705583cc6f0fafa7d6380ca (diff) |
Add ReadAllFd to test util
PiperOrigin-RevId: 321008185
Diffstat (limited to 'test/util/test_util.h')
-rw-r--r-- | test/util/test_util.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/test/util/test_util.h b/test/util/test_util.h index 109078fc7..0f9781038 100644 --- a/test/util/test_util.h +++ b/test/util/test_util.h @@ -567,6 +567,25 @@ ssize_t ApplyFileIoSyscall(F const& f, size_t const count) { } // namespace internal +inline PosixErrorOr<std::string> ReadAllFd(int fd) { + std::string all; + all.reserve(128 * 1024); // arbitrary. + + std::vector<char> buffer(16 * 1024); + for (;;) { + auto const bytes = RetryEINTR(read)(fd, buffer.data(), buffer.size()); + if (bytes < 0) { + return PosixError(errno, "file read"); + } + if (bytes == 0) { + return std::move(all); + } + if (bytes > 0) { + all.append(buffer.data(), bytes); + } + } +} + inline ssize_t ReadFd(int fd, void* buf, size_t count) { return internal::ApplyFileIoSyscall( [&](size_t completed) { |