File: reflink.rs

package info (click to toggle)
rust-reflink-copy 0.1.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: makefile: 2
file content (128 lines) | stat: -rw-r--r-- 3,575 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::fs::{self, File};
use std::io;
use std::path::Path;
use tempfile::tempdir;

use reflink_copy::{reflink, reflink_or_copy};

#[test]
fn reflink_file_does_not_exist() {
    let from = Path::new("test/nonexistent-bogus-path");
    let to = Path::new("test/other-bogus-path");

    reflink(from, to).unwrap_err();
    assert!(!from.exists());
    assert!(!to.exists());
}

#[test]
fn reflink_src_does_not_exist() {
    let tmpdir = tempdir().unwrap();
    let from = Path::new("test/nonexistent-bogus-path");
    let to = tmpdir.path().join("out.txt");

    fs::write(&to, b"hello").unwrap();
    assert!(reflink(from, &to).is_err());

    assert!(!from.exists());
    assert_eq!(fs::read(&to).unwrap(), b"hello");
}

#[test]
fn reflink_dest_is_dir() {
    let dir = tempdir().unwrap();
    let src_file_path = dir.path().join("src.txt");
    let _src_file = File::create(&src_file_path).unwrap();
    let err = reflink(&src_file_path, dir.path()).unwrap_err();
    println!("{:?}", err);
    if cfg!(windows) {
        assert_eq!(err.kind(), io::ErrorKind::PermissionDenied);
    } else {
        assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
    }
}

#[cfg(not(any(
    target_os = "macos",
    target_os = "ios",
    target_os = "tvos",
    target_os = "watchos"
)))]
#[test]
fn reflink_src_is_dir() {
    let dir = tempdir().unwrap();
    let dest_file_path = dir.path().join("dest.txt");

    let err = reflink(dir.path(), dest_file_path).unwrap_err();
    println!("{:?}", err);
    assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
}

#[test]
fn reflink_existing_dest_dir_results_in_error() {
    let dir = tempdir().unwrap();
    let src_file_path = dir.path().join("src");
    let dest_file_path = dir.path().join("dest");

    fs::create_dir(&src_file_path).unwrap();
    fs::create_dir(&dest_file_path).unwrap();

    let err = reflink(&src_file_path, &dest_file_path).unwrap_err();
    println!("{:?}", err);
    if cfg!(any(
        target_os = "macos",
        target_os = "ios",
        target_os = "tvos",
        target_os = "watchos"
    )) {
        assert_eq!(err.kind(), io::ErrorKind::AlreadyExists);
    } else {
        assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
    }
}

#[test]
fn reflink_existing_dest_results_in_error() {
    let dir = tempdir().unwrap();
    let src_file_path = dir.path().join("src.txt");
    let dest_file_path = dir.path().join("dest.txt");

    let _src_file = File::create(&src_file_path).unwrap();
    let _dest_file = File::create(&dest_file_path).unwrap();

    let err = reflink(&src_file_path, &dest_file_path).unwrap_err();
    println!("{:?}", err);
    assert_eq!(err.kind(), io::ErrorKind::AlreadyExists)
}

#[test]
fn reflink_ok() {
    let dir = tempdir().unwrap();
    let src_file_path = dir.path().join("src.txt");
    let dest_file_path = dir.path().join("dest.txt");

    fs::write(&src_file_path, b"this is a test").unwrap();

    let err = reflink(&src_file_path, dest_file_path);
    println!("{:?}", err);
    // do not panic for now, CI envs are old and will probably error out
    // assert_eq!(fs::read(&dest_file_path).unwrap(), b"this is a test");
}

#[test]
fn reflink_or_copy_ok() {
    let tmpdir = tempdir().unwrap();
    let input = tmpdir.path().join("in.txt");
    let out = tmpdir.path().join("out.txt");

    fs::write(&input, b"hello").unwrap();

    reflink_or_copy(&input, &out).unwrap();

    assert_eq!(fs::read(&out).unwrap(), b"hello");

    assert_eq!(
        input.metadata().unwrap().permissions(),
        out.metadata().unwrap().permissions()
    );
}