2019-01-16 09:15:39 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use std::thread;
|
2019-07-24 07:24:35 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-01-16 09:15:39 +00:00
|
|
|
use std::os::unix::io::FromRawFd;
|
2019-03-01 08:35:41 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-07-24 05:48:59 +00:00
|
|
|
use std::collections::HashSet;
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-05-18 13:13:31 +00:00
|
|
|
use futures::Poll;
|
2019-01-16 09:15:39 +00:00
|
|
|
use futures::stream::Stream;
|
|
|
|
|
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
use nix::sys::stat::Mode;
|
2019-01-17 10:38:22 +00:00
|
|
|
use nix::dir::Dir;
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-03-15 07:24:32 +00:00
|
|
|
use crate::pxar;
|
2019-05-18 13:13:31 +00:00
|
|
|
use crate::tools::wrapped_reader_stream::WrappedReaderStream;
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-03-14 09:54:09 +00:00
|
|
|
/// Stream implementation to encode and upload .pxar archives.
|
2019-02-14 10:11:39 +00:00
|
|
|
///
|
|
|
|
/// The hyper client needs an async Stream for file upload, so we
|
2019-03-14 09:54:09 +00:00
|
|
|
/// spawn an extra thread to encode the .pxar data and pipe it to the
|
2019-02-14 10:11:39 +00:00
|
|
|
/// consumer.
|
2019-03-14 09:54:09 +00:00
|
|
|
pub struct PxarBackupStream {
|
2019-05-20 09:20:33 +00:00
|
|
|
stream: Option<WrappedReaderStream<std::fs::File>>,
|
2019-01-16 09:15:39 +00:00
|
|
|
child: Option<thread::JoinHandle<()>>,
|
2019-07-24 07:24:35 +00:00
|
|
|
error: Arc<Mutex<Option<String>>>,
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 09:54:09 +00:00
|
|
|
impl Drop for PxarBackupStream {
|
2019-01-16 09:15:39 +00:00
|
|
|
|
|
|
|
fn drop(&mut self) {
|
2019-05-20 09:20:33 +00:00
|
|
|
self.stream = None;
|
2019-01-16 09:15:39 +00:00
|
|
|
self.child.take().unwrap().join().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 09:54:09 +00:00
|
|
|
impl PxarBackupStream {
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-07-24 10:21:25 +00:00
|
|
|
pub fn new(mut dir: Dir, path: PathBuf, device_set: Option<HashSet<u64>>, verbose: bool, skip_lost_and_found: bool) -> Result<Self, Error> {
|
2019-01-16 09:15:39 +00:00
|
|
|
|
|
|
|
let (rx, tx) = nix::unistd::pipe()?;
|
|
|
|
|
2019-05-18 13:13:31 +00:00
|
|
|
let buffer_size = 1024*1024;
|
2019-05-18 10:57:43 +00:00
|
|
|
nix::fcntl::fcntl(rx, nix::fcntl::FcntlArg::F_SETPIPE_SZ(buffer_size as i32))?;
|
|
|
|
|
2019-07-24 07:24:35 +00:00
|
|
|
let error = Arc::new(Mutex::new(None));
|
|
|
|
let error2 = error.clone();
|
|
|
|
|
2019-01-16 09:15:39 +00:00
|
|
|
let child = thread::spawn(move|| {
|
|
|
|
let mut writer = unsafe { std::fs::File::from_raw_fd(tx) };
|
2019-08-02 13:19:33 +00:00
|
|
|
if let Err(err) = pxar::Encoder::encode(path, &mut dir, &mut writer, device_set, verbose, skip_lost_and_found, pxar::flags::DEFAULT) {
|
2019-07-24 07:24:35 +00:00
|
|
|
let mut error = error2.lock().unwrap();
|
|
|
|
*error = Some(err.to_string());
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let pipe = unsafe { std::fs::File::from_raw_fd(rx) };
|
2019-05-18 13:13:31 +00:00
|
|
|
let stream = crate::tools::wrapped_reader_stream::WrappedReaderStream::new(pipe);
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-07-24 07:24:35 +00:00
|
|
|
Ok(Self {
|
|
|
|
stream: Some(stream),
|
|
|
|
child: Some(child),
|
|
|
|
error,
|
|
|
|
})
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-07-24 10:21:25 +00:00
|
|
|
pub fn open(dirname: &Path, device_set: Option<HashSet<u64>>, verbose: bool, skip_lost_and_found: bool) -> Result<Self, Error> {
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-01-18 15:50:15 +00:00
|
|
|
let dir = nix::dir::Dir::open(dirname, OFlag::O_DIRECTORY, Mode::empty())?;
|
2019-01-17 10:38:22 +00:00
|
|
|
let path = std::path::PathBuf::from(dirname);
|
|
|
|
|
2019-07-24 10:21:25 +00:00
|
|
|
Self::new(dir, path, device_set, verbose, skip_lost_and_found)
|
2019-01-17 10:38:22 +00:00
|
|
|
}
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 09:54:09 +00:00
|
|
|
impl Stream for PxarBackupStream {
|
2019-01-16 09:15:39 +00:00
|
|
|
|
|
|
|
type Item = Vec<u8>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Vec<u8>>, Error> {
|
2019-07-24 09:30:43 +00:00
|
|
|
{ // limit lock scope
|
|
|
|
let error = self.error.lock().unwrap();
|
|
|
|
if let Some(ref msg) = *error {
|
|
|
|
return Err(format_err!("{}", msg));
|
|
|
|
}
|
2019-07-24 07:24:35 +00:00
|
|
|
}
|
2019-05-20 09:20:33 +00:00
|
|
|
self.stream.as_mut().unwrap().poll().map_err(Error::from)
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
|
|
|
}
|