summaryrefslogtreecommitdiff
path: root/test/test_stat.rs
blob: 55f15c0771d0f24114fe30c9feebc4d07271e066 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use std::fs;
use std::fs::File;
#[cfg(not(target_os = "redox"))]
use std::os::unix::fs::symlink;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use std::os::unix::fs::PermissionsExt;
use std::os::unix::prelude::AsRawFd;
#[cfg(not(target_os = "redox"))]
use std::path::Path;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use std::time::{Duration, UNIX_EPOCH};

use libc::mode_t;
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use libc::{S_IFLNK, S_IFMT};

#[cfg(not(target_os = "redox"))]
use nix::errno::Errno;
#[cfg(not(target_os = "redox"))]
use nix::fcntl;
#[cfg(any(
    target_os = "linux",
    target_os = "ios",
    target_os = "macos",
    target_os = "freebsd",
    target_os = "netbsd"
))]
use nix::sys::stat::lutimes;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::stat::utimensat;
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::FchmodatFlags;
use nix::sys::stat::Mode;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::stat::UtimensatFlags;
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::{self};
use nix::sys::stat::{fchmod, stat};
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::{fchmodat, mkdirat};
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::stat::{futimens, utimes};

#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use nix::sys::stat::FileStat;

#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::time::{TimeSpec, TimeVal, TimeValLike};
#[cfg(not(target_os = "redox"))]
use nix::unistd::chdir;

#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use nix::Result;

#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn assert_stat_results(stat_result: Result<FileStat>) {
    let stats = stat_result.expect("stat call failed");
    assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
    assert!(stats.st_ino > 0); // inode is positive integer, exact number machine dependent
    assert!(stats.st_mode > 0); // must be positive integer
    assert_eq!(stats.st_nlink, 1); // there links created, must be 1
    assert_eq!(stats.st_size, 0); // size is 0 because we did not write anything to the file
    assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent
    assert!(stats.st_blocks <= 16); // Up to 16 blocks can be allocated for a blank file
}

#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
// (Android's st_blocks is ulonglong which is always non-negative.)
#[cfg_attr(target_os = "android", allow(unused_comparisons))]
#[allow(clippy::absurd_extreme_comparisons)] // Not absurd on all OSes
fn assert_lstat_results(stat_result: Result<FileStat>) {
    let stats = stat_result.expect("stat call failed");
    assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
    assert!(stats.st_ino > 0); // inode is positive integer, exact number machine dependent
    assert!(stats.st_mode > 0); // must be positive integer

    // st_mode is c_uint (u32 on Android) while S_IFMT is mode_t
    // (u16 on Android), and that will be a compile error.
    // On other platforms they are the same (either both are u16 or u32).
    assert_eq!(
        (stats.st_mode as usize) & (S_IFMT as usize),
        S_IFLNK as usize
    ); // should be a link
    assert_eq!(stats.st_nlink, 1); // there links created, must be 1
    assert!(stats.st_size > 0); // size is > 0 because it points to another file
    assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent

    // st_blocks depends on whether the machine's file system uses fast
    // or slow symlinks, so just make sure it's not negative
    assert!(stats.st_blocks >= 0);
}

#[test]
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn test_stat_and_fstat() {
    use nix::sys::stat::fstat;

    let tempdir = tempfile::tempdir().unwrap();
    let filename = tempdir.path().join("foo.txt");
    let file = File::create(&filename).unwrap();

    let stat_result = stat(&filename);
    assert_stat_results(stat_result);

    let fstat_result = fstat(file.as_raw_fd());
    assert_stat_results(fstat_result);
}

#[test]
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn test_fstatat() {
    let tempdir = tempfile::tempdir().unwrap();
    let filename = tempdir.path().join("foo.txt");
    File::create(&filename).unwrap();
    let dirfd =
        fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty());

    let result =
        stat::fstatat(dirfd.unwrap(), &filename, fcntl::AtFlags::empty());
    assert_stat_results(result);
}

#[test]
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn test_stat_fstat_lstat() {
    use nix::sys::stat::{fstat, lstat};

    let tempdir = tempfile::tempdir().unwrap();
    let filename = tempdir.path().join("bar.txt");
    let linkname = tempdir.path().join("barlink");

    File::create(&filename).unwrap();
    symlink("bar.txt", &linkname).unwrap();
    let link = File::open(&linkname).unwrap();

    // should be the same result as calling stat,
    // since it's a regular file
    let stat_result = stat(&filename);
    assert_stat_results(stat_result);

    let lstat_result = lstat(&linkname);
    assert_lstat_results(lstat_result);

    let fstat_result = fstat(link.as_raw_fd());
    assert_stat_results(fstat_result);
}

#[test]
fn test_fchmod() {
    let tempdir = tempfile::tempdir().unwrap();
    let filename = tempdir.path().join("foo.txt");
    let file = File::create(&filename).unwrap();

    let mut mode1 = Mode::empty();
    mode1.insert(Mode::S_IRUSR);
    mode1.insert(Mode::S_IWUSR);
    fchmod(file.as_raw_fd(), mode1).unwrap();

    let file_stat1 = stat(&filename).unwrap();
    assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());

    let mut mode2 = Mode::empty();
    mode2.insert(Mode::S_IROTH);
    fchmod(file.as_raw_fd(), mode2).unwrap();

    let file_stat2 = stat(&filename).unwrap();
    assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
}

#[test]
#[cfg(not(target_os = "redox"))]
fn test_fchmodat() {
    let _dr = crate::DirRestore::new();
    let tempdir = tempfile::tempdir().unwrap();
    let filename = "foo.txt";
    let fullpath = tempdir.path().join(filename);
    File::create(&fullpath).unwrap();

    let dirfd =
        fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
            .unwrap();

    let mut mode1 = Mode::empty();
    mode1.insert(Mode::S_IRUSR);
    mode1.insert(Mode::S_IWUSR);
    fchmodat(Some(dirfd), filename, mode1, FchmodatFlags::FollowSymlink)
        .unwrap();

    let file_stat1 = stat(&fullpath).unwrap();
    assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());

    chdir(tempdir.path()).unwrap();

    let mut mode2 = Mode::empty();
    mode2.insert(Mode::S_IROTH);
    fchmodat(None, filename, mode2, FchmodatFlags::FollowSymlink).unwrap();

    let file_stat2 = stat(&fullpath).unwrap();
    assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
}

/// Asserts that the atime and mtime in a file's metadata match expected values.
///
/// The atime and mtime are expressed with a resolution of seconds because some file systems
/// (like macOS's HFS+) do not have higher granularity.
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn assert_times_eq(
    exp_atime_sec: u64,
    exp_mtime_sec: u64,
    attr: &fs::Metadata,
) {
    assert_eq!(
        Duration::new(exp_atime_sec, 0),
        attr.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
    );
    assert_eq!(
        Duration::new(exp_mtime_sec, 0),
        attr.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
    );
}

#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_utimes() {
    let tempdir = tempfile::tempdir().unwrap();
    let fullpath = tempdir.path().join("file");
    drop(File::create(&fullpath).unwrap());

    utimes(&fullpath, &TimeVal::seconds(9990), &TimeVal::seconds(5550))
        .unwrap();
    assert_times_eq(9990, 5550, &fs::metadata(&fullpath).unwrap());
}

#[test]
#[cfg(any(
    target_os = "linux",
    target_os = "ios",
    target_os = "macos",
    target_os = "freebsd",
    target_os = "netbsd"
))]
fn test_lutimes() {
    let tempdir = tempfile::tempdir().unwrap();
    let target = tempdir.path().join("target");
    let fullpath = tempdir.path().join("symlink");
    drop(File::create(&target).unwrap());
    symlink(&target, &fullpath).unwrap();

    let exp_target_metadata = fs::symlink_metadata(&target).unwrap();
    lutimes(&fullpath, &TimeVal::seconds(4560), &TimeVal::seconds(1230))
        .unwrap();
    assert_times_eq(4560, 1230, &fs::symlink_metadata(&fullpath).unwrap());

    let target_metadata = fs::symlink_metadata(&target).unwrap();
    assert_eq!(
        exp_target_metadata.accessed().unwrap(),
        target_metadata.accessed().unwrap(),
        "atime of symlink target was unexpectedly modified"
    );
    assert_eq!(
        exp_target_metadata.modified().unwrap(),
        target_metadata.modified().unwrap(),
        "mtime of symlink target was unexpectedly modified"
    );
}

#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_futimens() {
    let tempdir = tempfile::tempdir().unwrap();
    let fullpath = tempdir.path().join("file");
    drop(File::create(&fullpath).unwrap());

    let fd = fcntl::open(&fullpath, fcntl::OFlag::empty(), stat::Mode::empty())
        .unwrap();

    futimens(fd, &TimeSpec::seconds(10), &TimeSpec::seconds(20)).unwrap();
    assert_times_eq(10, 20, &fs::metadata(&fullpath).unwrap());
}

