2023-07-07 19:20:21 -04:00
|
|
|
use crate::MAGIC;
|
|
|
|
|
2023-07-15 11:36:21 -04:00
|
|
|
#[cfg(feature = "parallel")]
|
|
|
|
use {
|
|
|
|
crate::FileType,
|
|
|
|
rayon::{iter::ParallelBridge, prelude::ParallelIterator},
|
|
|
|
std::sync::mpsc::Sender,
|
|
|
|
};
|
2023-07-04 01:19:07 -04:00
|
|
|
use {
|
2023-07-11 00:57:22 -04:00
|
|
|
crate::{Error, Node},
|
2023-07-04 01:19:07 -04:00
|
|
|
std::{
|
|
|
|
io::{ErrorKind, Read},
|
|
|
|
iter::Iterator,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
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-07 19:20:21 -04:00
|
|
|
pub struct Stream<R: Read + Send> {
|
2023-07-08 00:39:52 -04:00
|
|
|
pub length: u32,
|
2023-07-07 19:20:21 -04:00
|
|
|
reader: R,
|
2023-07-04 01:19:07 -04:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:20:21 -04:00
|
|
|
impl<R: Read + Send> Iterator for Stream<R> {
|
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,
|
|
|
|
}
|
|
|
|
|
2023-07-07 19:20:21 -04:00
|
|
|
impl<R: Read + Send> Stream<R> {
|
2023-12-15 18:49:36 -05:00
|
|
|
/// Creates a new archive
|
|
|
|
/// # Errors
|
|
|
|
/// Returns `crate::Error` if io fails or several other error conditions
|
2023-07-07 19:20:21 -04:00
|
|
|
pub fn new(mut reader: R) -> Result<Self, Error> {
|
2023-07-08 00:39:52 -04:00
|
|
|
let mut buf = [0; 11];
|
2023-07-07 19:20:21 -04:00
|
|
|
reader.read_exact(&mut buf)?;
|
2023-07-08 00:39:52 -04:00
|
|
|
let length = u32::from_le_bytes(buf[7..].try_into()?);
|
|
|
|
if buf[0..7] == MAGIC {
|
2023-07-15 11:36:21 -04:00
|
|
|
Ok(Self { length, reader })
|
2023-07-07 19:20:21 -04:00
|
|
|
} else {
|
|
|
|
Err(Error::InvalidMagic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:49:36 -05:00
|
|
|
/// Extracts and archive
|
|
|
|
/// # Errors
|
|
|
|
/// Returns `crate::Error` if io fails or several other error conditions
|
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-12-15 18:49:36 -05:00
|
|
|
/// Extracts and archive in parallel
|
|
|
|
/// # Errors
|
|
|
|
/// Returns `crate::Error` if io fails or several other error conditions
|
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| {
|
2023-07-08 00:39:52 -04:00
|
|
|
let n = n?;
|
|
|
|
n.extract(prefix)?;
|
|
|
|
match n.filetype {
|
|
|
|
FileType::Normal(f) => {
|
2023-07-15 11:36:21 -04:00
|
|
|
s.send(Message::FileExtracted {
|
|
|
|
name: n.name.clone(),
|
|
|
|
size: f.len,
|
|
|
|
})
|
|
|
|
.map_err(|_| Error::SenderError)?;
|
|
|
|
}
|
2023-07-08 00:39:52 -04:00
|
|
|
FileType::SoftLink(t) | FileType::HardLink(t) => {
|
2023-07-15 11:36:21 -04:00
|
|
|
s.send(Message::LinkCreated {
|
|
|
|
name: n.name.clone(),
|
|
|
|
target: t.clone(),
|
|
|
|
})
|
|
|
|
.map_err(|_| Error::SenderError)?;
|
|
|
|
}
|
2023-07-08 00:39:52 -04:00
|
|
|
FileType::Directory => {
|
2023-07-15 11:36:21 -04:00
|
|
|
s.send(Message::DirectoryCreated {
|
|
|
|
name: n.name.clone(),
|
|
|
|
})
|
|
|
|
.map_err(|_| Error::SenderError)?;
|
|
|
|
}
|
2023-07-08 00:39:52 -04:00
|
|
|
FileType::Block(_) | FileType::Character(_) | FileType::Fifo => {
|
2023-07-15 11:36:21 -04:00
|
|
|
s.send(Message::DeviceCreated {
|
|
|
|
name: n.name.clone(),
|
|
|
|
})
|
|
|
|
.map_err(|_| Error::SenderError)?;
|
|
|
|
}
|
2023-07-08 00:39:52 -04:00
|
|
|
FileType::Eof => {
|
|
|
|
s.send(Message::Eof).map_err(|_| Error::SenderError)?;
|
2023-07-15 11:36:21 -04:00
|
|
|
}
|
2023-07-08 00:39:52 -04:00
|
|
|
}
|
2023-07-05 23:50:28 -04:00
|
|
|
Ok::<(), Error>(())
|
|
|
|
})?;
|
2023-07-11 00:57:22 -04:00
|
|
|
sender.send(Message::Eof).map_err(|_| Error::SenderError)?;
|
2023-07-05 23:50:28 -04:00
|
|
|
Ok(())
|
2023-07-05 23:21:30 -04:00
|
|
|
}
|
|
|
|
}
|