Function reflink

Source
pub fn reflink<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()>
Expand description

Copies a file using COW semantics.

For compatibility reasons with macos, the target file will be created using OpenOptions::create_new. If you want to overwrite existing files, make sure you manually delete the target file first if it exists.

use reflink;
match reflink::reflink("src.txt", "dest.txt") {
    Ok(()) => println!("file has been reflinked"),
    Err(e) => println!("error while reflinking: {:?}", e)
}

§Implementation details per platform

§Linux / Android

Uses ioctl_ficlone. Supported file systems include btrfs and XFS (and maybe more in the future).

§OS X / ios

Uses clonefile library function. This is supported on OS X Version >=10.12 and iOS version >= 10.0 This will work on APFS partitions (which means most desktop systems are capable).

§Windows

Uses ioctl FSCTL_DUPLICATE_EXTENTS_TO_FILE. Only supports ReFS on Windows Server. Important note: The windows implementation is currently untested and probably buggy. Contributions/testers with access to a Windows Server welcome.

Examples found in repository?
examples/compare.rs (line 13)
7fn main() {
8    let mut base_file = fs::File::create("base.txt").unwrap();
9    let mut src = io::repeat(65).take(100 * 1024 * 1024); // 100 MB
10    io::copy(&mut src, &mut base_file).unwrap();
11
12    let before_reflink = Instant::now();
13    match reflink::reflink("base.txt", "reflinked.txt") {
14        Ok(()) => {},
15        Err(e) => {
16            println!("Error during reflinking:\n{:?}", e);
17            fs::remove_file("base.txt").unwrap();
18            return;
19        }
20    };
21    println!("Time to reflink: {:?}", Instant::now() - before_reflink);
22
23    let before_copy = Instant::now();
24    fs::copy("base.txt", "copied.txt").unwrap();
25    println!("Time to copy: {:?}", Instant::now() - before_copy);
26
27    fs::remove_file("base.txt").unwrap();
28    fs::remove_file("reflinked.txt").unwrap();
29    fs::remove_file("copied.txt").unwrap();
30}