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

View File

@ -4,16 +4,29 @@ mod args;
mod directory; mod directory;
mod unit; mod unit;
use clap::Parser;
use args::Args; use args::Args;
use directory::Directory; use directory::Directory;
use rayon::ThreadPoolBuilder;
use std::process::ExitCode; use std::process::ExitCode;
fn main() -> 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; let mut total = 0;
for path in args.iter() { for path in args.paths() {
let dir_structure = match Directory::new(path, &args) { let dir_structure = match Directory::new(path, &args) {
Ok(Some(ds)) => ds, Ok(Some(ds)) => ds,
// this only ever returns None when persistant, // this only ever returns None when persistant,
@ -39,7 +52,7 @@ fn main() -> ExitCode {
} else { } else {
println!( println!(
"{}: {}", "{}: {}",
dir_structure.path().display(), path,
args.unit().convert(dir_structure.size()) args.unit().convert(dir_structure.size())
); );
} }