taco-db  0.1.0
FSFile.h
Go to the documentation of this file.
1 #ifndef STORAGE_FSFILE_H
2 #define STORAGE_FSFILE_H
3 
4 #include "tdb.h"
5 
6 namespace taco {
7 
11 class FSFile {
12 public:
28  static FSFile *Open(const std::string& path, bool o_trunc,
29  bool o_direct, bool o_creat,
30  mode_t mode = 0600);
31 
36  ~FSFile();
37 
46  bool Reopen();
47 
54  void Close();
55 
59  bool IsOpen() const;
60 
68  void Delete() const;
69 
78  void Read(void *buf, size_t count, off_t offset);
79 
88  void Write(const void *buf, size_t count, off_t offset);
89 
97  void Allocate(size_t count);
98 
105  size_t Size() const noexcept;
106 
111  void Flush();
112 
113 private:
114  FSFile(const std::string &path, int fd, size_t flen, bool o_direct):
115  m_path(path),
116  m_o_direct(o_direct),
117  m_fd(fd),
118  m_flen(flen) {}
119 
128  void ReadImpl(void *buf, size_t count, off_t offset);
129 
138  void WriteImpl(const void *buf, size_t count, off_t offset);
139 
140  std::string m_path;
142  int m_fd;
143  atomic_size_t m_flen;
144 };
145 
153 bool fallocate_zerofill_fast(int fd, off_t offset, off_t len);
154 
155 } // namespace taco
156 
157 #endif // STORAGE_FSFILE_H
Represents an open file in the file system.
Definition: FSFile.h:11
void Write(const void *buf, size_t count, off_t offset)
Writes count bytes from the buffer buf into the file at the offset.
Definition: FSFile.cpp:109
~FSFile()
Desctructs the FSFile.
Definition: FSFile.cpp:44
size_t Size() const noexcept
Returns the size of the file.
Definition: FSFile.cpp:153
void ReadImpl(void *buf, size_t count, off_t offset)
Reads count bytes from the file at the offset into the buffer buf.
Definition: FSFile.cpp:166
int m_fd
Definition: FSFile.h:142
bool m_o_direct
Definition: FSFile.h:141
void Close()
Closes the file if it is still open.
Definition: FSFile.cpp:74
static FSFile * Open(const std::string &path, bool o_trunc, bool o_direct, bool o_creat, mode_t mode=0600)
Opens or creates a file in the file system for read and write.
Definition: FSFile.cpp:14
bool IsOpen() const
Returns whether this file is open.
Definition: FSFile.cpp:85
FSFile(const std::string &path, int fd, size_t flen, bool o_direct)
Definition: FSFile.h:114
void Flush()
Flushes the data written to the disk.
Definition: FSFile.cpp:158
void Delete() const
Deletes the file from the file system.
Definition: FSFile.cpp:90
atomic_size_t m_flen
Definition: FSFile.h:143
std::string m_path
Definition: FSFile.h:140
void Allocate(size_t count)
Allocates count bytes at the end of the file and zeros those bytes.
Definition: FSFile.cpp:120
bool Reopen()
Opens the file again.
Definition: FSFile.cpp:49
void WriteImpl(const void *buf, size_t count, off_t offset)
Writes count bytes from the buffer buf into the file at the offset.
Definition: FSFile.cpp:190
void Read(void *buf, size_t count, off_t offset)
Reads count bytes from the file at the offset into the buffer buf.
Definition: FSFile.cpp:98
Definition: datum.h:28
bool fallocate_zerofill_fast(int fd, off_t offset, off_t len)
Calls fallocate(2) with mode FALLOC_FL_ZERO_RANGE if it is available.
Definition: FSFile_private.cpp:27