a portable locking-aware exception-friendly file (with RAII API) More...

#include <File.h>

Collaboration diagram for File:

Public Types

typedef FileOpeningConfig Be
 convenient shorthand for File() callers More...
 

Public Member Functions

 File (const SBuf &aFilename, const FileOpeningConfig &cfg)
 opens More...
 
 ~File ()
 closes More...
 
 File (const File &)=delete
 
Fileoperator= (const File &)=delete
 
 File (File &&other)
 
Fileoperator= (File &&other)
 
const SBufname () const
 
void truncate ()
 makes the file size (and the current I/O offset) zero More...
 
SBuf readSmall (SBuf::size_type minBytes, SBuf::size_type maxBytes)
 read(2) for small files More...
 
void writeAll (const SBuf &data)
 write(2) with a "wrote everything" check More...
 
void synchronize ()
 fsync(2) More...
 

Static Public Member Functions

static FileOptional (const SBuf &aName, const FileOpeningConfig &cfg)
 

Protected Member Functions

bool isOpen () const
 
void open (const FileOpeningConfig &cfg)
 opens (or creates) the file More...
 
void lock (const FileOpeningConfig &cfg)
 calls lockOnce() as many times as necessary (including zero) More...
 
void lockOnce (const FileOpeningConfig &cfg)
 locks, blocking or returning immediately depending on the lock waiting mode More...
 
void close ()
 
SBuf sysCallFailure (const char *callName, const char *error) const
 
SBuf sysCallError (const char *callName, const int savedErrno) const
 

Private Types

typedef int Handle
 

Private Attributes

SBuf name_
 location on disk More...
 
Handle fd_ = InvalidHandle
 OS-specific file handle. More...
 

Static Private Attributes

static const Handle InvalidHandle = -1
 

Detailed Description

Definition at line 66 of file File.h.

Member Typedef Documentation

Definition at line 69 of file File.h.

typedef int File::Handle
private

Definition at line 120 of file File.h.

Constructor & Destructor Documentation

File::File ( const SBuf aFilename,
const FileOpeningConfig cfg 
)

Definition at line 134 of file File.cc.

References close(), debugs, lock(), name_, and open().

Referenced by Optional().

File::~File ( )

Definition at line 150 of file File.cc.

References close(), debugs, and name_.

File::File ( const File )
delete
File::File ( File &&  other)

Definition at line 156 of file File.cc.

Member Function Documentation

void File::close ( )
protected

Definition at line 195 of file File.cc.

References DBG_IMPORTANT, debugs, fd_, isOpen(), sysCallError(), and sysCallFailure().

Referenced by File(), and ~File().

bool File::isOpen ( ) const
inlineprotected

Definition at line 94 of file File.h.

References fd_, and InvalidHandle.

Referenced by close().

void File::lock ( const FileOpeningConfig cfg)
protected

Definition at line 317 of file File.cc.

References debugs, FileOpeningConfig::lockAttempts, lockOnce(), Must, FileOpeningConfig::RetryGapUsec, and xusleep().

Referenced by File().

void File::lockOnce ( const FileOpeningConfig cfg)
protected

Definition at line 338 of file File.cc.

References debugs, fd_, FileOpeningConfig::flockMode, name_, sysCallError(), sysCallFailure(), and TexcHere.

Referenced by lock().

const SBuf& File::name ( ) const
inline

Definition at line 83 of file File.h.

References name_.

File& File::operator= ( const File )
delete
File & File::operator= ( File &&  other)

Definition at line 162 of file File.cc.

References fd_.

File * File::Optional ( const SBuf aName,
const FileOpeningConfig cfg 
)
static
Returns
nil if File() throws or a new File object (otherwise)

Definition at line 123 of file File.cc.

References debugs, and File().

Referenced by Instance::ThrowIfAlreadyRunning().

SBuf File::readSmall ( SBuf::size_type  minBytes,
SBuf::size_type  maxBytes 
)
void File::synchronize ( )

Definition at line 300 of file File.cc.

References fd_, sysCallError(), sysCallFailure(), and TexcHere.

Referenced by Instance::WriteOurPid().

SBuf File::sysCallError ( const char *  callName,
const int  savedErrno 
) const
protected
Returns
a description of an errno-based system call failure

Definition at line 368 of file File.cc.

References sysCallFailure(), and xstrerr().

Referenced by close(), lockOnce(), open(), readSmall(), synchronize(), truncate(), and writeAll().

SBuf File::sysCallFailure ( const char *  callName,
const char *  error 
) const
protected
Returns
a description a system call-related failure

Definition at line 361 of file File.cc.

References name_, and ToSBuf().

Referenced by close(), lockOnce(), open(), readSmall(), synchronize(), sysCallError(), truncate(), and writeAll().

void File::truncate ( )

Definition at line 214 of file File.cc.

References fd_, sysCallError(), sysCallFailure(), and TexcHere.

Referenced by Instance::WriteOurPid().

void File::writeAll ( const SBuf data)

Definition at line 278 of file File.cc.

References DWORD, fd_, SBuf::length(), SBuf::rawContent(), sysCallError(), sysCallFailure(), and TexcHere.

Referenced by Instance::WriteOurPid().

Member Data Documentation

Handle File::fd_ = InvalidHandle
private

Definition at line 123 of file File.h.

Referenced by close(), isOpen(), lockOnce(), open(), operator=(), readSmall(), synchronize(), truncate(), and writeAll().

const Handle File::InvalidHandle = -1
staticprivate

Definition at line 121 of file File.h.

Referenced by isOpen(), and open().

SBuf File::name_
private

Definition at line 113 of file File.h.

Referenced by File(), lockOnce(), name(), open(), sysCallFailure(), and ~File().


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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors