Skip to content

Commit d655eed

Browse files
naoNao89RenjiSann
authored andcommitted
feat(cksum): improve debug output for single file operations
1 parent 5fd26c0 commit d655eed

File tree

5 files changed

+157
-4
lines changed

5 files changed

+157
-4
lines changed

src/uu/cksum/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ path = "src/cksum.rs"
1919

2020
[dependencies]
2121
clap = { workspace = true }
22-
uucore = { workspace = true, features = ["checksum", "encoding", "sum"] }
22+
uucore = { workspace = true, features = [
23+
"checksum",
24+
"encoding",
25+
"sum",
26+
"hardware",
27+
] }
2328
hex = { workspace = true }
2429
fluent = { workspace = true }
2530

src/uu/cksum/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cksum-help-status = don't output anything, status code shows success
2727
cksum-help-quiet = don't print OK for each successfully verified file
2828
cksum-help-ignore-missing = don't fail or report status for missing files
2929
cksum-help-zero = end each output line with NUL, not newline, and disable file name escaping
30+
cksum-help-debug = print CPU hardware capability detection info used by cksum
3031
3132
# Error messages
3233
cksum-error-is-directory = { $file }: Is a directory

src/uu/cksum/locales/fr-FR.ftl

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

src/uu/cksum/src/cksum.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,34 @@ use uucore::checksum::{
2020
sanitize_sha2_sha3_length_str,
2121
};
2222
use uucore::error::UResult;
23+
use uucore::hardware::CpuFeatures;
2324
use uucore::line_ending::LineEnding;
2425
use uucore::{format_usage, translate};
2526

27+
/// Print CPU hardware capability detection information to stderr
28+
/// This matches GNU cksum's --debug behavior
29+
fn print_cpu_debug_info() {
30+
let features = CpuFeatures::detect();
31+
32+
fn print_feature(name: &str, available: bool) {
33+
if available {
34+
eprintln!("cksum: using {name} hardware support");
35+
} else {
36+
eprintln!("cksum: {name} support not detected");
37+
}
38+
}
39+
40+
// x86/x86_64
41+
print_feature("avx512", features.has_avx512());
42+
print_feature("avx2", features.has_avx2());
43+
print_feature("pclmul", features.has_pclmul());
44+
45+
// ARM aarch64
46+
if cfg!(target_arch = "aarch64") {
47+
print_feature("vmull", features.has_vmull());
48+
}
49+
}
50+
2651
mod options {
2752
pub const ALGORITHM: &str = "algorithm";
2853
pub const FILE: &str = "file";
@@ -40,6 +65,7 @@ mod options {
4065
pub const IGNORE_MISSING: &str = "ignore-missing";
4166
pub const QUIET: &str = "quiet";
4267
pub const ZERO: &str = "zero";
68+
pub const DEBUG: &str = "debug";
4369
}
4470

4571
/// 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<()> {
181207
matches.get_flag(options::BASE64),
182208
);
183209

210+
// Print hardware debug info if requested
211+
if matches.get_flag(options::DEBUG) {
212+
print_cpu_debug_info();
213+
}
214+
184215
let opts = ChecksumComputeOptions {
185216
algo_kind: algo,
186217
output_format,
@@ -317,5 +348,11 @@ pub fn uu_app() -> Command {
317348
.help(translate!("cksum-help-zero"))
318349
.action(ArgAction::SetTrue),
319350
)
351+
.arg(
352+
Arg::new(options::DEBUG)
353+
.long(options::DEBUG)
354+
.help(translate!("cksum-help-debug"))
355+
.action(ArgAction::SetTrue),
356+
)
320357
.after_help(translate!("cksum-after-help"))
321358
}

tests/by-util/test_cksum.rs

Lines changed: 112 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use uutests::util::TestScenario;
1010
use uutests::util::log_info;
1111
use uutests::util_name;
1212

