refactor: add copy method to CompressionEngine and update mode_diff to use it
This commit is contained in:
@@ -44,14 +44,18 @@ pub trait CompressionEngine {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cat(&self, file_path: PathBuf) -> Result<()> {
|
fn copy(&self, file_path: PathBuf, writer: &mut dyn Write) -> Result<()> {
|
||||||
let mut reader = self.open(file_path)?;
|
let mut reader = self.open(file_path)?;
|
||||||
let mut stdout = io::stdout().lock();
|
io::copy(&mut reader, writer)?;
|
||||||
io::copy(&mut reader, &mut stdout)?;
|
writer.flush()?;
|
||||||
stdout.flush()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cat(&self, file_path: PathBuf) -> Result<()> {
|
||||||
|
let mut stdout = io::stdout().lock();
|
||||||
|
self.copy(file_path, &mut stdout)
|
||||||
|
}
|
||||||
|
|
||||||
fn magic(&self, file_path: PathBuf) -> Result<String> {
|
fn magic(&self, file_path: PathBuf) -> Result<String> {
|
||||||
let mut buffer = [0; libc::BUFSIZ as usize];
|
let mut buffer = [0; libc::BUFSIZ as usize];
|
||||||
let mut reader = self.open(file_path)?;
|
let mut reader = self.open(file_path)?;
|
||||||
|
|||||||
@@ -40,6 +40,13 @@ impl CompressionEngine for CompressionEngineGZip {
|
|||||||
|
|
||||||
Ok(Box::new(AutoFinishGzEncoder::new(gzip_write)))
|
Ok(Box::new(AutoFinishGzEncoder::new(gzip_write)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn copy(&self, file_path: PathBuf, writer: &mut dyn Write) -> Result<()> {
|
||||||
|
let mut reader = self.open(file_path)?;
|
||||||
|
io::copy(&mut reader, writer)?;
|
||||||
|
writer.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AutoFinishGzEncoder<W: Write> {
|
pub struct AutoFinishGzEncoder<W: Write> {
|
||||||
|
|||||||
@@ -38,5 +38,12 @@ impl CompressionEngine for CompressionEngineLZ4 {
|
|||||||
|
|
||||||
Ok(Box::new(lz4_write))
|
Ok(Box::new(lz4_write))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn copy(&self, file_path: PathBuf, writer: &mut dyn Write) -> Result<()> {
|
||||||
|
let mut reader = self.open(file_path)?;
|
||||||
|
io::copy(&mut reader, writer)?;
|
||||||
|
writer.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,4 +30,11 @@ impl CompressionEngine for CompressionEngineNone {
|
|||||||
debug!("COMPRESSION: Writting to {:?} using {:?}", file_path, *self);
|
debug!("COMPRESSION: Writting to {:?} using {:?}", file_path, *self);
|
||||||
Ok(Box::new(File::create(file_path)?))
|
Ok(Box::new(File::create(file_path)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn copy(&self, file_path: PathBuf, writer: &mut dyn Write) -> Result<()> {
|
||||||
|
let mut reader = self.open(file_path)?;
|
||||||
|
io::copy(&mut reader, writer)?;
|
||||||
|
writer.flush()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -535,8 +535,11 @@ fn mode_diff(cmd: &mut Command, args: Args, ids: &mut Vec<i64>, tags: &mut Vec<S
|
|||||||
let stdout_a = unsafe { std::fs::File::from_raw_fd(fd_a_write) };
|
let stdout_a = unsafe { std::fs::File::from_raw_fd(fd_a_write) };
|
||||||
let stdout_b = unsafe { std::fs::File::from_raw_fd(fd_b_write) };
|
let stdout_b = unsafe { std::fs::File::from_raw_fd(fd_b_write) };
|
||||||
|
|
||||||
compression_engine_a.cat(item_path_a.clone())?;
|
let mut stdout_a = unsafe { std::fs::File::from_raw_fd(fd_a_write) };
|
||||||
compression_engine_b.cat(item_path_b.clone())?;
|
let mut stdout_b = unsafe { std::fs::File::from_raw_fd(fd_b_write) };
|
||||||
|
|
||||||
|
compression_engine_a.copy(item_path_a.clone(), &mut stdout_a)?;
|
||||||
|
compression_engine_b.copy(item_path_b.clone(), &mut stdout_b)?;
|
||||||
|
|
||||||
let output = child.wait_with_output().expect("Failed to wait on diff command");
|
let output = child.wait_with_output().expect("Failed to wait on diff command");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user