Debian Codegraph @

debian/rust-vendor/nix/test/sys/test_stat.rs

raw
1 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
2 use std::fs;
3 use std::fs::File;
4 #[cfg(not(target_os = "redox"))]
5 use std::os::unix::fs::symlink;
6 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
7 use std::os::unix::fs::PermissionsExt;
8 #[cfg(not(target_os = "redox"))]
9 use std::path::Path;
10 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
11 use std::time::{Duration, UNIX_EPOCH};
12
13 use libc::mode_t;
14 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
15 use libc::{S_IFLNK, S_IFMT};
16
17 #[cfg(not(target_os = "redox"))]
18 use nix::errno::Errno;
19 #[cfg(not(target_os = "redox"))]
20 use nix::fcntl;
21 #[cfg(any(
22 target_os = "linux",
23 apple_targets,
24 target_os = "freebsd",
25 target_os = "netbsd"
26 ))]
27 use nix::sys::stat::lutimes;
28 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
29 use nix::sys::stat::utimensat;
30 #[cfg(not(target_os = "redox"))]
31 use nix::sys::stat::FchmodatFlags;
32 use nix::sys::stat::Mode;
33 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
34 use nix::sys::stat::UtimensatFlags;
35 #[cfg(not(target_os = "redox"))]
36 use nix::sys::stat::{self};
37 use nix::sys::stat::{fchmod, stat};
38 #[cfg(not(target_os = "redox"))]
39 use nix::sys::stat::{fchmodat, mkdirat};
40 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
41 use nix::sys::stat::{futimens, utimes};
42
43 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
44 use nix::sys::stat::FileStat;
45
46 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
47 use nix::sys::time::{TimeSpec, TimeVal, TimeValLike};
48 #[cfg(not(target_os = "redox"))]
49 use nix::unistd::chdir;
50
51 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
52 use nix::Result;
53
54 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
55 fn assert_stat_results(stat_result: Result<FileStat>) {
56 let stats = stat_result.expect("stat call failed");
57 assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
58 assert!(stats.st_ino > 0); // inode is positive integer, exact number machine dependent
59 assert!(stats.st_mode > 0); // must be positive integer
60 assert_eq!(stats.st_nlink, 1); // there links created, must be 1
61 assert_eq!(stats.st_size, 0); // size is 0 because we did not write anything to the file
62 assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent
63 assert!(stats.st_blocks <= 16); // Up to 16 blocks can be allocated for a blank file
64 }
65
66 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
67 // (Android's st_blocks is ulonglong which is always non-negative.)
68 #[cfg_attr(target_os = "android", allow(unused_comparisons))]
69 #[allow(clippy::absurd_extreme_comparisons)] // Not absurd on all OSes
70 fn assert_lstat_results(stat_result: Result<FileStat>) {
71 let stats = stat_result.expect("stat call failed");
72 assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
73 assert!(stats.st_ino > 0); // inode is positive integer, exact number machine dependent
74 assert!(stats.st_mode > 0); // must be positive integer
75
76 // st_mode is c_uint (u32 on Android) while S_IFMT is mode_t
77 // (u16 on Android), and that will be a compile error.
78 // On other platforms they are the same (either both are u16 or u32).
79 assert_eq!(
80 (stats.st_mode as usize) & (S_IFMT as usize),
81 S_IFLNK as usize
82 ); // should be a link
83 assert_eq!(stats.st_nlink, 1); // there links created, must be 1
84 assert!(stats.st_size > 0); // size is > 0 because it points to another file
85 assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent
86
87 // st_blocks depends on whether the machine's file system uses fast
88 // or slow symlinks, so just make sure it's not negative
89 assert!(stats.st_blocks >= 0);
90 }
91
92 #[test]
93 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
94 fn test_stat_and_fstat() {
95 use nix::sys::stat::fstat;
96
97 let tempdir = tempfile::tempdir().unwrap();
98 let filename = tempdir.path().join("foo.txt");
99 let file = File::create(&filename).unwrap();
100
101 let stat_result = stat(&filename);
102 assert_stat_results(stat_result);
103
104 let fstat_result = fstat(&file);
105 assert_stat_results(fstat_result);
106 }
107
108 #[test]
109 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
110 fn test_fstatat() {
111 let tempdir = tempfile::tempdir().unwrap();
112 let filename = tempdir.path().join("foo.txt");
113 File::create(&filename).unwrap();
114 let dirfd =
115 fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
116 .unwrap();
117
118 let result = stat::fstatat(&dirfd, &filename, fcntl::AtFlags::empty());
119 assert_stat_results(result);
120 }
121
122 #[test]
123 #[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
124 fn test_stat_fstat_lstat() {
125 use nix::sys::stat::{fstat, lstat};
126
127 let tempdir = tempfile::tempdir().unwrap();
128 let filename = tempdir.path().join("bar.txt");
129 let linkname = tempdir.path().join("barlink");
130
131 File::create(&filename).unwrap();
132 symlink("bar.txt", &linkname).unwrap();
133 let link = File::open(&linkname).unwrap();
134
135 // should be the same result as calling stat,
136 // since it's a regular file
137 let stat_result = stat(&filename);
138 assert_stat_results(stat_result);
139
140 let lstat_result = lstat(&linkname);
141 assert_lstat_results(lstat_result);
142
143 let fstat_result = fstat(&link);
144 assert_stat_results(fstat_result);
145 }
146
147 #[test]
148 fn test_fchmod() {
149 let tempdir = tempfile::tempdir().unwrap();
150 let filename = tempdir.path().join("foo.txt");
151 let file = File::create(&filename).unwrap();
152
153 let mut mode1 = Mode::empty();
154 mode1.insert(Mode::S_IRUSR);
155 mode1.insert(Mode::S_IWUSR);
156 fchmod(&file, mode1).unwrap();
157
158 let file_stat1 = stat(&filename).unwrap();
159 assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());
160
161 let mut mode2 = Mode::empty();
162 mode2.insert(Mode::S_IROTH);
163 fchmod(&file, mode2).unwrap();
164
165 let file_stat2 = stat(&filename).unwrap();
166 assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
167 }
168
169 #[test]
170 #[cfg(not(target_os = "redox"))]
171 fn test_fchmodat() {
172 let _dr = crate::DirRestore::new();
173 let tempdir = tempfile::tempdir().unwrap();
174 let filename = "foo.txt";
175 let fullpath = tempdir.path().join(filename);
176 File::create(&fullpath).unwrap();
177
178 let dirfd =
179 fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
180 .unwrap();
181
182 let mut mode1 = Mode::empty();
183 mode1.insert(Mode::S_IRUSR);
184 mode1.insert(Mode::S_IWUSR);
185 fchmodat(&dirfd, filename, mode1, FchmodatFlags::FollowSymlink).unwrap();
186
187 let file_stat1 = stat(&fullpath).unwrap();
188 assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());
189
190 chdir(tempdir.path()).unwrap();
191
192 let mut mode2 = Mode::empty();
193 mode2.insert(Mode::S_IROTH);
194 fchmodat(
195 fcntl::AT_FDCWD,
196 filename,
197 mode2,
198 FchmodatFlags::FollowSymlink,
199 )
200 .unwrap();
201
202 let file_stat2 = stat(&fullpath).unwrap();
203 assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
204 }
205
206 /// Asserts that the atime and mtime in a file's metadata match expected values.
207 ///
208 /// The atime and mtime are expressed with a resolution of seconds because some file systems
209 /// (like macOS's HFS+) do not have higher granularity.
210 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
211 fn assert_times_eq(
212 exp_atime_sec: u64,
213 exp_mtime_sec: u64,
214 attr: &fs::Metadata,
215 ) {
216 assert_eq!(
217 Duration::new(exp_atime_sec, 0),
218 attr.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
219 );
220 assert_eq!(
221 Duration::new(exp_mtime_sec, 0),
222 attr.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
223 );
224 }
225
226 #[test]
227 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
228 fn test_utimes() {
229 let tempdir = tempfile::tempdir().unwrap();
230 let fullpath = tempdir.path().join("file");
231 drop(File::create(&fullpath).unwrap());
232
233 utimes(&fullpath, &TimeVal::seconds(9990), &TimeVal::seconds(5550))
234 .unwrap();
235 assert_times_eq(9990, 5550, &fs::metadata(&fullpath).unwrap());
236 }
237
238 #[test]
239 #[cfg(any(
240 target_os = "linux",
241 apple_targets,
242 target_os = "freebsd",
243 target_os = "netbsd"
244 ))]
245 fn test_lutimes() {
246 let tempdir = tempfile::tempdir().unwrap();
247 let target = tempdir.path().join("target");
248 let fullpath = tempdir.path().join("symlink");
249 drop(File::create(&target).unwrap());
250 symlink(&target, &fullpath).unwrap();
251
252 let exp_target_metadata = fs::symlink_metadata(&target).unwrap();
253 lutimes(&fullpath, &TimeVal::seconds(4560), &TimeVal::seconds(1230))
254 .unwrap();
255 assert_times_eq(4560, 1230, &fs::symlink_metadata(&fullpath).unwrap());
256
257 let target_metadata = fs::symlink_metadata(&target).unwrap();
258 assert_eq!(
259 exp_target_metadata.accessed().unwrap(),
260 target_metadata.accessed().unwrap(),
261 "atime of symlink target was unexpectedly modified"
262 );
263 assert_eq!(
264 exp_target_metadata.modified().unwrap(),
265 target_metadata.modified().unwrap(),
266 "mtime of symlink target was unexpectedly modified"
267 );
268 }
269
270 #[test]
271 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
272 fn test_futimens() {
273 let tempdir = tempfile::tempdir().unwrap();
274 let fullpath = tempdir.path().join("file");
275 drop(File::create(&fullpath).unwrap());
276
277 let fd = fcntl::open(&fullpath, fcntl::OFlag::empty(), stat::Mode::empty())
278 .unwrap();
279
280 futimens(&fd, &TimeSpec::seconds(10), &TimeSpec::seconds(20)).unwrap();
281 assert_times_eq(10, 20, &fs::metadata(&fullpath).unwrap());
282 }
283
284 #[test]
285 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
286 fn test_utimensat() {
287 let _dr = crate::DirRestore::new();
288 let tempdir = tempfile::tempdir().unwrap();
289 let filename = "foo.txt";
290 let fullpath = tempdir.path().join(filename);
291 drop(File::create(&fullpath).unwrap());
292
293 let dirfd =
294 fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
295 .unwrap();
296
297 utimensat(
298 &dirfd,
299 filename,
300 &TimeSpec::seconds(12345),
301 &TimeSpec::seconds(678),
302 UtimensatFlags::FollowSymlink,
303 )
304 .unwrap();
305 assert_times_eq(12345, 678, &fs::metadata(&fullpath).unwrap());
306
307 chdir(tempdir.path()).unwrap();
308
309 utimensat(
310 fcntl::AT_FDCWD,
311 filename,
312 &TimeSpec::seconds(500),
313 &TimeSpec::seconds(800),
314 UtimensatFlags::FollowSymlink,
315 )
316 .unwrap();
317 assert_times_eq(500, 800, &fs::metadata(&fullpath).unwrap());
318 }
319
320 #[test]
321 #[cfg(not(target_os = "redox"))]
322 fn test_mkdirat_success_path() {
323 let tempdir = tempfile::tempdir().unwrap();
324 let filename = "example_subdir";
325 let dirfd =
326 fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
327 .unwrap();
328 mkdirat(&dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed");
329 assert!(Path::exists(&tempdir.path().join(filename)));
330 }
331
332 #[test]
333 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
334 fn test_mkdirat_success_mode() {
335 let expected_bits =
336 stat::SFlag::S_IFDIR.bits() | stat::Mode::S_IRWXU.bits();
337 let tempdir = tempfile::tempdir().unwrap();
338 let filename = "example_subdir";
339 let dirfd =
340 fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
341 .unwrap();
342 mkdirat(&dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed");
343 let permissions = fs::metadata(tempdir.path().join(filename))
344 .unwrap()
345 .permissions();
346 let mode = permissions.mode();
347 assert_eq!(mode as mode_t, expected_bits)
348 }
349
350 #[test]
351 #[cfg(not(target_os = "redox"))]
352 fn test_mkdirat_fail() {
353 let tempdir = tempfile::tempdir().unwrap();
354 let not_dir_filename = "example_not_dir";
355 let filename = "example_subdir_dir";
356 let dirfd = fcntl::open(
357 &tempdir.path().join(not_dir_filename),
358 fcntl::OFlag::O_CREAT,
359 stat::Mode::empty(),
360 )
361 .unwrap();
362 let result = mkdirat(dirfd, filename, Mode::S_IRWXU).unwrap_err();
363 assert_eq!(result, Errno::ENOTDIR);
364 }
365
366 #[test]
367 #[cfg(not(any(
368 freebsdlike,
369 apple_targets,
370 target_os = "haiku",
371 target_os = "redox",
372 target_os = "solaris"
373 )))]
374 fn test_mknod() {
375 use stat::{lstat, mknod, SFlag};
376
377 let file_name = "test_file";
378 let tempdir = tempfile::tempdir().unwrap();
379 let target = tempdir.path().join(file_name);
380 mknod(&target, SFlag::S_IFREG, Mode::S_IRWXU, 0).unwrap();
381 let mode = lstat(&target).unwrap().st_mode as mode_t;
382 assert_eq!(mode & libc::S_IFREG, libc::S_IFREG);
383 assert_eq!(mode & libc::S_IRWXU, libc::S_IRWXU);
384 }
385
386 #[test]
387 #[cfg(not(any(
388 solarish,
389 freebsdlike,
390 apple_targets,
391 target_os = "haiku",
392 target_os = "redox"
393 )))]
394 fn test_mknodat() {
395 use fcntl::{AtFlags, OFlag};
396 use nix::dir::Dir;
397 use stat::{fstatat, mknodat, SFlag};
398
399 let file_name = "test_file";
400 let tempdir = tempfile::tempdir().unwrap();
401 let target_dir =
402 Dir::open(tempdir.path(), OFlag::O_DIRECTORY, Mode::S_IRWXU).unwrap();
403 mknodat(&target_dir, file_name, SFlag::S_IFREG, Mode::S_IRWXU, 0).unwrap();
404 let mode = fstatat(&target_dir, file_name, AtFlags::AT_SYMLINK_NOFOLLOW)
405 .unwrap()
406 .st_mode as mode_t;
407 assert_eq!(mode & libc::S_IFREG, libc::S_IFREG);
408 assert_eq!(mode & libc::S_IRWXU, libc::S_IRWXU);
409 }
410
411 #[test]
412 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
413 fn test_futimens_unchanged() {
414 let tempdir = tempfile::tempdir().unwrap();
415 let fullpath = tempdir.path().join("file");
416 drop(File::create(&fullpath).unwrap());
417 let fd = fcntl::open(&fullpath, fcntl::OFlag::empty(), stat::Mode::empty())
418 .unwrap();
419
420 let old_atime = fs::metadata(fullpath.as_path())
421 .unwrap()
422 .accessed()
423 .unwrap();
424 let old_mtime = fs::metadata(fullpath.as_path())
425 .unwrap()
426 .modified()
427 .unwrap();
428
429 futimens(&fd, &TimeSpec::UTIME_OMIT, &TimeSpec::UTIME_OMIT).unwrap();
430
431 let new_atime = fs::metadata(fullpath.as_path())
432 .unwrap()
433 .accessed()
434 .unwrap();
435 let new_mtime = fs::metadata(fullpath.as_path())
436 .unwrap()
437 .modified()
438 .unwrap();
439 assert_eq!(old_atime, new_atime);
440 assert_eq!(old_mtime, new_mtime);
441 }
442
443 #[test]
444 #[cfg(not(any(target_os = "redox", target_os = "haiku")))]
445 fn test_utimensat_unchanged() {
446 let _dr = crate::DirRestore::new();
447 let tempdir = tempfile::tempdir().unwrap();
448 let filename = "foo.txt";
449 let fullpath = tempdir.path().join(filename);
450 drop(File::create(&fullpath).unwrap());
451 let dirfd =
452 fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
453 .unwrap();
454
455 let old_atime = fs::metadata(fullpath.as_path())
456 .unwrap()
457 .accessed()
458 .unwrap();
459 let old_mtime = fs::metadata(fullpath.as_path())
460 .unwrap()
461 .modified()
462 .unwrap();
463 utimensat(
464 &dirfd,
465 filename,
466 &TimeSpec::UTIME_OMIT,
467 &TimeSpec::UTIME_OMIT,
468 UtimensatFlags::NoFollowSymlink,
469 )
470 .unwrap();
471 let new_atime = fs::metadata(fullpath.as_path())
472 .unwrap()
473 .accessed()
474 .unwrap();
475 let new_mtime = fs::metadata(fullpath.as_path())
476 .unwrap()
477 .modified()
478 .unwrap();
479 assert_eq!(old_atime, new_atime);
480 assert_eq!(old_mtime, new_mtime);
481 }
482
483 // The conversion is not useless on all platforms.
484 #[allow(clippy::useless_conversion)]
485 #[cfg(target_os = "freebsd")]
486 #[test]
487 fn test_chflags() {
488 use nix::{
489 sys::stat::{fstat, FileFlag},
490 unistd::chflags,
491 };
492 use tempfile::NamedTempFile;
493
494 let f = NamedTempFile::new().unwrap();
495
496 let initial =
497 FileFlag::from_bits_truncate(fstat(&f).unwrap().st_flags.into());
498 // UF_OFFLINE is preserved by all FreeBSD file systems, but not interpreted
499 // in any way, so it's handy for testing.
500 let commanded = initial ^ FileFlag::UF_OFFLINE;
501
502 chflags(f.path(), commanded).unwrap();
503
504 let changed =
505 FileFlag::from_bits_truncate(fstat(&f).unwrap().st_flags.into());
506
507 assert_eq!(commanded, changed);
508 }