From 203df3473fe01fafea9b3e4ccdb28a286b047dfe Mon Sep 17 00:00:00 2001 From: Isaac Parker Date: Sun, 17 Nov 2019 19:20:58 -0800 Subject: [PATCH] Gracefully handle bad or invalid symlinks (#29) --- src/lib.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cb8a1ba..e515a9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -481,15 +481,17 @@ fn get_bytes( path: &Path, usage_flag : bool ) -> u64 { fn color_from_path<'a>( path : &Path, color_dict : &'a HashMap ) -> Option<&'a str> { if try_is_symlink( path ) { - if path.read_link().unwrap().exists() { - if let Some( col ) = color_dict.get( &"ln".to_string() ) { - return Some( &col ); - } - } else { - if let Some( col ) = color_dict.get( &"or".to_string() ) { - return Some( &col ); + let path_link = path.read_link(); + if path_link.is_ok() { + if path_link.unwrap().exists() { + if let Some(col) = color_dict.get(&"ln".to_string()) { + return Some(&col); + } } } + if let Some( col ) = color_dict.get( &"or".to_string() ) { + return Some( &col ); + } } let metadata = path.symlink_metadata(); if metadata.is_ok() {