Skip to main content

replace_file

Function replace_file 

Source
pub fn replace_file(from: &Path, to: &Path) -> Result<()>
Expand description

Replaces to with from, atomically where the platform allows it.

This is std::fs::rename everywhere. On Windows, std tries MoveFileExW and, when that is refused with ERROR_ACCESS_DENIED — as it is while any handle is open on to — retries through FileRenameInfoEx with POSIX semantics, which replace to regardless (Windows 10 1607 and later, on a file system that supports it). A bare MoveFileExW stops at the refusal.

The rename is not durable on its own on any platform: callers follow it with sync_parent_directory. They rename from the same directory, so the MOVEFILE_WRITE_THROUGH flag std does not pass would add nothing — it only flushes a move that crosses volumes.