From 5be106eeae611c1ebd8d0cfa3d1f16cd86e3a3fa Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 24 Jul 2019 09:24:35 +0200 Subject: [PATCH] src/client/pxar_backup_stream.rs: correctly pass errors to stream --- src/client/pxar_backup_stream.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/client/pxar_backup_stream.rs b/src/client/pxar_backup_stream.rs index 8e24e9bb..20228369 100644 --- a/src/client/pxar_backup_stream.rs +++ b/src/client/pxar_backup_stream.rs @@ -1,6 +1,7 @@ use failure::*; use std::thread; +use std::sync::{Arc, Mutex}; use std::os::unix::io::FromRawFd; use std::path::{Path, PathBuf}; use std::collections::HashSet; @@ -23,6 +24,7 @@ use crate::tools::wrapped_reader_stream::WrappedReaderStream; pub struct PxarBackupStream { stream: Option>, child: Option>, + error: Arc>>, } impl Drop for PxarBackupStream { @@ -42,17 +44,26 @@ impl PxarBackupStream { let buffer_size = 1024*1024; nix::fcntl::fcntl(rx, nix::fcntl::FcntlArg::F_SETPIPE_SZ(buffer_size as i32))?; + let error = Arc::new(Mutex::new(None)); + let error2 = error.clone(); + let child = thread::spawn(move|| { let mut writer = unsafe { std::fs::File::from_raw_fd(tx) }; if let Err(err) = pxar::Encoder::encode(path, &mut dir, &mut writer, device_set, verbose, pxar::CA_FORMAT_DEFAULT) { eprintln!("pxar encode failed - {}", err); + let mut error = error2.lock().unwrap(); + *error = Some(err.to_string()); } }); let pipe = unsafe { std::fs::File::from_raw_fd(rx) }; let stream = crate::tools::wrapped_reader_stream::WrappedReaderStream::new(pipe); - Ok(Self { stream: Some(stream), child: Some(child) }) + Ok(Self { + stream: Some(stream), + child: Some(child), + error, + }) } pub fn open(dirname: &Path, device_set: Option>, verbose: bool) -> Result { @@ -70,6 +81,10 @@ impl Stream for PxarBackupStream { type Error = Error; fn poll(&mut self) -> Poll>, Error> { + let error = self.error.lock().unwrap(); + if let Some(ref msg) = *error { + return Err(format_err!("{}", msg)); + } self.stream.as_mut().unwrap().poll().map_err(Error::from) } }