Skip to content

Commit 639f393

Browse files
committed
Disable xattr APIs on MacOS
And add a CI run that covers it. Motivated by wanting to use this crate in bcvk. Signed-off-by: Colin Walters <[email protected]>
1 parent a73a0d7 commit 639f393

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

.github/workflows/rust.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,12 @@ jobs:
117117
run: cargo build --all-targets
118118
- name: cargo test
119119
run: cargo test --all-targets
120-
tests-windows:
121-
name: Tests, Windows
122-
runs-on: windows-latest
120+
build-and-test:
121+
name: Build and test on ${{ matrix.host }}
122+
runs-on: ${{ matrix.host }}
123+
strategy:
124+
matrix:
125+
host: [windows-latest, macos-latest]
123126
steps:
124127
- name: Check out repository
125128
uses: actions/checkout@v5

src/cmdext.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl CapStdExtCommandExt for std::process::Command {
8787
}
8888
}
8989

90-
#[cfg(test)]
90+
#[cfg(all(test, any(target_os = "android", target_os = "linux")))]
9191
mod tests {
9292
use super::*;
9393
use std::sync::Arc;
@@ -99,8 +99,13 @@ mod tests {
9999
let tempd = cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
100100
let tempd_fd = Arc::new(tempd.as_fd().try_clone_to_owned()?);
101101
let n = tempd_fd.as_raw_fd() + i;
102-
let st = std::process::Command::new("ls")
103-
.arg(format!("/proc/self/fd/{n}"))
102+
#[cfg(any(target_os = "android", target_os = "linux"))]
103+
let path = format!("/proc/self/fd/{n}");
104+
#[cfg(not(any(target_os = "android", target_os = "linux")))]
105+
let path = format!("/dev/fd/{n}");
106+
let st = std::process::Command::new("/usr/bin/env")
107+
.arg("readlink")
108+
.arg(path)
104109
.take_fd_n(tempd_fd, n)
105110
.status()?;
106111
assert!(st.success());

src/dirext.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,16 @@ pub trait CapStdExtDirExt {
244244
/// to determine, and `None` will be returned in those cases.
245245
fn is_mountpoint(&self, path: impl AsRef<Path>) -> Result<Option<bool>>;
246246

247-
#[cfg(not(windows))]
247+
#[cfg(any(target_os = "android", target_os = "linux"))]
248248
/// Get the value of an extended attribute. If the attribute is not present,
249249
/// this function will return `Ok(None)`.
250250
fn getxattr(&self, path: impl AsRef<Path>, key: impl AsRef<OsStr>) -> Result<Option<Vec<u8>>>;
251251

252-
#[cfg(not(windows))]
252+
#[cfg(any(target_os = "android", target_os = "linux"))]
253253
/// List all extended attribute keys for this path.
254254
fn listxattrs(&self, path: impl AsRef<Path>) -> Result<crate::XattrList>;
255255

256-
#[cfg(not(windows))]
256+
#[cfg(any(target_os = "android", target_os = "linux"))]
257257
/// Set the value of an extended attribute.
258258
fn setxattr(
259259
&self,
@@ -614,6 +614,10 @@ where
614614

615615
// Ensure that the target path isn't absolute, and doesn't
616616
// have any parent references.
617+
#[cfg_attr(
618+
not(any(target_os = "android", target_os = "linux", test)),
619+
allow(dead_code)
620+
)]
617621
pub(crate) fn validate_relpath_no_uplinks(path: &Path) -> Result<&Path> {
618622
let is_absolute = path.is_absolute();
619623
let contains_uplinks = path
@@ -828,17 +832,17 @@ impl CapStdExtDirExt for Dir {
828832
is_mountpoint_impl_statx(self, path.as_ref()).map_err(Into::into)
829833
}
830834

831-
#[cfg(not(windows))]
835+
#[cfg(any(target_os = "android", target_os = "linux"))]
832836
fn getxattr(&self, path: impl AsRef<Path>, key: impl AsRef<OsStr>) -> Result<Option<Vec<u8>>> {
833837
crate::xattrs::impl_getxattr(self, path.as_ref(), key.as_ref())
834838
}
835839

836-
#[cfg(not(windows))]
840+
#[cfg(any(target_os = "android", target_os = "linux"))]
837841
fn listxattrs(&self, path: impl AsRef<Path>) -> Result<crate::XattrList> {
838842
crate::xattrs::impl_listxattrs(self, path.as_ref())
839843
}
840844

841-
#[cfg(not(windows))]
845+
#[cfg(any(target_os = "android", target_os = "linux"))]
842846
fn setxattr(
843847
&self,
844848
path: impl AsRef<Path>,

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ pub mod dirext;
2222
mod rootdir;
2323
#[cfg(any(target_os = "android", target_os = "linux"))]
2424
pub use rootdir::*;
25-
#[cfg(not(windows))]
25+
#[cfg(any(target_os = "android", target_os = "linux"))]
2626
mod xattrs;
27-
#[cfg(not(windows))]
27+
#[cfg(any(target_os = "android", target_os = "linux"))]
2828
pub use xattrs::XattrList;
2929

3030
#[cold]
31+
#[cfg_attr(
32+
not(any(target_os = "android", target_os = "linux", test)),
33+
allow(dead_code)
34+
)]
3135
pub(crate) fn escape_attempt() -> io::Error {
3236
io::Error::new(
3337
io::ErrorKind::PermissionDenied,

tests/it/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cap_std_ext::RootDir;
1212
#[cfg(unix)]
1313
use rustix::path::DecInt;
1414
use std::cmp::Ordering;
15+
#[cfg(any(target_os = "android", target_os = "linux"))]
1516
use std::ffi::OsStr;
1617
use std::io::Write;
1718
use std::ops::ControlFlow;
@@ -33,7 +34,8 @@ fn take_fd() -> Result<()> {
3334
c.stdout(std::process::Stdio::piped());
3435
let s = c.output()?;
3536
assert!(s.status.success());
36-
assert_eq!(s.stdout.as_slice(), b"11\n");
37+
let out = String::from_utf8_lossy(&s.stdout);
38+
assert_eq!(out.trim(), "11");
3739
Ok(())
3840
}
3941

@@ -43,7 +45,10 @@ fn fchdir() -> Result<()> {
4345
static CONTENTS: &[u8] = b"hello world";
4446

4547
fn new_cmd() -> Command {
48+
#[cfg(any(target_os = "android", target_os = "linux"))]
4649
let mut c = Command::new("/usr/bin/cat");
50+
#[cfg(not(any(target_os = "android", target_os = "linux")))]
51+
let mut c = Command::new("/bin/cat");
4752
c.arg("somefile");
4853
c
4954
}
@@ -718,7 +723,7 @@ fn test_walk_noxdev() -> Result<()> {
718723
}
719724

720725
#[test]
721-
#[cfg(not(windows))]
726+
#[cfg(any(target_os = "android", target_os = "linux"))]
722727
fn test_xattrs() -> Result<()> {
723728
use std::os::unix::ffi::OsStrExt;
724729

@@ -779,7 +784,7 @@ fn test_xattrs() -> Result<()> {
779784
}
780785

781786
#[test]
782-
#[cfg(not(windows))]
787+
#[cfg(any(target_os = "android", target_os = "linux"))]
783788
fn test_big_xattr() -> Result<()> {
784789
let td = &cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
785790

0 commit comments

Comments
 (0)