Skip to content

Commit 9a97c18

Browse files
committed
ls: when a file has capabilities (setcap), change the color
Should fix tests/ls/capability.sh
1 parent c22642b commit 9a97c18

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/uu/ls/src/colors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ pub(crate) fn color_name(
156156
target_symlink: Option<&PathData>,
157157
wrap: bool,
158158
) -> String {
159+
#[cfg(any(not(unix), target_os = "android", target_os = "macos"))]
160+
let has_capabilities = false;
161+
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
162+
// Check if the file has capabilities
163+
let has_capabilities = uucore::fsxattr::has_acl(path.p_buf.as_path());
164+
165+
// If the file has capabilities, use a specific style for `ca` (capabilities)
166+
if has_capabilities {
167+
if let Some(style) = style_manager
168+
.colors
169+
.style_for_indicator(Indicator::Capabilities)
170+
{
171+
return style_manager.apply_style(Some(style), name, wrap);
172+
}
173+
}
174+
159175
if !path.must_dereference {
160176
// If we need to dereference (follow) a symlink, we will need to get the metadata
161177
if let Some(de) = &path.de {

tests/by-util/test_ls.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55
// spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup lrwx somefile somegroup somehiddenbackup somehiddenfile tabsize aaaaaaaa bbbb cccc dddddddd ncccc neee naaaaa nbcdef nfffff dired subdired tmpfs mdir COLORTERM mexe bcdef mfoo
6+
// spell-checker:ignore (words) fakeroot setcap
67
#![allow(
78
clippy::similar_names,
89
clippy::too_many_lines,
@@ -5516,3 +5517,49 @@ fn test_suffix_case_sensitivity() {
55165517
/* cSpell:enable */
55175518
);
55185519
}
5520+
5521+
#[cfg(all(unix, target_os = "linux"))]
5522+
#[test]
5523+
fn test_ls_capabilities() {
5524+
let scene = TestScenario::new(util_name!());
5525+
let at = &scene.fixtures;
5526+
5527+
// Test must be run as root (or with `sudo -E`)
5528+
// fakeroot setcap cap_net_bind_service=ep /tmp/file_name
5529+
// doesn't trigger an error and fails silently
5530+
if scene.cmd("whoami").run().stdout_str() != "root\n" {
5531+
return;
5532+
}
5533+
at.mkdir("test");
5534+
at.mkdir("test/dir");
5535+
at.touch("test/cap_pos");
5536+
at.touch("test/dir/cap_neg");
5537+
at.touch("test/dir/cap_pos");
5538+
5539+
let files = ["test/cap_pos", "test/dir/cap_pos"];
5540+
for file in &files {
5541+
scene
5542+
.cmd("sudo")
5543+
.args(&[
5544+
"-E",
5545+
"--non-interactive",
5546+
"setcap",
5547+
"cap_net_bind_service=ep",
5548+
at.plus(file).to_str().unwrap(),
5549+
])
5550+
.succeeds();
5551+
}
5552+
5553+
let ls_colors = "di=:ca=30;41";
5554+
5555+
scene
5556+
.ucmd()
5557+
.env("LS_COLORS", ls_colors)
5558+
.arg("--color=always")
5559+
.arg("test/cap_pos")
5560+
.arg("test/dir")
5561+
.succeeds()
5562+
.stdout_contains("\x1b[30;41mtest/cap_pos") // spell-checker:disable-line
5563+
.stdout_contains("\x1b[30;41mcap_pos") // spell-checker:disable-line
5564+
.stdout_does_not_contain("0;41mtest/dir/cap_neg"); // spell-checker:disable-line
5565+
}

0 commit comments

Comments
 (0)