Merge pull request #312 from vbmade2000/omit-header

Implement omit-header flag
This commit is contained in:
Sylvestre Ledru
2026-06-03 16:28:10 +02:00
committed by GitHub
2 changed files with 59 additions and 6 deletions
+11 -5
View File
@@ -9,7 +9,7 @@ use std::os::unix::fs::{MetadataExt, PermissionsExt};
use uucore::error::UResult;
use uzers::{get_group_by_gid, get_user_by_uid};
fn print_file_acl(file_path: &str) -> std::io::Result<()> {
fn print_file_acl(file_path: &str, omit_header: bool) -> std::io::Result<()> {
let metadata = fs::metadata(file_path)?;
// Fetching owner and group names
@@ -43,9 +43,11 @@ fn print_file_acl(file_path: &str) -> std::io::Result<()> {
);
// Generating the output
println!("# file: {file_path}");
println!("# owner: {owner}");
println!("# group: {group}");
if !omit_header {
println!("# file: {file_path}");
println!("# owner: {owner}");
println!("# group: {group}");
}
println!("user::{user_perms}");
println!("group::{group_perms}");
println!("other::{other_perms}");
@@ -57,10 +59,13 @@ fn print_file_acl(file_path: &str) -> std::io::Result<()> {
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
// Get the omit-header flag value
let omit_header = matches.get_flag("omit-header");
// Example: Handle file arguments
if let Some(files) = matches.get_many::<String>("file") {
for file in files {
print_file_acl(file)?;
print_file_acl(file, omit_header)?;
// Implement the logic to fetch and display the ACLs using xattr
match xattr::list(file) {
@@ -109,6 +114,7 @@ pub fn uu_app() -> Command {
Arg::new("omit-header")
.short('c')
.long("omit-header")
.action(ArgAction::SetTrue)
.help("Do not display the comment header"),
)
.arg(
+48 -1
View File
@@ -3,9 +3,56 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use uutests::new_ucmd;
use pretty_assertions::assert_eq;
use uutests::util::TestScenario;
use uutests::{new_ucmd, util_name};
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
#[test]
fn test_basic_output_without_file() {
let res = new_ucmd!().succeeds();
let stdout = res.no_stderr().stdout_str();
// getfacl command shows no output if not given a file
assert_eq!(stdout.len(), 0);
}
#[test]
fn test_basic_output_with_file() {
// Create basic file with default permissions. No ACLs.
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("test_file");
let res = scene.ucmd().arg("test_file").succeeds();
let stdout = res.no_stderr().stdout_str();
assert!(stdout.contains("# file: test_file"));
assert!(stdout.contains("# owner:"));
assert!(stdout.contains("# group:"));
assert!(stdout.contains("user:"));
assert!(stdout.contains("group:"));
assert!(stdout.contains("other:"));
}
#[test]
fn test_basic_output_with_file_with_omit_header() {
// Create basic file with default permissions. No ACLs.
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("test_file");
let res = scene.ucmd().arg("test_file").arg("-c").succeeds();
let stdout = res.no_stderr().stdout_str();
assert_eq!(stdout.contains("# file: test_file"), false);
assert_eq!(stdout.contains("# owner:"), false);
assert_eq!(stdout.contains("# group:"), false);
assert!(stdout.contains("user:"));
assert!(stdout.contains("group:"));
assert!(stdout.contains("other:"));
}