13-
const ALGOS: [&str; 12] = [
14-
"sysv", "bsd", "crc", "crc32b", "md5", "sha1", "sha224", "sha256", "sha384", "sha512",
15-
"blake2b", "sm3",
13+
const ALGOS: [&str; 11] = [
14+
"sysv", "bsd", "crc", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "blake2b", "sm3",
1615
];
1716
const SHA_LENGTHS: [u32; 4] = [224, 256, 384, 512];
1817

@@ -2876,3 +2875,113 @@ mod format_mix {
28762875
.stderr_contains("cksum: WARNING: 1 line is improperly formatted");
28772876
}
28782877
}
2878+
2879+
#[cfg(not(target_os = "android"))]
2880+
mod debug_flag {
2881+
use super::*;
2882+
2883+
#[test]
2884+
fn test_debug_flag() {
2885+
// Test with default CRC algorithm - should output CPU feature detection
2886+
new_ucmd!()
2887+
.arg("--debug")
2888+
.arg("lorem_ipsum.txt")
2889+
.succeeds()
2890+
.stdout_is_fixture("crc_single_file.expected")
2891+
.stderr_contains("avx512")
2892+
.stderr_contains("avx2")
2893+
.stderr_contains("pclmul");
2894+
2895+
// Test with MD5 algorithm - CPU detection should be same regardless of algorithm
2896+
new_ucmd!()
2897+
.arg("--debug")
2898+
.arg("-a")
2899+
.arg("md5")
2900+
.arg("lorem_ipsum.txt")
2901+
.succeeds()
2902+
.stdout_is_fixture("md5_single_file.expected")
2903+
.stderr_contains("avx512")
2904+
.stderr_contains("avx2")
2905+
.stderr_contains("pclmul");
2906+
2907+
// Test with stdin - CPU detection should appear once
2908+
new_ucmd!()
2909+
.arg("--debug")
2910+
.pipe_in("test")
2911+
.succeeds()
2912+
.stderr_contains("avx512")
2913+
.stderr_contains("avx2")
2914+
.stderr_contains("pclmul");
2915+
2916+
// Test with multiple files - CPU detection should appear once, not per file
2917+
new_ucmd!()
2918+
.arg("--debug")
2919+
.arg("lorem_ipsum.txt")
2920+
.arg("alice_in_wonderland.txt")
2921+
.succeeds()
2922+
.stdout_is_fixture("crc_multiple_files.expected")
2923+
.stderr_str_check(|stderr| {
2924+
// Verify CPU detection happens only once by checking the count of each feature line
2925+
let avx512_count = stderr
2926+
.lines()
2927+
.filter(|line| line.contains("avx512"))
2928+
.count();
2929+
let avx2_count = stderr.lines().filter(|line| line.contains("avx2")).count();
2930+
let pclmul_count = stderr
2931+
.lines()
2932+
.filter(|line| line.contains("pclmul"))
2933+
.count();
2934+
2935+
avx512_count == 1 && avx2_count == 1 && pclmul_count == 1
2936+
});
2937+
}
2938+
2939+
#[test]
2940+
fn test_debug_with_algorithms() {
2941+
// Test with SHA256 - CPU detection should be same regardless of algorithm
2942+
new_ucmd!()
2943+
.arg("--debug")
2944+
.arg("-a")
2945+
.arg("sha256")
2946+
.arg("lorem_ipsum.txt")
2947+
.succeeds()
2948+
.stderr_contains("avx512")
2949+
.stderr_contains("avx2")
2950+
.stderr_contains("pclmul");
2951+
2952+
// Test with BLAKE2b default length
2953+
new_ucmd!()
2954+
.arg("--debug")
2955+
.arg("-a")
2956+
.arg("blake2b")
2957+
.arg("lorem_ipsum.txt")
2958+
.succeeds()
2959+
.stderr_contains("avx512")
2960+
.stderr_contains("avx2")
2961+
.stderr_contains("pclmul");
2962+
2963+
// Test with BLAKE2b custom length
2964+
new_ucmd!()
2965+
.arg("--debug")
2966+
.arg("-a")
2967+
.arg("blake2b")
2968+
.arg("--length")
2969+
.arg("256")
2970+
.arg("lorem_ipsum.txt")
2971+
.succeeds()
2972+
.stderr_contains("avx512")
2973+
.stderr_contains("avx2")
2974+
.stderr_contains("pclmul");
2975+
2976+
// Test with SHA1
2977+
new_ucmd!()
2978+
.arg("--debug")
2979+
.arg("-a")
2980+
.arg("sha1")
2981+
.arg("lorem_ipsum.txt")
2982+
.succeeds()
2983+
.stderr_contains("avx512")
2984+
.stderr_contains("avx2")
2985+
.stderr_contains("pclmul");
2986+
}
2987+
}

0 commit comments

Comments
 (0)