Skip to content

Commit

Permalink
♻️ Simplify extract
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Jan 8, 2024
1 parent df9e0f0 commit a65fb9a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
65 changes: 46 additions & 19 deletions cli/src/command/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,23 @@ fn extract_archive(args: ExtractArgs, verbosity: Verbosity) -> io::Result<()> {

let mut hard_kink_entries = Vec::new();

let file = File::open(&args.file.archive)?;
let mut reader = Archive::read_header(file)?;
let mut num_archive = 1;

let (tx, rx) = std::sync::mpsc::channel();
loop {
for entry in reader.entries_with_password(password.clone()) {
run_extract(
&args.file.archive,
password.clone(),
|entry| {
let item = entry?;
let item_path = PathBuf::from(item.header().path().as_str());
if !globs.is_empty() && !globs.par_iter().any(|glob| glob.matches_path(&item_path)) {
if verbosity == Verbosity::Verbose {
println!("Skip: {}", item.header().path())
}
continue;
return Ok(());
}
progress_bar.let_ref(|pb| pb.inc_length(1));
if item.header().data_kind() == DataKind::HardLink {
hard_kink_entries.push(item);
continue;
return Ok(());
}
let tx = tx.clone();
let password = password.clone();
Expand All @@ -90,19 +88,16 @@ fn extract_archive(args: ExtractArgs, verbosity: Verbosity) -> io::Result<()> {
))
.unwrap_or_else(|e| panic!("{e}: {}", item_path.display()));
});
}
if reader.next_archive() {
num_archive += 1;
let part_n_name = part_name(&args.file.archive, num_archive).unwrap();
Ok(())
},
|path, num| {
let next_file_path = part_name(path, num).unwrap();
if verbosity == Verbosity::Verbose {
println!("Detect split: search {}", part_n_name.display());
println!("Detect split: search {}", next_file_path.display());
}
let file = File::open(part_n_name)?;
reader = reader.read_next_archive(file)?;
} else {
break;
}
}
next_file_path
},
)?;
drop(tx);
for result in rx {
result?;
Expand Down Expand Up @@ -134,6 +129,38 @@ fn extract_archive(args: ExtractArgs, verbosity: Verbosity) -> io::Result<()> {
Ok(())
}

fn run_extract<P, F, N, NP>(
path: P,
password: Option<String>,
mut extractor: F,
mut get_next_file_path: N,
) -> io::Result<()>
where
P: AsRef<Path>,
F: FnMut(io::Result<RegularEntry>) -> io::Result<()>,
N: FnMut(&Path, usize) -> NP,
NP: AsRef<Path>,
{
let path = path.as_ref();
let file = File::open(path)?;
let mut reader = Archive::read_header(file)?;
let mut num_archive = 1;
loop {
for entry in reader.entries_with_password(password.clone()) {
extractor(entry)?;
}
if reader.next_archive() {
num_archive += 1;
let next_file_path = get_next_file_path(path, num_archive);
let file = File::open(next_file_path)?;
reader = reader.read_next_archive(file)?;
} else {
break;
}
}
Ok(())
}

fn extract_entry(
item_path: PathBuf,
item: RegularEntry,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/utils/path.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::{Path, PathBuf};

#[inline]
pub(crate) fn part_name<P: AsRef<Path>>(p: P, n: u32) -> Option<PathBuf> {
pub(crate) fn part_name<P: AsRef<Path>>(p: P, n: usize) -> Option<PathBuf> {
#[inline]
fn with_ext(p: &Path, n: u32) -> Option<PathBuf> {
fn with_ext(p: &Path, n: usize) -> Option<PathBuf> {
let name = p.file_stem()?;
if let Some(ext) = p.extension() {
Some(PathBuf::from(name).with_extension(format!("part{n}.{}", ext.to_string_lossy())))
Expand Down

0 comments on commit a65fb9a

Please sign in to comment.