diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 666a0e982..23269017d 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -140,6 +140,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let check = matches.get_flag(options::CHECK); + let check_flag = |flag| match (check, matches.get_flag(flag)) { + (_, false) => Ok(false), + (true, true) => Ok(true), + (false, true) => Err(ChecksumError::CheckOnlyFlag(flag.into())), + }; + + // Each of the following flags are only expected in --check mode. + // If we encounter them otherwise, end with an error. + let ignore_missing = check_flag(options::IGNORE_MISSING)?; + let warn = check_flag(options::WARN)?; + let quiet = check_flag(options::QUIET)?; + let strict = check_flag(options::STRICT)?; + let status = check_flag(options::STATUS)?; + let algo_cli = matches .get_one::(options::ALGORITHM) .map(AlgoKind::from_cksum) @@ -166,11 +180,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let text_flag = matches.get_flag(options::TEXT); let binary_flag = matches.get_flag(options::BINARY); - let strict = matches.get_flag(options::STRICT); - let status = matches.get_flag(options::STATUS); - let warn = matches.get_flag(options::WARN); - let ignore_missing = matches.get_flag(options::IGNORE_MISSING); - let quiet = matches.get_flag(options::QUIET); let tag = matches.get_flag(options::TAG); if tag || binary_flag || text_flag { @@ -191,6 +200,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Not --check + // Print hardware debug info if requested + if matches.get_flag(options::DEBUG) { + print_cpu_debug_info(); + } + // Set the default algorithm to CRC when not '--check'ing. let algo_kind = algo_cli.unwrap_or(AlgoKind::Crc); @@ -199,22 +213,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let algo = SizedAlgoKind::from_unsized(algo_kind, length)?; let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO)); - let output_format = figure_out_output_format( - algo, - tag, - binary, - matches.get_flag(options::RAW), - matches.get_flag(options::BASE64), - ); - - // Print hardware debug info if requested - if matches.get_flag(options::DEBUG) { - print_cpu_debug_info(); - } - let opts = ChecksumComputeOptions { algo_kind: algo, - output_format, + output_format: figure_out_output_format( + algo, + tag, + binary, + matches.get_flag(options::RAW), + matches.get_flag(options::BASE64), + ), line_ending, }; diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index a096238f9..047d6889c 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -164,16 +164,27 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { binary_flag_default }; let check = matches.get_flag("check"); - let status = matches.get_flag("status"); - let quiet = matches.get_flag("quiet"); - let strict = matches.get_flag("strict"); - let warn = matches.get_flag("warn"); - let ignore_missing = matches.get_flag("ignore-missing"); - if ignore_missing && !check { - // --ignore-missing needs -c - return Err(ChecksumError::IgnoreNotCheck.into()); - } + let check_flag = |flag| match (check, matches.get_flag(flag)) { + (_, false) => Ok(false), + (true, true) => Ok(true), + (false, true) => Err(ChecksumError::CheckOnlyFlag(flag.into())), + }; + + // Each of the following flags are only expected in --check mode. + // If we encounter them otherwise, end with an error. + let ignore_missing = check_flag("ignore-missing")?; + let warn = check_flag("warn")?; + let quiet = check_flag("quiet")?; + let strict = check_flag("strict")?; + let status = check_flag("status")?; + + let files = matches.get_many::(options::FILE).map_or_else( + // No files given, read from stdin. + || Box::new(iter::once(OsStr::new("-"))) as Box>, + // At least one file given, read from them. + |files| Box::new(files.map(OsStr::new)) as Box>, + ); if check { // on Windows, allow --binary/--text to be used with --check @@ -188,13 +199,6 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { } } - // Execute the checksum validation based on the presence of files or the use of stdin - // Determine the source of input: a list of files or stdin. - let input = matches.get_many::(options::FILE).map_or_else( - || iter::once(OsStr::new("-")).collect::>(), - |files| files.map(OsStr::new).collect::>(), - ); - let verbose = ChecksumVerbose::new(status, quiet, warn); let opts = ChecksumValidateOptions { @@ -204,16 +208,11 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { }; // Execute the checksum validation - return perform_checksum_validation(input.iter().copied(), Some(algo_kind), length, opts); - } else if quiet { - return Err(ChecksumError::QuietNotCheck.into()); - } else if strict { - return Err(ChecksumError::StrictNotCheck.into()); + return perform_checksum_validation(files, Some(algo_kind), length, opts); } - let line_ending = LineEnding::from_zero_flag(matches.get_flag("zero")); - let algo = SizedAlgoKind::from_unsized(algo_kind, length)?; + let line_ending = LineEnding::from_zero_flag(matches.get_flag("zero")); let opts = ChecksumComputeOptions { algo_kind: algo, @@ -227,13 +226,6 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> { line_ending, }; - let files = matches.get_many::(options::FILE).map_or_else( - // No files given, read from stdin. - || Box::new(iter::once(OsStr::new("-"))) as Box>, - // At least one file given, read from them. - |files| Box::new(files.map(OsStr::new)) as Box>, - ); - // Show the hashsum of the input perform_checksum_computation(opts, files) } diff --git a/src/uucore/src/lib/features/checksum/mod.rs b/src/uucore/src/lib/features/checksum/mod.rs index 455a4e1bf..2f3d28b41 100644 --- a/src/uucore/src/lib/features/checksum/mod.rs +++ b/src/uucore/src/lib/features/checksum/mod.rs @@ -373,12 +373,9 @@ impl SizedAlgoKind { pub enum ChecksumError { #[error("the --raw option is not supported with multiple files")] RawMultipleFiles, - #[error("the --ignore-missing option is meaningful only when verifying checksums")] - IgnoreNotCheck, - #[error("the --strict option is meaningful only when verifying checksums")] - StrictNotCheck, - #[error("the --quiet option is meaningful only when verifying checksums")] - QuietNotCheck, + + #[error("the --{0} option is meaningful only when verifying checksums")] + CheckOnlyFlag(String), // --length sanitization errors #[error("--length required for {}", .0.quote())]