2019-01-16 09:15:39 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use std::thread;
|
|
|
|
use std::os::unix::io::FromRawFd;
|
2019-03-01 08:35:41 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
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-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-03-08 08:33:53 +00:00
|
|
|
pub fn new(mut dir: Dir, path: PathBuf, all_file_systems: bool, verbose: 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-01-16 09:15:39 +00:00
|
|
|
let child = thread::spawn(move|| {
|
|
|
|
let mut writer = unsafe { std::fs::File::from_raw_fd(tx) };
|
2019-05-23 11:10:20 +00:00
|
|
|
if let Err(err) = pxar::Encoder::encode(path, &mut dir, &mut writer, all_file_systems, verbose, pxar::CA_FORMAT_DEFAULT) {
|
2019-03-14 09:54:09 +00:00
|
|
|
eprintln!("pxar encode failed - {}", err);
|
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-05-20 09:20:33 +00:00
|
|
|
Ok(Self { stream: Some(stream), child: Some(child) })
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
2019-01-17 10:38:22 +00:00
|
|
|
|
2019-03-08 08:33:53 +00:00
|
|
|
pub fn open(dirname: &Path, all_file_systems: bool, verbose: 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-03-08 08:33:53 +00:00
|
|
|
Self::new(dir, path, all_file_systems, verbose)
|
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-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
|
|
|
}
|
|
|
|
}
|