2019-08-23 12:36:51 +00:00
|
|
|
use std::collections::HashSet;
|
2019-11-08 09:35:48 +00:00
|
|
|
use std::io::Write;
|
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-08-23 12:36:51 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
use std::thread;
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-08-23 12:36:51 +00:00
|
|
|
use failure::*;
|
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-11-08 09:35:48 +00:00
|
|
|
use crate::backup::CatalogWriter;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
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-08-23 12:36:51 +00:00
|
|
|
pin_utils::unsafe_pinned!(stream: Option<WrappedReaderStream<std::fs::File>>);
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
pub fn new<W: Write + Send + 'static>(
|
2019-08-09 07:46:49 +00:00
|
|
|
mut dir: Dir,
|
|
|
|
path: PathBuf,
|
|
|
|
device_set: Option<HashSet<u64>>,
|
|
|
|
verbose: bool,
|
|
|
|
skip_lost_and_found: bool,
|
2019-11-08 09:35:48 +00:00
|
|
|
catalog: Arc<Mutex<CatalogWriter<W>>>,
|
2020-01-10 11:50:06 +00:00
|
|
|
entries_max: usize,
|
2019-08-09 07:46:49 +00:00
|
|
|
) -> 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-08-09 07:46:49 +00:00
|
|
|
let catalog = catalog.clone();
|
2019-10-22 13:07:51 +00:00
|
|
|
let exclude_pattern = Vec::new();
|
2019-08-09 07:46:49 +00:00
|
|
|
let child = thread::spawn(move || {
|
|
|
|
let mut guard = catalog.lock().unwrap();
|
2019-01-16 09:15:39 +00:00
|
|
|
let mut writer = unsafe { std::fs::File::from_raw_fd(tx) };
|
2019-10-22 13:07:51 +00:00
|
|
|
if let Err(err) = pxar::Encoder::encode(
|
|
|
|
path,
|
|
|
|
&mut dir,
|
|
|
|
&mut writer,
|
|
|
|
Some(&mut *guard),
|
|
|
|
device_set,
|
|
|
|
verbose,
|
|
|
|
skip_lost_and_found,
|
|
|
|
pxar::flags::DEFAULT,
|
|
|
|
exclude_pattern,
|
2020-01-10 11:50:06 +00:00
|
|
|
entries_max,
|
2019-10-22 13:07:51 +00:00
|
|
|
) {
|
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-11-08 09:35:48 +00:00
|
|
|
pub fn open<W: Write + Send + 'static>(
|
2019-08-09 07:46:49 +00:00
|
|
|
dirname: &Path,
|
|
|
|
device_set: Option<HashSet<u64>>,
|
|
|
|
verbose: bool,
|
|
|
|
skip_lost_and_found: bool,
|
2019-11-08 09:35:48 +00:00
|
|
|
catalog: Arc<Mutex<CatalogWriter<W>>>,
|
2020-01-10 11:50:06 +00:00
|
|
|
entries_max: usize,
|
2019-08-09 07:46:49 +00:00
|
|
|
) -> 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);
|
|
|
|
|
2020-01-10 11:50:06 +00:00
|
|
|
Self::new(dir, path, device_set, verbose, skip_lost_and_found, catalog, entries_max)
|
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
|
|
|
|
2019-08-23 12:36:51 +00:00
|
|
|
type Item = Result<Vec<u8>, Error>;
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-08-23 12:36:51 +00:00
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
2019-07-24 09:30:43 +00:00
|
|
|
{ // limit lock scope
|
|
|
|
let error = self.error.lock().unwrap();
|
|
|
|
if let Some(ref msg) = *error {
|
2019-08-23 12:36:51 +00:00
|
|
|
return Poll::Ready(Some(Err(format_err!("{}", msg))));
|
2019-07-24 09:30:43 +00:00
|
|
|
}
|
2019-07-24 07:24:35 +00:00
|
|
|
}
|
2019-08-23 12:36:51 +00:00
|
|
|
let res = self.as_mut()
|
|
|
|
.stream()
|
|
|
|
.as_pin_mut()
|
|
|
|
.unwrap()
|
|
|
|
.poll_next(cx);
|
|
|
|
Poll::Ready(futures::ready!(res)
|
|
|
|
.map(|v| v.map_err(Error::from))
|
|
|
|
)
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
|
|
|
}
|