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
|
|
|
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
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-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.
|
|
|
|
///
|
|
|
|
/// Note: The currect implementation is not fully ansync and can block.
|
2019-03-14 09:54:09 +00:00
|
|
|
pub struct PxarBackupStream {
|
2019-01-16 09:15:39 +00:00
|
|
|
pipe: Option<std::fs::File>,
|
|
|
|
buffer: Vec<u8>,
|
|
|
|
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) {
|
|
|
|
drop(self.pipe.take());
|
|
|
|
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-05-18 10:57:43 +00:00
|
|
|
let buffer_size = 1024*1024;
|
|
|
|
let mut buffer = Vec::with_capacity(buffer_size);
|
2019-01-16 09:15:39 +00:00
|
|
|
unsafe { buffer.set_len(buffer.capacity()); }
|
|
|
|
|
|
|
|
let (rx, tx) = nix::unistd::pipe()?;
|
|
|
|
|
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-03-15 07:24:32 +00:00
|
|
|
if let Err(err) = pxar::Encoder::encode(path, &mut dir, &mut writer, all_file_systems, verbose) {
|
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) };
|
|
|
|
|
|
|
|
Ok(Self { pipe: Some(pipe), buffer, child: Some(child) })
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
// Note: This is not async!!
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Vec<u8>>, Error> {
|
|
|
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
2019-01-17 10:38:22 +00:00
|
|
|
loop {
|
2019-01-16 09:15:39 +00:00
|
|
|
let pipe = match self.pipe {
|
|
|
|
Some(ref mut pipe) => pipe,
|
|
|
|
None => unreachable!(),
|
|
|
|
};
|
|
|
|
match pipe.read(&mut self.buffer) {
|
|
|
|
Ok(n) => {
|
|
|
|
if n == 0 {
|
|
|
|
return Ok(Async::Ready(None))
|
|
|
|
} else {
|
|
|
|
let data = self.buffer[..n].to_vec();
|
|
|
|
return Ok(Async::Ready(Some(data)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {
|
|
|
|
// try again
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
return Err(err.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|