From 7f2431dcfafde11d50167b72fdf3faf7d2d2d6dc Mon Sep 17 00:00:00 2001 From: Sayafdine Said Date: Sat, 25 May 2024 23:39:14 +0200 Subject: [PATCH] refactor: add cfg_aliases --- rust/Cargo.lock | 7 +++++ rust/Cargo.toml | 3 ++ rust/build.rs | 16 ++++++++++ rust/src/context.rs | 25 ++++------------ rust/src/flags.rs | 10 +------ rust/src/tests/chflags.rs | 58 ++++++++++++++++++++++--------------- rust/src/tests/chmod.rs | 4 +-- rust/src/tests/mod.rs | 27 +++-------------- rust/src/tests/utimensat.rs | 24 ++------------- rust/src/utils.rs | 2 +- 10 files changed, 76 insertions(+), 100 deletions(-) create mode 100644 rust/build.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 1cec0b58..feb55dc2 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -66,6 +66,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "ctor" version = "0.1.26" @@ -291,6 +297,7 @@ version = "0.1.0" dependencies = [ "anyhow", "caps", + "cfg_aliases", "exacl", "figment", "gumdrop", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 7c945b34..411f070f 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -28,3 +28,6 @@ caps = "0.5.4" [[bin]] name = "pjdfstest" path = "src/main.rs" + +[build-dependencies] +cfg_aliases = "0.2.1" diff --git a/rust/build.rs b/rust/build.rs new file mode 100644 index 00000000..1ae41c4c --- /dev/null +++ b/rust/build.rs @@ -0,0 +1,16 @@ +use cfg_aliases::cfg_aliases; + +fn main() { + // Setup cfg aliases + cfg_aliases! { + // OS-exclusive syscalls + chflags: { any(target_os = "openbsd", target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly", target_os = "macos", target_os = "ios") }, + lchmod: { any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly") }, + lchflags: { any(target_os = "openbsd", target_os = "netbsd", target_os = "freebsd", + target_os = "dragonfly", target_os = "macos", target_os = "ios") }, + // OS-exclusive features + file_flags: { any(target_os = "openbsd", target_os = "netbsd", target_os = "freebsd", + target_os = "dragonfly", target_os = "macos", target_os = "ios") }, + birthtime: { any(target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd") } + } +} diff --git a/rust/src/context.rs b/rust/src/context.rs index a6bff511..7dff32e8 100644 --- a/rust/src/context.rs +++ b/rust/src/context.rs @@ -78,7 +78,7 @@ pub struct TestContext<'a> { features_config: &'a FeaturesConfig, auth_entries: DummyAuthEntries<'a>, #[cfg(target_os = "freebsd")] - jail: Option + jail: Option, } pub struct SerializedTestContext<'a> { @@ -171,7 +171,7 @@ impl<'a> TestContext<'a> { features_config: &config.features, auth_entries: DummyAuthEntries::new(entries), #[cfg(target_os = "freebsd")] - jail: None + jail: None, } } @@ -303,15 +303,7 @@ impl<'a> Drop for TestContext<'a> { _ => continue, }; - if cfg!(any( - target_os = "openbsd", - target_os = "netbsd", - target_os = "freebsd", - target_os = "dragonfly", - target_os = "macos", - target_os = "ios" - )) || entry.file_type().is_dir() - { + if cfg!(lchflags) || entry.file_type().is_dir() { let file_stat = match lstat(entry.path()) { Ok(s) => s, _ => continue, @@ -324,14 +316,7 @@ impl<'a> Drop for TestContext<'a> { // We remove all flags // TODO: Some platforms do not support lchflags, write chflagsat alternative for those (openbsd, macos, ios?) - #[cfg(any( - target_os = "openbsd", - target_os = "netbsd", - target_os = "freebsd", - target_os = "dragonfly", - target_os = "macos", - target_os = "ios" - ))] + #[cfg(lchflags)] { use crate::utils::lchflags; use nix::{libc::fflags_t, sys::stat::FileFlag}; @@ -415,7 +400,7 @@ impl FileBuilder { &path, )?; - #[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] + #[cfg(lchmod)] if let Some(mode) = self.mode { lchmod(&path, mode)?; } diff --git a/rust/src/flags.rs b/rust/src/flags.rs index 0d6c0aa8..22c4a55b 100644 --- a/rust/src/flags.rs +++ b/rust/src/flags.rs @@ -9,15 +9,7 @@ macro_rules! flags { $flag),* } - #[cfg(any( - target_os = "openbsd", - target_os = "netbsd", - target_os = "freebsd", - target_os = "dragonfly", - target_os = "macos", - target_os = "ios", - target_os = "watchos", - ))] + #[cfg(file_flags)] impl From<$enum> for nix::sys::stat::FileFlag { fn from(flag: $enum) -> Self { match flag { diff --git a/rust/src/tests/chflags.rs b/rust/src/tests/chflags.rs index 2ab42098..cdd859e2 100644 --- a/rust/src/tests/chflags.rs +++ b/rust/src/tests/chflags.rs @@ -7,7 +7,7 @@ use nix::{ unistd::chflags, }; -#[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] +#[cfg(lchflags)] use crate::utils::lchflags; use crate::{ context::{FileType, SerializedTestContext, TestContext}, @@ -28,23 +28,27 @@ use super::{ fn get_flags(ctx: &TestContext) -> (FileFlag, FileFlag, FileFlag) { static USER_FLAGS: OnceLock> = OnceLock::new(); - USER_FLAGS.get_or_init(|| HashSet::from([ - FileFlags::UF_NODUMP, - FileFlags::UF_IMMUTABLE, - FileFlags::UF_APPEND, - #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] - FileFlags::UF_NOUNLINK, - #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] - FileFlags::UF_OPAQUE, - ])); + USER_FLAGS.get_or_init(|| { + HashSet::from([ + FileFlags::UF_NODUMP, + FileFlags::UF_IMMUTABLE, + FileFlags::UF_APPEND, + #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] + FileFlags::UF_NOUNLINK, + #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] + FileFlags::UF_OPAQUE, + ]) + }); static SYSTEM_FLAGS: OnceLock> = OnceLock::new(); - SYSTEM_FLAGS.get_or_init(|| HashSet::from([ - FileFlags::SF_ARCHIVED, - FileFlags::SF_IMMUTABLE, - FileFlags::SF_APPEND, - #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] - FileFlags::SF_NOUNLINK, - ])); + SYSTEM_FLAGS.get_or_init(|| { + HashSet::from([ + FileFlags::SF_ARCHIVED, + FileFlags::SF_IMMUTABLE, + FileFlags::SF_APPEND, + #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] + FileFlags::SF_NOUNLINK, + ]) + }); let allflags: FileFlag = ctx .features_config() @@ -95,7 +99,7 @@ fn set_flags(ctx: &mut TestContext, ft: FileType) { let file = ctx.create(ft).unwrap(); assert!(chflags(&file, FileFlag::empty()).is_ok()); - #[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] + #[cfg(lchflags)] for flags_set in [flags, user_flags, system_flags, FileFlag::empty()] { assert!(lchflags(&file, FileFlag::empty()).is_ok()); assert!(lchflags(&file, flags_set).is_ok()); @@ -126,12 +130,12 @@ fn set_flags_symlink(ctx: &mut TestContext) { } } -#[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] +#[cfg(lchflags)] crate::test_case! { /// lchflags changes flags without following symlinks lchflags_set_flags_no_follow_symlink, root, FileSystemFeature::Chflags } -#[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] +#[cfg(lchflags)] fn lchflags_set_flags_no_follow_symlink(ctx: &mut TestContext) { let (flags, user_flags, system_flags) = get_flags(ctx); @@ -173,7 +177,7 @@ fn changed_ctime_success(ctx: &mut TestContext, ft: FileType) { let file = ctx.create(ft).unwrap(); - #[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] + #[cfg(lchflags)] for flag in allflags.into_iter().chain(once(FileFlag::empty())) { assert_ctime_changed(ctx, &file, || { assert!(lchflags(&file, flag).is_ok()); @@ -207,7 +211,7 @@ fn unchanged_ctime_failed(ctx: &mut SerializedTestContext, ft: FileType) { let file = ctx.create(ft).unwrap(); - #[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] + #[cfg(lchflags)] for flag in allflags.into_iter().chain(once(FileFlag::empty())) { assert_ctime_unchanged(ctx, &file, || { ctx.as_user(user, None, || { @@ -254,7 +258,11 @@ fn securelevel(ctx: &mut TestContext, ft: FileType) { let jail = jail.start().unwrap(); ctx.set_jail(jail); - for flag in [FileFlags::SF_IMMUTABLE, FileFlags::SF_APPEND, FileFlags::SF_NOUNLINK] { + for flag in [ + FileFlags::SF_IMMUTABLE, + FileFlags::SF_APPEND, + FileFlags::SF_NOUNLINK, + ] { let file = ctx.create(ft.clone()).unwrap(); lchflags(&file, flag.into()).unwrap(); @@ -267,7 +275,9 @@ fn securelevel(ctx: &mut TestContext, ft: FileType) { .output() .unwrap(); assert!(!r.status.success()); - assert!(OsStr::from_bytes(&r.stderr).to_string_lossy().contains("Operation not permitted")); + assert!(OsStr::from_bytes(&r.stderr) + .to_string_lossy() + .contains("Operation not permitted")); } } diff --git a/rust/src/tests/chmod.rs b/rust/src/tests/chmod.rs index df5059bd..99b9a0af 100644 --- a/rust/src/tests/chmod.rs +++ b/rust/src/tests/chmod.rs @@ -5,7 +5,7 @@ use crate::{ utils::{chmod, ALLPERMS}, }; -#[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] +#[cfg(lchmod)] use crate::utils::lchmod; use nix::{ @@ -194,7 +194,7 @@ fn eftype(ctx: &mut SerializedTestContext, ft: FileType) { assert_eq!(file_stat.st_mode & ALLPERMS_STICKY, original_mode.bits()); } -#[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] +#[cfg(lchmod)] mod lchmod { use super::*; diff --git a/rust/src/tests/mod.rs b/rust/src/tests/mod.rs index 2bfe0e66..4049abef 100644 --- a/rust/src/tests/mod.rs +++ b/rust/src/tests/mod.rs @@ -4,26 +4,11 @@ use std::os::unix::fs::MetadataExt as StdMetadataExt; use std::{fs::metadata, path::Path}; -#[cfg(any( - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" -))] -use nix::sys::stat::stat; use nix::sys::time::TimeSpec; use crate::test::TestContext; -#[cfg(any( - target_os = "openbsd", - target_os = "netbsd", - target_os = "freebsd", - target_os = "dragonfly", - target_os = "macos", - target_os = "ios", -))] +#[cfg(chflags)] pub mod chflags; pub mod chmod; pub mod chown; @@ -134,16 +119,12 @@ impl AsTimeInvariant for nix::sys::stat::FileStat { } } -#[cfg(any( - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(birthtime)] // Note: can't be a method of MetadataExt, because StdMetadataExt lacks a // birthtime() method. fn birthtime_ts(path: &Path) -> TimeSpec { + use nix::sys::stat::stat; + let sb = stat(path).unwrap(); TimeSpec::new(sb.st_birthtime, sb.st_birthtime_nsec) } diff --git a/rust/src/tests/utimensat.rs b/rust/src/tests/utimensat.rs index fbf3109b..622a3d29 100644 --- a/rust/src/tests/utimensat.rs +++ b/rust/src/tests/utimensat.rs @@ -3,13 +3,7 @@ use std::{ os::unix::fs::symlink, }; -#[cfg(any( - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(birthtime)] use crate::tests::birthtime_ts; use crate::tests::MetadataExt; use crate::utils::chmod; @@ -99,25 +93,13 @@ fn utime_omit(ctx: &mut TestContext) { assert_eq!(md.mtime_ts(), date2); } -#[cfg(any( - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(birthtime)] crate::test_case! { /// utimensat can update birthtimes // utimensat/03.t birthtime, FileSystemFeature::Utimensat, FileSystemFeature::StatStBirthtime } -#[cfg(any( - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(birthtime)] fn birthtime(ctx: &mut TestContext) { let date1 = TimeSpec::seconds(100000000); // Sat Mar 3 02:46:40 MST 1973 let date2 = TimeSpec::seconds(200000000); // Mon May 3 13:33:20 MDT 1976 diff --git a/rust/src/utils.rs b/rust/src/utils.rs index fb02270d..24509b75 100644 --- a/rust/src/utils.rs +++ b/rust/src/utils.rs @@ -73,7 +73,7 @@ pub fn get_mountpoint(base_path: &Path) -> Result<&Path, anyhow::Error> { } /// Safe wrapper for `lchflags`. -#[cfg(any(target_os = "netbsd", target_os = "freebsd", target_os = "dragonfly"))] +#[cfg(lchflags)] pub fn lchflags( path: &P, flags: nix::sys::stat::FileFlag,