thanks rustfmt

This commit is contained in:
Lynne Megido 2021-02-22 00:15:09 +10:00
parent 7a2f009622
commit 9091850ec5
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
6 changed files with 50 additions and 50 deletions

View file

@ -7,20 +7,27 @@ pub enum ExtensionSet {
Videos, Videos,
Media, Media,
Documents, Documents,
Archives Archives,
} }
impl ExtensionSet { impl ExtensionSet {
pub fn extensions(&self) -> Vec<&str> { pub fn extensions(&self) -> Vec<&str> {
match self { match self {
Self::Images => vec!["png", "jpg", "jpeg", "webp", "raw", "gif", "apng", "tga", "bmp", "tif", "tiff", "heif", Self::Images => vec![
"avif", "jp2", "mng", "svg"], "png", "jpg", "jpeg", "webp", "raw", "gif", "apng", "tga", "bmp", "tif", "tiff", "heif", "avif", "jp2", "mng",
Self::Videos => vec!["webm", "mp4", "mkv", "mov", "avi", "m4v", "wmv", "bik", "ogv", "qt", "3gp", "3g2", "divx"], "svg",
Self::Audio => vec!["ogg", "oga", "opus", "mp3", "m4a", "aac", "flac", "ape", "midi", "mid", "alac", "wav", ],
"aiff", "aa3", "at3"], Self::Videos => vec![
Self::Documents => vec!["doc", "docx", "ppt", "pptx", "xls", "xlsx", "csv", "tsv", "pdf", "odt", "ods", "odp"], "webm", "mp4", "mkv", "mov", "avi", "m4v", "wmv", "bik", "ogv", "qt", "3gp", "3g2", "divx",
],
Self::Audio => vec![
"ogg", "oga", "opus", "mp3", "m4a", "aac", "flac", "ape", "midi", "mid", "alac", "wav", "aiff", "aa3", "at3",
],
Self::Documents => vec![
"doc", "docx", "ppt", "pptx", "xls", "xlsx", "csv", "tsv", "pdf", "odt", "ods", "odp",
],
Self::Archives => vec!["zip", "tar", "gz", "zst", "xz", "rar", "7z", "bz", "bz2"], Self::Archives => vec!["zip", "tar", "gz", "zst", "xz", "rar", "7z", "bz", "bz2"],
_ => todo!() _ => todo!(),
} }
} }
} }

View file

