Skip to main content

Module atomic_write

Module atomic_write 

Source
Expand description

Atomic file write helpers for the EnvironmentStore.

The store mutates persistent state through the same pattern everywhere:

  1. Create a NamedTempFile in the same directory as the target so the final rename is intra-filesystem (otherwise rename would fall back to copy+unlink and lose atomicity).
  2. Write the bytes; flush() + sync_all() so the data hits disk.
  3. persist(target) atomically renames over the existing target.
  4. On Unix, fsync() the parent directory so the rename itself is durable across power loss.

Callers that want to back up the current target before clobbering it should call copy_to_backup first.

Enums§

AtomicWriteError

Functions§

atomic_write_bytes
Atomically write bytes to target, fsyncing the parent directory afterward.
atomic_write_json
Atomically write value as pretty-printed JSON (trailing newline) to target.
copy_to_backup
If target exists, copy it to <backup_dir>/<target.file_name>.<rfc3339-utc>.bak and return the backup path. If it does not exist, return Ok(None).