Skip to content

Commit

Permalink
Add example CLI app for printing file ACL
Browse files Browse the repository at this point in the history
  • Loading branch information
intgr committed Apr 5, 2020
1 parent 46ca60d commit 15e5815
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions examples/printacl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Example CLI app to read and print ACL of a file.
//!
//! Run with command: `cargo run --example printacl /some/path`

use posix_acl::Qualifier::*;
use posix_acl::{ACLEntry, PosixACL, ACL_EXECUTE, ACL_READ, ACL_WRITE};
use std::env::args_os;
use std::path::Path;
use std::process::exit;

fn print_acl(acl: PosixACL) {
let entries = acl.entries();
if entries.is_empty() {
// Happens
println!(" (no entries)")
}
for entry in entries {
let ACLEntry { qual, perm } = entry;
println!(
" {}:{}{}{}",
match qual {
Undefined => "invalid:".into(),
UserObj => "user:".into(),
GroupObj => "group:".into(),
Other => "other:".into(),
User(uid) => format!("user:{}", uid),
Group(gid) => format!("group:{}", gid),
Mask => "mask:".into(),
},
if perm & ACL_READ != 0 { "r" } else { "-" },
if perm & ACL_WRITE != 0 { "w" } else { "-" },
if perm & ACL_EXECUTE != 0 { "x" } else { "-" },
)
}
}

fn main() {
let args = args_os();
if args.len() <= 1 {
println!("Usage: printacl [FILE ...]");
exit(1);
}

for filename in args.skip(1) {
let path = Path::new(&filename);
match PosixACL::read_acl(path) {
Ok(acl) => {
println!("{} ACL:", path.display());
print_acl(acl)
}
Err(e) => eprintln!("{}: {}", path.display(), e),
}
if path.is_dir() {
match PosixACL::read_default_acl(path) {
Ok(acl) => {
println!("{} DEFAULT:", path.display());
print_acl(acl)
}
Err(e) => eprintln!("{}: {}", path.display(), e),
}
}
println!();
}
}

0 comments on commit 15e5815

Please sign in to comment.