Skip to main content

atomic_write

Function atomic_write 

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

Atomically write content to a file via a temporary file and rename.

Resolves symlinks at the target path before persisting so the rename writes through to the symlink’s target file rather than replacing the symlink itself with a regular file (common when configs are mounted into containers via symlinks).

Preserves the target file’s existing permissions on Unix. NamedTempFile creates the temp with 0600 by default; persisting it directly would downgrade a target previously at 0644 (or the user’s local default) to owner-only, breaking shared workspaces and CI runners that rely on the pre-existing read bit. When the target does not yet exist, leave the temp’s mode as the OS default (the umask-respecting permissions the process would have produced via std::fs::write).