diff options
Diffstat (limited to 'src/posix_extras.cc')
-rw-r--r-- | src/posix_extras.cc | 18 |
1 files changed, 18 insertions, 0 deletions
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 <array> #include <cerrno> +#include <cstdint> #include <experimental/optional> #include <stdexcept> #include <system_error> +#include <vector> #include <dirent.h> #include <fcntl.h> @@ -101,6 +103,22 @@ File File::OpenAt(const char* const path, const int flags, return result; } +std::vector<std::uint8_t> File::Read(off_t offset, size_t bytes) const { + std::vector<std::uint8_t> 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<size_t>(bytes_read); + offset += bytes_read; + bytes -= static_cast<size_t>(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"); |