diff --git a/src/main.rs b/src/main.rs index 56f9d78..cf36cd9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,16 +94,13 @@ fn main() { debug!("Checking files regardless of extensions"); } - let entries = scan_directory(&args.dir, extensions.as_ref(), excludes.as_ref(), &args.get_scan_opts()); - - if entries.is_none() { + let entries = match scan_directory(&args.dir, extensions.as_ref(), excludes.as_ref(), &args.get_scan_opts()) { // no need to log anything for fatal errors - fif will already have printed something obvious like // "[ERROR] /fake/path: No such file or directory (os error 2)". we can assume that if this has happened, the dir // given as input doesn't exist or is otherwise unreadable. - exit(exitcode::NOINPUT); - } - - let entries = entries.unwrap(); + None => exit(exitcode::NOINPUT), + Some(e) => e, + }; if entries.is_empty() { warn!("No files matching requested options found."); @@ -237,26 +234,21 @@ fn extension_from_path(path: &Path) -> Option<&OsStr> { path.extension() } /// [`ScanError::Mime`] will be returned, meaning that the file was scanned successfully, but a mimetype could not be /// determined. fn scan_file(entry: &DirEntry, canonical_paths: bool) -> Result { + let path = entry.path(); // try to determine mimetype for this entry - let result = inspectors::mime_type(MIMEDB.get().unwrap(), entry.path()); - - if result.is_err() { + let result = match inspectors::mime_type(MIMEDB.get().unwrap(), path) { // an error occurred while trying to read the file - return Err(ScanError::File(entry.path())); - } - - let result = result.unwrap(); - if result.is_none() { + Err(_) => return Err(ScanError::File(path)), // the file was read successfully, but we were unable to determine its mimetype - return Err(ScanError::Mime(entry.path())); - } - - let result = result.unwrap(); + Ok(None) => return Err(ScanError::Mime(path)), + // a mimetype was found! + Ok(Some(result)) => result, + }; // set of known extensions for the given mimetype let known_exts = inspectors::mime_extension_lookup(result.essence_str().into()); // file extension for this particular file - let entry_ext = extension_from_path(entry.path()); + let entry_ext = extension_from_path(path); let valid = match known_exts { // there is a known set of extensions for this mimetype, and the file has an extension @@ -266,12 +258,12 @@ fn scan_file(entry: &DirEntry, canonical_paths: bool) -> Result path, - Err(_) => return Err(ScanError::File(entry.path())) + Err(_) => return Err(ScanError::File(entry.path())), } } else { - entry.path().to_path_buf() // :c + path.to_path_buf() // :c }; Ok(Findings { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 895c044..36287eb 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -87,9 +87,9 @@ fn recommend_ext() { fn simple_directory() { use crate::parameters::ScanOpts; use std::borrow::Borrow; - use std::fs::{File, canonicalize}; - use std::io::Write; use std::env::set_current_dir; + use std::fs::{canonicalize, File}; + use std::io::Write; use tempfile::tempdir; // set of files to scan. all but the last files have magic numbers corresponding to their extension, except for