A class that wraps a file output stream, providing improved tolerance for write failures. More...
Inherits noncopyable.
Public Member Functions | |
TF_API | TfAtomicOfstreamWrapper (const std::string &filePath) |
Constructor. More... | |
TF_API | ~TfAtomicOfstreamWrapper () |
Destructor. Calls Cancel(). More... | |
TF_API bool | Open (std::string *reason=0) |
Opens the temporary file for writing. More... | |
TF_API bool | Commit (std::string *reason=0) |
Synchronizes the temporary file contents to disk, and renames the temporary file into the file path passed to Open. More... | |
TF_API bool | Cancel (std::string *reason=0) |
Closes the temporary file and removes it from disk, if it exists. More... | |
std::ofstream & | GetStream () |
Returns the stream. More... | |
A class that wraps a file output stream, providing improved tolerance for write failures.
The wrapper opens an output file stream to a temporary file on the same file system as the desired destination file, and if no errors occur while writing the temporary file, it can be renamed atomically to the destination file name. In this way, write failures are encountered while writing the temporary file content, rather than while writing the destination file. This ensures that, if the destination existed prior to writing, it is left untouched in the event of a write failure, and if the destination did not exist, a partial file is not written.
Definition at line 79 of file atomicOfstreamWrapper.h.
|
explicit |
Constructor.
TF_API ~TfAtomicOfstreamWrapper | ( | ) |
Destructor. Calls Cancel().
TF_API bool Cancel | ( | std::string * | reason = 0 | ) |
Closes the temporary file and removes it from disk, if it exists.
TF_API bool Commit | ( | std::string * | reason = 0 | ) |
Synchronizes the temporary file contents to disk, and renames the temporary file into the file path passed to Open.
If the file path passed to the constructor names an existing file, the file, the file is atomically replaced with the temporary file. If the rename fails, false is returned and reason
is set to the reason for failure.
|
inline |
Returns the stream.
If this is called before a call to Open, the returned file stream is not yet initialized. If called after Commit or Cancel, the returned file stream is closed.
Definition at line 109 of file atomicOfstreamWrapper.h.
TF_API bool Open | ( | std::string * | reason = 0 | ) |
Opens the temporary file for writing.
If the destination directory does not exist, it is created. If the destination directory exists but is unwritable, the destination directory cannot be created, or the temporary file cannot be opened for writing in the destination directory, this method returns false and reason
is set to the reason for failure.