Function atomic_write

Source
pub fn atomic_write(
    path: &Path,
    mode_perms: u32,
    fsync: bool,
    op: impl FnOnce(&mut File) -> Result<()>,
) -> Result<File>
Expand description

Create a temp file and then rename it into the specified path to achieve atomicity. The temp file is created in the same directory as path to ensure the rename is not cross filesystem. If fysnc is true, the file will be fsynced before and after renaming, and the directory will by fsynced after renaming.

mode_perms is required but does nothing on windows. mode_perms is not automatically umasked.

The renamed file is returned. Any further data written to the file will not be atomic since the file is already visibile to readers.

Note that the rename operation will fail on windows if the destination file exists and is open.