diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index b5b1d6df2cf..493b95ac601 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(), @@ -2850,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"."); @@ -2980,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"."); 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