Skip to content

Commit

Permalink
refactor: add cfg_aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
saidsay-so committed May 25, 2024
1 parent f871a78 commit 7f2431d
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 100 deletions.
7 changes: 7 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ caps = "0.5.4"
[[bin]]
name = "pjdfstest"
path = "src/main.rs"

[build-dependencies]
cfg_aliases = "0.2.1"
16 changes: 16 additions & 0 deletions rust/build.rs
Original file line number Diff line number Diff line change
@@ -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") }
}
}
25 changes: 5 additions & 20 deletions rust/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct TestContext<'a> {
features_config: &'a FeaturesConfig,
auth_entries: DummyAuthEntries<'a>,
#[cfg(target_os = "freebsd")]
jail: Option<jail::RunningJail>
jail: Option<jail::RunningJail>,
}

pub struct SerializedTestContext<'a> {
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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};
Expand Down Expand Up @@ -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)?;
}
Expand Down
10 changes: 1 addition & 9 deletions rust/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
58 changes: 34 additions & 24 deletions rust/src/tests/chflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -28,23 +28,27 @@ use super::{

fn get_flags(ctx: &TestContext) -> (FileFlag, FileFlag, FileFlag) {
static USER_FLAGS: OnceLock<HashSet<FileFlags>> = 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<HashSet<FileFlags>> = 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()
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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, || {
Expand Down Expand Up @@ -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();

Expand All @@ -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"));
}
}

Expand Down
4 changes: 2 additions & 2 deletions rust/src/tests/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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::*;

Expand Down
27 changes: 4 additions & 23 deletions rust/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down
24 changes: 3 additions & 21 deletions rust/src/tests/utimensat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: ?Sized + nix::NixPath>(
path: &P,
flags: nix::sys::stat::FileFlag,
Expand Down

0 comments on commit 7f2431d

Please sign in to comment.