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");
}
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<Findings, ScanError> {
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<Findings, ScanEr
};
let path = if canonical_paths {
match std::fs::canonicalize(entry.path()) {
match std::fs::canonicalize(path) {
Ok(path) => 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 {

View File

@ -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