diff --git a/Cargo.lock b/Cargo.lock index 0ef909d73..cbff54c4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2608,7 +2608,6 @@ dependencies = [ "unicode-width", "uucore", "uutils_term_grid", - "xattr", ] [[package]] diff --git a/src/uu/ls/Cargo.toml b/src/uu/ls/Cargo.toml index 38312eefc..dc79c6f93 100644 --- a/src/uu/ls/Cargo.toml +++ b/src/uu/ls/Cargo.toml @@ -27,6 +27,7 @@ uucore = { workspace = true, features = [ "colors", "entries", "fs", + "fsxattr", "quoting-style", "version-cmp", ] } @@ -34,9 +35,6 @@ once_cell = { workspace = true } selinux = { workspace = true, optional = true } hostname = { workspace = true } -[target.'cfg(unix)'.dependencies] -xattr = { workspace = true } - [[bin]] name = "ls" path = "src/main.rs" diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 1c89cd353..ed100477f 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) somegroup nlink tabsize dired subdired dtype colorterm getxattr +// spell-checker:ignore (ToDO) somegroup nlink tabsize dired subdired dtype colorterm use clap::{ builder::{NonEmptyStringValueParser, ValueParser}, @@ -36,7 +36,8 @@ 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"))))] +use uucore::fsxattr::has_acl; #[cfg(any( target_os = "linux", target_os = "macos", @@ -2621,18 +2622,6 @@ fn display_grid( Ok(()) } -#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))] -fn file_has_acl>(file: P) -> bool { - // don't use exacl here, it is doing more getxattr call then needed - match xattr::list(file) { - Ok(acl) => { - // if we have extra attributes, we have an acl - acl.count() > 0 - } - Err(_) => false, - } -} - /// This writes to the BufWriter out a single string of the output of `ls -l`. /// /// It writes the following keys, in order: @@ -2680,7 +2669,7 @@ fn display_item_long( // TODO: See how Mac should work here let is_acl_set = false; #[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))] - let is_acl_set = file_has_acl(item.display_name.as_os_str()); + let is_acl_set = has_acl(item.display_name.as_os_str()); write!( output_display, "{}{}{} {}", diff --git a/src/uucore/src/lib/features/fsxattr.rs b/src/uucore/src/lib/features/fsxattr.rs index 7bda023f9..36ec0ba8e 100644 --- a/src/uucore/src/lib/features/fsxattr.rs +++ b/src/uucore/src/lib/features/fsxattr.rs @@ -66,6 +66,26 @@ pub fn apply_xattrs>( Ok(()) } +/// Checks if a file has an Access Control List (ACL) based on its extended attributes. +/// +/// # Arguments +/// +/// * `file` - A reference to the path of the file. +/// +/// # Returns +/// +/// `true` if the file has extended attributes (indicating an ACL), `false` otherwise. +pub fn has_acl>(file: P) -> bool { + // don't use exacl here, it is doing more getxattr call then needed + match xattr::list(file) { + Ok(acl) => { + // if we have extra attributes, we have an acl + acl.count() > 0 + } + Err(_) => false, + } +} + #[cfg(test)] mod tests { use super::*; @@ -113,4 +133,20 @@ mod tests { test_value ); } + + #[test] + fn test_file_has_acl() { + let temp_dir = tempdir().unwrap(); + let file_path = temp_dir.path().join("test_file.txt"); + + File::create(&file_path).unwrap(); + + assert!(!has_acl(&file_path)); + + let test_attr = "user.test_acl"; + let test_value = b"test value"; + xattr::set(&file_path, test_attr, test_value).unwrap(); + + assert!(has_acl(&file_path)); + } }