From 6aa9095865017c06a61f0cb10c1f1231a89b761c Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 21 Feb 2016 19:23:11 -0500 Subject: Implement fgetattr and read --- src/posix_extras.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/posix_extras.cc') diff --git a/src/posix_extras.cc b/src/posix_extras.cc index 7529251..ac6e3b2 100644 --- a/src/posix_extras.cc +++ b/src/posix_extras.cc @@ -16,9 +16,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -101,6 +103,22 @@ File File::OpenAt(const char* const path, const int flags, return result; } +std::vector File::Read(off_t offset, size_t bytes) const { + std::vector result(bytes, 0); + size_t cursor = 0; + ssize_t bytes_read; + while (0 < (bytes_read = pread(fd_, result.data() + cursor, bytes, offset))) { + cursor += static_cast(bytes_read); + offset += bytes_read; + bytes -= static_cast(bytes_read); + } + if (bytes_read == -1) { + throw SystemError(); + } + result.resize(cursor); + return result; +} + void File::UnlinkAt(const char* const path) const { if (path[0] == '/') { throw std::invalid_argument("absolute path"); -- cgit v1.2.3