BeeeOn Gateway
v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
|
SafeWriter is a helper that allows to perform file writes that are atomic. SafeWriter always rewrites a whole file. However, it either rewrites that file or it does nothing. More...
#include <SafeWriter.h>
Public Member Functions | |
SafeWriter (const Poco::File &tmpFile) | |
SafeWriter (const Poco::File &tmpFile, const std::string &ext) | |
std::ostream & | stream (bool force=false) |
void | flush () |
Flush the given stream. It is recommended to prefer this call instead of stream().flush() because the underlying stream implementation might not propagate flushing properly. | |
void | reset () |
Reset the writer and allow to try writing again before commit succeeds. More... | |
std::pair < Poco::DigestEngine::Digest, size_t > | finalize () |
void | commitAs (const Poco::File &targetFile) |
Commit the prepared temporary file as follows: More... | |
Protected Member Functions | |
void | createLockFile (bool force) |
Create the temporary lock file atomically. It fails in the case when the lock file already exists unless force is true. If the parameter force is true, then the already existing temporary lock file is first removed and then again recreated. We continue this process until we successfully create the temporary file atomically. More... | |
SafeWriter is a helper that allows to perform file writes that are atomic. SafeWriter always rewrites a whole file. However, it either rewrites that file or it does nothing.
The actual writes are performed into a temporary file first. If writing fails. the file is simply removed. If writing succeeds, the SafeWriter checks it for consistency (via SHA-1 checksum) and if the stored file's checksum is correct, it renames the temporary file to the target file (this should be atomic operation on most operating systems).
void SafeWriter::commitAs | ( | const Poco::File & | targetFile | ) |
Commit the prepared temporary file as follows:
|
protected |
Create the temporary lock file atomically. It fails in the case when the lock file already exists unless force is true. If the parameter force is true, then the already existing temporary lock file is first removed and then again recreated. We continue this process until we successfully create the temporary file atomically.
FileExistsException | when the lock file already exists |
pair< DigestEngine::Digest, size_t > SafeWriter::finalize | ( | ) |
Finalize writing into the underlying stream. This is not a commit but no more writes are possible after this call.
void SafeWriter::reset | ( | ) |
Reset the writer and allow to try writing again before commit succeeds.
IllegalStateException | if already committed |
ostream & SafeWriter::stream | ( | bool | force = false | ) |
Poco::IllegalStateException | in the case when the writter has already been committed. |