Skip to main content

Module atomic

Module atomic 

Source
Expand description

Atomic file writes.

Plain fs::write truncates the target to zero length and then writes the new contents in place. A crash / kill / disk-full between those two steps leaves the file empty or half-written โ€” catastrophic for session, checkpoint, and plugin-lockfile state that is rewritten in full on every save. write_atomic writes to a temp sibling, fsyncs it, then renames over the target, so a reader always sees either the old complete file or the new complete file.

Functionsยง

write_atomic
Write bytes to path atomically: temp file in the same directory โ†’ sync_all โ†’ rename over the destination. The rename is atomic on the same filesystem (and replaces an existing target on both Unix and Windows).
write_atomic_with_mode
Like write_atomic, but create the temp file with the given Unix permission mode (e.g. 0o600) so the renamed destination is never even briefly world-readable. mode is ignored on non-Unix, where directory ACLs scope the file. Use this for secret-bearing files such as the config.