Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ strum = { version = "0.28.0", features = ["derive"] }
tar = "0.4.42"
tempfile = "3.10.1"
time = { version = "0.3.36", default-features = false }
unrar = { version = "0.5.7", optional = true }
unrar = { package = "unrar-ng", version = "0.7.6", optional = true }
xz2 = "0.1.7"
zip = { version = "6", default-features = false, features = [
"time",
Expand Down
61 changes: 43 additions & 18 deletions src/archive/rar.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
//! Contains RAR-specific building and unpacking functions

use std::path::Path;
use std::path::{Path, PathBuf};

use unrar::Archive;
use unrar::{
Archive, ExtractEvent,
error::{Code, UnrarError, When},
};

use crate::{
error::{Error, Result},
error::{Error, FinalError, Result},
info,
list::{FileInArchive, ListFileType},
utils::BytesFmt,
utils::{BytesFmt, PathFmt},
};

/// Unpacks the archive given by `archive_path` into the folder given by `output_folder`.
Expand All @@ -19,24 +22,46 @@ pub fn unpack_archive(archive_path: &Path, output_folder: &Path, password: Optio
None => Archive::new(archive_path),
};

let mut archive = archive.open_for_processing()?;
let mut files_unpacked = 0;
let archive = archive.open_for_processing()?;

let mut files_unpacked: u64 = 0;
let mut first_err: Option<(PathBuf, i32)> = None;

while let Some(header) = archive.read_header()? {
let entry = header.entry();
archive = if entry.is_file() {
let cb_result = archive.extract_all_with_callback(output_folder, |event| match event {
ExtractEvent::Ok { filename, size } => {
info!("extracted ({}) {}", BytesFmt(size), PathFmt(&filename));
files_unpacked += 1;
true
}
ExtractEvent::Err { filename, error_code } => {
first_err = Some((filename, error_code));
// Returning false cancels the rest of the extraction so any
// additional per-file errors don't get silently swallowed.
false
}
ExtractEvent::LargeDictWarning {
dict_size_kb,
max_dict_size_kb,
} => {
info!(
"extracted ({}) {}",
BytesFmt(entry.unpacked_size),
entry.filename.display(),
"archive requires {} KiB dictionary; this build supports up to {} KiB",
dict_size_kb, max_dict_size_kb,
);
files_unpacked += 1;
header.extract_with_base(output_folder)?
} else {
header.skip()?
};
}
// Reject the oversized dictionary so the DLL fails the
// operation with Code::LargeDict instead of silently
// proceeding with a result it cannot actually produce.
false
}
_ => true,
});

if let Some((path, code)) = first_err {
let inner = UnrarError::from(Code::from(code), When::Process).to_string();
return Err(Error::Custom {
reason: FinalError::with_title(format!("failed to extract {}", PathFmt(&path))).detail(inner),
});
}
let _status = cb_result?;
Ok(files_unpacked)
}

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl From<zip::result::ZipError> for Error {
impl From<unrar::error::UnrarError> for Error {
fn from(err: unrar::error::UnrarError) -> Self {
Self::Custom {
reason: FinalError::with_title("Unexpected error in rar archive").detail(format!("{:?}", err.code)),
reason: FinalError::with_title("Unexpected error in rar archive").detail(err.to_string()),
}
}
}
Expand Down
Loading