mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
ls: when a file has capabilities (setcap), change the color
Should fix tests/ls/capability.sh
This commit is contained in:
parent
c22642b68c
commit
9a97c18877
2 changed files with 63 additions and 0 deletions
|
@ -156,6 +156,22 @@ pub(crate) fn color_name(
|
||||||
target_symlink: Option<&PathData>,
|
target_symlink: Option<&PathData>,
|
||||||
wrap: bool,
|
wrap: bool,
|
||||||
) -> String {
|
) -> 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
|
||||||
|
.colors
|
||||||
|
.style_for_indicator(Indicator::Capabilities)
|
||||||
|
{
|
||||||
|
return style_manager.apply_style(Some(style), name, wrap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !path.must_dereference {
|
if !path.must_dereference {
|
||||||
// If we need to dereference (follow) a symlink, we will need to get the metadata
|
// If we need to dereference (follow) a symlink, we will need to get the metadata
|
||||||
if let Some(de) = &path.de {
|
if let Some(de) = &path.de {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// For the full copyright and license information, please view the LICENSE
|
// For the full copyright and license information, please view the LICENSE
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
// 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
|
// 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
|
||||||
|
// spell-checker:ignore (words) fakeroot setcap
|
||||||
#![allow(
|
#![allow(
|
||||||
clippy::similar_names,
|
clippy::similar_names,
|
||||||
clippy::too_many_lines,
|
clippy::too_many_lines,
|
||||||
|
@ -5516,3 +5517,49 @@ fn test_suffix_case_sensitivity() {
|
||||||
/* cSpell:enable */
|
/* cSpell:enable */
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(unix, target_os = "linux"))]
|
||||||
|
#[test]
|
||||||
|
fn test_ls_capabilities() {
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
|
let at = &scene.fixtures;
|
||||||
|
|
||||||
|
// Test must be run as root (or with `sudo -E`)
|
||||||
|
// fakeroot setcap cap_net_bind_service=ep /tmp/file_name
|
||||||
|
// doesn't trigger an error and fails silently
|
||||||
|
if scene.cmd("whoami").run().stdout_str() != "root\n" {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
at.mkdir("test");
|
||||||
|
at.mkdir("test/dir");
|
||||||
|
at.touch("test/cap_pos");
|
||||||
|
at.touch("test/dir/cap_neg");
|
||||||
|
at.touch("test/dir/cap_pos");
|
||||||
|
|
||||||
|
let files = ["test/cap_pos", "test/dir/cap_pos"];
|
||||||
|
for file in &files {
|
||||||
|
scene
|
||||||
|
.cmd("sudo")
|
||||||
|
.args(&[
|
||||||
|
"-E",
|
||||||
|
"--non-interactive",
|
||||||
|
"setcap",
|
||||||
|
"cap_net_bind_service=ep",
|
||||||
|
at.plus(file).to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.succeeds();
|
||||||
|
}
|
||||||
|
|
||||||
|
let ls_colors = "di=:ca=30;41";
|
||||||
|
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.env("LS_COLORS", ls_colors)
|
||||||
|
.arg("--color=always")
|
||||||
|
.arg("test/cap_pos")
|
||||||
|
.arg("test/dir")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_contains("\x1b[30;41mtest/cap_pos") // spell-checker:disable-line
|
||||||
|
.stdout_contains("\x1b[30;41mcap_pos") // spell-checker:disable-line
|
||||||
|
.stdout_does_not_contain("0;41mtest/dir/cap_neg"); // spell-checker:disable-line
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue