Skip to content

Commit

Permalink
fixed formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lmkra committed Sep 26, 2023
1 parent 0323f06 commit 0e287d3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
70 changes: 38 additions & 32 deletions src/archive/rar.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
//! Contains RAR-specific building and unpacking functions

use crate::{list::FileInArchive, info, warning};
use crate::{info, list::FileInArchive, warning};
use std::path::Path;
use unrar::{self, Archive};

/// Unpacks the archive given by `archive_path` into the folder given by `output_folder`.
/// Assumes that output_folder is empty
pub fn unpack_archive(archive_path: &Path, output_folder: &Path, quiet: bool) -> crate::Result<usize> {

assert!(output_folder.read_dir().expect("dir exists").count() == 0);

let mut archive = Archive::new(archive_path).open_for_processing()?;
let mut unpacked = 0;

while let Some(header) = archive.read_header()? {
let entry = header.entry();
archive = if entry.is_file() {
if ! quiet {
info!(inaccessible, "{} extracted. ({})", entry.filename.display(), entry.unpacked_size);
}
unpacked += 1;
header.extract_with_base(output_folder)?
} else {
header.skip()?
};
}

Ok(unpacked)
assert!(output_folder.read_dir().expect("dir exists").count() == 0);

let mut archive = Archive::new(archive_path).open_for_processing()?;
let mut unpacked = 0;

while let Some(header) = archive.read_header()? {
let entry = header.entry();
archive = if entry.is_file() {
if !quiet {
info!(
inaccessible,
"{} extracted. ({})",
entry.filename.display(),
entry.unpacked_size
);
}
unpacked += 1;
header.extract_with_base(output_folder)?
} else {
header.skip()?
};
}

Ok(unpacked)
}

/// List contents of `archive_path`, returning a vector of archive entries
pub fn list_archive(archive_path: &Path) -> impl Iterator<Item = crate::Result<FileInArchive>> {
Archive::new(archive_path).open_for_listing().expect("cannot open archive")
.into_iter()
.map(|item| {
let item = item?;
let is_dir = item.is_directory();
let path = item.filename;

Ok(FileInArchive{ path, is_dir })
})
Archive::new(archive_path)
.open_for_listing()
.expect("cannot open archive")
.into_iter()
.map(|item| {
let item = item?;
let is_dir = item.is_directory();
let path = item.filename;

Ok(FileInArchive { path, is_dir })
})
}

pub fn no_compression_notice() {
const MESSAGE: &str = "Creating '.rar' archives is not supported due to licensing restrictions";
const MESSAGE: &str = "Creating '.rar' archives is not supported due to licensing restrictions";

warning!("{}", MESSAGE);
warning!("{}", MESSAGE);
}
4 changes: 3 additions & 1 deletion src/commands/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ pub fn decompress_file(
Box::new(|output_dir| crate::archive::rar::unpack_archive(input_file_path, output_dir, quiet))
};

if let ControlFlow::Continue(files) = smart_unpack(unpack_fn, output_dir, &output_file_path, question_policy)? {
if let ControlFlow::Continue(files) =
smart_unpack(unpack_fn, output_dir, &output_file_path, question_policy)?
{
files
} else {
return Ok(());
Expand Down
14 changes: 7 additions & 7 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ pub fn list_archive_contents(
Box::new(crate::archive::zip::list_archive(zip_archive))
}
Rar => {
if formats.len() > 1 {
let mut temp_file = tempfile::NamedTempFile::new()?;
io::copy(&mut reader, &mut temp_file)?;
Box::new(crate::archive::rar::list_archive(temp_file.path()))
} else {
Box::new(crate::archive::rar::list_archive(archive_path))
}
if formats.len() > 1 {
let mut temp_file = tempfile::NamedTempFile::new()?;
io::copy(&mut reader, &mut temp_file)?;
Box::new(crate::archive::rar::list_archive(temp_file.path()))
} else {
Box::new(crate::archive::rar::list_archive(archive_path))
}
}
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
panic!("Not an archive! This should never happen, if it does, something is wrong with `CompressionFormat::is_archive()`. Please report this error!");
Expand Down
3 changes: 1 addition & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,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(format!("{:?}", err.code)),
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
mod utils;

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

use fs_err as fs;
use parse_display::Display;
Expand Down Expand Up @@ -161,10 +161,9 @@ fn test_unpack_rar_single(input: &Path) -> Result<(), Box<dyn std::error::Error>
fn unpack_rar() -> Result<(), Box<dyn std::error::Error>> {
let mut datadir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?);
datadir.push("tests/data");
["testfile.rar3.rar.gz", "testfile.rar5.rar"].iter()
.try_for_each(|path| {
test_unpack_rar_single(&datadir.join(path))
})?;
["testfile.rar3.rar.gz", "testfile.rar5.rar"]
.iter()
.try_for_each(|path| test_unpack_rar_single(&datadir.join(path)))?;

Ok(())
}
}

0 comments on commit 0e287d3

Please sign in to comment.