blob: fa6f5eca392b0dc95e282b7a75a6f8305fb0a354 [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
Willy Tu2b6683f2022-02-20 01:45:42 -08009#include <optional>
Patrick Venture15f0f942020-07-09 09:38:18 -070010#include <string>
11
12namespace binstore
13{
14
15class 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 Tu2b6683f2022-02-20 01:45:42 -080025 explicit SysFileImpl(const std::string& path,
26 std::optional<size_t> offset = std::nullopt,
Patrick Venture15f0f942020-07-09 09:38:18 -070027 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