Skip to content

Commit

Permalink
show @ if extended attributes are present
Browse files Browse the repository at this point in the history
  • Loading branch information
accidentaldevelopment committed Dec 1, 2023
1 parent 8937c47 commit c1b9a0d
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions src/meta/access_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use std::path::Path;
#[derive(Clone, Debug)]
pub struct AccessControl {
has_acl: bool,
has_xattr: bool,
selinux_context: String,
smack_context: String,
}

impl AccessControl {
#[cfg(not(unix))]
pub fn for_path(_: &Path) -> Self {
Self::from_data(false, &[], &[])
Self::from_data(false, false, &[], &[])
}

#[cfg(unix)]
Expand All @@ -27,14 +28,27 @@ impl AccessControl {
.unwrap_or_default()
.unwrap_or_default();

Self::from_data(has_acl, &selinux_context, &smack_context)
let has_xattr = has_acl
|| !selinux_context.is_empty()
|| !smack_context.is_empty()
|| xattr::list(path)
.map(|x| x.into_iter().count() > 0)
.unwrap_or(false);

Self::from_data(has_acl, has_xattr, &selinux_context, &smack_context)
}

fn from_data(has_acl: bool, selinux_context: &[u8], smack_context: &[u8]) -> Self {
fn from_data(
has_acl: bool,
has_xattr: bool,
selinux_context: &[u8],
smack_context: &[u8],
) -> Self {
let selinux_context = String::from_utf8_lossy(selinux_context).to_string();
let smack_context = String::from_utf8_lossy(smack_context).to_string();
Self {
has_acl,
has_xattr,
selinux_context,
smack_context,
}
Expand All @@ -43,6 +57,8 @@ impl AccessControl {
pub fn render_method(&self, colors: &Colors) -> ColoredString {
if self.has_acl {
colors.colorize('+', &Elem::Acl)
} else if self.has_xattr {
colors.colorize('@', &Elem::Acl)
} else if !self.selinux_context.is_empty() || !self.smack_context.is_empty() {
colors.colorize('.', &Elem::Context)
} else {
Expand Down Expand Up @@ -92,17 +108,36 @@ mod test {
#[test]
fn test_acl_only_indicator() {
// actual file would collide with proper AC data, no permission to scrub those
let access_control = AccessControl::from_data(true, &[], &[]);
let access_control = AccessControl::from_data(true, false, &[], &[]);

assert_eq!(
String::from("+").with(Color::DarkCyan),
access_control.render_method(&Colors::new(ThemeOption::Default))
);
}

#[test]
fn test_extended_attr_indicator() {
let access_control = AccessControl::from_data(false, true, &[], &[]);

assert_eq!(
String::from("@").with(Color::DarkCyan),
access_control.render_method(&Colors::new(ThemeOption::Default))
);
}

#[test]
fn test_extended_attr_with_acl_indicator() {
let access_control = AccessControl::from_data(true, true, &[], &[]);

assert_eq!(
String::from("+").with(Color::DarkCyan),
access_control.render_method(&Colors::new(ThemeOption::Default))
);
}
#[test]
fn test_smack_only_indicator() {
let access_control = AccessControl::from_data(false, &[], &[b'a']);
let access_control = AccessControl::from_data(false, false, &[], &[b'a']);

assert_eq!(
String::from(".").with(Color::Cyan),
Expand All @@ -112,7 +147,7 @@ mod test {

#[test]
fn test_acl_and_selinux_indicator() {
let access_control = AccessControl::from_data(true, &[b'a'], &[]);
let access_control = AccessControl::from_data(true, false, &[b'a'], &[]);

assert_eq!(
String::from("+").with(Color::DarkCyan),
Expand All @@ -122,7 +157,7 @@ mod test {

#[test]
fn test_selinux_context() {
let access_control = AccessControl::from_data(false, &[b'a'], &[]);
let access_control = AccessControl::from_data(false, false, &[b'a'], &[]);

assert_eq!(
String::from("a").with(Color::Cyan),
Expand All @@ -132,7 +167,7 @@ mod test {

#[test]
fn test_selinux_and_smack_context() {
let access_control = AccessControl::from_data(false, &[b'a'], &[b'b']);
let access_control = AccessControl::from_data(false, false, &[b'a'], &[b'b']);

assert_eq!(
String::from("a+b").with(Color::Cyan),
Expand All @@ -142,7 +177,7 @@ mod test {

#[test]
fn test_no_context() {
let access_control = AccessControl::from_data(false, &[], &[]);
let access_control = AccessControl::from_data(false, false, &[], &[]);

assert_eq!(
String::from("?").with(Color::Cyan),
Expand Down

0 comments on commit c1b9a0d

Please sign in to comment.