2019-03-08 15:55:54 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use std::thread;
|
|
|
|
use std::os::unix::io::FromRawFd;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::io::Write;
|
|
|
|
|
2019-03-14 09:54:09 +00:00
|
|
|
use crate::pxar::decoder::*;
|
2019-03-08 15:55:54 +00:00
|
|
|
|
2019-03-14 09:54:09 +00:00
|
|
|
/// Writer implementation to deccode a .pxar archive (download).
|
2019-03-08 15:55:54 +00:00
|
|
|
|
2019-03-15 06:20:22 +00:00
|
|
|
pub struct PxarDecodeWriter {
|
2019-03-08 15:55:54 +00:00
|
|
|
pipe: Option<std::fs::File>,
|
|
|
|
child: Option<thread::JoinHandle<()>>,
|
|
|
|
}
|
|
|
|
|
2019-03-15 06:20:22 +00:00
|
|
|
impl Drop for PxarDecodeWriter {
|
2019-03-08 15:55:54 +00:00
|
|
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
drop(self.pipe.take());
|
|
|
|
self.child.take().unwrap().join().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 06:20:22 +00:00
|
|
|
impl PxarDecodeWriter {
|
2019-03-08 15:55:54 +00:00
|
|
|
|
2019-03-15 06:20:22 +00:00
|
|
|
pub fn new(base: &Path, verbose: bool) -> Result<Self, Error> {
|
2019-03-08 15:55:54 +00:00
|
|
|
let (rx, tx) = nix::unistd::pipe()?;
|
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
let base = PathBuf::from(base);
|
2019-03-08 15:55:54 +00:00
|
|
|
|
|
|
|
let child = thread::spawn(move|| {
|
|
|
|
let mut reader = unsafe { std::fs::File::from_raw_fd(rx) };
|
2019-03-14 09:54:09 +00:00
|
|
|
let mut decoder = PxarDecoder::new(&mut reader);
|
2019-03-15 06:20:22 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
if let Err(err) = decoder.restore(&base, & |path| {
|
2019-03-15 06:20:22 +00:00
|
|
|
if verbose {
|
|
|
|
println!("RESTORE: {:?}", path);
|
|
|
|
}
|
2019-03-08 15:55:54 +00:00
|
|
|
Ok(())
|
|
|
|
}) {
|
2019-03-14 09:54:09 +00:00
|
|
|
eprintln!("pxar decode failed - {}", err);
|
2019-03-08 15:55:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let pipe = unsafe { std::fs::File::from_raw_fd(tx) };
|
|
|
|
|
|
|
|
Ok(Self { pipe: Some(pipe), child: Some(child) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 06:20:22 +00:00
|
|
|
impl Write for PxarDecodeWriter {
|
2019-03-08 15:55:54 +00:00
|
|
|
|
|
|
|
fn write(&mut self, buffer: &[u8]) -> Result<usize, std::io::Error> {
|
|
|
|
let pipe = match self.pipe {
|
|
|
|
Some(ref mut pipe) => pipe,
|
|
|
|
None => unreachable!(),
|
|
|
|
};
|
|
|
|
pipe.write(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), std::io::Error> {
|
|
|
|
let pipe = match self.pipe {
|
|
|
|
Some(ref mut pipe) => pipe,
|
|
|
|
None => unreachable!(),
|
|
|
|
};
|
|
|
|
pipe.flush()
|
|
|
|
}
|
|
|
|
}
|