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.
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] |
| 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++