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
This commit is contained in:
andy.boot
2020-01-16 23:50:56 +00:00
parent 5541df6a73
commit b9c27f9838
2 changed files with 9 additions and 8 deletions
+1 -1
View File
@@ -100,7 +100,7 @@ pub fn get_dir_tree(
fn get_allowed_filesystems(top_level_names: &HashSet<String>) -> Option<HashSet<u64>> { fn get_allowed_filesystems(top_level_names: &HashSet<String>) -> Option<HashSet<u64>> {
let mut limit_filesystems: HashSet<u64> = HashSet::new(); let mut limit_filesystems: HashSet<u64> = HashSet::new();
for file_name in top_level_names.iter() { 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); limit_filesystems.insert(a);
} }
} }
+8 -7
View File
@@ -1,5 +1,6 @@
use jwalk::DirEntry; use jwalk::DirEntry;
use std::fs; use std::fs;
use std::io;
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn get_block_size() -> u64 { 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")] #[cfg(target_family = "unix")]
pub fn get_filesystem(file_path: &str) -> Option<u64> { pub fn get_filesystem(file_path: &str) -> Result<u64, io::Error> {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
let metadata = fs::metadata(file_path).unwrap(); let metadata = fs::metadata(file_path)?;
Some(metadata.dev()) Ok(metadata.dev())
} }
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
pub fn get_device(file_path: &str) -> Option<u64> { pub fn get_device(file_path: &str) -> Result<u64, io::Error> {
use std::os::windows::fs::MetadataExt; use std::os::windows::fs::MetadataExt;
let metadata = fs::metadata(file_path).unwrap(); let metadata = fs::metadata(file_path)?;
Some(metadata.volume_serial_number()) Ok(metadata.volume_serial_number())
} }
#[cfg(all(not(target_family = "windows"), not(target_family = "unix")))] #[cfg(all(not(target_family = "windows"), not(target_family = "unix")))]
pub fn get_device(file_path: &str) -> Option<u64> { pub fn get_device(file_path: &str) -> Result<u64, io::Error> {
None None
} }