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