2 releases

0.1.1 Dec 12, 2024
0.1.0 Dec 12, 2024

#778 in Filesystem

Download history 189/week @ 2024-12-06 151/week @ 2024-12-13 38/week @ 2024-12-20 10/week @ 2024-12-27 7/week @ 2025-01-03 53/week @ 2025-01-10 18/week @ 2025-01-17 2/week @ 2025-01-24 21/week @ 2025-01-31 22/week @ 2025-02-07 20/week @ 2025-02-14 40/week @ 2025-02-21 36/week @ 2025-02-28

128 downloads per month

MIT license

5KB
64 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

~85KB