From b7e8ce93de54a0f897832877255bed7005b08f14 Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Mon, 13 Jul 2020 12:22:01 -0700 Subject: Add ReadAllFd to test util PiperOrigin-RevId: 321008185 --- test/util/test_util.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/util/test_util.h') 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 ReadAllFd(int fd) { + std::string all; + all.reserve(128 * 1024); // arbitrary. + + std::vector 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) { -- cgit v1.2.3