diff --git a/src/compression.rs b/src/compression.rs index 7697560..8b49f75 100755 --- a/src/compression.rs +++ b/src/compression.rs @@ -1,8 +1,9 @@ -use anyhow::{Result}; +use anyhow::{anyhow,Result,Context}; use strum::IntoEnumIterator; use std::path::PathBuf; use std::io::{Read,Write}; use std::io; +use std::process::{Command,Stdio}; use log::*; @@ -12,6 +13,7 @@ extern crate enum_map; use enum_map::enum_map; use enum_map::{EnumMap,Enum}; + pub mod none; pub mod lz4; pub mod gzip; @@ -50,6 +52,37 @@ pub trait CompressionEngine { Ok(()) } + fn magic(&self, file_path: PathBuf) -> Result { + let mut buffer = [0; libc::BUFSIZ as usize]; + let mut reader = self.open(file_path)?; + let buffer_size = reader.read(&mut buffer[..])?; + + //let cookie = magic::Cookie::open(magic::cookie::Flags::ERROR)?; + + // load the system's default database + //let database = Default::default(); + //let cookie = cookie.load(&database).map_err(|error| { anyhow!(error.to_string())})?; + //let magic = cookie.buffer(&buffer[0..n])?; + + let program = "file"; + let args = ["-bE", "-"]; + + let process = Command::new(program) + .args(args.clone()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .context(anyhow!("Unable to spawn child process: {:?} {:?}", program, args))?; + + let mut stdin = process.stdin.unwrap(); + stdin.write_all(&buffer[0..buffer_size])?; + drop(stdin); + + let mut magic = String::new(); + process.stdout.unwrap().read_to_string(&mut magic)?; + Ok(magic) + } + fn size(&self, file_path: PathBuf) -> Result { let mut reader = self.open(file_path)?; let mut buffer = [0; libc::BUFSIZ as usize]; diff --git a/src/main.rs b/src/main.rs index b48d730..6b1dea2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -740,6 +740,19 @@ fn mode_info(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &mut Vec Cell::new(magic.as_str()), + Err(e) => Cell::new(&e.to_string()).with_style(Attr::ForegroundColor(color::RED)).with_style(Attr::Bold) + }; + + table.add_row(Row::new(vec![ + Cell::new("File Magic").with_style(Attr::Bold), + file_magic_cell + ])); + table.add_row(Row::new(vec![ Cell::new("Tags").with_style(Attr::Bold), Cell::new(&item_tags.join(" "))