From ed54ae2452055b138d13e8f0b1e928f5d150abb0 Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Fri, 5 Feb 2021 19:15:12 +1000 Subject: [PATCH] work toward parallelisation --- Cargo.toml | 1 + src/inspectors.rs | 28 ++++++++++++++++++++ src/main.rs | 67 +++++++---------------------------------------- 3 files changed, 39 insertions(+), 57 deletions(-) create mode 100644 src/inspectors.rs diff --git a/Cargo.toml b/Cargo.toml index 144c847..c990bd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ license = "GPL-3.0-or-later" [dependencies] walkdir = "2.3.1" structopt = "0.3.21" +#clap = "3.0.0-beta.2" log = "0.4.14" env_logger = "0.8.2" smartstring = "0.2.6" diff --git a/src/inspectors.rs b/src/inspectors.rs new file mode 100644 index 0000000..a9b489e --- /dev/null +++ b/src/inspectors.rs @@ -0,0 +1,28 @@ +// use xdg_mime::SharedMimeInfo; +// use std::path::Path; +// use std::io; +// use mime_guess::Mime; +// use std::fs::File; +// use std::io::Read; + +// pub fn mime_type(db: &SharedMimeInfo, filepath: &Path) -> io::Result, > { +// // attempt to read up to the 256 bytes of the file +// let mut buffer = [0; 256]; +// let mut file = File::open(filepath)?; +// +// file.read(&mut buffer)?; +// +// Ok(db.get_mime_type_for_data(&buffer).map(|m| m.0)) +// } + +// pub fn get_ext_from_mime(mime: &Mime) -> Option { +// match mime_guess::get_mime_extensions(mime) // get a list of possible extensions for this mime type +// .map(|g| g[0]) { // take the first option in the list and return it as a string +// // jpeg files are given the primary extension "jpe", due to the extension list being stored in alphabetical order. +// // to handle this particular case, swap "jpe" out for "jpg", and leave everything else the same, making sure we +// // convert the &strs to Strings. +// Some("jpe") => Some(String::from("jpg")), +// Some(ext) => Some(String::from(ext)), +// None => None +// } +// } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index bcf2337..16889af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,18 +15,13 @@ // along with this program. If not, see . mod parameters; +mod inspectors; use std::path::{Path}; use walkdir::{WalkDir, DirEntry}; use smartstring::alias::String; use structopt::StructOpt; -use std::fs::File; -use std::io; -use std::io::Read; -use mime_guess; -use xdg_mime::SharedMimeInfo; -use mime_guess::Mime; -use log::{warn, error}; +use log::{info}; // TODO: test if this actually works on a windows machine #[cfg(windows)] @@ -45,7 +40,7 @@ fn is_hidden(entry: &DirEntry) -> bool { entry.file_name().to_str().map_or(false, |f| f.starts_with('.') && f != ".") } -fn ext_match(args: ¶meters::Parameters, entry: &DirEntry) -> bool { +fn wanted_file(args: ¶meters::Parameters, entry: &DirEntry) -> bool { if !args.scan_hidden && is_hidden(entry) { // skip hidden files and directories. this check is performed first because it's very lightweight. return false; @@ -70,63 +65,21 @@ fn ext_match(args: ¶meters::Parameters, entry: &DirEntry) -> bool { true } -fn mime_type(db: &SharedMimeInfo, filepath: &Path) -> io::Result, > { - // attempt to read up to the 256 bytes of the file - let mut buffer = [0; 256]; - let mut file = File::open(filepath)?; - - file.read(&mut buffer)?; - - Ok(db.get_mime_type_for_data(&buffer).map(|m| m.0)) -} - -fn get_ext_from_mime(mime: &Mime) -> Option { - match mime_guess::get_mime_extensions(mime) // get a list of possible extensions for this mime type - .map(|g| g[0]) { // take the first option in the list and return it as a string - // jpeg files are given the primary extension "jpe", due to the extension list being stored in alphabetical order. - // to handle this particular case, swap "jpe" out for "jpg", and leave everything else the same, making sure we - // convert the &strs to Strings. - Some("jpe") => Some(String::from("jpg")), - Some(ext) => Some(String::from(ext)), - None => None - } -} - fn main() { let args = parameters::Parameters::from_args(); + // env_logger::init(); let db = xdg_mime::SharedMimeInfo::new(); println!("{:#?}", args); // println!("{:#?}", args.dirs); println!("=====\nIterating directory: {:?}\n=====", args.dirs); let stepper = WalkDir::new(&args.dirs).into_iter(); - for entry in stepper.filter_entry(|e| ext_match(&args, e)) { - let entry = entry.unwrap(); - if !entry.file_type().is_file() { - // println!("{} is not a file", entry.path().display()); - continue - } + let entries: Vec = stepper + .filter_entry(|e| wanted_file(&args, e)) // filter out unwanted files + .filter_map(|e| e.ok()) // ignore anything that fails, e.g. files we don't have read access on + .collect(); - // let result = tree_magic_mini::from_filepath(entry.path()).unwrap_or("???"); - let result = mime_type(&db, entry.path()); - if let Err(error) = result { - error!("{}", error); - continue - } + info!("Found {} items to check", entries.len()); - let result = result.unwrap(); - if result.is_none() { - warn!("Couldn't determine mimetype for {}", entry.path().display()); - continue - } - - let result = result.unwrap(); - - println!( - "{} has type {}, extension should be {:?}", - entry.path().display(), - result, - get_ext_from_mime(&result) - ); - } + // println!("{:#?}", entries); }