OpendTect-6_4  6.4
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
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...

Public Member Functions

 SafeFileIO (const char *, bool locked=false)
 
 ~SafeFileIO ()
 
bool open (bool forread, bool ignorelock=false)
 
const char * errMsg () const
 
od_istreamistrm ()
 
od_ostreamostrm ()
 
void closeFail (bool keeplock=false)
 
bool closeSuccess (bool keeplock=false)
 
const char * fileName () const
 
bool remove ()
 

Public Attributes

bool usebakwhenmissing_
 default=true More...
 
bool removebakonsuccess_
 default=false More...
 
int lockretries_
 default=10 More...
 
double lockwaitincr_
 default=0.5 (seconds) More...
 
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_
 
od_streamstrm_
 

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 
)
SafeFileIO::~SafeFileIO ( )

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
od_istream& SafeFileIO::istrm ( )
void SafeFileIO::mkLock ( bool  )
protected
bool SafeFileIO::open ( bool  forread,
bool  ignorelock = false 
)
bool SafeFileIO::openRead ( bool  )
protected
bool SafeFileIO::openWrite ( bool  )
protected
od_ostream& SafeFileIO::ostrm ( )
bool SafeFileIO::remove ( )
void SafeFileIO::rmLock ( )
protected
bool SafeFileIO::waitForLock ( ) const
protected

Member Data Documentation

bool SafeFileIO::allowlockremove_

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_
mutableprotected
const BufferString SafeFileIO::filenm_
protected
const bool SafeFileIO::locked_
protected
const BufferString SafeFileIO::lockfnm_
protected
int SafeFileIO::lockretries_

default=10

double SafeFileIO::lockwaitincr_

default=0.5 (seconds)

const BufferString SafeFileIO::newfnm_
protected
bool SafeFileIO::removebakonsuccess_

default=false

od_stream* SafeFileIO::strm_
protected
bool SafeFileIO::usebakwhenmissing_

default=true


Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019