Rearrange pipeline injector to a vec of filters
This commit is contained in:
+12
-47
@@ -1,8 +1,10 @@
|
||||
use crate::progress::BackupEvent;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// A trait for filters that can be applied to the output of a command before
|
||||
/// it is piped to another command.
|
||||
pub trait Filter {
|
||||
fn filter(&self, reader: Box<dyn Read>) -> Box<dyn Read>;
|
||||
}
|
||||
|
||||
pub fn exec_command(command: &Vec<&str>) -> Result<String, String> {
|
||||
if command.is_empty() {
|
||||
@@ -25,48 +27,10 @@ pub fn exec_command(command: &Vec<&str>) -> Result<String, String> {
|
||||
Ok(output_str)
|
||||
}
|
||||
|
||||
struct CountingReader<R: Read> {
|
||||
inner: R,
|
||||
sender: Sender<BackupEvent>,
|
||||
bytes: u64,
|
||||
last_send: Instant,
|
||||
total: Option<u64>,
|
||||
}
|
||||
|
||||
impl<R: Read> Read for CountingReader<R> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let n = self.inner.read(buf)?;
|
||||
self.bytes += n as u64;
|
||||
if self.last_send.elapsed().as_millis() >= 100 {
|
||||
self.sender
|
||||
.send(BackupEvent::BytesTransferred {
|
||||
bytes: self.bytes,
|
||||
estimated_total: self.total,
|
||||
})
|
||||
.ok();
|
||||
self.last_send = Instant::now();
|
||||
}
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read> CountingReader<R> {
|
||||
fn new(inner: R, sender: Sender<BackupEvent>, total: Option<u64>) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
sender,
|
||||
total,
|
||||
bytes: 0,
|
||||
last_send: Instant::now() - Duration::from_secs(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exec_piped_commands(
|
||||
source: &Vec<&str>,
|
||||
dest: &Vec<&str>,
|
||||
sender: Option<Sender<BackupEvent>>,
|
||||
total: Option<u64>,
|
||||
filters: Vec<Box<dyn Filter>>,
|
||||
) -> Result<(), String> {
|
||||
if source.is_empty() || dest.is_empty() {
|
||||
return Err("Source or destination command is empty".to_string());
|
||||
@@ -93,10 +57,11 @@ pub fn exec_piped_commands(
|
||||
let send_stdout = send_process.stdout.take().unwrap();
|
||||
let mut receive_stdin = receive_process.stdin.take().unwrap();
|
||||
|
||||
let mut reader: Box<dyn Read> = match sender {
|
||||
Some(s) => Box::new(CountingReader::new(send_stdout, s, total)),
|
||||
None => Box::new(send_stdout),
|
||||
};
|
||||
let mut reader: Box<dyn Read> = Box::new(send_stdout);
|
||||
for filter in filters {
|
||||
reader = filter.filter(reader);
|
||||
}
|
||||
|
||||
std::io::copy(&mut reader, &mut receive_stdin).map_err(|e| e.to_string())?;
|
||||
|
||||
let receive_status = receive_process.wait().map_err(|e| e.to_string())?;
|
||||
|
||||
Reference in New Issue
Block a user