From b9c27f98384567c7a71284742831ff1e9bc1256b Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Thu, 16 Jan 2020 23:50:56 +0000 Subject: [PATCH] get_filesystem returns Result instead of option Removes unwrap and returns a Result instead of panicing if an invalid path is given. Previously if the flag: '-x' was provided with an argument of an invalid directory the code would crash here --- src/utils/mod.rs | 2 +- src/utils/platform.rs | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 55d50d6..0f42788 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -100,7 +100,7 @@ pub fn get_dir_tree( fn get_allowed_filesystems(top_level_names: &HashSet) -> Option> { let mut limit_filesystems: HashSet = HashSet::new(); for file_name in top_level_names.iter() { - if let Some(a) = get_filesystem(file_name) { + if let Ok(a) = get_filesystem(file_name) { limit_filesystems.insert(a); } } diff --git a/src/utils/platform.rs b/src/utils/platform.rs index adb88fa..43d1a66 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -1,5 +1,6 @@ use jwalk::DirEntry; use std::fs; +use std::io; #[cfg(target_family = "unix")] fn get_block_size() -> u64 { @@ -41,20 +42,20 @@ pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, } #[cfg(target_family = "unix")] -pub fn get_filesystem(file_path: &str) -> Option { +pub fn get_filesystem(file_path: &str) -> Result { use std::os::unix::fs::MetadataExt; - let metadata = fs::metadata(file_path).unwrap(); - Some(metadata.dev()) + let metadata = fs::metadata(file_path)?; + Ok(metadata.dev()) } #[cfg(target_family = "windows")] -pub fn get_device(file_path: &str) -> Option { +pub fn get_device(file_path: &str) -> Result { use std::os::windows::fs::MetadataExt; - let metadata = fs::metadata(file_path).unwrap(); - Some(metadata.volume_serial_number()) + let metadata = fs::metadata(file_path)?; + Ok(metadata.volume_serial_number()) } #[cfg(all(not(target_family = "windows"), not(target_family = "unix")))] -pub fn get_device(file_path: &str) -> Option { +pub fn get_device(file_path: &str) -> Result { None }