mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 19:45:29 +00:00
style: cargo fmt
This commit is contained in:
parent
687662c153
commit
86d855b9a0
@ -8,7 +8,12 @@ use crate::{error::Error, list::FileInArchive, utils::logger::info};
|
||||
|
||||
/// 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, password: Option<impl AsRef<[u8]>>, quiet: bool) -> crate::Result<usize> {
|
||||
pub fn unpack_archive(
|
||||
archive_path: &Path,
|
||||
output_folder: &Path,
|
||||
password: Option<impl AsRef<[u8]>>,
|
||||
quiet: bool,
|
||||
) -> crate::Result<usize> {
|
||||
assert!(output_folder.read_dir().expect("dir exists").count() == 0);
|
||||
|
||||
let password = password.as_ref().map(|p| p.as_ref());
|
||||
@ -42,15 +47,16 @@ pub fn unpack_archive(archive_path: &Path, output_folder: &Path, password: Optio
|
||||
}
|
||||
|
||||
/// List contents of `archive_path`, returning a vector of archive entries
|
||||
pub fn list_archive(archive_path: &Path, password: Option<impl AsRef<[u8]>>) -> impl Iterator<Item = crate::Result<FileInArchive>> {
|
||||
pub fn list_archive(
|
||||
archive_path: &Path,
|
||||
password: Option<impl AsRef<[u8]>>,
|
||||
) -> impl Iterator<Item = crate::Result<FileInArchive>> {
|
||||
let password = password.as_ref().map(|p| p.as_ref());
|
||||
let archive = match password {
|
||||
Some(password) => Archive::with_password(archive_path, password),
|
||||
None => Archive::new(archive_path),
|
||||
};
|
||||
archive.open_for_listing()
|
||||
.expect("cannot open archive")
|
||||
.map(|item| {
|
||||
archive.open_for_listing().expect("cannot open archive").map(|item| {
|
||||
let item = item?;
|
||||
let is_dir = item.is_directory();
|
||||
let path = item.filename;
|
||||
|
@ -5,21 +5,21 @@ use std::{
|
||||
io::{self, Read, Seek, Write},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use bstr::ByteSlice;
|
||||
|
||||
use bstr::ByteSlice;
|
||||
use fs_err as fs;
|
||||
use same_file::Handle;
|
||||
use sevenz_rust::SevenZArchiveEntry;
|
||||
|
||||
use crate::{
|
||||
error::FinalError,
|
||||
list::FileInArchive,
|
||||
utils::{
|
||||
self, cd_into_same_dir_as,
|
||||
logger::{info, warning},
|
||||
Bytes, EscapedPathDisplay, FileVisibilityPolicy,
|
||||
},
|
||||
};
|
||||
use crate::list::FileInArchive;
|
||||
|
||||
pub fn compress_sevenz<W>(
|
||||
files: &[PathBuf],
|
||||
@ -99,7 +99,12 @@ where
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
pub fn decompress_sevenz<R>(reader: R, output_path: &Path, password: Option<impl AsRef<[u8]>>, quiet: bool) -> crate::Result<usize>
|
||||
pub fn decompress_sevenz<R>(
|
||||
reader: R,
|
||||
output_path: &Path,
|
||||
password: Option<impl AsRef<[u8]>>,
|
||||
quiet: bool,
|
||||
) -> crate::Result<usize>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
@ -159,7 +164,12 @@ where
|
||||
let password = password.as_ref().map(|p| p.as_ref());
|
||||
|
||||
match password {
|
||||
Some(password) => sevenz_rust::decompress_with_extract_fn_and_password(reader, output_path, sevenz_rust::Password::from(password.to_str().unwrap()), entry_extract_fn)?,
|
||||
Some(password) => sevenz_rust::decompress_with_extract_fn_and_password(
|
||||
reader,
|
||||
output_path,
|
||||
sevenz_rust::Password::from(password.to_str().unwrap()),
|
||||
entry_extract_fn,
|
||||
)?,
|
||||
None => sevenz_rust::decompress_with_extract_fn(reader, output_path, entry_extract_fn)?,
|
||||
};
|
||||
|
||||
@ -167,8 +177,10 @@ where
|
||||
}
|
||||
|
||||
/// List contents of `archive_path`, returning a vector of archive entries
|
||||
pub fn list_archive(archive_path: &Path, password: Option<impl AsRef<[u8]>>) -> impl Iterator<Item = crate::Result<FileInArchive>>
|
||||
{
|
||||
pub fn list_archive(
|
||||
archive_path: &Path,
|
||||
password: Option<impl AsRef<[u8]>>,
|
||||
) -> impl Iterator<Item = crate::Result<FileInArchive>> {
|
||||
let reader = fs::File::open(archive_path).unwrap();
|
||||
let password = password.as_ref().map(|p| p.as_ref());
|
||||
|
||||
@ -183,7 +195,13 @@ pub fn list_archive(archive_path: &Path, password: Option<impl AsRef<[u8]>>) ->
|
||||
};
|
||||
|
||||
match password {
|
||||
Some(password) => sevenz_rust::decompress_with_extract_fn_and_password(reader, ".", sevenz_rust::Password::from(password.to_str().unwrap()), entry_extract_fn).unwrap(),
|
||||
Some(password) => sevenz_rust::decompress_with_extract_fn_and_password(
|
||||
reader,
|
||||
".",
|
||||
sevenz_rust::Password::from(password.to_str().unwrap()),
|
||||
entry_extract_fn,
|
||||
)
|
||||
.unwrap(),
|
||||
None => sevenz_rust::decompress_with_extract_fn(reader, ".", entry_extract_fn).unwrap(),
|
||||
};
|
||||
|
||||
|
@ -9,8 +9,8 @@ use std::{
|
||||
sync::mpsc,
|
||||
thread,
|
||||
};
|
||||
use bstr::ByteSlice;
|
||||
|
||||
use bstr::ByteSlice;
|
||||
use filetime_creation::{set_file_mtime, FileTime};
|
||||
use fs_err as fs;
|
||||
use same_file::Handle;
|
||||
@ -29,7 +29,12 @@ use crate::{
|
||||
|
||||
/// Unpacks the archive given by `archive` into the folder given by `output_folder`.
|
||||
/// Assumes that output_folder is empty
|
||||
pub fn unpack_archive<R>(mut archive: ZipArchive<R>, output_folder: &Path, password: Option<impl AsRef<[u8]>>, quiet: bool) -> crate::Result<usize>
|
||||
pub fn unpack_archive<R>(
|
||||
mut archive: ZipArchive<R>,
|
||||
output_folder: &Path,
|
||||
password: Option<impl AsRef<[u8]>>,
|
||||
quiet: bool,
|
||||
) -> crate::Result<usize>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
@ -41,7 +46,9 @@ where
|
||||
|
||||
for idx in 0..archive.len() {
|
||||
let mut file = match password.clone() {
|
||||
Some(password) => archive.by_index_decrypt(idx, password.to_owned().as_bytes()).unwrap()
|
||||
Some(password) => archive
|
||||
.by_index_decrypt(idx, password.to_owned().as_bytes())
|
||||
.unwrap()
|
||||
.map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file"))?,
|
||||
None => archive.by_index(idx)?,
|
||||
};
|
||||
@ -99,7 +106,10 @@ where
|
||||
}
|
||||
|
||||
/// List contents of `archive`, returning a vector of archive entries
|
||||
pub fn list_archive<R>(mut archive: ZipArchive<R>, password: Option<impl AsRef<[u8]>>) -> impl Iterator<Item = crate::Result<FileInArchive>>
|
||||
pub fn list_archive<R>(
|
||||
mut archive: ZipArchive<R>,
|
||||
password: Option<impl AsRef<[u8]>>,
|
||||
) -> impl Iterator<Item = crate::Result<FileInArchive>>
|
||||
where
|
||||
R: Read + Seek + Send + 'static,
|
||||
{
|
||||
@ -119,7 +129,9 @@ where
|
||||
for idx in 0..archive.len() {
|
||||
let maybe_file_in_archive = (|| {
|
||||
let zip_result = match password.clone() {
|
||||
Some(password) => archive.by_index_decrypt(idx, password.to_owned().clone().as_bytes()).unwrap()
|
||||
Some(password) => archive
|
||||
.by_index_decrypt(idx, password.to_owned().clone().as_bytes())
|
||||
.unwrap()
|
||||
.map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file")),
|
||||
None => archive.by_index(idx),
|
||||
};
|
||||
|
@ -173,7 +173,9 @@ pub fn decompress_file(
|
||||
let unpack_fn: Box<dyn FnOnce(&Path) -> UnpackResult> = if formats.len() > 1 || input_is_stdin {
|
||||
let mut temp_file = tempfile::NamedTempFile::new()?;
|
||||
io::copy(&mut reader, &mut temp_file)?;
|
||||
Box::new(move |output_dir| crate::archive::rar::unpack_archive(temp_file.path(), output_dir, password, quiet))
|
||||
Box::new(move |output_dir| {
|
||||
crate::archive::rar::unpack_archive(temp_file.path(), output_dir, password, quiet)
|
||||
})
|
||||
} else {
|
||||
Box::new(|output_dir| crate::archive::rar::unpack_archive(input_file_path, output_dir, password, quiet))
|
||||
};
|
||||
@ -206,7 +208,9 @@ pub fn decompress_file(
|
||||
io::copy(&mut reader, &mut vec)?;
|
||||
|
||||
if let ControlFlow::Continue(files) = smart_unpack(
|
||||
|output_dir| crate::archive::sevenz::decompress_sevenz(io::Cursor::new(vec), output_dir, password, quiet),
|
||||
|output_dir| {
|
||||
crate::archive::sevenz::decompress_sevenz(io::Cursor::new(vec), output_dir, password, quiet)
|
||||
},
|
||||
output_dir,
|
||||
&output_file_path,
|
||||
question_policy,
|
||||
|
@ -6,13 +6,13 @@ use std::{
|
||||
use fs_err as fs;
|
||||
|
||||
use crate::{
|
||||
archive::sevenz,
|
||||
commands::warn_user_about_loading_zip_in_memory,
|
||||
extension::CompressionFormat::{self, *},
|
||||
list::{self, FileInArchive, ListOptions},
|
||||
utils::{io::lock_and_flush_output_stdio, user_wants_to_continue},
|
||||
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
||||
};
|
||||
use crate::archive::sevenz;
|
||||
|
||||
/// File at input_file_path is opened for reading, example: "archive.tar.gz"
|
||||
/// formats contains each format necessary for decompression, example: [Gz, Tar] (in decompression order)
|
||||
|
@ -222,7 +222,13 @@ pub fn run(
|
||||
println!();
|
||||
}
|
||||
let formats = extension::flatten_compression_formats(&formats);
|
||||
list_archive_contents(archive_path, formats, list_options, question_policy, args.password.as_deref())?;
|
||||
list_archive_contents(
|
||||
archive_path,
|
||||
formats,
|
||||
list_options,
|
||||
question_policy,
|
||||
args.password.as_deref(),
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
Loading…
x
Reference in New Issue
Block a user