Add default traits and open function to CompressionEngine

This commit is contained in:
Andrew Phillips
2023-09-20 19:01:50 +00:00
parent 1fe276d110
commit fdc4544068
5 changed files with 59 additions and 51 deletions

View File

@@ -1,6 +1,6 @@
use anyhow::{Context, Result, anyhow};
use std::fs::File;
use std::io::Write;
use std::io::{Read,Write};
use std::process::{Command,Stdio};
use std::path::PathBuf;
use std::env;
@@ -39,29 +39,23 @@ impl CompressionEngine for CompressionEngineProgram {
self.supported
}
fn cat(&self, file_path: PathBuf) -> Result<()> {
debug!("COMPRESSION: Outputting {:?} to STDOUT using {:?}", file_path, *self);
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>> {
debug!("COMPRESSION: Opening {:?} using {:?}", file_path, *self);
let program = self.program.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 mut process = Command::new(program.clone())
let process = Command::new(program.clone())
.args(args.clone())
.stdin(file)
.stdout(Stdio::piped())
.spawn()
.context(anyhow!("Unable to spawn child process: {:?} {:?}", program, args))?;
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))
}
Ok(Box::new(process.stdout.unwrap()))
}
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {