mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 20:15:27 +00:00
feat: cleanup codes and more error handling
This commit is contained in:
parent
76a459e02e
commit
f2b984bc7a
@ -1,4 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
env::current_dir,
|
||||||
io::{self, BufWriter, Cursor, Seek, Write},
|
io::{self, BufWriter, Cursor, Seek, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
@ -13,8 +14,6 @@ use crate::{
|
|||||||
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::copy_recursively;
|
|
||||||
|
|
||||||
/// Compress files into `output_file`.
|
/// Compress files into `output_file`.
|
||||||
///
|
///
|
||||||
/// # Arguments:
|
/// # Arguments:
|
||||||
@ -129,18 +128,28 @@ pub fn compress_files(
|
|||||||
return Ok(false);
|
return Ok(false);
|
||||||
},
|
},
|
||||||
SevenZip => {
|
SevenZip => {
|
||||||
let tmpdir = tempfile::tempdir()?;
|
let mut writer =
|
||||||
|
sevenz_rust::SevenZWriter::create(output_path).map_err(|e| crate::Error::SevenzipError(e))?;
|
||||||
|
|
||||||
for filep in files.iter() {
|
for filep in files.iter() {
|
||||||
if filep.is_dir() {
|
writer
|
||||||
copy_recursively(filep, tmpdir.path()
|
.push_archive_entry::<std::fs::File>(
|
||||||
.join(filep.strip_prefix(std::env::current_dir()?).expect("copy folder error")))?;
|
sevenz_rust::SevenZWriter::<std::fs::File>::create_archive_entry(
|
||||||
} else {
|
filep,
|
||||||
fs::copy(filep, tmpdir.path().join(filep.file_name().expect("no filename in file")))?;
|
filep
|
||||||
}
|
.strip_prefix(current_dir()?)
|
||||||
|
.expect("StripPrefix Failed")
|
||||||
|
.as_os_str()
|
||||||
|
.to_str()
|
||||||
|
.unwrap()
|
||||||
|
.to_string(),
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.map_err(|e| crate::Error::SevenzipError(e))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
sevenz_rust::compress_to_path(tmpdir.path(), output_path).expect("can't compress 7zip archive");
|
writer.finish()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,11 +165,7 @@ pub fn decompress_file(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
SevenZip => {
|
SevenZip => {
|
||||||
sevenz_rust::decompress_file(input_file_path, output_dir).map_err(
|
sevenz_rust::decompress_file(input_file_path, output_dir).map_err(|x| crate::Error::SevenzipError(x))?;
|
||||||
|x| {
|
|
||||||
crate::Error::SevenzipError(x)
|
|
||||||
}
|
|
||||||
)?;
|
|
||||||
fs::read_dir(output_dir)?.count()
|
fs::read_dir(output_dir)?.count()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
cell::RefCell,
|
||||||
io::{self, BufReader, Read},
|
io::{self, BufReader, Read},
|
||||||
path::Path,
|
path::Path,
|
||||||
cell::RefCell
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
@ -92,11 +92,13 @@ pub fn list_archive_contents(
|
|||||||
let a = RefCell::new(Vec::new());
|
let a = RefCell::new(Vec::new());
|
||||||
|
|
||||||
sevenz_rust::decompress_file_with_extract_fn(archive_path, ".", |entry, _, _| {
|
sevenz_rust::decompress_file_with_extract_fn(archive_path, ".", |entry, _, _| {
|
||||||
a.borrow_mut().push(Ok(FileInArchive{path: entry.name().into(), is_dir: entry.is_directory()}));
|
a.borrow_mut().push(Ok(FileInArchive {
|
||||||
|
path: entry.name().into(),
|
||||||
|
is_dir: entry.is_directory(),
|
||||||
|
}));
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}).map_err(
|
})
|
||||||
|e| crate::Error::SevenzipError(e)
|
.map_err(|e| crate::Error::SevenzipError(e))?;
|
||||||
)?;
|
|
||||||
Box::new(a.into_inner().into_iter())
|
Box::new(a.into_inner().into_iter())
|
||||||
}
|
}
|
||||||
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
|
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
|
||||||
|
@ -4,7 +4,7 @@ mod compress;
|
|||||||
mod decompress;
|
mod decompress;
|
||||||
mod list;
|
mod list;
|
||||||
|
|
||||||
use std::{ops::ControlFlow, path::{PathBuf, Path}, fs};
|
use std::{ops::ControlFlow, path::PathBuf};
|
||||||
|
|
||||||
use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
|
use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
|
||||||
use utils::colors;
|
use utils::colors;
|
||||||
@ -21,21 +21,6 @@ use crate::{
|
|||||||
warning, CliArgs, QuestionPolicy,
|
warning, CliArgs, QuestionPolicy,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Copy files from source to destination recursively.
|
|
||||||
fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> std::io::Result<()> {
|
|
||||||
fs::create_dir_all(&destination)?;
|
|
||||||
for entry in fs::read_dir(source)? {
|
|
||||||
let entry = entry?;
|
|
||||||
let filetype = entry.file_type()?;
|
|
||||||
if filetype.is_dir() {
|
|
||||||
copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
|
|
||||||
} else {
|
|
||||||
fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Warn the user that (de)compressing this .zip archive might freeze their system.
|
/// Warn the user that (de)compressing this .zip archive might freeze their system.
|
||||||
fn warn_user_about_loading_zip_in_memory() {
|
fn warn_user_about_loading_zip_in_memory() {
|
||||||
const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = "\n\
|
const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = "\n\
|
||||||
|
@ -35,7 +35,7 @@ pub enum Error {
|
|||||||
/// Invalid format passed to `--format`
|
/// Invalid format passed to `--format`
|
||||||
InvalidFormat { reason: String },
|
InvalidFormat { reason: String },
|
||||||
/// From sevenz_rust::Error
|
/// From sevenz_rust::Error
|
||||||
SevenzipError(sevenz_rust::Error)
|
SevenzipError(sevenz_rust::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Alias to std's Result with ouch's Error
|
/// Alias to std's Result with ouch's Error
|
||||||
@ -141,9 +141,7 @@ impl fmt::Display for Error {
|
|||||||
Error::UnsupportedZipArchive(reason) => FinalError::with_title("Unsupported zip archive").detail(*reason),
|
Error::UnsupportedZipArchive(reason) => FinalError::with_title("Unsupported zip archive").detail(*reason),
|
||||||
Error::InvalidFormat { reason } => FinalError::with_title("Invalid archive format").detail(reason.clone()),
|
Error::InvalidFormat { reason } => FinalError::with_title("Invalid archive format").detail(reason.clone()),
|
||||||
Error::Custom { reason } => reason.clone(),
|
Error::Custom { reason } => reason.clone(),
|
||||||
Error::SevenzipError( reason ) => {
|
Error::SevenzipError(reason) => FinalError::with_title("7z error").detail(reason.to_string()),
|
||||||
FinalError::with_title("7z error").detail(reason.to_string())
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(f, "{err}")
|
write!(f, "{err}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user