BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Public Member Functions | Protected Member Functions
BeeeOn::SafeWriter Class Reference

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>

Inheritance diagram for BeeeOn::SafeWriter:
BeeeOn::Loggable

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

Detailed Description

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).

Member Function Documentation

void SafeWriter::commitAs ( const Poco::File &  targetFile)

Commit the prepared temporary file as follows:

  • flush the underlying output stream
  • read back the temporary file and validate checksum of read-back data
  • rename the temporary lock file to the target destination
void SafeWriter::createLockFile ( bool  force)
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.

Exceptions
FileExistsExceptionwhen 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.

Returns
digest and size of the written data
void SafeWriter::reset ( )

Reset the writer and allow to try writing again before commit succeeds.

Exceptions
IllegalStateExceptionif already committed
ostream & SafeWriter::stream ( bool  force = false)
Returns
the underlying output stream unless already committed
Exceptions
Poco::IllegalStateExceptionin the case when the writter has already been committed.

The documentation for this class was generated from the following files: