taco-db
0.1.0
|
Represents an open file in the file system. More...
#include <storage/FSFile.h>
Public Member Functions | |
~FSFile () | |
Desctructs the FSFile. More... | |
bool | Reopen () |
Opens the file again. More... | |
void | Close () |
Closes the file if it is still open. More... | |
bool | IsOpen () const |
Returns whether this file is open. More... | |
void | Delete () const |
Deletes the file from the file system. More... | |
void | Read (void *buf, size_t count, off_t offset) |
Reads count bytes from the file at the offset into the buffer buf . More... | |
void | Write (const void *buf, size_t count, off_t offset) |
Writes count bytes from the buffer buf into the file at the offset . More... | |
void | Allocate (size_t count) |
Allocates count bytes at the end of the file and zeros those bytes. More... | |
size_t | Size () const noexcept |
Returns the size of the file. More... | |
void | Flush () |
Flushes the data written to the disk. More... | |
Static Public Member Functions | |
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. More... | |
Private Member Functions | |
FSFile (const std::string &path, int fd, size_t flen, bool o_direct) | |
void | ReadImpl (void *buf, size_t count, off_t offset) |
Reads count bytes from the file at the offset into the buffer buf . More... | |
void | WriteImpl (const void *buf, size_t count, off_t offset) |
Writes count bytes from the buffer buf into the file at the offset . More... | |
Private Attributes | |
std::string | m_path |
bool | m_o_direct |
int | m_fd |
atomic_size_t | m_flen |
Represents an open file in the file system.
taco::FSFile::~FSFile | ( | ) |
Desctructs the FSFile.
If the underlying file is still opened, it should close the file automatically.
|
inlineprivate |
void taco::FSFile::Allocate | ( | size_t | count | ) |
Allocates count
bytes at the end of the file and zeros those bytes.
This function is NOT thread-safe. The caller is responsible for not calling Allocate() from multiple threads. This function also does not need to impose any memory order on the file size.
void taco::FSFile::Close | ( | ) |
void taco::FSFile::Delete | ( | ) | const |
Deletes the file from the file system.
If it is currently open, it remains to exist until it is closed.
It logs a warning if the deletion fails and the log message contains a text description from ‘strerror(errno)’.
void taco::FSFile::Flush | ( | ) |
Flushes the data written to the disk.
It is a fatal error if the flush fails.
bool taco::FSFile::IsOpen | ( | ) | const |
Returns whether this file is open.
|
static |
Opens or creates a file in the file system for read and write.
Returns a non-null pointer on success. The boolean parameters o_trunc
, o_direct
and o_creat
specify whether O_TRUNC, O_DIRECT and O_CREAT should be added to the flags
passed to open(2).
If any error occurs, returns a null pointer and errno may be set by the failed syscall if the error is caused by the failed syscall. If the error is not associated with any syscall failure, then errno == 0. See ‘man 2 open’ for details.
The default permission of a created file is 0600.
Note: this function modifies errno
.
void taco::FSFile::Read | ( | void * | buf, |
size_t | count, | ||
off_t | offset | ||
) |
Reads count
bytes from the file at the offset
into the buffer buf
.
It is a fatal error if the specified range [offset
, offset
+ count
) falls out of the file. It is also a fatal error if the underlying syscall fails or only partially reads the file.
This function is thread-safe.
|
private |
Reads count
bytes from the file at the offset
into the buffer buf
.
This private function does not need to check whether the parameters are in range. Any error other than partial writes or interrupted syscall shall be treated as a fatal error.
This function should be thread-safe.
bool taco::FSFile::Reopen | ( | ) |
Opens the file again.
Returns ‘true’ on success. If any error occurs, returns ‘false’ and errno may be set by the failed syscall if the error is caused by the failed syscall. If the error is not associated with any syscall failure, then errno
== 0.
Note: this function modifies errno
.
|
noexcept |
Returns the size of the file.
This function should be thread-safe but it does not need to impose any memory order on the file size.
void taco::FSFile::Write | ( | const void * | buf, |
size_t | count, | ||
off_t | offset | ||
) |
Writes count
bytes from the buffer buf
into the file at the offset
.
It is a fatal error if the specified range [offset
, offset
count
) falls out of the file. It is also a fatal error if the underlying syscall fails or only partially writes the data.This function is thread-safe.
|
private |
Writes count
bytes from the buffer buf
into the file at the offset
.
This private function does not need to check whether the parameters are in range. Any error other than partial writes or interrupted syscall shall be treated as a fatal error.
This function should be thread-safe.
|
private |
|
private |
|
private |
|
private |