From 5442fe4e7fcc5782a6d79db0735e9879a46355b2 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 11 May 2025 20:01:30 +0200 Subject: [PATCH 1/2] ls: we should always look for context (aka selinux) --- src/uu/ls/src/ls.rs | 6 +----- tests/by-util/test_ls.rs | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index b5b1d6df2cf..03b165fbad1 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1987,11 +1987,7 @@ impl PathData { None => OnceCell::new(), }; - let security_context = if config.context { - get_security_context(config, &p_buf, must_dereference) - } else { - String::new() - }; + let security_context = get_security_context(config, &p_buf, must_dereference); Self { md: OnceCell::new(), diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index f943bc131c7..b551a34b070 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -4150,6 +4150,22 @@ fn test_ls_dangling_symlinks() { } } +#[test] +#[cfg(feature = "feat_selinux")] +fn test_ls_with_selinux_ext() { + if !uucore::selinux::is_selinux_enabled() { + println!("test skipped: Kernel has no support for SElinux context"); + return; + } + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + at.touch("foo"); + + let result = scene.ucmd().args(&["-l", "foo"]).succeeds(); + let line: Vec<_> = result.stdout_str().split(' ').collect(); + assert!(line[0].ends_with('.')); +} + #[test] #[cfg(feature = "feat_selinux")] fn test_ls_context1() { @@ -5344,7 +5360,11 @@ fn test_acl_display() { // ... // drwxr-xr-x+ 2 user group 40 Apr 21 12:44 with_acl // drwxr-xr-x 2 user group 40 Apr 21 12:44 without_acl - let re_with_acl = Regex::new(r"[a-z-]*\+ .*with_acl").unwrap(); + let re_with_acl = if uucore::selinux::is_selinux_enabled() { + Regex::new(r"[a-z-]*\. .*with_acl").unwrap() + } else { + Regex::new(r"[a-z-]*\+ .*with_acl").unwrap() + }; let re_without_acl = Regex::new(r"[a-z-]* .*without_acl").unwrap(); scene From a07426af2c9c52a2af6a7042b9b5fa94f547a1b5 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 11 May 2025 21:34:22 +0200 Subject: [PATCH 2/2] ls: simplify the code, we don't need the len --- src/uu/ls/src/ls.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 03b165fbad1..493b95ac601 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -2846,7 +2846,7 @@ fn display_item_long( #[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))] let is_acl_set = has_acl(item.display_name.as_os_str()); output_display.extend(display_permissions(md, true).as_bytes()); - if item.security_context.len() > 1 { + if !item.security_context.is_empty() { // GNU `ls` uses a "." character to indicate a file with a security context, // but not other alternate access method. output_display.extend(b"."); @@ -2976,7 +2976,7 @@ fn display_item_long( output_display.extend(leading_char.as_bytes()); output_display.extend(b"?????????"); - if item.security_context.len() > 1 { + if !item.security_context.is_empty() { // GNU `ls` uses a "." character to indicate a file with a security context, // but not other alternate access method. output_display.extend(b".");