Skip to content

Commit

Permalink
acl: add suport for mac
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Jan 22, 2024
1 parent 8493800 commit dfec73e
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 18 deletions.
15 changes: 10 additions & 5 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use std::{
};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use unicode_width::UnicodeWidthStr;
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
#[cfg(all(unix, not(target_os = "android")))]
use uucore::fsxattr::has_acl;
#[cfg(any(
target_os = "linux",
Expand Down Expand Up @@ -2665,10 +2665,9 @@ fn display_item_long(
output_display += " ";
}
if let Some(md) = item.get_metadata(out) {
#[cfg(any(not(unix), target_os = "android", target_os = "macos"))]
// TODO: See how Mac should work here
#[cfg(any(not(unix), target_os = "android"))]
let is_acl_set = false;
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
#[cfg(all(unix, not(target_os = "android")))]
let is_acl_set = has_acl(item.display_name.as_os_str());
write!(
output_display,
Expand All @@ -2683,7 +2682,13 @@ fn display_item_long(
},
if is_acl_set {
// if acl has been set, we display a "+" at the end of the file permissions
"+"
// and @ on mac
#[cfg(not(target_os = "macos"))]
let extended = "+";
#[cfg(target_os = "macos")]
let extended = "@";

extended
} else {
""
},
Expand Down
3 changes: 0 additions & 3 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use uucore::fs::{
are_hardlinks_or_one_way_symlink_to_same_file, are_hardlinks_to_same_file,
path_ends_with_terminator,
};
#[cfg(all(unix, not(target_os = "macos")))]
use uucore::fsxattr;
use uucore::update_control;

Expand Down Expand Up @@ -634,7 +633,6 @@ fn rename_with_fallback(
None
};

#[cfg(all(unix, not(target_os = "macos")))]
let xattrs =
fsxattr::retrieve_xattrs(from).unwrap_or_else(|_| std::collections::HashMap::new());

Expand All @@ -648,7 +646,6 @@ fn rename_with_fallback(
move_dir(from, to, &options)
};

#[cfg(all(unix, not(target_os = "macos")))]
fsxattr::apply_xattrs(to, xattrs).unwrap();

if let Err(err) = result {
Expand Down
2 changes: 1 addition & 1 deletion src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ sha3 = { workspace = true, optional = true }
blake2b_simd = { workspace = true, optional = true }
blake3 = { workspace = true, optional = true }
sm3 = { workspace = true, optional = true }
xattr = { workspace = true, optional = true }

[target.'cfg(unix)'.dependencies]
walkdir = { workspace = true, optional = true }
nix = { workspace = true, features = ["fs", "uio", "zerocopy", "signal"] }
xattr = { workspace = true, optional = true }

[dev-dependencies]
clap = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion src/uucore/src/lib/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub mod pipes;
#[cfg(all(unix, feature = "process"))]
pub mod process;

#[cfg(all(unix, not(target_os = "macos"), feature = "fsxattr"))]
#[cfg(all(unix, feature = "fsxattr"))]
pub mod fsxattr;
#[cfg(all(unix, not(target_os = "fuchsia"), feature = "signals"))]
pub mod signals;
Expand Down
2 changes: 1 addition & 1 deletion src/uucore/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub use crate::features::wide;
#[cfg(feature = "fsext")]
pub use crate::features::fsext;

#[cfg(all(unix, not(target_os = "macos"), feature = "fsxattr"))]
#[cfg(feature = "fsxattr")]
pub use crate::features::fsxattr;

//## core functions
Expand Down
25 changes: 23 additions & 2 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static TEST_MOUNT_MOUNTPOINT: &str = "mount";
static TEST_MOUNT_OTHER_FILESYSTEM_FILE: &str = "mount/DO_NOT_copy_me.txt";
#[cfg(unix)]
static TEST_NONEXISTENT_FILE: &str = "nonexistent_file.txt";
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
#[cfg(all(unix, not(target_os = "android")))]
use crate::common::util::compare_xattrs;

/// Assert that mode, ownership, and permissions of two metadata objects match.
Expand Down Expand Up @@ -3739,7 +3739,7 @@ fn test_cp_no_such() {
.stderr_is("cp: 'no-such/' is not a directory\n");
}

#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
#[cfg(all(unix, not(target_os = "android")))]
#[test]
fn test_acl_preserve() {
use std::process::Command;
Expand All @@ -3757,6 +3757,7 @@ fn test_acl_preserve() {
let path = at.plus_as_string(file);
// calling the command directly. xattr requires some dev packages to be installed
// and it adds a complex dependency just for a test
#[cfg(not(target_os = "macos"))]
match Command::new("setfacl")
.args(["-m", "group::rwx", &path1])
.status()
Expand All @@ -3772,6 +3773,26 @@ fn test_acl_preserve() {
return;
}
}
#[cfg(target_os = "macos")]
match Command::new("chmod")
.args([
"+a",
&format!("group:staff allow read,write,execute"),
&path,
])
.status()
.map(|status| status.code())
{
Ok(Some(0)) => {}
Ok(_) => {
println!("test skipped: chmod failed");
return;
}
Err(e) => {
println!("test skipped: chmod failed with {}", e);
return;
}
}

scene.ucmd().args(&["-p", &path, path2]).succeeds();

Expand Down
29 changes: 27 additions & 2 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4294,7 +4294,6 @@ fn test_term_colorterm() {
);
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_acl_display() {
use std::process::Command;
Expand All @@ -4307,6 +4306,7 @@ fn test_acl_display() {
let path = at.plus_as_string(path);
// calling the command directly. xattr requires some dev packages to be installed
// and it adds a complex dependency just for a test
#[cfg(not(target_os = "macos"))]
match Command::new("setfacl")
.args(["-d", "-m", "group::rwx", &path])
.status()
Expand All @@ -4322,10 +4322,35 @@ fn test_acl_display() {
return;
}
}
#[cfg(target_os = "macos")]
match Command::new("chmod")
.args([
"+a",
&format!("group:staff allow read,write,execute"),
&path,
])
.status()
.map(|status| status.code())
{
Ok(Some(0)) => {}
Ok(_) => {
println!("test skipped: chmod failed");
return;
}
Err(e) => {
println!("test skipped: chmod failed with {}", e);
return;
}
}

#[cfg(not(target_os = "macos"))]
let extended = "+";
#[cfg(target_os = "macos")]
let extended = "@";

scene
.ucmd()
.args(&["-lda", &path])
.succeeds()
.stdout_contains("+");
.stdout_contains(extended);
}
23 changes: 22 additions & 1 deletion tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,6 @@ fn test_mv_dir_into_path_slash() {
assert!(at.dir_exists("f/b"));
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_acl() {
use std::process::Command;
Expand All @@ -1589,6 +1588,7 @@ fn test_acl() {
let path = at.plus_as_string(file);
// calling the command directly. xattr requires some dev packages to be installed
// and it adds a complex dependency just for a test
#[cfg(not(target_os = "macos"))]
match Command::new("setfacl")
.args(["-m", "group::rwx", &path1])
.status()
Expand All @@ -1605,6 +1605,27 @@ fn test_acl() {
}
}

#[cfg(target_os = "macos")]
match Command::new("chmod")
.args([
"+a",
&format!("group:staff allow read,write,execute"),
&path1,
])
.status()
.map(|status| status.code())
{
Ok(Some(0)) => {}
Ok(_) => {
println!("test skipped: chmod failed");
return;
}
Err(e) => {
println!("test skipped: chmod failed with {}", e);
return;
}
}

scene.ucmd().arg(&path).arg(path2).succeeds();

assert!(compare_xattrs(&file, &file_target));
Expand Down
2 changes: 0 additions & 2 deletions tests/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ pub fn get_root_path() -> &'static str {
/// # Returns
///
/// `true` if both paths have the same set of extended attributes, `false` otherwise.
#[cfg(all(unix, not(target_os = "macos")))]
pub fn compare_xattrs<P: AsRef<std::path::Path>>(path1: P, path2: P) -> bool {
let get_sorted_xattrs = |path: P| {
xattr::list(path)
Expand Down Expand Up @@ -3396,7 +3395,6 @@ mod tests {
assert!(command.tmpd.is_some());
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_compare_xattrs() {
use tempfile::tempdir;
Expand Down

0 comments on commit dfec73e

Please sign in to comment.