feat(checksum): group flags under a ChecksumOptions struct

This commit is contained in:
Dorian Péron 2024-10-25 02:43:39 +02:00 committed by Sylvestre Ledru
parent 1d9e162637
commit 07b9fb8a20
3 changed files with 43 additions and 34 deletions

View file

@ -13,8 +13,8 @@ use std::iter;
use std::path::Path; use std::path::Path;
use uucore::checksum::{ use uucore::checksum::{
calculate_blake2b_length, detect_algo, digest_reader, perform_checksum_validation, calculate_blake2b_length, detect_algo, digest_reader, perform_checksum_validation,
ChecksumError, ALGORITHM_OPTIONS_BLAKE2B, ALGORITHM_OPTIONS_BSD, ALGORITHM_OPTIONS_CRC, ChecksumError, ChecksumOptions, ALGORITHM_OPTIONS_BLAKE2B, ALGORITHM_OPTIONS_BSD,
ALGORITHM_OPTIONS_SYSV, SUPPORTED_ALGORITHMS, ALGORITHM_OPTIONS_CRC, ALGORITHM_OPTIONS_SYSV, SUPPORTED_ALGORITHMS,
}; };
use uucore::{ use uucore::{
encoding, encoding,
@ -318,17 +318,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|| iter::once(OsStr::new("-")).collect::<Vec<_>>(), || iter::once(OsStr::new("-")).collect::<Vec<_>>(),
|files| files.map(OsStr::new).collect::<Vec<_>>(), |files| files.map(OsStr::new).collect::<Vec<_>>(),
); );
return perform_checksum_validation( let opts = ChecksumOptions {
files.iter().copied(), binary: binary_flag,
strict,
status,
warn,
binary_flag,
ignore_missing, ignore_missing,
quiet, quiet,
algo_option, status,
length, strict,
); warn,
};
return perform_checksum_validation(files.iter().copied(), algo_option, length, opts);
} }
let (tag, asterisk) = handle_tag_text_binary_flags(&matches)?; let (tag, asterisk) = handle_tag_text_binary_flags(&matches)?;

View file

