1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

move the file_has_acl function into uucore

This commit is contained in:
Sylvestre Ledru 2024-01-14 17:46:45 +01:00
parent 2ec4e9f784
commit 66637a6503
4 changed files with 41 additions and 19 deletions

1
Cargo.lock generated
View file

@ -2608,7 +2608,6 @@ dependencies = [
"unicode-width",
"uucore",
"uutils_term_grid",
"xattr",
]
[[package]]

View file

@ -27,6 +27,7 @@ uucore = { workspace = true, features = [
"colors",
"entries",
"fs",
"fsxattr",
"quoting-style",
"version-cmp",
] }
@ -34,9 +35,6 @@ 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"

View file

@ -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 getxattr
// spell-checker:ignore (ToDO) somegroup nlink tabsize dired subdired dtype colorterm
use clap::{
builder::{NonEmptyStringValueParser, ValueParser},
@ -36,7 +36,8 @@ use std::{
};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use unicode_width::UnicodeWidthStr;
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
use uucore::fsxattr::has_acl;
#[cfg(any(
target_os = "linux",
target_os = "macos",
@ -2621,18 +2622,6 @@ 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:
@ -2680,7 +2669,7 @@ fn display_item_long(
// 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());
let is_acl_set = has_acl(item.display_name.as_os_str());
write!(
output_display,
"{}{}{} {}",

View file

@ -66,6 +66,26 @@ pub fn apply_xattrs<P: AsRef<Path>>(
Ok(())
}
/// Checks if a file has an Access Control List (ACL) based on its extended attributes.
///
/// # Arguments
///
/// * `file` - A reference to the path of the file.
///
/// # Returns
///
/// `true` if the file has extended attributes (indicating an ACL), `false` otherwise.
pub fn 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,
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -113,4 +133,20 @@ mod tests {
test_value
);
}
#[test]
fn test_file_has_acl() {
let temp_dir = tempdir().unwrap();
let file_path = temp_dir.path().join("test_file.txt");
File::create(&file_path).unwrap();
assert!(!has_acl(&file_path));
let test_attr = "user.test_acl";
let test_value = b"test value";
xattr::set(&file_path, test_attr, test_value).unwrap();
assert!(has_acl(&file_path));
}
}