#file #utility #disk #path

safe-write

A safe file writing utility

3 releases

new 0.1.2 Apr 3, 2025
0.1.1 Dec 12, 2024
0.1.0 Dec 12, 2024

#849 in Filesystem

Download history 102/week @ 2024-12-14 41/week @ 2024-12-21 5/week @ 2024-12-28 13/week @ 2025-01-04 53/week @ 2025-01-11 12/week @ 2025-01-18 2/week @ 2025-01-25 23/week @ 2025-02-01 27/week @ 2025-02-08 15/week @ 2025-02-15 50/week @ 2025-02-22 40/week @ 2025-03-01 22/week @ 2025-03-08 37/week @ 2025-03-15 21/week @ 2025-03-22 118/week @ 2025-03-29

201 downloads per month

MIT license

5KB
67 lines

A crate for safely writing files using an atomic write pattern.

This crate implements a safe file writing strategy that helps prevent file corruption in case of system crashes or power failures. It follows these steps:

  1. Creates parent directories if they don't exist
  2. Writes content to a temporary file
  3. Ensures the content is fully written to disk
  4. Atomically renames the temporary file to the target path

Examples

use safe_write::safe_write;

let content = b"Hello, World!";
safe_write("example.txt", content).expect("Failed to write file");

Platform-specific behavior

On Windows, if the target file exists, it will be explicitly removed before the rename operation since Windows doesn't support atomic file replacement.

Dependencies

~84KB