#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_utimensat() {
    let _dr = crate::DirRestore::new();
    let tempdir = tempfile::tempdir().unwrap();
    let filename = "foo.txt";
    let fullpath = tempdir.path().join(filename);
    drop(File::create(&fullpath).unwrap());

    let dirfd =
        fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
            .unwrap();

    utimensat(
        Some(dirfd),
        filename,
        &TimeSpec::seconds(12345),
        &TimeSpec::seconds(678),
        UtimensatFlags::FollowSymlink,
    )
    .unwrap();
    assert_times_eq(12345, 678, &fs::metadata(&fullpath).unwrap());

    chdir(tempdir.path()).unwrap();

    utimensat(
        None,
        filename,
        &TimeSpec::seconds(500),
        &TimeSpec::seconds(800),
        UtimensatFlags::FollowSymlink,
    )
    .unwrap();
    assert_times_eq(500, 800, &fs::metadata(&fullpath).unwrap());
}

#[test]
#[cfg(not(target_os = "redox"))]
fn test_mkdirat_success_path() {
    let tempdir = tempfile::tempdir().unwrap();
    let filename = "example_subdir";
    let dirfd =
        fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
            .unwrap();
    mkdirat(dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed");
    assert!(Path::exists(&tempdir.path().join(filename)));
}

#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_mkdirat_success_mode() {
    let expected_bits =
        stat::SFlag::S_IFDIR.bits() | stat::Mode::S_IRWXU.bits();
    let tempdir = tempfile::tempdir().unwrap();
    let filename = "example_subdir";
    let dirfd =
        fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
            .unwrap();
    mkdirat(dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed");
    let permissions = fs::metadata(tempdir.path().join(filename))
        .unwrap()
        .permissions();
    let mode = permissions.mode();
    assert_eq!(mode as mode_t, expected_bits)
}

#[test]
#[cfg(not(target_os = "redox"))]
fn test_mkdirat_fail() {
    let tempdir = tempfile::tempdir().unwrap();
    let not_dir_filename = "example_not_dir";
    let filename = "example_subdir_dir";
    let dirfd = fcntl::open(
        &tempdir.path().join(not_dir_filename),
        fcntl::OFlag::O_CREAT,
        stat::Mode::empty(),
    )
    .unwrap();
    let result = mkdirat(dirfd, filename, Mode::S_IRWXU).unwrap_err();
    assert_eq!(result, Errno::ENOTDIR);
}

#[test]
#[cfg(not(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "ios",
    target_os = "macos",
    target_os = "haiku",
    target_os = "redox"
)))]
fn test_mknod() {
    use stat::{lstat, mknod, SFlag};

    let file_name = "test_file";
    let tempdir = tempfile::tempdir().unwrap();
    let target = tempdir.path().join(file_name);
    mknod(&target, SFlag::S_IFREG, Mode::S_IRWXU, 0).unwrap();
    let mode = lstat(&target).unwrap().st_mode as mode_t;
    assert_eq!(mode & libc::S_IFREG, libc::S_IFREG);
    assert_eq!(mode & libc::S_IRWXU, libc::S_IRWXU);
}

#[test]
#[cfg(not(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "illumos",
    target_os = "ios",
    target_os = "macos",
    target_os = "haiku",
    target_os = "redox"
)))]
fn test_mknodat() {
    use fcntl::{AtFlags, OFlag};
    use nix::dir::Dir;
    use stat::{fstatat, mknodat, SFlag};

    let file_name = "test_file";
    let tempdir = tempfile::tempdir().unwrap();
    let target_dir =
        Dir::open(tempdir.path(), OFlag::O_DIRECTORY, Mode::S_IRWXU).unwrap();
    mknodat(
        target_dir.as_raw_fd(),
        file_name,
        SFlag::S_IFREG,
        Mode::S_IRWXU,
        0,
    )
    .unwrap();
    let mode = fstatat(
        target_dir.as_raw_fd(),
        file_name,
        AtFlags::AT_SYMLINK_NOFOLLOW,
    )
    .unwrap()
    .st_mode as mode_t;
    assert_eq!(mode & libc::S_IFREG, libc::S_IFREG);
    assert_eq!(mode & libc::S_IRWXU, libc::S_IRWXU);
}