now we don't have to pass &db through three different functions, hooray
this also makes it easier to replace xdg_mime with something else in future
This commit is contained in:
parent
d7eb0de299
commit
bc4edecd21
1 changed files with 10 additions and 9 deletions
19
src/main.rs
19
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<xdg_mime::SharedMimeInfo> = 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<String> {
|
|||
map(|e| String::from(e.to_string_lossy())) // Convert from OsStr to String
|
||||
}
|
||||
|
||||
fn scan_file(db: &SharedMimeInfo, entry: &DirEntry) -> Result<Findings, (ScanError, PathBuf)> {
|
||||
fn scan_file(entry: &DirEntry) -> Result<Findings, (ScanError, PathBuf)> {
|
||||
// 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<Findings, (ScanErr
|
|||
})
|
||||
}
|
||||
|
||||
fn scan_from_walkdir(db: &SharedMimeInfo, entries: Vec<DirEntry>) -> Vec<Result<Findings, (ScanError, PathBuf)>> {
|
||||
fn scan_from_walkdir(entries: Vec<DirEntry>) -> Vec<Result<Findings, (ScanError, PathBuf)>> {
|
||||
#[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<DirEntry>) -> Vec<Result<
|
|||
.par_chunks(32) // split into chunks of 32
|
||||
.flat_map(|chunk| chunk // return Vec<...> instead of Chunk<Vec<...>>
|
||||
.iter() // iter over the chunk, which is a slice of DirEntry structs
|
||||
.map(|entry| scan_file(db, entry))
|
||||
.map(|entry| scan_file(entry))
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
.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 {
|
||||
|
|
Loading…
Reference in a new issue