@ -18,7 +18,7 @@ use std::io::{stdout, BufWriter};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use clap::Clap; use clap::Clap;
use log::{debug, info, trace, warn, error}; use log::{debug, error, info, trace, warn};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
#[cfg(feature = "multi-threaded")] #[cfg(feature = "multi-threaded")]
use rayon::prelude::*; use rayon::prelude::*;
@ -32,13 +32,13 @@ use crate::parameters::OutputFormat;
use crate::scanerror::ScanError; use crate::scanerror::ScanError;
use std::process::exit; use std::process::exit;
mod extensionset;
mod findings; mod findings;
mod formats; mod formats;
mod inspectors; mod inspectors;
mod mimedb; mod mimedb;
mod parameters; mod parameters;
mod scanerror; mod scanerror;
mod extensionset;
#[cfg(feature = "infer-backend")] #[cfg(feature = "infer-backend")]
static MIMEDB: OnceCell<mimedb::InferDb> = OnceCell::new(); static MIMEDB: OnceCell<mimedb::InferDb> = OnceCell::new();
@ -179,10 +179,7 @@ fn main() {
debug!("Iterating directory: {:?}", args.dirs); debug!("Iterating directory: {:?}", args.dirs);
let extensions: Vec<&str> = if let Some(exts) = &args.exts { let extensions: Vec<&str> = if let Some(exts) = &args.exts {
exts exts.iter().map(|s| s.as_str()).collect()
.iter()
.map(|s| s.as_str())
.collect()
} else if let Some(exts) = &args.ext_set { } else if let Some(exts) = &args.ext_set {
exts.extensions().to_vec() exts.extensions().to_vec()
} else { } else {
@ -199,9 +196,7 @@ fn main() {
if let Err(err) = &e { if let Err(err) = &e {
debug!("uh oh spaghettio!! {:#?}", e); debug!("uh oh spaghettio!! {:#?}", e);
// log errors to stdout, and remove them from the iterator // log errors to stdout, and remove them from the iterator
let path = err let path = err.path().map_or("General error".into(), Path::to_string_lossy);
.path()
.map_or("General error".into(), Path::to_string_lossy);
if err.depth() == 0 { if err.depth() == 0 {
// if something goes wrong while trying to read the root directory, we're probably not going to get much done // if something goes wrong while trying to read the root directory, we're probably not going to get much done
@ -211,8 +206,12 @@ fn main() {
// TODO: is there a way to just say `map_or(x, |y| y).thing()` instead of `map_or(x.thing(), |y| y.thing())`? // TODO: is there a way to just say `map_or(x, |y| y).thing()` instead of `map_or(x.thing(), |y| y.thing())`?
// i don't care whether i'm returning a walkdir error or an io error, i just care about whether or not it // i don't care whether i'm returning a walkdir error or an io error, i just care about whether or not it
// implements ToString (which they both do). map_or doesn't work on trait objects though :( // implements ToString (which they both do). map_or doesn't work on trait objects though :(
error!("{}: {}", path, err.io_error().map_or(err.to_string(), |e|e.to_string())); error!(
return None "{}: {}",
path,
err.io_error().map_or(err.to_string(), |e| e.to_string())
);
return None;
} }
e.ok() e.ok()
}) })
@ -235,9 +234,8 @@ fn main() {
let results: Vec<_> = scan_from_walkdir(entries) let results: Vec<_> = scan_from_walkdir(entries)
.into_iter() .into_iter()
.filter(|result| .filter(
result.is_err() |result| result.is_err() || !result.as_ref().unwrap().valid,
|| !result.as_ref().unwrap().valid
// TODO: find a way to trace! the valid files without doing ↓ // TODO: find a way to trace! the valid files without doing ↓
// || if result.as_ref().unwrap().valid { trace!("{:?} is fine", result.as_ref().unwrap().file); false } else { true } // || if result.as_ref().unwrap().valid { trace!("{:?} is fine", result.as_ref().unwrap().file); false } else { true }
) )
@ -256,15 +254,14 @@ fn main() {
} }
} }
if results.is_empty() { info!("All files have valid extensions!") } if results.is_empty() {
info!("All files have valid extensions!")
}
match args.output_format { match args.output_format {
OutputFormat::Script => { OutputFormat::Script => {
let s = Script::new(); let s = Script::new();
if s.write_all( if s.write_all(&results, &mut BufWriter::new(stdout().lock())).is_err() {
&results,
&mut BufWriter::new(stdout().lock())
).is_err() {
exit(exitcode::IOERR); exit(exitcode::IOERR);
} }
} }

View file

@ -20,8 +20,7 @@ impl MimeDb for InferDb {
// jpeg2000 support because why the stinch not // jpeg2000 support because why the stinch not
info.add("image/jpeg2000", ".jp2", |buf| { info.add("image/jpeg2000", ".jp2", |buf| {
buf.len() > 23 buf.len() > 23 && buf[..23] == b"\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A\x6A\x70\x32\x20"[..]
&& buf[..23] == b"\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A\x6A\x70\x32\x20"[..]
}); });
info.add("image/svg+xml", "svg", |buf| { info.add("image/svg+xml", "svg", |buf| {
@ -35,19 +34,14 @@ impl MimeDb for InferDb {
// whitespace (according to https://www.w3.org/TR/xml/#NT-S) // whitespace (according to https://www.w3.org/TR/xml/#NT-S)
b'\t' | b'\r' | b'\n' | b'\x20' => continue, b'\t' | b'\r' | b'\n' | b'\x20' => continue,
b'<' => break, b'<' => break,
_ => return false _ => return false,
} }
} }
// finally, to check whether or not the file is an SVG: // finally, to check whether or not the file is an SVG:
// - split the buffer up into chunks separated by the less than sign // - split the buffer up into chunks separated by the less than sign
// - check to see if this chunk starts with any of these identifiers: // - check to see if this chunk starts with any of these identifiers:
let identifiers: Vec<&[u8]> = vec![ let identifiers: Vec<&[u8]> = vec![b"svg", b"SVG", b"!DOCTYPE svg", b"!DOCTYPE SVG"];
b"svg",
b"SVG",
b"!DOCTYPE svg",
b"!DOCTYPE SVG"
];
// - if it does, the nested `any` will short circuit and immediately return true, causing the parent `any` to do // - if it does, the nested `any` will short circuit and immediately return true, causing the parent `any` to do
// the same // the same
// - and finally, if none of the chunks match, we'll return false // - and finally, if none of the chunks match, we'll return false
@ -55,11 +49,7 @@ impl MimeDb for InferDb {
// TODO: this is kind of messy, i'd like to clean it up somehow :( // TODO: this is kind of messy, i'd like to clean it up somehow :(
buf buf
.split(|c| *c == b'<') .split(|c| *c == b'<')
.any(|buf| { .any(|buf| identifiers.iter().any(|id| buf.starts_with(id)))
identifiers
.iter()
.any(|id| buf.starts_with(id))
})
}); });
// unmut // unmut

View file

@ -1,8 +1,8 @@
use std::path::PathBuf; use std::path::PathBuf;
use crate::extensionset::ExtensionSet;
use clap::Clap; use clap::Clap;
use smartstring::{LazyCompact, SmartString}; use smartstring::{LazyCompact, SmartString};
use crate::extensionset::ExtensionSet;
#[derive(Clap, PartialEq, Debug)] #[derive(Clap, PartialEq, Debug)]
pub enum OutputFormat { pub enum OutputFormat {
@ -13,7 +13,13 @@ pub enum OutputFormat {
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
pub struct Parameters { pub struct Parameters {
/// Only examine files with these extensions (Comma-separated list) /// Only examine files with these extensions (Comma-separated list)
#[clap(short, long, use_delimiter = true, require_delimiter = true, required_unless_present = "ext-set")] #[clap(
short,
long,
use_delimiter = true,
require_delimiter = true,
required_unless_present = "ext-set"
)]
pub exts: Option<Vec<SmartString<LazyCompact>>>, pub exts: Option<Vec<SmartString<LazyCompact>>>,
/// write good docs 0uo /// write good docs 0uo