Function renamore::rename_exclusive

source ·
pub fn rename_exclusive<F: AsRef<Path>, T: AsRef<Path>>(
    from: F,
    to: T
) -> Result<()>
Expand description

Rename a file without overwriting the destination path if it exists.

Unlike a combination of try_exists and rename, this operation is atomic. A potential TOCTTOU bug is avoided. There is no possibility of to coming into existence at just the wrong moment and being overwritten.

Platform-specific behaviour

On Linux, this calls renameat2 with RENAME_NOREPLACE. On Darwin (macOS, iOS, watchOS, tvOS), this calls renamex_np with RENAME_EXCL. On Windows, this calls MoveFileExW with no flags. On all other platforms, this returns ErrorKind::Unsupported unconditionally.

Errors

Performing this operation atomically is not supported on all platforms. If it’s not supported but the rename request is otherwise valid, then ErrorKind::Unsupported will be returned. If the operation is supported but a file at to exists, then ErrorKind::AlreadyExists will be returned.