Skip to main content

atomic_write

Function atomic_write 

Source
pub fn atomic_write(path: &Path, contents: &[u8], mode: u32) -> Result<()>
Expand description

Write contents to path atomically with the given permission mode.

Guarantees:

  • The file is created with mode from byte zero (no world-readable window even for secret-bearing files written with 0o600).
  • The file never appears half-written on disk — either the old contents or the new contents exist after this call, never a torn state. Achieved by writing to a sibling .<name>.tmp.<pid> file and rename-ing over the target (atomic on a single POSIX filesystem).

The temporary file is written to the same directory as path so the rename stays within one filesystem. sync_all is called before the rename so the new content hits disk before the rename metadata op.