Skip to content

Commit

Permalink
ls: Don't call the capabilites features of the system when passed an …
Browse files Browse the repository at this point in the history
…empty ca=

in LS_COLORS

In parallel, in the GNU test, adjust the GNU tests as we don't use libcap
but xattr instead.
  • Loading branch information
sylvestre committed Dec 21, 2024
1 parent 693f6a2 commit 47c72e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .vscode/cspell.dictionaries/jargon.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bytewise
canonicalization
canonicalize
canonicalizing
capget
codepoint
codepoints
codegen
Expand Down Expand Up @@ -65,6 +66,7 @@ kibi
kibibytes
libacl
lcase
llistxattr
lossily
lstat
mebi
Expand Down Expand Up @@ -108,6 +110,7 @@ seedable
semver
semiprime
semiprimes
setcap
setfacl
shortcode
shortcodes
Expand Down
26 changes: 15 additions & 11 deletions src/uu/ls/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,23 @@ pub(crate) fn color_name(
target_symlink: Option<&PathData>,
wrap: bool,
) -> String {
#[cfg(any(not(unix), target_os = "android", target_os = "macos"))]
let has_capabilities = false;
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
// Check if the file has capabilities
let has_capabilities = uucore::fsxattr::has_acl(path.p_buf.as_path());

// If the file has capabilities, use a specific style for `ca` (capabilities)
if has_capabilities {
if let Some(style) = style_manager
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
{
// Skip checking capabilities if LS_COLORS=ca=:
let capabilities = style_manager
.colors
.style_for_indicator(Indicator::Capabilities)
{
return style_manager.apply_style(Some(style), name, wrap);
.style_for_indicator(Indicator::Capabilities);

let has_capabilities = if capabilities.is_none() {
false
} else {
uucore::fsxattr::has_acl(path.p_buf.as_path())
};

// If the file has capabilities, use a specific style for `ca` (capabilities)
if has_capabilities {
return style_manager.apply_style(capabilities, name, wrap);
}
}

Expand Down
20 changes: 20 additions & 0 deletions util/gnu-patches/tests_ls_no_cap.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
diff --git a/tests/ls/no-cap.sh b/tests/ls/no-cap.sh
index 7707421fa..bc82e08ed 100755
--- a/tests/ls/no-cap.sh
+++ b/tests/ls/no-cap.sh
@@ -27,11 +27,11 @@ setcap 'cap_net_bind_service=ep' file ||
skip_ "setcap doesn't work"

LS_COLORS=ca=1; export LS_COLORS
-strace -e capget ls --color=always > /dev/null 2> out || fail=1
-$EGREP 'capget\(' out || skip_ "your ls doesn't call capget"
+strace -e llistxattr ls --color=always > /dev/null 2> out || fail=1
+$EGREP 'llistxattr\(' out || skip_ "your ls doesn't call llistxattr"

LS_COLORS=ca=:; export LS_COLORS
-strace -e capget ls --color=always > /dev/null 2> out || fail=1
-$EGREP 'capget\(' out && fail=1
+strace -e llistxattr ls --color=always > /dev/null 2> out || fail=1
+$EGREP 'llistxattr\(' out && fail=1

Exit $fail

0 comments on commit 47c72e0

Please sign in to comment.