Skip to main content

write_atomic

Function write_atomic 

Source
pub fn write_atomic(path: &Path, data: &[u8]) -> Result<()>
Expand description

Replace path with data atomically, without fsync.

The data goes to a uniquely named temp file beside path and is then renamed over path. Readers see the old file or the new file, never a mix. Concurrent writers each move their own complete payload into place, and the last rename wins. A failed write removes its temp file.

Use this function only for a cache that fallow can build again. After a crash or a power loss, the file can be empty or cut short. The cache loaders refuse such a file when its framing or its payload does not decode. On macOS, File::sync_all flushes the drive cache. That flush was 60% to 90% of the parse cache save after a one-file edit. For data that fallow cannot build again, use write_atomic_synced.

ยงErrors

Returns the IO error when path has no parent directory or file name, or when the temp file cannot be created, written, or renamed into place.