Compare commits
No commits in common. "22faadc34e9d27fd97621bf189a514a0f5e6c622" and "e3dddc3dd9898a073e24d835b1cc36e4af11690c" have entirely different histories.
22faadc34e
...
e3dddc3dd9
10
src/cli.rs
10
src/cli.rs
@ -27,11 +27,6 @@ pub fn extract() -> Command {
|
||||
.long("quiet")
|
||||
.visible_alias("silent")
|
||||
.action(ArgAction::SetFalse),
|
||||
Arg::new("verbose")
|
||||
.help("List the extracted files")
|
||||
.short('v')
|
||||
.long("verbose")
|
||||
.action(ArgAction::SetTrue),
|
||||
Arg::new("stdin")
|
||||
.help("Read archive from stdin")
|
||||
.short('i')
|
||||
@ -104,11 +99,6 @@ pub fn create() -> Command {
|
||||
.long("quiet")
|
||||
.visible_alias("silent")
|
||||
.action(ArgAction::SetFalse),
|
||||
Arg::new("verbose")
|
||||
.help("List the archived files")
|
||||
.short('v')
|
||||
.long("verbose")
|
||||
.action(ArgAction::SetTrue),
|
||||
Arg::new("stdout")
|
||||
.help("Write archive to stdout")
|
||||
.short('o')
|
||||
|
@ -95,13 +95,13 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
|
||||
handle = Some(thread::spawn(move || {
|
||||
for msg in &receiver {
|
||||
match msg {
|
||||
Message::NodeSaved(n) => {
|
||||
let name = n.name.split('/').last().unwrap();
|
||||
if let ListingKind::Normal(size) = n.kind {
|
||||
pb.set_prefix(format!("{name} added, {size} bytes"));
|
||||
} else {
|
||||
pb.set_prefix(format!("{name} added"));
|
||||
}
|
||||
Message::NodeCreated(s) => {
|
||||
pb.set_prefix(s.split('/').last().unwrap().to_string());
|
||||
pb.inc(1);
|
||||
}
|
||||
Message::NodeSaved { name, size } => {
|
||||
let name = name.split('/').last().unwrap();
|
||||
pb.set_prefix(format!("{name} added, {size} bytes"));
|
||||
pb.inc(1);
|
||||
}
|
||||
Message::Eof => {
|
||||
@ -242,37 +242,27 @@ fn progress(file: &str, receiver: &mpsc::Receiver<StreamMessage>, len: u64) -> u
|
||||
pb.println(format!("Extracting archive {file}"));
|
||||
for msg in receiver {
|
||||
match msg {
|
||||
StreamMessage::NodeExtracted(n) => {
|
||||
let name = n.name.split('/').last().unwrap();
|
||||
match n.kind {
|
||||
ListingKind::Normal(size) => {
|
||||
pb.set_prefix(format!("{name} extracted, {size} bytes"));
|
||||
pb.inc(1);
|
||||
total += size;
|
||||
}
|
||||
ListingKind::SoftLink(t)
|
||||
| ListingKind::HardLink(t) => {
|
||||
pb.set_prefix(format!("{name} -> {t}"));
|
||||
pb.inc(1);
|
||||
}
|
||||
ListingKind::Directory => {
|
||||
pb.set_prefix(format!("mkdir {name}"));
|
||||
pb.inc(1);
|
||||
}
|
||||
ListingKind::Block(_d)
|
||||
| ListingKind::Character(_d) => {
|
||||
pb.set_prefix(format!("mknod {name}"));
|
||||
pb.inc(1);
|
||||
}
|
||||
ListingKind::Fifo => {
|
||||
pb.set_prefix(format!("mkfifo {name}"));
|
||||
pb.inc(1);
|
||||
}
|
||||
ListingKind::Eof => {
|
||||
pb.finish_and_clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
StreamMessage::FileExtracted { name, size } => {
|
||||
let name = name.split('/').last().unwrap();
|
||||
pb.set_prefix(format!("{name} extracted, {size} bytes"));
|
||||
pb.inc(1);
|
||||
total += size;
|
||||
}
|
||||
StreamMessage::LinkCreated { name, target } => {
|
||||
let name = name.split('/').last().unwrap();
|
||||
let target = target.split('/').last().unwrap();
|
||||
pb.set_prefix(format!("{name} -> {target}"));
|
||||
pb.inc(1);
|
||||
}
|
||||
StreamMessage::DirectoryCreated { name } => {
|
||||
let name = name.split('/').last().unwrap();
|
||||
pb.set_prefix(format!("mkdir {name}"));
|
||||
pb.inc(1);
|
||||
}
|
||||
StreamMessage::DeviceCreated { name } => {
|
||||
let name = name.split('/').last().unwrap();
|
||||
pb.set_prefix(format!("mknod {name}"));
|
||||
pb.inc(1);
|
||||
}
|
||||
StreamMessage::Eof => {
|
||||
pb.finish_and_clear();
|
||||
|
24
src/lib.rs
24
src/lib.rs
@ -137,8 +137,18 @@ pub fn stream_archive<W: Write>(
|
||||
#[cfg(feature = "parallel")]
|
||||
#[derive(Debug)]
|
||||
pub enum Message {
|
||||
/// A `Node` has successfully been created from this path
|
||||
NodeCreated(
|
||||
/// The path of the created `Node`
|
||||
String,
|
||||
),
|
||||
/// The `Node` has successfully been written into the writer
|
||||
NodeSaved(Listing),
|
||||
NodeSaved {
|
||||
/// The path of the saved `Node`
|
||||
name: String,
|
||||
/// The size in bytes of the created `Node`
|
||||
size: u64,
|
||||
},
|
||||
/// An error occurred creating or writing out the node
|
||||
Err {
|
||||
/// The pathname of the node
|
||||
@ -231,7 +241,17 @@ pub fn par_stream_archive<W: Write + Send>(
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
s.send(Message::NodeSaved(node.into())).map_err(|_| Error::SenderError)?;
|
||||
match node.filetype {
|
||||
FileType::Normal(n) => s
|
||||
.send(Message::NodeSaved {
|
||||
name: node.name.clone(),
|
||||
size: n.len,
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?,
|
||||
_ => s
|
||||
.send(Message::NodeCreated(node.name.clone()))
|
||||
.map_err(|_| Error::SenderError)?,
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::MutexError)
|
||||
|
107
src/stream.rs
107
src/stream.rs
@ -2,7 +2,7 @@
|
||||
|
||||
#[cfg(feature = "parallel")]
|
||||
use {
|
||||
crate::{ FileType, Listing },
|
||||
crate::FileType,
|
||||
rayon::{iter::ParallelBridge, prelude::ParallelIterator},
|
||||
std::sync::mpsc::Sender,
|
||||
};
|
||||
@ -44,8 +44,30 @@ impl<R: Read> Iterator for Stream<R> {
|
||||
#[cfg(feature = "parallel")]
|
||||
#[derive(Debug)]
|
||||
pub enum Message {
|
||||
/// A Node has been successfully extracted
|
||||
NodeExtracted(Listing),
|
||||
/// A File node has been successfully extracted
|
||||
FileExtracted {
|
||||
/// The path of the extracted `Node`
|
||||
name: String,
|
||||
/// The size in bytes of the created file
|
||||
size: u64,
|
||||
},
|
||||
/// A link has been created from a `Node`
|
||||
LinkCreated {
|
||||
/// The path of the link
|
||||
name: String,
|
||||
/// The path to this link's target
|
||||
target: String,
|
||||
},
|
||||
/// A directory has been created from this `Node`
|
||||
DirectoryCreated {
|
||||
/// The directory's path
|
||||
name: String,
|
||||
},
|
||||
/// A device file has been created from this `Node`
|
||||
DeviceCreated {
|
||||
/// The path of the device file
|
||||
name: String,
|
||||
},
|
||||
/// An error occurred while extracting this `Node`
|
||||
Err {
|
||||
/// The pathame of the `Node` being extracted
|
||||
@ -113,12 +135,12 @@ impl<R: Read> Stream<R> {
|
||||
f: F,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
F: FnOnce(&Node, Option<u32>, Option<u32>) + Copy,
|
||||
F: FnOnce(Node, Option<u32>, Option<u32>) + Copy,
|
||||
{
|
||||
for node in self {
|
||||
let node = node?;
|
||||
node.extract(prefix, uid, gid)?;
|
||||
f(&node, uid, gid);
|
||||
f(node, uid, gid);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -126,7 +148,7 @@ impl<R: Read> Stream<R> {
|
||||
|
||||
#[cfg(feature = "parallel")]
|
||||
impl<R: Read + Send> Stream<R> {
|
||||
/// Extracts an archive in parallel
|
||||
/// Extracts and archive in parallel
|
||||
/// # Errors
|
||||
/// Returns `crate::Error` if io fails or several other error conditions
|
||||
pub fn par_extract(
|
||||
@ -148,12 +170,35 @@ impl<R: Read + Send> Stream<R> {
|
||||
return Err(error);
|
||||
}
|
||||
match n.filetype {
|
||||
FileType::Normal(f) => {
|
||||
s.send(Message::FileExtracted {
|
||||
name: n.name.clone(),
|
||||
size: f.len,
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::SoftLink(t) | FileType::HardLink(t) => {
|
||||
s.send(Message::LinkCreated {
|
||||
name: n.name.clone(),
|
||||
target: t.clone(),
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::Directory => {
|
||||
s.send(Message::DirectoryCreated {
|
||||
name: n.name.clone(),
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::Block(_) | FileType::Character(_) | FileType::Fifo => {
|
||||
s.send(Message::DeviceCreated {
|
||||
name: n.name.clone(),
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::Eof => {
|
||||
s.send(Message::Eof).map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
_ => {
|
||||
s.send(Message::NodeExtracted(n.into())).map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
}
|
||||
Ok::<(), Error>(())
|
||||
})?;
|
||||
@ -161,7 +206,7 @@ impl<R: Read + Send> Stream<R> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Extracts an archive in parallel and runs the passed in function for
|
||||
/// Extracts and archive in parallel and runs the passed in function for
|
||||
/// each `Node`
|
||||
/// # Errors
|
||||
/// Returns `crate::Error` if io fails or several other error conditions
|
||||
@ -174,28 +219,44 @@ impl<R: Read + Send> Stream<R> {
|
||||
f: F,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
F: FnOnce(&Node, Option<u32>, Option<u32>) + Copy + Send + Sync,
|
||||
F: FnOnce(Node, Option<u32>, Option<u32>) + Copy + Send + Sync,
|
||||
{
|
||||
let s = sender.clone();
|
||||
self.into_iter().par_bridge().try_for_each_with(s, |s, n| {
|
||||
let n = n?;
|
||||
if let Err(error) = n.extract(prefix, uid, gid) {
|
||||
s.send(Message::Err {
|
||||
name: n.name.clone(),
|
||||
error: error.clone(),
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
return Err(error);
|
||||
}
|
||||
n.extract(prefix, uid, gid)?;
|
||||
match n.filetype {
|
||||
FileType::Normal(ref f) => {
|
||||
s.send(Message::FileExtracted {
|
||||
name: n.name.clone(),
|
||||
size: f.len,
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::SoftLink(ref t) | FileType::HardLink(ref t) => {
|
||||
s.send(Message::LinkCreated {
|
||||
name: n.name.clone(),
|
||||
target: t.clone(),
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::Directory => {
|
||||
s.send(Message::DirectoryCreated {
|
||||
name: n.name.clone(),
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::Block(_) | FileType::Character(_) | FileType::Fifo => {
|
||||
s.send(Message::DeviceCreated {
|
||||
name: n.name.clone(),
|
||||
})
|
||||
.map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
FileType::Eof => {
|
||||
s.send(Message::Eof).map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
_ => {
|
||||
f(&n, uid, gid);
|
||||
s.send(Message::NodeExtracted(n.into())).map_err(|_| Error::SenderError)?;
|
||||
}
|
||||
}
|
||||
f(n, uid, gid);
|
||||
Ok::<(), Error>(())
|
||||
})?;
|
||||
sender.send(Message::Eof).map_err(|_| Error::SenderError)?;
|
||||
|
Loading…
Reference in New Issue
Block a user