0
0
mirror of https://github.com/TeFiLeDo/tree-owners.git synced 2024-11-15 00:46:16 +01:00
tree-owners/src/main.rs

59 lines
1.5 KiB
Rust
Raw Normal View History

2023-09-24 12:23:20 +02:00
use std::{fs::read_dir, os::linux::fs::MetadataExt, path::Path};
2023-09-24 05:38:22 +02:00
use anyhow::{ensure, Context, Result};
use clap::Parser;
2023-09-24 12:23:20 +02:00
use crate::{cli::Args, id::Ids, name::Names, output::Output};
2023-09-24 05:38:22 +02:00
2023-09-24 12:23:20 +02:00
mod cli;
2023-09-24 05:38:22 +02:00
mod id;
mod name;
mod output;
fn main() -> Result<()> {
human_panic::setup_panic!();
let args = Args::parse();
let mut ids = Ids::default();
for root in args.roots {
fs_entry(&root, &mut ids)?;
}
let output: Box<dyn Output> = match args.raw {
false => Box::new(Names::try_from(ids).context("failed to get names")?),
true => Box::new(ids),
};
let output = match args.json {
false => output.human_readable(),
true => output.json().context("failed json serialization")?,
};
println!("{output}");
Ok(())
}
2023-09-24 05:52:27 +02:00
/// Perform gid & uid gathering for a file, or a directory and its children.
2023-09-24 05:38:22 +02:00
fn fs_entry(entry: &Path, summary: &mut Ids) -> Result<()> {
let display = entry.display();
ensure!(
entry.is_symlink() || entry.exists(),
format!("{} doesn't exist", display)
);
let meta = entry
.symlink_metadata()
.context(format!("failed to get metadata for {}", display))?;
summary.users.insert(meta.st_uid());
summary.groups.insert(meta.st_gid());
if entry.is_dir() {
let children = read_dir(entry).context(format!("failed to read dir {}", display))?;
for e in children {
let e = e.context(format!("invalid child for {}", display))?;
2023-09-24 07:51:45 +02:00
fs_entry(&e.path(), summary)?;
2023-09-24 05:38:22 +02:00
}
}
Ok(())
}