Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ls: if acl are used, show the + in the perms #5816

Merged
merged 3 commits into from Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
"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");

Check warning on line 4317 in tests/by-util/test_ls.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_ls.rs#L4317

Added line #L4317 was not covered by tests
return;
}
Err(e) => {
println!("test skipped: setfacl failed with {}", e);

Check warning on line 4321 in tests/by-util/test_ls.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_ls.rs#L4320-L4321

Added lines #L4320 - L4321 were not covered by tests
return;
}

Check warning on line 4323 in tests/by-util/test_ls.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_ls.rs#L4323

Added line #L4323 was not covered by tests
}

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