summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/tests/fs/utimensat.rs
blob: f0fb7e27b6646974df236437d3a78831062fef70 (plain)
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
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
#[test]
fn test_utimensat() {
    use rustix::fs::{cwd, openat, statat, utimensat, AtFlags, Mode, OFlags, Timespec, Timestamps};

    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(
        cwd(),
        tmp.path(),
        OFlags::RDONLY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .unwrap();

    let _ = openat(
        &dir,
        "foo",
        OFlags::CREATE | OFlags::WRONLY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .unwrap();

    let times = Timestamps {
        last_access: Timespec {
            tv_sec: 44000,
            tv_nsec: 45000,
        },
        last_modification: Timespec {
            tv_sec: 46000,
            tv_nsec: 47000,
        },
    };
    utimensat(&dir, "foo", &times, AtFlags::empty()).unwrap();

    let after = statat(&dir, "foo", AtFlags::empty()).unwrap();

    assert_eq!(times.last_modification.tv_sec as u64, after.st_mtime as u64);
    #[cfg(not(target_os = "netbsd"))]
    assert_eq!(
        times.last_modification.tv_nsec as u64,
        after.st_mtime_nsec as u64
    );
    #[cfg(target_os = "netbsd")]
    assert_eq!(
        times.last_modification.tv_nsec as u64,
        after.st_mtimensec as u64
    );
    assert!(times.last_access.tv_sec as u64 >= after.st_atime as u64);
    #[cfg(not(target_os = "netbsd"))]
    assert!(
        times.last_access.tv_sec as u64 > after.st_atime as u64
            || times.last_access.tv_nsec as u64 >= after.st_atime_nsec as u64
    );
    #[cfg(target_os = "netbsd")]
    assert!(
        times.last_access.tv_sec as u64 > after.st_atime as u64
            || times.last_access.tv_nsec as u64 >= after.st_atimensec as u64
    );
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
#[test]
fn test_utimensat_noent() {
    use rustix::fs::{cwd, openat, utimensat, AtFlags, Mode, OFlags, Timespec, Timestamps};

    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(
        cwd(),
        tmp.path(),
        OFlags::RDONLY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .unwrap();

    let times = Timestamps {
        last_access: Timespec {
            tv_sec: 44000,
            tv_nsec: 45000,
        },
        last_modification: Timespec {
            tv_sec: 46000,
            tv_nsec: 47000,
        },
    };
    assert_eq!(
        utimensat(&dir, "foo", &times, AtFlags::empty()).unwrap_err(),
        rustix::io::Errno::NOENT
    );
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
#[test]
fn test_utimensat_notdir() {
    use rustix::fs::{cwd, openat, utimensat, AtFlags, Mode, OFlags, Timespec, Timestamps};

    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(
        cwd(),
        tmp.path(),
        OFlags::RDONLY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .unwrap();

    let foo = openat(
        &dir,
        "foo",
        OFlags::CREATE | OFlags::WRONLY | OFlags::CLOEXEC,
        Mode::empty(),
    )
    .unwrap();

    let times = Timestamps {
        last_access: Timespec {
            tv_sec: 44000,
            tv_nsec: 45000,
        },
        last_modification: Timespec {
            tv_sec: 46000,
            tv_nsec: 47000,
        },
    };
    assert_eq!(
        utimensat(&foo, "bar", &times, AtFlags::empty()).unwrap_err(),
        rustix::io::Errno::NOTDIR
    );
}