From a48c7782aca4e83738dcaa2f275040b682a816a7 Mon Sep 17 00:00:00 2001 From: Alexandru Macovei Date: Thu, 14 Jan 2021 00:52:04 +0200 Subject: [PATCH] Print correct message when paths are not found --- src/display.rs | 9 ++++++--- src/main.rs | 4 ++-- src/utils/mod.rs | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/display.rs b/src/display.rs index ed11bf8..7391c83 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,6 +1,6 @@ extern crate ansi_term; -use crate::utils::Node; +use crate::utils::{Errors, Node}; use self::ansi_term::Colour::Red; use lscolors::{LsColors, Style}; @@ -118,7 +118,7 @@ fn get_width_of_terminal() -> u16 { } pub fn draw_it( - permissions: bool, + errors: Errors, use_full_path: bool, is_reversed: bool, no_colors: bool, @@ -126,9 +126,12 @@ pub fn draw_it( by_filecount: bool, root_node: Node, ) { - if !permissions { + if errors.permissions { eprintln!("Did not have permissions for all directories"); } + if errors.not_found { + eprintln!("Not all directories were found"); + } let num_chars_needed_on_left_most = if by_filecount { let max_size = root_node.children.iter().map(|n| n.size).fold(0, max); max_size.separate_with_commas().chars().count() diff --git a/src/main.rs b/src/main.rs index 6a3a4c3..b034d7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -174,7 +174,7 @@ fn main() { let show_hidden = !options.is_present("ignore_hidden"); let simplified_dirs = simplify_dir_names(target_dirs); - let (permissions, nodes) = get_dir_tree( + let (errors, nodes) = get_dir_tree( &simplified_dirs, &ignore_directories, use_apparent_size, @@ -192,7 +192,7 @@ fn main() { let tree = build_tree(biggest_ones, depth); draw_it( - permissions, + errors, options.is_present("display_full_paths"), !options.is_present("reverse"), no_colors, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index b50a606..762f8ef 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -61,6 +61,11 @@ impl Node { } } +pub struct Errors { + pub permissions: bool, + pub not_found: bool, +} + pub fn is_a_parent_of>(parent: P, child: P) -> bool { let parent = parent.as_ref(); let child = child.as_ref(); @@ -119,6 +124,18 @@ fn prepare_walk_dir_builder>( builder } +fn is_not_found(e: &ignore::Error) -> bool { + use ignore::Error; + if let Error::WithPath { err, .. } = e { + if let Error::Io(e) = &**err { + if e.kind() == std::io::ErrorKind::NotFound { + return true; + } + } + } + false +} + pub fn get_dir_tree>( top_level_names: &HashSet

, ignore_directories: &Option>, @@ -126,10 +143,11 @@ pub fn get_dir_tree>( limit_filesystem: bool, by_filecount: bool, show_hidden: bool, -) -> (bool, HashMap) { +) -> (Errors, HashMap) { let (tx, rx) = channel::bounded::(1000); - let permissions_flag = AtomicBool::new(true); + let permissions_flag = AtomicBool::new(false); + let not_found_flag = AtomicBool::new(false); let t2 = HashSet::from_iter(top_level_names.iter().map(|p| p.as_ref().to_path_buf())); @@ -139,6 +157,7 @@ pub fn get_dir_tree>( walk_dir_builder.build_parallel().run(|| { let txc = tx.clone(); let pf = &permissions_flag; + let nf = ¬_found_flag; Box::new(move |path| { match path { Ok(p) => { @@ -164,12 +183,16 @@ pub fn get_dir_tree>( txc.send((p.into_path(), size, inode_device)).unwrap(); } None => { - pf.store(false, atomic::Ordering::Relaxed); + pf.store(true, atomic::Ordering::Relaxed); } } } - Err(_) => { - pf.store(false, atomic::Ordering::Relaxed); + Err(e) => { + if is_not_found(&e) { + nf.store(true, atomic::Ordering::Relaxed); + } else { + pf.store(true, atomic::Ordering::Relaxed); + } } }; WalkState::Continue @@ -178,7 +201,11 @@ pub fn get_dir_tree>( drop(tx); let data = t.join().unwrap(); - (permissions_flag.load(atomic::Ordering::SeqCst), data) + let errors = Errors { + permissions: permissions_flag.load(atomic::Ordering::SeqCst), + not_found: not_found_flag.load(atomic::Ordering::SeqCst), + }; + (errors, data) } fn create_reader_thread(