feat(cksum): improve debug output for single file operations
Some checks failed
CICD / Build (push) Blocked by required conditions
CICD / Style/cargo-deny (push) Waiting to run
CICD / Style/deps (push) Waiting to run
CICD / Documentation/warnings (push) Waiting to run
CICD / MinRustV (push) Waiting to run
CICD / Dependencies (push) Waiting to run
CICD / Build/Makefile (push) Blocked by required conditions
CICD / Build/stable (push) Blocked by required conditions
CICD / Build/nightly (push) Blocked by required conditions
CICD / Binary sizes (push) Blocked by required conditions
CICD / Tests/BusyBox test suite (push) Blocked by required conditions
CICD / Tests/Toybox test suite (push) Blocked by required conditions
CICD / Code Coverage (push) Waiting to run
CICD / Separate Builds (push) Waiting to run
CICD / Test all features separately (push) Blocked by required conditions
CICD / Build/SELinux (push) Blocked by required conditions
CICD / Build/SELinux-Stubs (Non-Linux) (push) Blocked by required conditions
CICD / Safe Traversal Security Check (push) Blocked by required conditions
GnuTests / Run GNU tests (native) (push) Waiting to run
GnuTests / Run GNU tests (SELinux) (push) Waiting to run
GnuTests / Aggregate GNU test results (push) Blocked by required conditions
Android / Test builds (push) Waiting to run
Benchmarks / Run benchmarks (CodSpeed) (push) Waiting to run
Code Quality / Style/format (push) Waiting to run
Code Quality / Style/lint (push) Waiting to run
Code Quality / Style/spelling (push) Waiting to run
Code Quality / Style/toml (push) Waiting to run
Code Quality / Style/Python (push) Waiting to run
Code Quality / Pre-commit hooks (push) Waiting to run
Devcontainer / Verify devcontainer (push) Waiting to run
FreeBSD / Style and Lint (push) Waiting to run
OpenBSD / Style and Lint (push) Waiting to run
OpenBSD / Tests (push) Waiting to run
WSL2 / Test (push) Waiting to run
FreeBSD / Tests (push) Waiting to run
CheckScripts / ShellScript/Check (push) Has been cancelled
CheckScripts / ShellScript/Format (push) Has been cancelled
Check uudoc Documentation Generation / Verify uudoc generates correct documentation (push) Has been cancelled

This commit is contained in:
naoNao89 2025-11-25 04:59:59 +07:00 committed by Dorian Péron
parent 5fd26c0671
commit d655eed489
5 changed files with 157 additions and 4 deletions

View file

@ -19,7 +19,12 @@ path = "src/cksum.rs"
[dependencies]
clap = { workspace = true }
uucore = { workspace = true, features = ["checksum", "encoding", "sum"] }
uucore = { workspace = true, features = [
"checksum",
"encoding",
"sum",
"hardware",
] }
hex = { workspace = true }
fluent = { workspace = true }

View file

@ -27,6 +27,7 @@ cksum-help-status = don't output anything, status code shows success
cksum-help-quiet = don't print OK for each successfully verified file
cksum-help-ignore-missing = don't fail or report status for missing files
cksum-help-zero = end each output line with NUL, not newline, and disable file name escaping
cksum-help-debug = print CPU hardware capability detection info used by cksum
# Error messages
cksum-error-is-directory = { $file }: Is a directory

View file

@ -27,6 +27,7 @@ cksum-help-status = ne rien afficher, le code de statut indique le succès
cksum-help-quiet = ne pas afficher OK pour chaque fichier vérifié avec succès
cksum-help-ignore-missing = ne pas échouer ou signaler le statut pour les fichiers manquants
cksum-help-zero = terminer chaque ligne de sortie avec NUL, pas un saut de ligne, et désactiver l'échappement des noms de fichiers
cksum-help-debug = afficher les informations de débogage sur la détection de la prise en charge matérielle du processeur
# Messages d'erreur
cksum-error-is-directory = { $file } : Est un répertoire

View file