@ -23,6 +23,7 @@ use uucore::checksum::digest_reader;
use uucore::checksum::escape_filename; use uucore::checksum::escape_filename;
use uucore::checksum::perform_checksum_validation; use uucore::checksum::perform_checksum_validation;
use uucore::checksum::ChecksumError; use uucore::checksum::ChecksumError;
use uucore::checksum::ChecksumOptions;
use uucore::checksum::HashAlgorithm; use uucore::checksum::HashAlgorithm;
use uucore::error::{FromIo, UResult}; use uucore::error::{FromIo, UResult};
use uucore::sum::{Digest, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Shake128, Shake256}; use uucore::sum::{Digest, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Shake128, Shake256};
@ -239,18 +240,21 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
|| iter::once(OsStr::new("-")).collect::<Vec<_>>(), || iter::once(OsStr::new("-")).collect::<Vec<_>>(),
|files| files.map(OsStr::new).collect::<Vec<_>>(), |files| files.map(OsStr::new).collect::<Vec<_>>(),
); );
let opts = ChecksumOptions {
binary,
ignore_missing,
quiet,
status,
strict,
warn,
};
// Execute the checksum validation // Execute the checksum validation
return perform_checksum_validation( return perform_checksum_validation(
input.iter().copied(), input.iter().copied(),
strict,
status,
warn,
binary,
ignore_missing,
quiet,
Some(algo.name), Some(algo.name),
Some(algo.bits), Some(algo.bits),
opts,
); );
} else if quiet { } else if quiet {
return Err(ChecksumError::QuietNotCheck.into()); return Err(ChecksumError::QuietNotCheck.into());

View file

@ -75,6 +75,17 @@ struct ChecksumResult {
pub failed_open_file: i32, pub failed_open_file: i32,
} }
/// This struct regroups CLI flags.
#[derive(Debug, Default, Clone, Copy)]
pub struct ChecksumOptions {
pub binary: bool,
pub ignore_missing: bool,
pub quiet: bool,
pub status: bool,
pub strict: bool,
pub warn: bool,
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ChecksumError { pub enum ChecksumError {
#[error("the --raw option is not supported with multiple files")] #[error("the --raw option is not supported with multiple files")]
@ -505,17 +516,11 @@ fn identify_algo_name_and_length(
/*** /***
* Do the checksum validation (can be strict or not) * Do the checksum validation (can be strict or not)
*/ */
#[allow(clippy::too_many_arguments)]
pub fn perform_checksum_validation<'a, I>( pub fn perform_checksum_validation<'a, I>(
files: I, files: I,
strict: bool,
status: bool,
warn: bool,
binary: bool,
ignore_missing: bool,
quiet: bool,
algo_name_input: Option<&str>, algo_name_input: Option<&str>,
length_input: Option<usize>, length_input: Option<usize>,
opts: ChecksumOptions,
) -> UResult<()> ) -> UResult<()>
where where
I: Iterator<Item = &'a OsStr>, I: Iterator<Item = &'a OsStr>,
@ -610,7 +615,8 @@ where
// manage the input file // manage the input file
let file_to_check = let file_to_check =
match get_file_to_check(&real_filename_to_check, ignore_missing, &mut res) { match get_file_to_check(&real_filename_to_check, opts.ignore_missing, &mut res)
{
Some(file) => file, Some(file) => file,
None => continue, None => continue,
}; };
@ -619,11 +625,11 @@ where
let create_fn = &mut algo.create_fn; let create_fn = &mut algo.create_fn;
let mut digest = create_fn(); let mut digest = create_fn();
let (calculated_checksum, _) = let (calculated_checksum, _) =
digest_reader(&mut digest, &mut file_reader, binary, algo.bits).unwrap(); digest_reader(&mut digest, &mut file_reader, opts.binary, algo.bits).unwrap();
// Do the checksum validation // Do the checksum validation
if expected_checksum == calculated_checksum { if expected_checksum == calculated_checksum {
if !quiet && !status { if !opts.quiet && !opts.status {
print_file_report( print_file_report(
std::io::stdout(), std::io::stdout(),
filename_to_check, filename_to_check,
@ -633,7 +639,7 @@ where
} }
correct_format += 1; correct_format += 1;
} else { } else {
if !status { if !opts.status {
print_file_report( print_file_report(
std::io::stdout(), std::io::stdout(),
filename_to_check, filename_to_check,
@ -648,7 +654,7 @@ where
// Don't show any warning for empty or commented lines. // Don't show any warning for empty or commented lines.
continue; continue;
} }
if warn { if opts.warn {
let algo = if let Some(algo_name_input) = algo_name_input { let algo = if let Some(algo_name_input) = algo_name_input {
algo_name_input.to_uppercase() algo_name_input.to_uppercase()
} else { } else {
@ -670,7 +676,7 @@ where
// not a single line correctly formatted found // not a single line correctly formatted found
// return an error // return an error
if !properly_formatted { if !properly_formatted {
if !status { if !opts.status {
return Err(ChecksumError::NoProperlyFormattedChecksumLinesFound { return Err(ChecksumError::NoProperlyFormattedChecksumLinesFound {
filename: get_filename_for_output(filename_input, input_is_stdin), filename: get_filename_for_output(filename_input, input_is_stdin),
} }
@ -682,9 +688,9 @@ where
} }
// if any incorrectly formatted line, show it // if any incorrectly formatted line, show it
cksum_output(&res, status); cksum_output(&res, opts.status);
if ignore_missing && correct_format == 0 { if opts.ignore_missing && correct_format == 0 {
// we have only bad format // we have only bad format
// and we had ignore-missing // and we had ignore-missing
eprintln!( eprintln!(
@ -696,13 +702,13 @@ where
} }
// strict means that we should have an exit code. // strict means that we should have an exit code.
if strict && res.bad_format > 0 { if opts.strict && res.bad_format > 0 {
set_exit_code(1); set_exit_code(1);
} }
// if we have any failed checksum verification, we set an exit code // if we have any failed checksum verification, we set an exit code
// except if we have ignore_missing // except if we have ignore_missing
if (res.failed_cksum > 0 || res.failed_open_file > 0) && !ignore_missing { if (res.failed_cksum > 0 || res.failed_open_file > 0) && !opts.ignore_missing {
set_exit_code(1); set_exit_code(1);
} }
} }