More ways to rename files.
The Rust standard library offers [std::fs::rename
] for renaming files.
Sometimes, that's not enough. Consider the example of renaming a file but
aborting the operation if something already exists at the destination path.
That can be achieved using the Rust standard library but ensuring that the
operation is atomic can only be achieved using platform-specific APIs.
Without using platform-specific APIs, a [TOCTTOU] bug can be introduced.
This library aims to provide a cross-platform interface to these APIs.
Renaming a file without the possibility of accidentally overwriting anything
can be done using [rename_exclusive
]. It should be noted that this feature
is not supported by all combinations of operation system and file system.
The return value will indicate whether the operation was performed
atomically or whether a non-atomic fallback was used.
```rust use std::io::Result; use std::path::PathBuf;
fn main() -> Result<()> { let from = PathBuf::from("old.txt"); let to = PathBuf::from("new.txt");
if renamore::rename_exclusive(&from, &to)? {
// `new.txt` was definitely not overwritten.
println!("The operation was atomic");
} else {
// `new.txt` was probably not overwritten.
println!("The operation was not atomic");
}
Ok(())
} ```
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.