/swp

Swap two paths, atomically or as close as

Primary LanguageRustOtherNOASSERTION

swp (wip)

Swap two paths, atomically or as close as.

This tool uses (in order of preference):

  • the renameat2 syscall on Linux 3.15+; (atomic)
  • the renamex_np function on macOS and APFS; (atomic)
  • the exchangedata syscall on macOS; (atomic)
  • the renameat2 syscall on WSL2, assuming it supports that when released; (atomic)
  • the MoveFileTransacted function 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.

Install

From source:

cargo install swp

Or use a prebuilt release.

Usage

$ 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,
])?;

Bindings

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.

About

Made by Félix Saparelli.

Prior art:

Licensed under Artistic 2.0.