2019-08-23 12:36:51 +00:00
|
|
|
use std::collections::HashSet;
|
2019-11-08 09:35:48 +00:00
|
|
|
use std::io::Write;
|
2020-01-22 10:37:16 +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
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{format_err, Error};
|
2019-01-16 09:15:39 +00:00
|
|
|
use futures::stream::Stream;
|
2020-03-23 14:03:18 +00:00
|
|
|
use nix::dir::Dir;
|
2019-01-16 09:15:39 +00:00
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
use pathpatterns::MatchEntry;
|
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
use crate::backup::CatalogWriter;
|
2019-08-16 10:27:17 +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 {
|
2020-01-22 10:37:16 +00:00
|
|
|
rx: Option<std::sync::mpsc::Receiver<Result<Vec<u8>, Error>>>,
|
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) {
|
2020-01-22 10:37:16 +00:00
|
|
|
self.rx = 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-11-08 09:35:48 +00:00
|
|
|
pub fn new<W: Write + Send + 'static>(
|
2020-03-23 14:03:18 +00:00
|
|
|
dir: Dir,
|
|
|
|
_path: PathBuf,
|
2019-08-09 07:46:49 +00:00
|
|
|
device_set: Option<HashSet<u64>>,
|
2020-03-23 14:03:18 +00:00
|
|
|
_verbose: bool,
|
2019-08-09 07:46:49 +00:00
|
|
|
skip_lost_and_found: bool,
|
2019-11-08 09:35:48 +00:00
|
|
|
catalog: Arc<Mutex<CatalogWriter<W>>>,
|
2020-06-09 11:17:55 +00:00
|
|
|
patterns: Vec<MatchEntry>,
|
2020-01-10 11:50:06 +00:00
|
|
|
entries_max: usize,
|
2019-08-09 07:46:49 +00:00
|
|
|
) -> Result<Self, Error> {
|
2020-01-22 10:37:16 +00:00
|
|
|
let (tx, rx) = std::sync::mpsc::sync_channel(10);
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
let buffer_size = 256 * 1024;
|
2019-05-18 10:57:43 +00:00
|
|
|
|
2019-07-24 07:24:35 +00:00
|
|
|
let error = Arc::new(Mutex::new(None));
|
2020-03-23 14:03:18 +00:00
|
|
|
let child = std::thread::Builder::new()
|
|
|
|
.name("PxarBackupStream".to_string())
|
|
|
|
.spawn({
|
|
|
|
let error = Arc::clone(&error);
|
|
|
|
move || {
|
|
|
|
let mut catalog_guard = catalog.lock().unwrap();
|
|
|
|
let writer = std::io::BufWriter::with_capacity(
|
|
|
|
buffer_size,
|
|
|
|
crate::tools::StdChannelWriter::new(tx),
|
|
|
|
);
|
|
|
|
|
|
|
|
let writer = pxar::encoder::sync::StandardWriter::new(writer);
|
|
|
|
if let Err(err) = crate::pxar::create_archive(
|
|
|
|
dir,
|
|
|
|
writer,
|
2020-06-09 11:17:55 +00:00
|
|
|
patterns,
|
2020-06-10 09:03:42 +00:00
|
|
|
crate::pxar::Flags::DEFAULT,
|
2020-03-23 14:03:18 +00:00
|
|
|
device_set,
|
|
|
|
skip_lost_and_found,
|
|
|
|
|_| Ok(()),
|
|
|
|
entries_max,
|
|
|
|
Some(&mut *catalog_guard),
|
|
|
|
) {
|
|
|
|
let mut error = error.lock().unwrap();
|
|
|
|
*error = Some(err.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})?;
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2019-07-24 07:24:35 +00:00
|
|
|
Ok(Self {
|
2020-01-22 10:37:16 +00:00
|
|
|
rx: Some(rx),
|
2019-07-24 07:24:35 +00:00
|
|
|
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-06-09 11:17:55 +00:00
|
|
|
patterns: Vec<MatchEntry>,
|
2020-01-10 11:50:06 +00:00
|
|
|
entries_max: usize,
|
2019-08-09 07:46:49 +00:00
|
|
|
) -> Result<Self, Error> {
|
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-03-23 14:03:18 +00:00
|
|
|
Self::new(
|
|
|
|
dir,
|
|
|
|
path,
|
|
|
|
device_set,
|
|
|
|
verbose,
|
|
|
|
skip_lost_and_found,
|
|
|
|
catalog,
|
2020-06-09 11:17:55 +00:00
|
|
|
patterns,
|
2020-03-23 14:03:18 +00:00
|
|
|
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-08-23 12:36:51 +00:00
|
|
|
type Item = Result<Vec<u8>, Error>;
|
2019-01-16 09:15:39 +00:00
|
|
|
|
2020-01-22 10:37:16 +00:00
|
|
|
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
|
2020-03-23 14:03:18 +00:00
|
|
|
{
|
|
|
|
// limit lock scope
|
2019-07-24 09:30:43 +00:00
|
|
|
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
|
|
|
}
|
2020-01-22 10:37:16 +00:00
|
|
|
|
|
|
|
match crate::tools::runtime::block_in_place(|| self.rx.as_ref().unwrap().recv()) {
|
|
|
|
Ok(data) => Poll::Ready(Some(data)),
|
|
|
|
Err(_) => {
|
|
|
|
let error = self.error.lock().unwrap();
|
|
|
|
if let Some(ref msg) = *error {
|
|
|
|
return Poll::Ready(Some(Err(format_err!("{}", msg))));
|
|
|
|
}
|
|
|
|
Poll::Ready(None) // channel closed, no error
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 09:15:39 +00:00
|
|
|
}
|
|
|
|
}
|