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 SBuf &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

◆ Be

Definition at line 69 of file File.h.

◆ Handle

typedef int File::Handle
private

Definition at line 120 of file File.h.

Constructor & Destructor Documentation

◆ File() [1/3]

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

Definition at line 135 of file File.cc.

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

Referenced by Optional().

◆ ~File()

File::~File ( )

Definition at line 151 of file File.cc.

References close(), debugs, and name_.

◆ File() [2/3]

File::File ( const File )
delete

◆ File() [3/3]

File::File ( File &&  other)

Definition at line 157 of file File.cc.

Member Function Documentation

◆ close()

void File::close ( )
protected

Definition at line 196 of file File.cc.

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

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

◆ isOpen()

bool File::isOpen ( ) const
inlineprotected

Definition at line 94 of file File.h.

References fd_, and InvalidHandle.

Referenced by close().

◆ lock()

void File::lock ( const FileOpeningConfig cfg)
protected

Definition at line 321 of file File.cc.

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

Referenced by File().

◆ lockOnce()

void File::lockOnce ( const FileOpeningConfig cfg)
protected

Definition at line 342 of file File.cc.

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

Referenced by lock().

◆ name()

const SBuf & File::name ( ) const
inline

Definition at line 83 of file File.h.

References name_.

◆ open()

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

Definition at line 163 of file File.cc.

References fd_.

◆ Optional()

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

Definition at line 124 of file File.cc.

References File(), and debugs.

Referenced by Instance::ThrowIfAlreadyRunning().

◆ readSmall()

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

◆ synchronize()

void File::synchronize ( )

Definition at line 304 of file File.cc.

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

Referenced by Instance::WriteOurPid().

◆ sysCallError()

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

Definition at line 372 of file File.cc.

References sysCallFailure(), and xstrerr().

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

◆ sysCallFailure()

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

Definition at line 365 of file File.cc.

References error(), name_, and ToSBuf().

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

◆ truncate()

void File::truncate ( )

Definition at line 215 of file File.cc.

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

Referenced by Instance::WriteOurPid().

◆ writeAll()

void File::writeAll ( const SBuf data)

Definition at line 280 of file File.cc.

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

Referenced by Instance::WriteOurPid().

Member Data Documentation

◆ fd_

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

◆ InvalidHandle

const Handle File::InvalidHandle = -1
staticprivate

Definition at line 121 of file File.h.

Referenced by isOpen(), and open().

◆ name_

SBuf File::name_
private

Definition at line 113 of file File.h.

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


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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors