Skip to content

Commit

Permalink
ls: if acl are used, show the + in the perms (uutils#5816)
Browse files Browse the repository at this point in the history
* ls: if acl are used, show the + in the perms

Tested by tests/mkdir/p-acl.sh

* CICD.yml: fix small formatting issue

---------

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
  • Loading branch information
sylvestre and cakebaker committed Jan 19, 2024
1 parent e860b32 commit dd73b27
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
20 changes: 16 additions & 4 deletions .github/workflows/CICD.yml
Expand Up @@ -612,11 +612,23 @@ jobs:
run: |
## Install/setup prerequisites
case '${{ matrix.job.target }}' in
arm-unknown-linux-gnueabihf) sudo apt-get -y update ; sudo apt-get -y install gcc-arm-linux-gnueabihf ;;
aarch64-unknown-linux-*) sudo apt-get -y update ; sudo apt-get -y install gcc-aarch64-linux-gnu ;;
*-redox*) sudo apt-get -y update ; sudo apt-get -y install fuse3 libfuse-dev ;;
arm-unknown-linux-gnueabihf)
sudo apt-get -y update
sudo apt-get -y install gcc-arm-linux-gnueabihf
;;
aarch64-unknown-linux-*)
sudo apt-get -y update
sudo apt-get -y install gcc-aarch64-linux-gnu
;;
*-redox*)
sudo apt-get -y update
sudo apt-get -y install fuse3 libfuse-dev
;;
# Update binutils if MinGW due to https://github.com/rust-lang/rust/issues/112368
x86_64-pc-windows-gnu) C:/msys64/usr/bin/pacman.exe -Syu --needed mingw-w64-x86_64-gcc --noconfirm ; echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH ;;
x86_64-pc-windows-gnu)
C:/msys64/usr/bin/pacman.exe -Syu --needed mingw-w64-x86_64-gcc --noconfirm
echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH
;;
esac
case '${{ matrix.job.os }}' in
macos-latest) brew install coreutils ;; # needed for testing
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/uu/ls/Cargo.toml
Expand Up @@ -34,6 +34,9 @@ once_cell = { workspace = true }
selinux = { workspace = true, optional = true }
hostname = { workspace = true }

[target.'cfg(unix)'.dependencies]
xattr = { workspace = true }

[[bin]]
name = "ls"
path = "src/main.rs"
Expand Down
28 changes: 26 additions & 2 deletions src/uu/ls/src/ls.rs
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) somegroup nlink tabsize dired subdired dtype colorterm
// spell-checker:ignore (ToDO) somegroup nlink tabsize dired subdired dtype colorterm getxattr

use clap::{
builder::{NonEmptyStringValueParser, ValueParser},
Expand Down Expand Up @@ -36,6 +36,7 @@ use std::{
};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use unicode_width::UnicodeWidthStr;

#[cfg(any(
target_os = "linux",
target_os = "macos",
Expand Down Expand Up @@ -2620,6 +2621,18 @@ fn display_grid(
Ok(())
}

#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
fn file_has_acl<P: AsRef<Path>>(file: P) -> bool {
// don't use exacl here, it is doing more getxattr call then needed
match xattr::list(file) {
Ok(acl) => {
// if we have extra attributes, we have an acl
acl.count() > 0
}
Err(_) => false,
}
}

/// This writes to the BufWriter out a single string of the output of `ls -l`.
///
/// It writes the following keys, in order:
Expand Down Expand Up @@ -2663,9 +2676,14 @@ fn display_item_long(
output_display += " ";
}
if let Some(md) = item.get_metadata(out) {
#[cfg(any(not(unix), target_os = "android", target_os = "macos"))]
// TODO: See how Mac should work here
let is_acl_set = false;
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
let is_acl_set = file_has_acl(item.display_name.as_os_str());
write!(
output_display,
"{}{} {}",
"{}{}{} {}",
display_permissions(md, true),
if item.security_context.len() > 1 {
// GNU `ls` uses a "." character to indicate a file with a security context,
Expand All @@ -2674,6 +2692,12 @@ fn display_item_long(
} else {
""
},
if is_acl_set {
// if acl has been set, we display a "+" at the end of the file permissions
"+"
} else {
""
},
pad_left(&display_symlink_count(md), padding.link_count)
)
.unwrap();
Expand Down
36 changes: 36 additions & 0 deletions tests/by-util/test_ls.rs
Expand Up @@ -4293,3 +4293,39 @@ fn test_term_colorterm() {
"exe"
);
}

#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn test_acl_display() {
use std::process::Command;

let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let path = "a42";
at.mkdir(path);

let path = at.plus_as_string(path);
// calling the command directly. xattr requires some dev packages to be installed
// and it adds a complex dependency just for a test
match Command::new("setfacl")
.args(["-d", "-m", "group::rwx", &path])
.status()
.map(|status| status.code())
{
Ok(Some(0)) => {}
Ok(_) => {
println!("test skipped: setfacl failed");
return;
}
Err(e) => {
println!("test skipped: setfacl failed with {}", e);
return;
}
}

scene
.ucmd()
.args(&["-lda", &path])
.succeeds()
.stdout_contains("+");
}

0 comments on commit dd73b27

Please sign in to comment.