Altered directory api for optimisations when -t is not specified

main
nick 2024-07-09 15:41:55 -04:00
parent a2eabb62eb
commit 848455d0a4
2 changed files with 30 additions and 13 deletions

View File

@ -31,14 +31,18 @@ impl Directory {
// NOTE: I go back and forth on canonicalize()ing all the time.
// I feel like it changes every commit. The performance loss seems
// to be negligible, even when I do crazy things like `hb -p /`
let name = match path.canonicalize() {
Ok(path) => path,
Err(_) if args.persistant() => return Ok(None),
Err(e) => return Err(e.into()),
}
.file_name()
.map_or_else(|| OsString::from("/"), ToOwned::to_owned)
.into();
let name = if args.tree() {
match path.canonicalize() {
Ok(path) => path,
Err(_) if args.persistant() => return Ok(None),
Err(e) => return Err(e.into()),
}
.file_name()
.map_or_else(|| OsString::from("/"), ToOwned::to_owned)
.into()
} else {
PathBuf::new()
};
// symlink_metadata() is the same as metadata() but it doesn't
// traverse symlinks, so that we can exclude them if necessary
@ -94,7 +98,7 @@ impl Directory {
(Err(e), false) => return Err(e),
};
size += dir.size;
if args.should_print(dir.path()) {
if args.tree() && args.should_print(dir.path()) {
// since size was increased, this just prevents
// the directory from appearing in printing
children.push(dir);
@ -109,7 +113,7 @@ impl Directory {
.try_reduce(
|| (0, Vec::new()),
|(asize, mut avec), (bsize, bvec)| {
avec.extend(bvec);
if args.tree() { avec.extend(bvec); }
Result::Ok((asize + bsize, avec))
}
) {

View File

@ -4,16 +4,29 @@ mod args;
mod directory;
mod unit;
use clap::Parser;
use args::Args;
use directory::Directory;
use rayon::ThreadPoolBuilder;
use std::process::ExitCode;
fn main() -> ExitCode {
let args = Args::parse_and_process();
let args = Args::parse().post_process();
match ThreadPoolBuilder::new().num_threads(args.threads()).build_global() {
Ok(()) => (),
Err(e) => {
if !args.minimal() && !args.quiet() {
eprintln!("hb: {e}");
}
return ExitCode::FAILURE;
}
};
let mut total = 0;
for path in args.iter() {
for path in args.paths() {
let dir_structure = match Directory::new(path, &args) {
Ok(Some(ds)) => ds,
// this only ever returns None when persistant,
@ -39,7 +52,7 @@ fn main() -> ExitCode {
} else {
println!(
"{}: {}",
dir_structure.path().display(),
path,
args.unit().convert(dir_structure.size())
);
}