@ -20,9 +20,34 @@ use uucore::checksum::{
sanitize_sha2_sha3_length_str,
};
use uucore::error::UResult;
use uucore::hardware::CpuFeatures;
use uucore::line_ending::LineEnding;
use uucore::{format_usage, translate};
/// Print CPU hardware capability detection information to stderr
/// This matches GNU cksum's --debug behavior
fn print_cpu_debug_info() {
let features = CpuFeatures::detect();
fn print_feature(name: &str, available: bool) {
if available {
eprintln!("cksum: using {name} hardware support");
} else {
eprintln!("cksum: {name} support not detected");
}
}
// x86/x86_64
print_feature("avx512", features.has_avx512());
print_feature("avx2", features.has_avx2());
print_feature("pclmul", features.has_pclmul());
// ARM aarch64
if cfg!(target_arch = "aarch64") {
print_feature("vmull", features.has_vmull());
}
}
mod options {
pub const ALGORITHM: &str = "algorithm";
pub const FILE: &str = "file";
@ -40,6 +65,7 @@ mod options {
pub const IGNORE_MISSING: &str = "ignore-missing";
pub const QUIET: &str = "quiet";
pub const ZERO: &str = "zero";
pub const DEBUG: &str = "debug";
}
/// cksum has a bunch of legacy behavior. We handle this in this function to
@ -181,6 +207,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
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,
@ -317,5 +348,11 @@ pub fn uu_app() -> Command {
.help(translate!("cksum-help-zero"))
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::DEBUG)
.long(options::DEBUG)
.help(translate!("cksum-help-debug"))
.action(ArgAction::SetTrue),
)
.after_help(translate!("cksum-after-help"))
}

View file

@ -10,9 +10,8 @@ use uutests::util::TestScenario;
use uutests::util::log_info;
use uutests::util_name;
const ALGOS: [&str; 12] = [
"sysv", "bsd", "crc", "crc32b", "md5", "sha1", "sha224", "sha256", "sha384", "sha512",
"blake2b", "sm3",
const ALGOS: [&str; 11] = [
"sysv", "bsd", "crc", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "blake2b", "sm3",
];
const SHA_LENGTHS: [u32; 4] = [224, 256, 384, 512];
@ -2876,3 +2875,113 @@ mod format_mix {
.stderr_contains("cksum: WARNING: 1 line is improperly formatted");
}
}
#[cfg(not(target_os = "android"))]
mod debug_flag {
use super::*;
#[test]
fn test_debug_flag() {
// Test with default CRC algorithm - should output CPU feature detection
new_ucmd!()
.arg("--debug")
.arg("lorem_ipsum.txt")
.succeeds()
.stdout_is_fixture("crc_single_file.expected")
.stderr_contains("avx512")
.stderr_contains("avx2")
.stderr_contains("pclmul");
// Test with MD5 algorithm - CPU detection should be same regardless of algorithm
new_ucmd!()
.arg("--debug")
.arg("-a")
.arg("md5")
.arg("lorem_ipsum.txt")
.succeeds()
.stdout_is_fixture("md5_single_file.expected")
.stderr_contains("avx512")
.stderr_contains("avx2")
.stderr_contains("pclmul");
// Test with stdin - CPU detection should appear once
new_ucmd!()
.arg("--debug")
.pipe_in("test")
.succeeds()
.stderr_contains("avx512")
.stderr_contains("avx2")
.stderr_contains("pclmul");
// Test with multiple files - CPU detection should appear once, not per file
new_ucmd!()
.arg("--debug")
.arg("lorem_ipsum.txt")
.arg("alice_in_wonderland.txt")
.succeeds()
.stdout_is_fixture("crc_multiple_files.expected")
.stderr_str_check(|stderr| {
// Verify CPU detection happens only once by checking the count of each feature line
let avx512_count = stderr
.lines()
.filter(|line| line.contains("avx512"))
.count();
let avx2_count = stderr.lines().filter(|line| line.contains("avx2")).count();
let pclmul_count = stderr
.lines()
.filter(|line| line.contains("pclmul"))
.count();
avx512_count == 1 && avx2_count == 1 && pclmul_count == 1
});
}
#[test]
fn test_debug_with_algorithms() {
// Test with SHA256 - CPU detection should be same regardless of algorithm
new_ucmd!()
.arg("--debug")
.arg("-a")
.arg("sha256")
.arg("lorem_ipsum.txt")
.succeeds()
.stderr_contains("avx512")
.stderr_contains("avx2")
.stderr_contains("pclmul");
// Test with BLAKE2b default length
new_ucmd!()
.arg("--debug")
.arg("-a")
.arg("blake2b")
.arg("lorem_ipsum.txt")
.succeeds()
.stderr_contains("avx512")
.stderr_contains("avx2")
.stderr_contains("pclmul");
// Test with BLAKE2b custom length
new_ucmd!()
.arg("--debug")
.arg("-a")
.arg("blake2b")
.arg("--length")
.arg("256")
.arg("lorem_ipsum.txt")
.succeeds()
.stderr_contains("avx512")
.stderr_contains("avx2")
.stderr_contains("pclmul");
// Test with SHA1
new_ucmd!()
.arg("--debug")
.arg("-a")
.arg("sha1")
.arg("lorem_ipsum.txt")
.succeeds()
.stderr_contains("avx512")
.stderr_contains("avx2")
.stderr_contains("pclmul");
}
}