Skip to main content

Module atomic

Module atomic 

Source
Expand description

Crash-safe file writes.

Every write of something a user would mind losing goes through write_atomically. std::fs::write truncates the target and then fills it, so a crash, a kill, or a power cut partway through leaves a half-written file where the previous good one used to be โ€” and for the document save that is the drawing itself.

Functionsยง

parent_dir ๐Ÿ”’
The directory holding target. A bare relative filename has an empty parent, which names nothing openable.
resolve ๐Ÿ”’
Follow symlinks, so writing a symlinked document replaces what it points at โ€” the way fs::write does. Renaming onto the link would instead replace the link with a regular file and quietly detach it from its target.
sync_dir ๐Ÿ”’
Flush the directory entry itself, so the rename survives a power cut and not just a process crash.
temp_path ๐Ÿ”’
A dot-prefixed sibling of target. It has to share the directory: rename is only atomic within one filesystem.
write_and_replace ๐Ÿ”’
write_atomically
Write contents to path so that dying partway through leaves the previous contents intact: the bytes land in a sibling temporary file, are flushed to the storage device, and only then replace path with one atomic rename.