Show file magic in --info, currently uses the file command
This commit is contained in:
@@ -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<String> {
|
||||
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<usize> {
|
||||
let mut reader = self.open(file_path)?;
|
||||
let mut buffer = [0; libc::BUFSIZ as usize];
|
||||
|
||||
13
src/main.rs
13
src/main.rs
@@ -740,6 +740,19 @@ fn mode_info(cmd: &mut Command, args: Args, ids: &mut Vec<i64>, tags: &mut Vec<S
|
||||
file_size_cell
|
||||
]));
|
||||
|
||||
let compression_engine = compression::get_engine(compression_type).expect("Unable to get compression engine");
|
||||
let magic = compression_engine.magic(item_path.clone());
|
||||
|
||||
let file_magic_cell = match magic {
|
||||
Ok(magic) => 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(" "))
|
||||
|
||||
Reference in New Issue
Block a user