taco-db  0.1.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
taco::FSFile Class Reference

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 FSFileOpen (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
 

Detailed Description

Represents an open file in the file system.

Constructor & Destructor Documentation

◆ ~FSFile()

taco::FSFile::~FSFile ( )

Desctructs the FSFile.

If the underlying file is still opened, it should close the file automatically.

◆ FSFile()

taco::FSFile::FSFile ( const std::string &  path,
int  fd,
size_t  flen,
bool  o_direct 
)
inlineprivate

Member Function Documentation

◆ Allocate()

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.

◆ Close()

void taco::FSFile::Close ( )

Closes the file if it is still open.

It logs a warning if it fails to close the file (the log message contains a text description of the error returned by ‘strerror(errno)’). After the call, no function other than Reopen(), Close() or the destructor may be called.

◆ Delete()

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)’.

◆ Flush()

void taco::FSFile::Flush ( )

Flushes the data written to the disk.

It is a fatal error if the flush fails.

◆ IsOpen()

bool taco::FSFile::IsOpen ( ) const

Returns whether this file is open.

◆ Open()

FSFile * taco::FSFile::Open ( const std::string &  path,
bool  o_trunc,
bool  o_direct,
bool  o_creat,
mode_t  mode = 0600 
)
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.

◆ Read()

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.

◆ ReadImpl()

void taco::FSFile::ReadImpl ( void *  buf,
size_t  count,
off_t  offset 
)
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.

◆ Reopen()

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.

◆ Size()

size_t taco::FSFile::Size ( ) const
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.

◆ Write()

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.

◆ WriteImpl()

void taco::FSFile::WriteImpl ( const void *  buf,
size_t  count,
off_t  offset 
)
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.

Member Data Documentation

◆ m_fd

int taco::FSFile::m_fd
private

◆ m_flen

atomic_size_t taco::FSFile::m_flen
private

◆ m_o_direct

bool taco::FSFile::m_o_direct
private

◆ m_path

std::string taco::FSFile::m_path
private

The documentation for this class was generated from the following files: