Wrapper class that guarantees that file commit atomicity. More...
#include <fs_commit.hpp>
Public Member Functions | |
atomic_commit (const std::string &filename) | |
Constructor. More... | |
atomic_commit (const atomic_commit &)=delete | |
atomic_commit & | operator= (const atomic_commit &)=delete |
~atomic_commit () | |
scoped_ostream & | ostream () |
Returns the write stream associated with the file. More... | |
void | commit () |
Commits the new file contents to disk atomically. More... | |
Private Attributes | |
std::string | temp_name_ |
std::string | dest_name_ |
scoped_ostream | out_ |
int | outfd_ |
Wrapper class that guarantees that file commit atomicity.
It is possible for a signal or exception to cause a file write to be aborted in the middle of the process, leaving potentially inconsistent contents behind which may be read by the same or another application later and result in errors or further data loss.
This wrapper prevents this by providing callers with an interface to request a write stream that will be actually associated to a temporary file. Once the caller is done with the file, it should call the commit() method to complete the process so that the temporary replaces the original in an atomic step.
The rationale for using an explicit commit() method instead of the class destructor is that the destructor will also be invoked during exception handling, which could still cause the destination file to end up in an inconsistent state.
Note that if the destructor is invoked before commit() is, the temporary file will be left behind. This is deliberate so as to provide a way for the user to look at the resulting file and optionally try to reconcile it against the original.
Definition at line 52 of file fs_commit.hpp.
filesystem::atomic_commit::atomic_commit | ( | const std::string & | filename | ) |
Constructor.
filesystem::io_exception | if the operation fails in some way. |
Definition at line 185 of file fs_commit.cpp.
References dest_name_, LOG_FS, and temp_name_.
|
delete |
filesystem::atomic_commit::~atomic_commit | ( | ) |
Definition at line 199 of file fs_commit.cpp.
References ERR_FS, and temp_name_.
void filesystem::atomic_commit::commit | ( | ) |
Commits the new file contents to disk atomically.
filesystem::io_exception | if the operation fails in some way. |
Definition at line 206 of file fs_commit.cpp.
References dest_name_, ERR_FS, LOG_FS, outfd_, and temp_name_.
Referenced by campaignd::server::load_config(), and campaignd::server::write_config().
|
delete |
|
inline |
Returns the write stream associated with the file.
Before commit() is invoked, this refers to the temporary file; afterwards, to the destination.
Definition at line 73 of file fs_commit.hpp.
References out_.
Referenced by campaignd::server::load_config(), and campaignd::server::write_config().
|
private |
Definition at line 87 of file fs_commit.hpp.
Referenced by atomic_commit(), and commit().
|
private |
Definition at line 88 of file fs_commit.hpp.
Referenced by ostream().
|
private |
Definition at line 90 of file fs_commit.hpp.
Referenced by commit().
|
private |
Definition at line 86 of file fs_commit.hpp.
Referenced by atomic_commit(), commit(), and ~atomic_commit().