code cleanup

This commit is contained in:
Lynne Megido 2021-06-18 15:36:05 +10:00
parent 3b731a7c61
commit 4f78d93975
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
2 changed files with 17 additions and 25 deletions

View file

@ -94,16 +94,13 @@ fn main() {
debug!("Checking files regardless of extensions"); debug!("Checking files regardless of extensions");
} }
let entries = scan_directory(&args.dir, extensions.as_ref(), excludes.as_ref(), &args.get_scan_opts()); let entries = match scan_directory(&args.dir, extensions.as_ref(), excludes.as_ref(), &args.get_scan_opts()) {
if entries.is_none() {
// no need to log anything for fatal errors - fif will already have printed something obvious like // 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 // "[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. // given as input doesn't exist or is otherwise unreadable.
exit(exitcode::NOINPUT); None => exit(exitcode::NOINPUT),
} Some(e) => e,
};
let entries = entries.unwrap();
if entries.is_empty() { if entries.is_empty() {
warn!("No files matching requested options found."); 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 /// [`ScanError::Mime`] will be returned, meaning that the file was scanned successfully, but a mimetype could not be
/// determined. /// determined.
fn scan_file(entry: &DirEntry, canonical_paths: bool) -> Result<Findings, ScanError> { fn scan_file(entry: &DirEntry, canonical_paths: bool) -> Result<Findings, ScanError> {
let path = entry.path();
// try to determine mimetype for this entry // try to determine mimetype for this entry
let result = inspectors::mime_type(MIMEDB.get().unwrap(), entry.path()); let result = match inspectors::mime_type(MIMEDB.get().unwrap(), path) {
if result.is_err() {
// an error occurred while trying to read the file // an error occurred while trying to read the file
return Err(ScanError::File(entry.path())); Err(_) => return Err(ScanError::File(path)),
}
let result = result.unwrap();
if result.is_none() {
// the file was read successfully, but we were unable to determine its mimetype // the file was read successfully, but we were unable to determine its mimetype
return Err(ScanError::Mime(entry.path())); Ok(None) => return Err(ScanError::Mime(path)),
} // a mimetype was found!
Ok(Some(result)) => result,
let result = result.unwrap(); };
// set of known extensions for the given mimetype // set of known extensions for the given mimetype
let known_exts = inspectors::mime_extension_lookup(result.essence_str().into()); let known_exts = inspectors::mime_extension_lookup(result.essence_str().into());
// file extension for this particular file // 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 { let valid = match known_exts {
// there is a known set of extensions for this mimetype, and the file has an extension // 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<Findings, ScanEr
}; };
let path = if canonical_paths { let path = if canonical_paths {
match std::fs::canonicalize(entry.path()) { match std::fs::canonicalize(path) {
Ok(path) => path, Ok(path) => path,
Err(_) => return Err(ScanError::File(entry.path())) Err(_) => return Err(ScanError::File(entry.path())),
} }
} else { } else {
entry.path().to_path_buf() // :c path.to_path_buf() // :c
}; };
Ok(Findings { Ok(Findings {

View file

@ -87,9 +87,9 @@ fn recommend_ext() {
fn simple_directory() { fn simple_directory() {
use crate::parameters::ScanOpts; use crate::parameters::ScanOpts;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::fs::{File, canonicalize};
use std::io::Write;
use std::env::set_current_dir; use std::env::set_current_dir;
use std::fs::{canonicalize, File};
use std::io::Write;
use tempfile::tempdir; use tempfile::tempdir;
// set of files to scan. all but the last files have magic numbers corresponding to their extension, except for // set of files to scan. all but the last files have magic numbers corresponding to their extension, except for