From d96ae60d21dd94a110cb96caef1bf80014ac7f5f Mon Sep 17 00:00:00 2001 From: Dorian Peron Date: Tue, 16 Dec 2025 16:28:26 +0100 Subject: [PATCH 1/2] checksum: Unify the handling of check-only flags --- src/uu/cksum/src/cksum.rs | 45 ++++++++++-------- src/uu/hashsum/src/hashsum.rs | 52 +++++++++------------ src/uucore/src/lib/features/checksum/mod.rs | 9 ++-- 3 files changed, 51 insertions(+), 55 deletions(-) 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())] From 74f12d5d3babe95b3e26e109a91de436fde89419 Mon Sep 17 00:00:00 2001 From: Dorian Peron Date: Sat, 20 Dec 2025 19:29:06 +0100 Subject: [PATCH 2/2] cksum: remove unneeded `hex` dependency --- Cargo.lock | 1 - fuzz/Cargo.lock | 1 - src/uu/cksum/Cargo.toml | 1 - src/uu/cksum/src/cksum.rs | 6 +++--- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37b3362e6..4a28fb1dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3126,7 +3126,6 @@ dependencies = [ "clap", "codspeed-divan-compat", "fluent", - "hex", "tempfile", "uucore", ] diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 90934a271..2b519a989 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -1575,7 +1575,6 @@ version = "0.5.0" dependencies = [ "clap", "fluent", - "hex", "uucore", ] diff --git a/src/uu/cksum/Cargo.toml b/src/uu/cksum/Cargo.toml index 7e62c5c8f..840397273 100644 --- a/src/uu/cksum/Cargo.toml +++ b/src/uu/cksum/Cargo.toml @@ -25,7 +25,6 @@ uucore = { workspace = true, features = [ "sum", "hardware", ] } -hex = { workspace = true } fluent = { workspace = true } [dev-dependencies] diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 23269017d..30eabcaac 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -22,7 +22,7 @@ use uucore::checksum::{ use uucore::error::UResult; use uucore::hardware::{HasHardwareFeatures as _, SimdPolicy}; use uucore::line_ending::LineEnding; -use uucore::{format_usage, translate}; +use uucore::{format_usage, show_error, translate}; /// Print CPU hardware capability detection information to stderr /// This matches GNU cksum's --debug behavior @@ -31,9 +31,9 @@ fn print_cpu_debug_info() { fn print_feature(name: &str, available: bool) { if available { - eprintln!("cksum: using {name} hardware support"); + show_error!("using {name} hardware support"); } else { - eprintln!("cksum: {name} support not detected"); + show_error!("{name} support not detected"); } }