mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +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`.
|
/// Unpacks the archive given by `archive_path` into the folder given by `output_folder`.
|
||||||
/// Assumes that output_folder is empty
|
/// 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);
|
assert!(output_folder.read_dir().expect("dir exists").count() == 0);
|
||||||
|
|
||||||
let password = password.as_ref().map(|p| p.as_ref());
|
let password = password.as_ref().map(|p| p.as_ref());
|
||||||
@ -42,21 +47,22 @@ pub fn unpack_archive(archive_path: &Path, output_folder: &Path, password: Optio
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// List contents of `archive_path`, returning a vector of archive entries
|
/// 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 password = password.as_ref().map(|p| p.as_ref());
|
||||||
let archive = match password {
|
let archive = match password {
|
||||||
Some(password) => Archive::with_password(archive_path, password),
|
Some(password) => Archive::with_password(archive_path, password),
|
||||||
None => Archive::new(archive_path),
|
None => Archive::new(archive_path),
|
||||||
};
|
};
|
||||||
archive.open_for_listing()
|
archive.open_for_listing().expect("cannot open archive").map(|item| {
|
||||||
.expect("cannot open archive")
|
let item = item?;
|
||||||
.map(|item| {
|
let is_dir = item.is_directory();
|
||||||
let item = item?;
|
let path = item.filename;
|
||||||
let is_dir = item.is_directory();
|
|
||||||
let path = item.filename;
|
|
||||||
|
|
||||||
Ok(FileInArchive { path, is_dir })
|
Ok(FileInArchive { path, is_dir })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn no_compression() -> Error {
|
pub fn no_compression() -> Error {
|
||||||
|
@ -5,21 +5,21 @@ use std::{
|
|||||||
io::{self, Read, Seek, Write},
|
io::{self, Read, Seek, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
use bstr::ByteSlice;
|
|
||||||
|
|
||||||
|
use bstr::ByteSlice;
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
use same_file::Handle;
|
use same_file::Handle;
|
||||||
use sevenz_rust::SevenZArchiveEntry;
|
use sevenz_rust::SevenZArchiveEntry;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::FinalError,
|
error::FinalError,
|
||||||
|
list::FileInArchive,
|
||||||
utils::{
|
utils::{
|
||||||
self, cd_into_same_dir_as,
|
self, cd_into_same_dir_as,
|
||||||
logger::{info, warning},
|
logger::{info, warning},
|
||||||
Bytes, EscapedPathDisplay, FileVisibilityPolicy,
|
Bytes, EscapedPathDisplay, FileVisibilityPolicy,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use crate::list::FileInArchive;
|
|
||||||
|
|
||||||
pub fn compress_sevenz<W>(
|
pub fn compress_sevenz<W>(
|
||||||
files: &[PathBuf],
|
files: &[PathBuf],
|
||||||
@ -99,7 +99,12 @@ where
|
|||||||
Ok(bytes)
|
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
|
where
|
||||||
R: Read + Seek,
|
R: Read + Seek,
|
||||||
{
|
{
|
||||||
@ -150,7 +155,7 @@ where
|
|||||||
Some(ft::FileTime::from_system_time(entry.last_modified_date().into())),
|
Some(ft::FileTime::from_system_time(entry.last_modified_date().into())),
|
||||||
Some(ft::FileTime::from_system_time(entry.creation_date().into())),
|
Some(ft::FileTime::from_system_time(entry.creation_date().into())),
|
||||||
)
|
)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(true)
|
Ok(true)
|
||||||
@ -159,7 +164,12 @@ where
|
|||||||
let password = password.as_ref().map(|p| p.as_ref());
|
let password = password.as_ref().map(|p| p.as_ref());
|
||||||
|
|
||||||
match password {
|
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)?,
|
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
|
/// 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 reader = fs::File::open(archive_path).unwrap();
|
||||||
let password = password.as_ref().map(|p| p.as_ref());
|
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 {
|
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(),
|
None => sevenz_rust::decompress_with_extract_fn(reader, ".", entry_extract_fn).unwrap(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ use std::{
|
|||||||
sync::mpsc,
|
sync::mpsc,
|
||||||
thread,
|
thread,
|
||||||
};
|
};
|
||||||
use bstr::ByteSlice;
|
|
||||||
|
|
||||||
|
use bstr::ByteSlice;
|
||||||
use filetime_creation::{set_file_mtime, FileTime};
|
use filetime_creation::{set_file_mtime, FileTime};
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
use same_file::Handle;
|
use same_file::Handle;
|
||||||
@ -29,7 +29,12 @@ use crate::{
|
|||||||
|
|
||||||
/// Unpacks the archive given by `archive` into the folder given by `output_folder`.
|
/// Unpacks the archive given by `archive` into the folder given by `output_folder`.
|
||||||
/// Assumes that output_folder is empty
|
/// 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
|
where
|
||||||
R: Read + Seek,
|
R: Read + Seek,
|
||||||
{
|
{
|
||||||
@ -41,7 +46,9 @@ where
|
|||||||
|
|
||||||
for idx in 0..archive.len() {
|
for idx in 0..archive.len() {
|
||||||
let mut file = match password.clone() {
|
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"))?,
|
.map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file"))?,
|
||||||
None => archive.by_index(idx)?,
|
None => archive.by_index(idx)?,
|
||||||
};
|
};
|
||||||
@ -99,7 +106,10 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// List contents of `archive`, returning a vector of archive entries
|
/// 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
|
where
|
||||||
R: Read + Seek + Send + 'static,
|
R: Read + Seek + Send + 'static,
|
||||||
{
|
{
|
||||||
@ -119,7 +129,9 @@ where
|
|||||||
for idx in 0..archive.len() {
|
for idx in 0..archive.len() {
|
||||||
let maybe_file_in_archive = (|| {
|
let maybe_file_in_archive = (|| {
|
||||||
let zip_result = match password.clone() {
|
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")),
|
.map_err(|_| zip::result::ZipError::UnsupportedArchive("Password required to decrypt file")),
|
||||||
None => archive.by_index(idx),
|
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 unpack_fn: Box<dyn FnOnce(&Path) -> UnpackResult> = if formats.len() > 1 || input_is_stdin {
|
||||||
let mut temp_file = tempfile::NamedTempFile::new()?;
|
let mut temp_file = tempfile::NamedTempFile::new()?;
|
||||||
io::copy(&mut reader, &mut temp_file)?;
|
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 {
|
} else {
|
||||||
Box::new(|output_dir| crate::archive::rar::unpack_archive(input_file_path, output_dir, password, quiet))
|
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)?;
|
io::copy(&mut reader, &mut vec)?;
|
||||||
|
|
||||||
if let ControlFlow::Continue(files) = smart_unpack(
|
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_dir,
|
||||||
&output_file_path,
|
&output_file_path,
|
||||||
question_policy,
|
question_policy,
|
||||||
|
@ -6,13 +6,13 @@ use std::{
|
|||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
archive::sevenz,
|
||||||
commands::warn_user_about_loading_zip_in_memory,
|
commands::warn_user_about_loading_zip_in_memory,
|
||||||
extension::CompressionFormat::{self, *},
|
extension::CompressionFormat::{self, *},
|
||||||
list::{self, FileInArchive, ListOptions},
|
list::{self, FileInArchive, ListOptions},
|
||||||
utils::{io::lock_and_flush_output_stdio, user_wants_to_continue},
|
utils::{io::lock_and_flush_output_stdio, user_wants_to_continue},
|
||||||
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
||||||
};
|
};
|
||||||
use crate::archive::sevenz;
|
|
||||||
|
|
||||||
/// File at input_file_path is opened for reading, example: "archive.tar.gz"
|
/// 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)
|
/// formats contains each format necessary for decompression, example: [Gz, Tar] (in decompression order)
|
||||||
|
@ -222,7 +222,13 @@ pub fn run(
|
|||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
let formats = extension::flatten_compression_formats(&formats);
|
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(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user