From b141b85ea7f820e5ac073b527bc6ece73fa5a9a2 Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Sat, 27 Feb 2021 12:02:49 +1000 Subject: [PATCH] use xdg-mime by default on linux, infer elsewhere --- Cargo.lock | 1 + Cargo.toml | 11 ++++++----- src/inspectors.rs | 7 +++++-- src/main.rs | 10 +++++----- src/mimedb.rs | 11 ++++++----- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8806fb6..2876ad8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,6 +179,7 @@ name = "fif" version = "0.2.4" dependencies = [ "cached", + "cfg-if", "clap", "env_logger", "exitcode", diff --git a/Cargo.toml b/Cargo.toml index 2c334a0..a64126f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,10 @@ exclude = [".idea/", "Cross.toml", "*.sh"] #license-file = "LICENSE" [features] -default = ["multi-threaded", "infer-backend"] +default = ["multi-threaded"] multi-threaded = ["rayon"] -infer-backend = ["infer"] -xdg-mime-backend = ["xdg-mime"] +infer-backend = [] +xdg-mime-backend = [] [dependencies] walkdir = "2.3.1" @@ -27,13 +27,14 @@ smartstring = "0.2.6" mime_guess = "2.0.3" snailquote = "0.3.0" once_cell = "1.5.2" +infer = { version = "0.3.4" } rayon = { version = "1.5.0", optional = true } -infer = { version = "0.3.4", optional = true } exitcode = "1.1.2" +cfg-if = "1.0.0" # use git version while waiting on a release incorporating https://github.com/ebassi/xdg-mime-rs/commit/de5a6dd [target.'cfg(not(target_os = "windows"))'.dependencies] -xdg-mime = {git = "https://github.com/ebassi/xdg-mime-rs", version = "0.3", rev = "de5a6dd", optional = true } +xdg-mime = {git = "https://github.com/ebassi/xdg-mime-rs", version = "0.3", rev = "de5a6dd" } [dependencies.clap] version = "3.0.0-beta.2" diff --git a/src/inspectors.rs b/src/inspectors.rs index b741e43..e3855e5 100644 --- a/src/inspectors.rs +++ b/src/inspectors.rs @@ -10,8 +10,6 @@ use smartstring::alias::String; use crate::mimedb::MimeDb; -// use log::{debug, warn}; - // rather than reading once into a large buffer, it tends to be faster to first try identifying the file from a small // chunk read from the top, and *then* proceeding with the large buffer. many file formats can be easily identified by // the first 128 bytes. of course, not all formats can, and some (OOXML...) require reading a long ways in. @@ -87,6 +85,11 @@ cached! { } else if mime == Mime::from_str("application/msword").unwrap() { // classic office files considered harmful vec![String::from("doc"), String::from("xls"), String::from("ppt")] + + } else if mime == Mime::from_str("application/zip").unwrap() { + // neither xdg-mime nor infer seem to be able to detect office XML files properly... + [vec![String::from("zip"), String::from("docx"), String::from("xlsx"), String::from("pptx")], possible_exts].concat() + } else { possible_exts }) diff --git a/src/main.rs b/src/main.rs index 4a0b369..e14d1c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,10 +40,10 @@ mod mimedb; mod parameters; mod scanerror; -#[cfg(feature = "infer-backend")] +#[cfg(any(all(not(target_os = "linux"), not(feature = "xdg-mime-backend")), all(target_os = "linux", feature = "infer-backend")))] static MIMEDB: OnceCell = OnceCell::new(); -#[cfg(feature = "xdg-mime-backend")] +#[cfg(any(all(target_os = "linux", not(feature = "infer-backend")), all(not(target_os = "linux"), not(feature = "xdg-mime-backend"))))] static MIMEDB: OnceCell = OnceCell::new(); // TODO: test if this actually works on a windows machine @@ -162,13 +162,13 @@ fn main() { // .target(env_logger::Target::Stdout) // log to stdout rather than stderr .init(); - #[cfg(feature = "infer-backend")] - MIMEDB + #[cfg(any(all(not(target_os = "linux"), not(feature = "xdg-mime-backend")), all(target_os = "linux", feature = "infer-backend")))] + MIMEDB .set(mimedb::InferDb::init()) .or(Err("Failed to initialise Infer backend!")) .unwrap(); - #[cfg(feature = "xdg-mime-backend")] + #[cfg(any(all(target_os = "linux", not(feature = "infer-backend")), all(not(target_os = "linux"), not(feature = "xdg-mime-backend"))))] MIMEDB .set(mimedb::XdgDb::init()) .or(Err("Failed to initialise XDG Mime backend!")) diff --git a/src/mimedb.rs b/src/mimedb.rs index f7f36ba..a7c8e4b 100644 --- a/src/mimedb.rs +++ b/src/mimedb.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "infer-backend")] +#[cfg(any(all(not(target_os = "linux"), not(feature = "xdg-mime-backend")), all(target_os = "linux", feature = "infer-backend")))] use std::str::FromStr; use mime_guess::Mime; @@ -8,11 +8,12 @@ pub trait MimeDb { fn get_type(&self, data: &[u8]) -> Option; } -#[cfg(feature = "infer-backend")] +#[cfg(any(all(not(target_os = "linux"), not(feature = "xdg-mime-backend")), all(target_os = "linux", feature = "infer-backend")))] pub struct InferDb { db: infer::Infer, } +#[cfg(any(all(not(target_os = "linux"), not(feature = "xdg-mime-backend")), all(target_os = "linux", feature = "infer-backend")))] fn open_document_check(buf: &[u8], kind: &str) -> bool { let mime = format!("application/vnd.oasis.opendocument.{}", kind); let mime = mime.as_bytes(); @@ -20,7 +21,7 @@ fn open_document_check(buf: &[u8], kind: &str) -> bool { buf.len() > 38 + mime.len() && buf.starts_with(b"PK\x03\x04") && buf[38..mime.len() + 38] == mime[..] } -#[cfg(feature = "infer-backend")] +#[cfg(any(all(not(target_os = "linux"), not(feature = "xdg-mime-backend")), all(target_os = "linux", feature = "infer-backend")))] impl MimeDb for InferDb { fn init() -> Self { let mut info = infer::Infer::new(); @@ -82,12 +83,12 @@ impl MimeDb for InferDb { } } -#[cfg(feature = "xdg-mime-backend")] +#[cfg(any(all(target_os = "linux", not(feature = "infer-backend")), all(not(target_os = "linux"), not(feature = "xdg-mime-backend"))))] pub struct XdgDb { db: xdg_mime::SharedMimeInfo, } -#[cfg(feature = "xdg-mime-backend")] +#[cfg(any(all(target_os = "linux", not(feature = "infer-backend")), all(not(target_os = "linux"), not(feature = "xdg-mime-backend"))))] impl MimeDb for XdgDb { fn init() -> Self { Self {