Swap two paths, atomically or as close as.
This tool uses (in order of preference):
- the
renameat2syscall on Linux 3.15+; (atomic) - the
renamex_npfunction on macOS and APFS; (atomic) - the
exchangedatasyscall on macOS; (atomic) - the
renameat2syscall on WSL2, assuming it supports that when released; (atomic) - the
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.
From source:
cargo install swp
Or use a prebuilt release.
$ swp filea fileb
With -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:
- C
- Node.js
- Ruby
- PHP
You can also use a general dynlib .so or .dll.
All these are available in the pre-built releases.
Made by Félix Saparelli.
Prior art:
- renameat2 tool: linux only
- swap: linux only
- fs-swap: platform support, errors if unsupported
Licensed under Artistic 2.0.