Add default traits and open function to CompressionEngine
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
use anyhow::{Result};
|
use anyhow::{Result};
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::io::Write;
|
use std::io::{Read,Write};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
use log::*;
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
extern crate enum_map;
|
extern crate enum_map;
|
||||||
use enum_map::enum_map;
|
use enum_map::enum_map;
|
||||||
use enum_map::{EnumMap,Enum};
|
use enum_map::{EnumMap,Enum};
|
||||||
@@ -31,9 +35,38 @@ pub enum CompressionType {
|
|||||||
|
|
||||||
|
|
||||||
pub trait CompressionEngine {
|
pub trait CompressionEngine {
|
||||||
fn is_supported(&self) -> bool;
|
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>>;
|
||||||
fn cat(&self, file_path: PathBuf) -> Result<()>;
|
|
||||||
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>>;
|
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>>;
|
||||||
|
|
||||||
|
fn is_supported(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cat(&self, file_path: PathBuf) -> Result<()> {
|
||||||
|
let mut reader = self.open(file_path)?;
|
||||||
|
let mut stdout = io::stdout().lock();
|
||||||
|
io::copy(&mut reader, &mut stdout)?;
|
||||||
|
stdout.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn size(&self, file_path: PathBuf) -> Result<usize> {
|
||||||
|
let mut reader = self.open(file_path)?;
|
||||||
|
let mut buffer = [0; libc::BUFSIZ as usize];
|
||||||
|
let mut size: usize = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let n = reader.read(&mut buffer[..libc::BUFSIZ as usize])?;
|
||||||
|
if n == 0 {
|
||||||
|
debug!("COMPREESSION: EOF");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
size = size + n;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Write;
|
use std::io::{Read,Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use log::*;
|
use log::*;
|
||||||
|
|
||||||
@@ -25,17 +25,11 @@ impl CompressionEngine for CompressionEngineGZip {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cat(&self, file_path: PathBuf) -> Result<()> {
|
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>> {
|
||||||
debug!("COMPRESSION: Outputting {:?} to STDOUT using {:?}", file_path, *self);
|
debug!("COMPRESSION: Opening {:?} using {:?}", file_path, *self);
|
||||||
|
|
||||||
let mut stdout = io::stdout().lock();
|
|
||||||
let file = File::open(file_path)?;
|
let file = File::open(file_path)?;
|
||||||
let mut gzip_read = GzDecoder::new(file);
|
Ok(Box::new(GzDecoder::new(file)))
|
||||||
|
|
||||||
io::copy(&mut gzip_read, &mut stdout)?;
|
|
||||||
stdout.flush()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io::{Read,Write};
|
||||||
use std::io::Write;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use log::*;
|
use log::*;
|
||||||
|
|
||||||
@@ -24,17 +23,11 @@ impl CompressionEngine for CompressionEngineLZ4 {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cat(&self, file_path: PathBuf) -> Result<()> {
|
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>> {
|
||||||
debug!("COMPRESSION: Outputting {:?} to STDOUT using {:?}", file_path, *self);
|
debug!("COMPRESSION: Opening {:?} using {:?}", file_path, *self);
|
||||||
|
|
||||||
let mut stdout = io::stdout().lock();
|
|
||||||
let file = File::open(file_path)?;
|
let file = File::open(file_path)?;
|
||||||
let mut lz4_read = FrameDecoder::new(file);
|
Ok(Box::new(FrameDecoder::new(file)))
|
||||||
|
|
||||||
io::copy(&mut lz4_read, &mut stdout)?;
|
|
||||||
stdout.flush()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io::{Read,Write};
|
||||||
use std::io::Write;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use log::*;
|
use log::*;
|
||||||
|
|
||||||
@@ -17,19 +16,14 @@ impl CompressionEngineNone {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CompressionEngine for CompressionEngineNone {
|
impl CompressionEngine for CompressionEngineNone {
|
||||||
fn is_supported(&self) -> bool {
|
fn size(&self, file_path: PathBuf) -> Result<usize> {
|
||||||
true
|
let item_file_metadata = file_path.metadata()?;
|
||||||
|
Ok(item_file_metadata.len() as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cat(&self, file_path: PathBuf) -> Result<()> {
|
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>> {
|
||||||
debug!("COMPRESSION: Outputting {:?} to STDOUT using {:?}", file_path, *self);
|
debug!("COMPRESSION: Opening {:?} using {:?}", file_path, *self);
|
||||||
let mut stdout = io::stdout().lock();
|
Ok(Box::new(File::open(file_path)?))
|
||||||
let mut file = File::open(file_path)?;
|
|
||||||
|
|
||||||
io::copy(&mut file, &mut stdout)?;
|
|
||||||
stdout.flush()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use anyhow::{Context, Result, anyhow};
|
use anyhow::{Context, Result, anyhow};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::{Read,Write};
|
||||||
use std::process::{Command,Stdio};
|
use std::process::{Command,Stdio};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::env;
|
use std::env;
|
||||||
@@ -39,29 +39,23 @@ impl CompressionEngine for CompressionEngineProgram {
|
|||||||
self.supported
|
self.supported
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cat(&self, file_path: PathBuf) -> Result<()> {
|
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>> {
|
||||||
debug!("COMPRESSION: Outputting {:?} to STDOUT using {:?}", file_path, *self);
|
debug!("COMPRESSION: Opening {:?} using {:?}", file_path, *self);
|
||||||
|
|
||||||
let program = self.program.clone();
|
let program = self.program.clone();
|
||||||
let args = self.decompress.clone();
|
let args = self.decompress.clone();
|
||||||
|
|
||||||
debug!("COMPRESSION: Executing command: {:?} {:?} writing to {:?}", program, args, file_path);
|
debug!("COMPRESSION: Executing command: {:?} {:?} reading from {:?}", program, args, file_path);
|
||||||
|
|
||||||
let file = File::open(file_path).context("Unable to open file for reading")?;
|
let file = File::open(file_path).context("Unable to open file for reading")?;
|
||||||
|
|
||||||
let mut process = Command::new(program.clone())
|
let process = Command::new(program.clone())
|
||||||
.args(args.clone())
|
.args(args.clone())
|
||||||
.stdin(file)
|
.stdin(file)
|
||||||
|
.stdout(Stdio::piped())
|
||||||
.spawn()
|
.spawn()
|
||||||
.context(anyhow!("Unable to spawn child process: {:?} {:?}", program, args))?;
|
.context(anyhow!("Unable to spawn child process: {:?} {:?}", program, args))?;
|
||||||
|
Ok(Box::new(process.stdout.unwrap()))
|
||||||
let result = process.wait()
|
|
||||||
.context(anyhow!("Unable to wait for child process: {:?} {:?}", program, args))?;
|
|
||||||
|
|
||||||
if result.success() {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(anyhow!("Decompression program returned {}", result))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user