Skip to main content

atomic_write

Function atomic_write 

Source
pub async fn atomic_write(
    path: impl AsRef<Path> + Send + Sync,
    bytes: &[u8],
) -> Result<(), WriteError>
Expand description

Atomically overwrite the file at path with bytes.

The new contents are written to a temporary file which is then renamed over path. To keep that rename atomic, the temporary file is staged in the same directory as the canonical destination: if path (or one of its parent directories) is a symlink crossing a filesystem boundary, staging the temporary file beside the symlink would put it on a different mount point and the rename would fail with a cross-device link error (OS 18). Resolving to the real location first avoids that.

If the destination already exists its permissions are preserved. Otherwise, on Unix the file is created with 0600 permissions (configuration files may contain secrets and should not be world-readable); on other platforms the default permissions are used. Any missing parent directories are created.

ยงErrors

[WriteError] if the destination cannot be resolved, the temporary file cannot be written, or the final rename fails.