Patrick Venture | 15f0f94 | 2020-07-09 09:38:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "sys.hpp" |
| 4 | #include "sys_file.hpp" |
| 5 | |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 9 | #include <optional> |
Patrick Venture | 15f0f94 | 2020-07-09 09:38:18 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | namespace binstore |
| 13 | { |
| 14 | |
| 15 | class SysFileImpl : public SysFile |
| 16 | { |
| 17 | public: |
| 18 | /** |
| 19 | * @brief Constructs sysFile specified by path and offset |
| 20 | * @param path The file path |
| 21 | * @param offset The byte offset relatively. Reading a sysfile at position 0 |
| 22 | * actually reads underlying file at 'offset' |
| 23 | * @param sys Syscall operation interface |
| 24 | */ |
Willy Tu | 2b6683f | 2022-02-20 01:45:42 -0800 | [diff] [blame] | 25 | explicit SysFileImpl(const std::string& path, |
| 26 | std::optional<size_t> offset = std::nullopt, |
Patrick Venture | 15f0f94 | 2020-07-09 09:38:18 -0700 | [diff] [blame] | 27 | const internal::Sys* sys = &internal::sys_impl); |
| 28 | ~SysFileImpl(); |
| 29 | SysFileImpl() = delete; |
| 30 | SysFileImpl(const SysFileImpl&) = delete; |
| 31 | SysFileImpl& operator=(SysFileImpl) = delete; |
| 32 | |
| 33 | size_t readToBuf(size_t pos, size_t count, char* buf) const override; |
| 34 | std::string readAsStr(size_t pos, size_t count) const override; |
| 35 | std::string readRemainingAsStr(size_t pos) const override; |
| 36 | void writeStr(const std::string& data, size_t pos) override; |
| 37 | |
| 38 | private: |
| 39 | int fd_; |
| 40 | size_t offset_; |
| 41 | void lseek(size_t pos) const; |
| 42 | const internal::Sys* sys; |
| 43 | }; |
| 44 | |
| 45 | } // namespace binstore |