]>
Commit | Line | Data |
---|---|---|
e8edbbd4 DM |
1 | use failure::*; |
2 | ||
3 | use std::thread; | |
4 | use std::os::unix::io::FromRawFd; | |
17d6979a | 5 | use std::path::{Path, PathBuf}; |
e8edbbd4 DM |
6 | |
7 | use futures::{Async, Poll}; | |
8 | use futures::stream::Stream; | |
9 | ||
10 | use nix::fcntl::OFlag; | |
11 | use nix::sys::stat::Mode; | |
23bb8780 | 12 | use nix::dir::Dir; |
e8edbbd4 DM |
13 | |
14 | use crate::catar::encoder::*; | |
15 | ||
151c6ce2 DM |
16 | /// Stream implementation to encode and upload .catar archives. |
17 | /// | |
18 | /// The hyper client needs an async Stream for file upload, so we | |
19 | /// spawn an extra thread to encode the .catar data and pipe it to the | |
20 | /// consumer. | |
21 | /// | |
22 | /// Note: The currect implementation is not fully ansync and can block. | |
e8edbbd4 DM |
23 | pub struct CaTarBackupStream { |
24 | pipe: Option<std::fs::File>, | |
25 | buffer: Vec<u8>, | |
26 | child: Option<thread::JoinHandle<()>>, | |
27 | } | |
28 | ||
29 | impl Drop for CaTarBackupStream { | |
30 | ||
31 | fn drop(&mut self) { | |
32 | drop(self.pipe.take()); | |
33 | self.child.take().unwrap().join().unwrap(); | |
34 | } | |
35 | } | |
36 | ||
37 | impl CaTarBackupStream { | |
38 | ||
eed6db39 | 39 | pub fn new(mut dir: Dir, path: PathBuf, all_file_systems: bool, verbose: bool) -> Result<Self, Error> { |
e8edbbd4 DM |
40 | let mut buffer = Vec::with_capacity(4096); |
41 | unsafe { buffer.set_len(buffer.capacity()); } | |
42 | ||
43 | let (rx, tx) = nix::unistd::pipe()?; | |
44 | ||
e8edbbd4 DM |
45 | let child = thread::spawn(move|| { |
46 | let mut writer = unsafe { std::fs::File::from_raw_fd(tx) }; | |
eed6db39 | 47 | if let Err(err) = CaTarEncoder::encode(path, &mut dir, &mut writer, all_file_systems, verbose) { |
e8edbbd4 DM |
48 | eprintln!("catar encode failed - {}", err); |
49 | } | |
50 | }); | |
51 | ||
52 | let pipe = unsafe { std::fs::File::from_raw_fd(rx) }; | |
53 | ||
54 | Ok(Self { pipe: Some(pipe), buffer, child: Some(child) }) | |
55 | } | |
23bb8780 | 56 | |
eed6db39 | 57 | pub fn open(dirname: &Path, all_file_systems: bool, verbose: bool) -> Result<Self, Error> { |
23bb8780 | 58 | |
728797d0 | 59 | let dir = nix::dir::Dir::open(dirname, OFlag::O_DIRECTORY, Mode::empty())?; |
23bb8780 DM |
60 | let path = std::path::PathBuf::from(dirname); |
61 | ||
eed6db39 | 62 | Self::new(dir, path, all_file_systems, verbose) |
23bb8780 | 63 | } |
e8edbbd4 DM |
64 | } |
65 | ||
66 | impl Stream for CaTarBackupStream { | |
67 | ||
68 | type Item = Vec<u8>; | |
69 | type Error = Error; | |
70 | ||
71 | // Note: This is not async!! | |
72 | ||
73 | fn poll(&mut self) -> Poll<Option<Vec<u8>>, Error> { | |
74 | ||
75 | use std::io::Read; | |
76 | ||
23bb8780 | 77 | loop { |
e8edbbd4 DM |
78 | let pipe = match self.pipe { |
79 | Some(ref mut pipe) => pipe, | |
80 | None => unreachable!(), | |
81 | }; | |
82 | match pipe.read(&mut self.buffer) { | |
83 | Ok(n) => { | |
84 | if n == 0 { | |
85 | return Ok(Async::Ready(None)) | |
86 | } else { | |
87 | let data = self.buffer[..n].to_vec(); | |
88 | return Ok(Async::Ready(Some(data))) | |
89 | } | |
90 | } | |
91 | Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => { | |
92 | // try again | |
93 | } | |
94 | Err(err) => { | |
95 | return Err(err.into()) | |
96 | } | |
97 | }; | |
98 | } | |
99 | } | |
100 | } |