work toward parallelisation

This commit is contained in:
Lynne Megido 2021-02-05 19:15:12 +10:00
parent d99eb281b3
commit ed54ae2452
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
3 changed files with 39 additions and 57 deletions

View File

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

28
src/inspectors.rs Normal file
View File

@ -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<Option<Mime>, > {
// // 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<String> {
// 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
// }
// }

View File

@ -15,18 +15,13 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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: &parameters::Parameters, entry: &DirEntry) -> bool {
fn wanted_file(args: &parameters::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: &parameters::Parameters, entry: &DirEntry) -> bool {
true
}
fn mime_type(db: &SharedMimeInfo, filepath: &Path) -> io::Result<Option<Mime>, > {
// 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<String> {
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<DirEntry> = 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);
}