Swap two paths, atomically or as close as.
This tool uses (in order of preference):
renameat2syscall on Linux 3.15+; (atomic)
renamex_npfunction on macOS and APFS; (atomic)
exchangedatasyscall on macOS; (atomic)
renameat2syscall on WSL2, assuming it supports that when released; (atomic)
MoveFileTransactedfunction on Windows systems that support Transactional NTFS; (atomic)
- as first fallback, for small files, memory-mapping both files and swapping their contents;
- as second fallback, hardlinks and renaming;
- as third fallback, renames only.
cargo install swp
Or use a prebuilt release.
$ swp filea fileb
-v, it prints out the swapped paths on success:
$ swp -v filea fileb filea <-> fileb
The tool is also a library:
let a = PathBuf::from("filea"); let b = PathBuf::from("fileb"); swp::swap_paths(&a, &b)?;
And you can select one or more strategies if you don't want the default behaviour:
swp::swap_paths_with_strategies(&a, &b, &[ swp::Strategy::RenameAt2, swp::Strategy::ExchangeData, swp::Strategy::TwoRenames, ])?;
There are official binding libraries for:
You can also use a general dynlib
All these are available in the pre-built releases.
Made by Félix Saparelli.
Licensed under Artistic 2.0.