OpendTect  7.0
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 ()
 
void closeFail (bool keeplock=false)
 
bool closeSuccess (bool keeplock=false)
 
const char * errMsg () const
 
const char * fileName () const
 
od_istreamistrm ()
 
bool open (bool forread, bool ignorelock=false)
 
od_ostreamostrm ()
 
bool remove ()
 

Public Attributes

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

Protected Member Functions

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

Protected Attributes

const BufferString bakfnm_
 
BufferString errmsg_
 
const BufferString filenm_
 
const bool locked_
 
const BufferString lockfnm_
 
const BufferString newfnm_
 
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::SafeFileIO ( const char *  ,
bool  locked = false 
)

◆ ~SafeFileIO()

SafeFileIO::~SafeFileIO ( )

Member Function Documentation

◆ closeFail()

void SafeFileIO::closeFail ( bool  keeplock = false)
inline

◆ closeSuccess()

bool SafeFileIO::closeSuccess ( bool  keeplock = false)
inline

◆ commitWrite()

bool SafeFileIO::commitWrite ( )
protected

◆ doClose()

bool SafeFileIO::doClose ( bool  ,
bool   
)
protected

◆ errMsg()

const char* SafeFileIO::errMsg ( ) const
inline

◆ fileName()

const char* SafeFileIO::fileName ( ) const
inline

◆ haveLock()

bool SafeFileIO::haveLock ( ) const
protected

◆ istrm()

od_istream& SafeFileIO::istrm ( )

◆ mkLock()

void SafeFileIO::mkLock ( bool  )
protected

◆ open()

bool SafeFileIO::open ( bool  forread,
bool  ignorelock = false 
)

◆ openRead()

bool SafeFileIO::openRead ( bool  )
protected

◆ openWrite()

bool SafeFileIO::openWrite ( bool  )
protected

◆ ostrm()

od_ostream& SafeFileIO::ostrm ( )

◆ remove()

bool SafeFileIO::remove ( )

◆ rmLock()

void SafeFileIO::rmLock ( )
protected

◆ waitForLock()

bool SafeFileIO::waitForLock ( ) const
protected

Member Data Documentation

◆ allowlockremove_

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

◆ bakfnm_

const BufferString SafeFileIO::bakfnm_
protected

◆ errmsg_

BufferString SafeFileIO::errmsg_
mutableprotected

◆ filenm_

const BufferString SafeFileIO::filenm_
protected

◆ locked_

const bool SafeFileIO::locked_
protected

◆ lockfnm_

const BufferString SafeFileIO::lockfnm_
protected

◆ lockretries_

int SafeFileIO::lockretries_

default=10

◆ lockwaitincr_

double SafeFileIO::lockwaitincr_

default=0.5 (seconds)

◆ newfnm_

const BufferString SafeFileIO::newfnm_
protected

◆ removebakonsuccess_

bool SafeFileIO::removebakonsuccess_

default=false

◆ strm_

od_stream* SafeFileIO::strm_
protected

◆ usebakwhenmissing_

bool SafeFileIO::usebakwhenmissing_

default=true


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