Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unrar decompressor to support filter/map and generate paths consistent with zip implementatoin #20

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
945 changes: 454 additions & 491 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[workspace]
members = ["decompress", "xtask"]
resolver = "2"
2 changes: 1 addition & 1 deletion decompress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bzip2 = { version = "0.4.3", optional = true }
flate2 = { version = "1.0.25", optional = true }
xz = { version = "0.1.0", optional = true }
zstd = { version = "0.12.0", optional = true }
unrar = { version = "0.4.4", optional = true }
unrar = { version = "0.5.6", optional = true }
infer = "0.12.0"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion decompress/src/decompressors/ar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::decompressors::utils::normalize_mode;
use crate::{DecompressError, Decompression, Decompressor, ExtractOpts, Listing};
use ar::Archive;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -130,6 +129,7 @@ impl Decompressor for Ar {

#[cfg(unix)]
{
use crate::decompressors::utils::normalize_mode;
use std::os::unix::fs::PermissionsExt;
let mode = normalize_mode(mode);
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode))?;
Expand Down
51 changes: 32 additions & 19 deletions decompress/src/decompressors/tar_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use std::{
path::{Path, PathBuf},
};

use crate::decompressors::utils::normalize_mode;
use crate::{DecompressError, ExtractOpts};
use tar::Archive;
use tar::{Archive, EntryType};

pub fn tar_list(out: &mut Archive<Box<dyn Read>>) -> Result<Vec<String>, DecompressError> {
Ok(out
Expand All @@ -33,7 +32,7 @@ pub fn tar_extract(

// alternative impl: just unpack, and then mv everything back X levels
for entry in out.entries()? {
let entry = entry?;
let mut entry = entry?;
let filepath = entry.path()?;

// strip prefixed components. this can be 0 parts, in which case strip does not happen.
Expand All @@ -54,29 +53,43 @@ pub fn tar_extract(

let outpath: Cow<'_, Path> = (opts.map)(outpath.as_path());

if entry.header().entry_type() != tar::EntryType::Directory {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(p)?;
match entry.header().entry_type() {
EntryType::Directory => {}
EntryType::Regular => {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(p)?;
}
}
}

let mut outfile = fs::File::create(&outpath)?;
let mut outfile = fs::File::create(&outpath)?;

#[cfg(unix)]
let h = entry.header().mode();
#[cfg(unix)]
let h = entry.header().mode();

io::copy(&mut BufReader::new(entry), &mut outfile)?;
files.push(outpath.to_string_lossy().to_string());
io::copy(&mut BufReader::new(entry), &mut outfile)?;
files.push(outpath.to_string_lossy().to_string());

#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(mode) = h {
let mode = normalize_mode(mode);
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode))?;
#[cfg(unix)]
{
use crate::decompressors::utils::normalize_mode;
use std::os::unix::fs::PermissionsExt;
if let Ok(mode) = h {
let mode = normalize_mode(mode);
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode))?;
}
}
}
EntryType::Symlink => {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(p)?;
}
}

entry.unpack(&outpath)?;
}
e => todo!("Unsupported entry type {e:?}"),
}
}
Ok(files)
Expand Down
70 changes: 45 additions & 25 deletions decompress/src/decompressors/unrar.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::path::Path;
use unrar::FileHeader;

use crate::{DecompressError, Decompression, Decompressor, ExtractOpts, Listing};

macro_rules! check {
($e : expr) => {
$e.map_err(|e| crate::DecompressError::Error(e.to_string()))?
};
}

lazy_static! {
static ref RE: Regex = Regex::new(r"(?i)\.rar$").unwrap();
}
Expand Down Expand Up @@ -36,44 +43,57 @@ impl Decompressor for Unrar {
}

fn list(&self, archive: &Path) -> Result<Listing, DecompressError> {
let res = unrar::Archive::new(archive.to_string_lossy().to_string())
.list()
.map_err(|e| DecompressError::Error(e.to_string()))?
.process()
.map_err(|e| DecompressError::Error(e.to_string()))?;
fn enclosed_name(h: FileHeader) -> String {
let temp = h.filename.to_string_lossy();

#[cfg(windows)]
let mut s = temp.replace("\\", "/");

#[cfg(unix)]
let mut s = temp.into_owned();

Ok(Listing {
id: "rar",
entries: res
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>(),
})
if h.is_directory() && !s.ends_with("/") {
s.push('/')
}

s
}

let rar = check!(unrar::Archive::new(archive).open_for_listing());
let entries = rar
.into_iter()
.map(|header| Ok(enclosed_name(check!(header))))
.collect::<Result<Vec<_>, DecompressError>>()?;
Ok(Listing { id: "rar", entries })
}

fn decompress(
&self,
archive: &Path,
to: &Path,
_opts: &ExtractOpts,
opts: &ExtractOpts,
) -> Result<Decompression, DecompressError> {
use std::fs;
if !to.exists() {
fs::create_dir_all(to)?;
}

let res = unrar::Archive::new(archive.to_string_lossy().to_string())
.extract_to(to.to_string_lossy().to_string())
.map_err(|e| DecompressError::Error(e.to_string()))?
.process()
.map_err(|e| DecompressError::Error(e.to_string()))?;
let mut rar = check!(unrar::Archive::new(archive).open_for_processing());
let mut files = Vec::new();
while let Some(header) = check!(rar.read_header()) {
let entry = header.entry();
if entry.is_directory() || entry.is_file() {
let output_path = to.join(&entry.filename);
if output_path != to && (opts.filter)(&output_path) {
let output_path = (opts.map)(&output_path);
files.push(output_path.to_string_lossy().into_owned());
rar = check!(header.extract_to(output_path));
continue;
}
}
rar = check!(header.skip());
}

Ok(Decompression {
id: "rar",
files: res
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>(),
})
Ok(Decompression { id: "rar", files })
}
}
1 change: 1 addition & 0 deletions decompress/src/decompressors/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(unix)]
pub fn normalize_mode(mode: u32) -> u32 {
if mode == 0 {
0o644
Expand Down
6 changes: 2 additions & 4 deletions decompress/src/decompressors/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use lazy_static::lazy_static;
use regex::Regex;
use zip::ZipArchive;

use crate::{
decompressors::utils::normalize_mode, DecompressError, Decompression, Decompressor,
ExtractOpts, Listing,
};
use crate::{DecompressError, Decompression, Decompressor, ExtractOpts, Listing};

lazy_static! {
static ref RE: Regex = Regex::new(r"(?i)\.zip$").unwrap();
Expand Down Expand Up @@ -120,6 +117,7 @@ impl Decompressor for Zip {
// Get and Set permissions
#[cfg(unix)]
{
use crate::decompressors::utils::normalize_mode;
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
let mode = normalize_mode(mode);
Expand Down
1 change: 0 additions & 1 deletion decompress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ impl Decompress {
let mt = res.map(|t| t.mime_type());
mt.and_then(|mt| self.decompressors.iter().find(|dec| dec.test_mimetype(mt)))
} else {
println!("f: {:?} ", archive.as_ref());
self.decompressors
.iter()
.find(|dec| dec.test(archive.as_ref()))
Expand Down