Skip to main content

write_atomic

Function write_atomic 

Source
pub fn write_atomic(path: &Path, contents: &str) -> Result<()>
Expand description

Write contents to path atomically and durably: write a sibling temp file, flush it to disk, rename over the target, then flush the directory.

The rename alone makes the change atomic for a concurrent reader — nobody ever sees half a file. It does not make it survive a power loss, and those are two different guarantees that are easy to conflate. Without the first fsync the rename can land while the new file’s contents are still only in page cache, so the file comes back empty; without the second the rename itself can be lost, so the file comes back missing or with its previous contents.

The cost is one flush per config write. These files are written when a user changes a setting or a layout settles, not in a loop, and what is being protected is the only record of something the user cannot reconstruct — their profiles, their keybinds.