diff --git a/src/main.rs b/src/main.rs index 5d19eeb..ab05a3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,21 +14,20 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use std::fmt::{self, Display}; use std::io::{BufWriter, stdout}; use std::path::{Path, PathBuf}; use clap::Clap; use log::{debug, info, trace, warn}; +use once_cell::sync::OnceCell; use rayon::prelude::*; use smartstring::alias::String; use walkdir::{DirEntry, WalkDir}; -use xdg_mime::SharedMimeInfo; +use crate::findings::Findings; use crate::formats::{Format, Script}; use crate::parameters::OutputFormat; use crate::scanerror::ScanError; -use crate::findings::Findings; mod parameters; mod inspectors; @@ -36,6 +35,8 @@ mod formats; mod scanerror; mod findings; +static MIMEDB: OnceCell = OnceCell::new(); + // TODO: test if this actually works on a windows machine - not there's much of a point right now, considering // xdg-mime-rs doesn't support windows #[cfg(windows)] @@ -80,9 +81,9 @@ fn extension_from_path(path: &Path) -> Option { map(|e| String::from(e.to_string_lossy())) // Convert from OsStr to String } -fn scan_file(db: &SharedMimeInfo, entry: &DirEntry) -> Result { +fn scan_file(entry: &DirEntry) -> Result { // try to determine mimetype for this entry - let result = inspectors::mime_type(db, entry.path()); + let result = inspectors::mime_type(MIMEDB.get().unwrap(), entry.path()); if result.is_err() { // an error occurred while trying to read the file @@ -120,7 +121,7 @@ fn scan_file(db: &SharedMimeInfo, entry: &DirEntry) -> Result) -> Vec> { +fn scan_from_walkdir(entries: Vec) -> Vec> { #[cfg(feature = "multi-threaded")] { // rather than using a standard par_iter, split the entries into chunks of 32 first. // this allows each spawned thread to handle 16 files before before closing, rather than creating a new thread for @@ -129,7 +130,7 @@ fn scan_from_walkdir(db: &SharedMimeInfo, entries: Vec) -> Vec instead of Chunk> .iter() // iter over the chunk, which is a slice of DirEntry structs - .map(|entry| scan_file(db, entry)) + .map(|entry| scan_file(entry)) .collect::>() ) .collect() @@ -153,7 +154,7 @@ fn main() { // .target(env_logger::Target::Stdout) // log to stdout rather than stderr .init(); - let db = xdg_mime::SharedMimeInfo::new(); + MIMEDB.set(xdg_mime::SharedMimeInfo::new()).or(Err("Failed to initialise MIMEDB")).unwrap(); debug!("Iterating directory: {:?}", args.dirs); let stepper = WalkDir::new(&args.dirs).into_iter(); @@ -165,7 +166,7 @@ fn main() { trace!("Found {} items to check", entries.len()); - let results = scan_from_walkdir(&db, entries); + let results = scan_from_walkdir(entries); for result in &results { match result {