|
| | AtomicFileWriter (fs::path p) |
| | Construct an atomic writer targeting p with default options.
|
| | AtomicFileWriter (fs::path p, Options options) |
| | Construct an atomic writer targeting p with explicit options.
|
|
| DFX_PRIVATE_STATE_DECLARE_RULE_OF_5 (AtomicFileWriter) |
| fs::path const & | path () const noexcept |
| | Returns the target path.
|
| State | state () const noexcept |
| | Returns the current state.
|
| bool | isOpen () const noexcept |
| | Convenience: true if state() == State::Open.
|
| bool | isDiscarded () const noexcept |
| | Convenience: true if state() == State::Discarded.
|
| bool | isPublished () const noexcept |
| | Convenience: true if the file has been published (atomically visible).
|
| bool | isDurable () const noexcept |
| | Convenience: true if the file publish operation is durable.
|
| void | write (std::byte const *data, std::size_t len) |
| | Append raw bytes to the staged file.
|
| void | write (std::span< std::byte const > data) |
| | Append raw bytes to the staged file.
|
| void | write (std::string_view data) |
| | Append text/binary data to the staged file.
|
| void | commit () |
| | Publish the staged file to the target path.
|
| void | discard () noexcept |
| | Discard the staged file and release resources.
|
Atomically writes a file by staging data and publishing it in a single step.
AtomicFileWriter implements the common "write-then-atomic-replace" pattern with explicit durability options.
The writer creates a temporary inode in the same directory as the target file, writes all data to it, and on commit() publishes it atomically as the final path.
This class provides two distinct notions of success:
- Published: the directory entry has been updated atomically to point to the new file.
- Durable: the publish operation has been made durable against power loss by syncing the parent directory (when enabled).
- Note
- The destructor never commits implicitly. If you never call commit(), changes are not published. The destructor will call discard() and release resources.
- commit() is idempotent after a successful commit (Published/Durable). Calling it again is a no-op.
- Obviously once discarded, the writer cannot be committed.
- Durability guarantees
- When Options::fsyncFile is enabled, commit() syncs the staged file contents prior to publishing. When Options::fsyncDir is enabled, commit() also attempts to sync the parent directory so that the directory entry update survives a crash or power loss.
If syncing the directory fails, the file may still be Published (visible and replaced) but not Durable (not guaranteed to persist across sudden power loss). In that case the state remains Published and a warning may be emitted.
- Thread-safety
- This class is not thread-safe. External synchronization is required if accessed from multiple threads.
- Typical usage
w.write(serializedJson);
w.commit();
Atomically writes a file by staging data and publishing it in a single step.
Definition AtomicFileWriter.hpp:64