Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes

SafeFileIO Class Reference

Protects file IO when you can't afford to have partly written things after write errors or have a file garbled by multiple access. More...

List of all members.

Public Member Functions

 SafeFileIO (const char *, bool locked=false)
bool open (bool forread, bool ignorelock=false)
const char * errMsg () const
std::istream & istrm ()
std::ostream & ostrm ()
StreamDatastrmdata ()
void closeFail (bool keeplock=false)
bool closeSuccess (bool keeplock=false)
const char * fileName () const
bool remove ()

Public Attributes

bool usebakwhenmissing_
 default=true
bool removebakonsuccess_
 default=false
int lockretries_
 default=10
double lockwaitincr_
 default=0.5 (seconds)
bool allowlockremove_

Protected Member Functions

bool openRead (bool)
bool openWrite (bool)
bool commitWrite ()
bool doClose (bool, bool)
bool haveLock () const
bool waitForLock () const
void mkLock (bool)
void rmLock ()

Protected Attributes

const bool locked_
const BufferString filenm_
const BufferString lockfnm_
const BufferString bakfnm_
const BufferString newfnm_
BufferString errmsg_
StreamData sd_

Detailed Description

Protects file IO when you can't afford to have partly written things after write errors or have a file garbled by multiple access.

Use the locking only when multiple processes can concurrently write to the same file. For most purposes, you won't need the locking, which is kind of expensive, too.

Note that you can either read or write the file, not both at the same time. At the end, one of closeFail() or closeSuccess() is absolutely mandatory. Thus, when something goes wrong during writing, use closeFail(), otherwise use closeSuccess(). For writing, these do different things, and closeSuccess() can fail.

When you write, you write to a new file, which will be renamed to the target filename on success (after the previous version is renamed to .bak). Note that success during writing (i.e. the integrity of the newly written file) is something you determine yourself.

When you use locking, you sometimes need to re-read the original file before writing. In that case, you need to keep the lock that was made for the reading. There is where you want to use the 'ignorelock' and 'keeplock' flags. Otherwise, don't specify these.


Constructor & Destructor Documentation

SafeFileIO::SafeFileIO ( const char *  ,
bool  locked = false 
)

Member Function Documentation

void SafeFileIO::closeFail ( bool  keeplock = false  )  [inline]
bool SafeFileIO::closeSuccess ( bool  keeplock = false  )  [inline]
bool SafeFileIO::commitWrite (  )  [protected]
bool SafeFileIO::doClose ( bool  ,
bool   
) [protected]
const char* SafeFileIO::errMsg (  )  const [inline]
const char* SafeFileIO::fileName (  )  const [inline]
bool SafeFileIO::haveLock (  )  const [protected]
std::istream& SafeFileIO::istrm (  )  [inline]
void SafeFileIO::mkLock ( bool   )  [protected]
bool SafeFileIO::open ( bool  forread,
bool  ignorelock = false 
)
bool SafeFileIO::openRead ( bool   )  [protected]
bool SafeFileIO::openWrite ( bool   )  [protected]
std::ostream& SafeFileIO::ostrm (  )  [inline]
bool SafeFileIO::remove (  ) 
void SafeFileIO::rmLock (  )  [protected]
StreamData& SafeFileIO::strmdata (  )  [inline]
bool SafeFileIO::waitForLock (  )  const [protected]

Member Data Documentation

default=true when true, will remove the lock after retries i.e. we'll assume the lock is phony then this is safety-- but robustness++

const BufferString SafeFileIO::bakfnm_ [protected]
BufferString SafeFileIO::errmsg_ [mutable, protected]
const BufferString SafeFileIO::filenm_ [protected]
const bool SafeFileIO::locked_ [protected]

default=10

default=0.5 (seconds)

const BufferString SafeFileIO::newfnm_ [protected]

default=false

default=true