2023-07-04 01:19:07 -04:00
|
|
|
use {
|
|
|
|
crate::{Error, Node},
|
|
|
|
std::{
|
|
|
|
io::{ErrorKind, Read},
|
|
|
|
iter::Iterator,
|
|
|
|
},
|
|
|
|
};
|
2023-07-05 23:50:28 -04:00
|
|
|
#[cfg(feature = "parallel")]
|
|
|
|
use {
|
|
|
|
rayon::{iter::ParallelBridge, prelude::ParallelIterator},
|
|
|
|
std::{ops::DerefMut, sync::mpsc::Sender},
|
|
|
|
};
|
2023-07-04 01:19:07 -04:00
|
|
|
|
2023-07-04 20:13:38 -04:00
|
|
|
/// An iterator over a series of archive `Node`'s. This struct is generic over any
|
|
|
|
/// type which implements `Read`, such as a file or a network stream.
|
2023-07-04 01:19:07 -04:00
|
|
|
#[derive(Debug)]
|
2023-07-05 23:50:28 -04:00
|
|
|
pub struct Stream<T: Read + Send> {
|
2023-07-04 01:19:07 -04:00
|
|
|
reader: T,
|
|
|
|
}
|
|
|
|
|
2023-07-05 23:50:28 -04:00
|
|
|
impl<T: Read + Send> From<T> for Stream<T> {
|
2023-07-04 01:19:07 -04:00
|
|
|
fn from(value: T) -> Self {
|
|
|
|
Self { reader: value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 23:50:28 -04:00
|
|
|
impl<T: Read + Send> Iterator for Stream<T> {
|
2023-07-04 01:19:07 -04:00
|
|
|
type Item = Result<Node, Error>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match Node::read(&mut self.reader) {
|
|
|
|
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => None,
|
2023-07-05 00:25:31 -04:00
|
|
|
Ok(f) => match f.filetype {
|
|
|
|
crate::FileType::Eof => None,
|
|
|
|
_ => Some(Ok(f)),
|
|
|
|
},
|
2023-07-04 01:19:07 -04:00
|
|
|
x => Some(x),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 23:21:30 -04:00
|
|
|
|
2023-07-05 23:50:28 -04:00
|
|
|
#[cfg(feature = "parallel")]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Message {
|
|
|
|
FileExtracted { name: String, size: u64 },
|
|
|
|
LinkCreated { name: String, target: String },
|
|
|
|
DirectoryCreated { name: String },
|
|
|
|
DeviceCreated { name: String },
|
|
|
|
Err(Error),
|
|
|
|
Eof,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Read + Send> Stream<T> {
|
2023-07-05 23:21:30 -04:00
|
|
|
pub fn extract(&mut self, prefix: Option<&str>) -> Result<(), Error> {
|
2023-07-05 23:50:28 -04:00
|
|
|
for node in self {
|
|
|
|
node?.extract(prefix)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-07-05 23:21:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "parallel")]
|
2023-07-05 23:50:28 -04:00
|
|
|
pub fn par_extract(
|
|
|
|
&mut self,
|
|
|
|
prefix: Option<&str>,
|
|
|
|
sender: &Sender<Message>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let s = sender.clone();
|
|
|
|
self.into_iter().par_bridge().try_for_each_with(s, |s, n| {
|
|
|
|
n?.extract(prefix)?;
|
|
|
|
Ok::<(), Error>(())
|
|
|
|
})?;
|
|
|
|
Ok(())
|
2023-07-05 23:21:30 -04:00
|
|
|
}
|
|
|
|
}
|