summaryrefslogtreecommitdiff
path: root/test/test_stat.rs
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_stat.rs')
-rw-r--r--test/test_stat.rs223
1 files changed, 138 insertions, 85 deletions
diff --git a/test/test_stat.rs b/test/test_stat.rs
index 3a09eca5..de5a964e 100644
--- a/test/test_stat.rs
+++ b/test/test_stat.rs
@@ -2,43 +2,45 @@
use std::fs;
use std::fs::File;
#[cfg(not(target_os = "redox"))]
-use std::os::unix::fs::{symlink};
+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::fs::PermissionsExt;
use std::os::unix::prelude::AsRawFd;
-#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
-use std::time::{Duration, UNIX_EPOCH};
#[cfg(not(target_os = "redox"))]
use std::path::Path;
+#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
+use std::time::{Duration, UNIX_EPOCH};
-#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
-use libc::{S_IFMT, S_IFLNK};
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::errno::Errno;
+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};
-#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
-use nix::sys::stat::{futimens, utimes};
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::{utimensat};
-#[cfg(any(target_os = "linux",
- target_os = "ios",
- target_os = "macos",
- target_os = "freebsd",
- target_os = "netbsd"))]
-use nix::sys::stat::lutimes;
-#[cfg(not(target_os = "redox"))]
-use nix::sys::stat::{FchmodatFlags};
-#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
-use nix::sys::stat::{UtimensatFlags};
-use nix::sys::stat::Mode;
+use nix::sys::stat::{futimens, utimes};
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use nix::sys::stat::FileStat;
@@ -54,32 +56,35 @@ 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
+ 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
+#[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
+ 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
+ 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
@@ -108,13 +113,11 @@ 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 dirfd =
+ fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty());
- let result = stat::fstatat(dirfd.unwrap(),
- &filename,
- fcntl::AtFlags::empty());
+ let result =
+ stat::fstatat(dirfd.unwrap(), &filename, fcntl::AtFlags::empty());
assert_stat_results(result);
}
@@ -174,12 +177,15 @@ fn test_fchmodat() {
let fullpath = tempdir.path().join(filename);
File::create(&fullpath).unwrap();
- let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).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();
+ 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());
@@ -199,13 +205,19 @@ fn test_fchmodat() {
/// 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) {
+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());
+ attr.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
+ );
assert_eq!(
Duration::new(exp_mtime_sec, 0),
- attr.modified().unwrap().duration_since(UNIX_EPOCH).unwrap());
+ attr.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
+ );
}
#[test]
@@ -215,16 +227,19 @@ fn test_utimes() {
let fullpath = tempdir.path().join("file");
drop(File::create(&fullpath).unwrap());
- utimes(&fullpath, &TimeVal::seconds(9990), &TimeVal::seconds(5550)).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"))]
+#[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");
@@ -233,14 +248,21 @@ fn test_lutimes() {
symlink(&target, &fullpath).unwrap();
let exp_target_metadata = fs::symlink_metadata(&target).unwrap();
- lutimes(&fullpath, &TimeVal::seconds(4560), &TimeVal::seconds(1230)).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");
+ 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]
@@ -250,7 +272,8 @@ fn test_futimens() {
let fullpath = tempdir.path().join("file");
drop(File::create(&fullpath).unwrap());
- let fd = fcntl::open(&fullpath, fcntl::OFlag::empty(), stat::Mode::empty()).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());
@@ -265,16 +288,30 @@ fn test_utimensat() {
let fullpath = tempdir.path().join(filename);
drop(File::create(&fullpath).unwrap());
- let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()).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();
+ 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();
+ utimensat(
+ None,
+ filename,
+ &TimeSpec::seconds(500),
+ &TimeSpec::seconds(800),
+ UtimensatFlags::FollowSymlink,
+ )
+ .unwrap();
assert_times_eq(500, 800, &fs::metadata(&fullpath).unwrap());
}
@@ -283,7 +320,9 @@ fn test_utimensat() {
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();
+ let dirfd =
+ fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
+ .unwrap();
assert!((mkdirat(dirfd, filename, Mode::S_IRWXU)).is_ok());
assert!(Path::exists(&tempdir.path().join(filename)));
}
@@ -291,12 +330,17 @@ fn test_mkdirat_success_path() {
#[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 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();
+ let dirfd =
+ fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
+ .unwrap();
assert!((mkdirat(dirfd, filename, Mode::S_IRWXU)).is_ok());
- let permissions = fs::metadata(tempdir.path().join(filename)).unwrap().permissions();
+ let permissions = fs::metadata(tempdir.path().join(filename))
+ .unwrap()
+ .permissions();
let mode = permissions.mode();
assert_eq!(mode as mode_t, expected_bits)
}
@@ -305,21 +349,27 @@ fn test_mkdirat_success_mode() {
#[cfg(not(target_os = "redox"))]
fn test_mkdirat_fail() {
let tempdir = tempfile::tempdir().unwrap();
- let not_dir_filename= "example_not_dir";
+ 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 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")))]
+#[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};
@@ -333,13 +383,15 @@ fn test_mknod() {
}
#[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")))]
+#[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;
@@ -347,7 +399,8 @@ fn test_mknodat() {
let file_name = "test_file";
let tempdir = tempfile::tempdir().unwrap();
- let target_dir = Dir::open(tempdir.path(), OFlag::O_DIRECTORY, Mode::S_IRWXU).unwrap();
+ let target_dir =
+ Dir::open(tempdir.path(), OFlag::O_DIRECTORY, Mode::S_IRWXU).unwrap();
mknodat(
target_dir.as_raw_fd(),
file_name,