Print correct message when paths are not found

This commit is contained in:
Alexandru Macovei
2021-01-14 00:52:04 +02:00
committed by andy.boot
parent e0347b0b43
commit a48c7782ac
3 changed files with 41 additions and 11 deletions
+6 -3
View File
@@ -1,6 +1,6 @@
extern crate ansi_term; extern crate ansi_term;
use crate::utils::Node; use crate::utils::{Errors, Node};
use self::ansi_term::Colour::Red; use self::ansi_term::Colour::Red;
use lscolors::{LsColors, Style}; use lscolors::{LsColors, Style};
@@ -118,7 +118,7 @@ fn get_width_of_terminal() -> u16 {
} }
pub fn draw_it( pub fn draw_it(
permissions: bool, errors: Errors,
use_full_path: bool, use_full_path: bool,
is_reversed: bool, is_reversed: bool,
no_colors: bool, no_colors: bool,
@@ -126,9 +126,12 @@ pub fn draw_it(
by_filecount: bool, by_filecount: bool,
root_node: Node, root_node: Node,
) { ) {
if !permissions { if errors.permissions {
eprintln!("Did not have permissions for all directories"); 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 num_chars_needed_on_left_most = if by_filecount {
let max_size = root_node.children.iter().map(|n| n.size).fold(0, max); let max_size = root_node.children.iter().map(|n| n.size).fold(0, max);
max_size.separate_with_commas().chars().count() max_size.separate_with_commas().chars().count()
+2 -2
View File
@@ -174,7 +174,7 @@ fn main() {
let show_hidden = !options.is_present("ignore_hidden"); let show_hidden = !options.is_present("ignore_hidden");
let simplified_dirs = simplify_dir_names(target_dirs); let simplified_dirs = simplify_dir_names(target_dirs);
let (permissions, nodes) = get_dir_tree( let (errors, nodes) = get_dir_tree(
&simplified_dirs, &simplified_dirs,
&ignore_directories, &ignore_directories,
use_apparent_size, use_apparent_size,
@@ -192,7 +192,7 @@ fn main() {
let tree = build_tree(biggest_ones, depth); let tree = build_tree(biggest_ones, depth);
draw_it( draw_it(
permissions, errors,
options.is_present("display_full_paths"), options.is_present("display_full_paths"),
!options.is_present("reverse"), !options.is_present("reverse"),
no_colors, no_colors,
+33 -6
View File
@@ -61,6 +61,11 @@ impl Node {
} }
} }
pub struct Errors {
pub permissions: bool,
pub not_found: bool,
}
pub fn is_a_parent_of<P: AsRef<Path>>(parent: P, child: P) -> bool { pub fn is_a_parent_of<P: AsRef<Path>>(parent: P, child: P) -> bool {
let parent = parent.as_ref(); let parent = parent.as_ref();
let child = child.as_ref(); let child = child.as_ref();
@@ -119,6 +124,18 @@ fn prepare_walk_dir_builder<P: AsRef<Path>>(
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<P: AsRef<Path>>( pub fn get_dir_tree<P: AsRef<Path>>(
top_level_names: &HashSet<P>, top_level_names: &HashSet<P>,
ignore_directories: &Option<Vec<PathBuf>>, ignore_directories: &Option<Vec<PathBuf>>,
@@ -126,10 +143,11 @@ pub fn get_dir_tree<P: AsRef<Path>>(
limit_filesystem: bool, limit_filesystem: bool,
by_filecount: bool, by_filecount: bool,
show_hidden: bool, show_hidden: bool,
) -> (bool, HashMap<PathBuf, u64>) { ) -> (Errors, HashMap<PathBuf, u64>) {
let (tx, rx) = channel::bounded::<PathData>(1000); let (tx, rx) = channel::bounded::<PathData>(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())); 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<P: AsRef<Path>>(
walk_dir_builder.build_parallel().run(|| { walk_dir_builder.build_parallel().run(|| {
let txc = tx.clone(); let txc = tx.clone();
let pf = &permissions_flag; let pf = &permissions_flag;
let nf = &not_found_flag;
Box::new(move |path| { Box::new(move |path| {
match path { match path {
Ok(p) => { Ok(p) => {
@@ -164,12 +183,16 @@ pub fn get_dir_tree<P: AsRef<Path>>(
txc.send((p.into_path(), size, inode_device)).unwrap(); txc.send((p.into_path(), size, inode_device)).unwrap();
} }
None => { None => {
pf.store(false, atomic::Ordering::Relaxed); pf.store(true, atomic::Ordering::Relaxed);
} }
} }
} }
Err(_) => { Err(e) => {
pf.store(false, atomic::Ordering::Relaxed); if is_not_found(&e) {
nf.store(true, atomic::Ordering::Relaxed);
} else {
pf.store(true, atomic::Ordering::Relaxed);
}
} }
}; };
WalkState::Continue WalkState::Continue
@@ -178,7 +201,11 @@ pub fn get_dir_tree<P: AsRef<Path>>(
drop(tx); drop(tx);
let data = t.join().unwrap(); 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( fn create_reader_thread(