mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 13:07:46 +00:00
feat(checksum): extract file processing into a separate function
This commit is contained in:
parent
df0da55645
commit
afcf93b3e3
1 changed files with 122 additions and 88 deletions
|
@ -92,6 +92,25 @@ impl From<ChecksumError> for LineCheckError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::enum_variant_names)]
|
||||||
|
enum FileCheckError {
|
||||||
|
UError(Box<dyn UError>),
|
||||||
|
NonCriticalError,
|
||||||
|
CriticalError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Box<dyn UError>> for FileCheckError {
|
||||||
|
fn from(value: Box<dyn UError>) -> Self {
|
||||||
|
Self::UError(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ChecksumError> for FileCheckError {
|
||||||
|
fn from(value: ChecksumError) -> Self {
|
||||||
|
Self::UError(Box::new(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// This struct regroups CLI flags.
|
/// This struct regroups CLI flags.
|
||||||
#[derive(Debug, Default, Clone, Copy)]
|
#[derive(Debug, Default, Clone, Copy)]
|
||||||
pub struct ChecksumOptions {
|
pub struct ChecksumOptions {
|
||||||
|
@ -656,20 +675,12 @@ fn process_checksum_line(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
fn process_checksum_file(
|
||||||
* Do the checksum validation (can be strict or not)
|
filename_input: &OsStr,
|
||||||
*/
|
cli_algo_name: Option<&str>,
|
||||||
pub fn perform_checksum_validation<'a, I>(
|
cli_algo_length: Option<usize>,
|
||||||
files: I,
|
|
||||||
algo_name_input: Option<&str>,
|
|
||||||
length_input: Option<usize>,
|
|
||||||
opts: ChecksumOptions,
|
opts: ChecksumOptions,
|
||||||
) -> UResult<()>
|
) -> Result<(), FileCheckError> {
|
||||||
where
|
|
||||||
I: Iterator<Item = &'a OsStr>,
|
|
||||||
{
|
|
||||||
// if cksum has several input files, it will print the result for each file
|
|
||||||
for filename_input in files {
|
|
||||||
let mut correct_format = 0;
|
let mut correct_format = 0;
|
||||||
let mut properly_formatted = false;
|
let mut properly_formatted = false;
|
||||||
let mut res = ChecksumResult::default();
|
let mut res = ChecksumResult::default();
|
||||||
|
@ -685,7 +696,7 @@ where
|
||||||
// Could not read the file, show the error and continue to the next file
|
// Could not read the file, show the error and continue to the next file
|
||||||
show_error!("{e}");
|
show_error!("{e}");
|
||||||
set_exit_code(1);
|
set_exit_code(1);
|
||||||
continue;
|
return Err(FileCheckError::NonCriticalError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -699,7 +710,7 @@ where
|
||||||
};
|
};
|
||||||
show_error!("{e}");
|
show_error!("{e}");
|
||||||
set_exit_code(1);
|
set_exit_code(1);
|
||||||
continue;
|
return Err(FileCheckError::NonCriticalError);
|
||||||
};
|
};
|
||||||
|
|
||||||
for (i, line) in lines.iter().enumerate() {
|
for (i, line) in lines.iter().enumerate() {
|
||||||
|
@ -710,14 +721,14 @@ where
|
||||||
&chosen_regex,
|
&chosen_regex,
|
||||||
is_algo_based_format,
|
is_algo_based_format,
|
||||||
&mut res,
|
&mut res,
|
||||||
algo_name_input,
|
cli_algo_name,
|
||||||
length_input,
|
cli_algo_length,
|
||||||
&mut properly_formatted,
|
&mut properly_formatted,
|
||||||
&mut correct_format,
|
&mut correct_format,
|
||||||
opts,
|
opts,
|
||||||
) {
|
) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(LineCheckError::UError(e)) => return Err(e),
|
Err(LineCheckError::UError(e)) => return Err(e.into()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -731,8 +742,7 @@ where
|
||||||
.into());
|
.into());
|
||||||
}
|
}
|
||||||
set_exit_code(1);
|
set_exit_code(1);
|
||||||
|
return Err(FileCheckError::CriticalError);
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if any incorrectly formatted line, show it
|
// if any incorrectly formatted line, show it
|
||||||
|
@ -759,6 +769,30 @@ where
|
||||||
if (res.failed_cksum > 0 || res.failed_open_file > 0) && !opts.ignore_missing {
|
if (res.failed_cksum > 0 || res.failed_open_file > 0) && !opts.ignore_missing {
|
||||||
set_exit_code(1);
|
set_exit_code(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Do the checksum validation (can be strict or not)
|
||||||
|
*/
|
||||||
|
pub fn perform_checksum_validation<'a, I>(
|
||||||
|
files: I,
|
||||||
|
algo_name_input: Option<&str>,
|
||||||
|
length_input: Option<usize>,
|
||||||
|
opts: ChecksumOptions,
|
||||||
|
) -> UResult<()>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = &'a OsStr>,
|
||||||
|
{
|
||||||
|
// if cksum has several input files, it will print the result for each file
|
||||||
|
for filename_input in files {
|
||||||
|
use FileCheckError::*;
|
||||||
|
match process_checksum_file(filename_input, algo_name_input, length_input, opts) {
|
||||||
|
Err(UError(e)) => return Err(e),
|
||||||
|
Err(CriticalError) => break,
|
||||||
|
Err(NonCriticalError) | Ok(_) => continue,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue