1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

feat(checksum): extract file processing into a separate function

This commit is contained in:
Dorian Péron 2024-10-25 03:17:17 +02:00 committed by Sylvestre Ledru
parent df0da55645
commit afcf93b3e3

View file

@ -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.
#[derive(Debug, Default, Clone, Copy)]
pub struct ChecksumOptions {
@ -656,20 +675,12 @@ fn process_checksum_line(
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>,
fn process_checksum_file(
filename_input: &OsStr,
cli_algo_name: Option<&str>,
cli_algo_length: 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 {
) -> Result<(), FileCheckError> {
let mut correct_format = 0;
let mut properly_formatted = false;
let mut res = ChecksumResult::default();
@ -685,7 +696,7 @@ where
// Could not read the file, show the error and continue to the next file
show_error!("{e}");
set_exit_code(1);
continue;
return Err(FileCheckError::NonCriticalError);
}
}
};
@ -699,7 +710,7 @@ where
};
show_error!("{e}");
set_exit_code(1);
continue;
return Err(FileCheckError::NonCriticalError);
};
for (i, line) in lines.iter().enumerate() {
@ -710,14 +721,14 @@ where
&chosen_regex,
is_algo_based_format,
&mut res,
algo_name_input,
length_input,
cli_algo_name,
cli_algo_length,
&mut properly_formatted,
&mut correct_format,
opts,
) {
Ok(_) => (),
Err(LineCheckError::UError(e)) => return Err(e),
Err(LineCheckError::UError(e)) => return Err(e.into()),
};
}
@ -731,8 +742,7 @@ where
.into());
}
set_exit_code(1);
return Ok(());
return Err(FileCheckError::CriticalError);
}
// 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 {
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(())