Skip to content

Commit 8ea0463

Browse files
committed
cksum: transition to checksum_common
1 parent f6c7b6f commit 8ea0463

File tree

5 files changed

+17
-125
lines changed

5 files changed

+17
-125
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/cksum/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ uucore = { workspace = true, features = [
2525
"sum",
2626
"hardware",
2727
] }
28+
uu_checksum_common = { workspace = true }
2829
fluent = { workspace = true }
2930

3031
[dev-dependencies]

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,3 @@ cksum-after-help = DIGEST determines the digest algorithm and default output for
1212
- sha3: (only available through cksum)
1313
- blake2b: (equivalent to b2sum)
1414
- sm3: (only available through cksum)
15-
16-
# Help messages
17-
cksum-help-algorithm = select the digest type to use. See DIGEST below
18-
cksum-help-untagged = create a reversed style checksum, without digest type
19-
cksum-help-tag = create a BSD style checksum, undo --untagged (default)
20-
cksum-help-length = digest length in bits; must not exceed the max for the blake2 algorithm and must be a multiple of 8
21-
cksum-help-raw = emit a raw binary digest, not hexadecimal
22-
cksum-help-strict = exit non-zero for improperly formatted checksum lines
23-
cksum-help-check = read hashsums from the FILEs and check them
24-
cksum-help-base64 = emit a base64 digest, not hexadecimal
25-
cksum-help-warn = warn about improperly formatted checksum lines
26-
cksum-help-status = don't output anything, status code shows success
27-
cksum-help-quiet = don't print OK for each successfully verified file
28-
cksum-help-ignore-missing = don't fail or report status for missing files
29-
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

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,3 @@ cksum-after-help = DIGEST détermine l'algorithme de condensé et le format de s
1212
- sha3 : (disponible uniquement via cksum)
1313
- blake2b : (équivalent à b2sum)
1414
- sm3 : (disponible uniquement via cksum)
15-
16-
# Messages d'aide
17-
cksum-help-algorithm = sélectionner le type de condensé à utiliser. Voir DIGEST ci-dessous
18-
cksum-help-untagged = créer une somme de contrôle de style inversé, sans type de condensé
19-
cksum-help-tag = créer une somme de contrôle de style BSD, annuler --untagged (par défaut)
20-
cksum-help-length = longueur du condensé en bits ; ne doit pas dépasser le maximum pour l'algorithme blake2 et doit être un multiple de 8
21-
cksum-help-raw = émettre un condensé binaire brut, pas hexadécimal
22-
cksum-help-strict = sortir avec un code non-zéro pour les lignes de somme de contrôle mal formatées
23-
cksum-help-check = lire les sommes de hachage des FICHIERs et les vérifier
24-
cksum-help-base64 = émettre un condensé base64, pas hexadécimal
25-
cksum-help-warn = avertir des lignes de somme de contrôle mal formatées
26-
cksum-help-status = ne rien afficher, le code de statut indique le succès
27-
cksum-help-quiet = ne pas afficher OK pour chaque fichier vérifié avec succès
28-
cksum-help-ignore-missing = ne pas échouer ou signaler le statut pour les fichiers manquants
29-
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

src/uu/cksum/src/cksum.rs

Lines changed: 15 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@
77

88
use clap::builder::ValueParser;
99
use clap::{Arg, ArgAction, Command};
10-
use std::ffi::OsString;
11-
use uucore::checksum::compute::{
12-
ChecksumComputeOptions, OutputFormat, perform_checksum_computation,
13-
};
14-
use uucore::checksum::validate::{
15-
ChecksumValidateOptions, ChecksumVerbose, perform_checksum_validation,
16-
};
10+
11+
use uu_checksum_common::{checksum_main, options};
12+
13+
use uucore::checksum::compute::OutputFormat;
1714
use uucore::checksum::{
18-
AlgoKind, ChecksumError, SUPPORTED_ALGORITHMS, SizedAlgoKind, calculate_blake2b_length_str,
15+
AlgoKind, ChecksumError, SUPPORTED_ALGORITHMS, calculate_blake2b_length_str,
1916
sanitize_sha2_sha3_length_str,
2017
};
2118
use uucore::error::UResult;
2219
use uucore::hardware::{HasHardwareFeatures as _, SimdPolicy};
23-
use uucore::line_ending::LineEnding;
2420
use uucore::{format_usage, show_error, translate};
2521

2622
/// Print CPU hardware capability detection information to stderr
@@ -47,26 +43,6 @@ fn print_cpu_debug_info() {
4743
}
4844
}
4945

50-
mod options {
51-
pub const ALGORITHM: &str = "algorithm";
52-
pub const FILE: &str = "file";
53-
pub const UNTAGGED: &str = "untagged";
54-
pub const TAG: &str = "tag";
55-
pub const LENGTH: &str = "length";
56-
pub const RAW: &str = "raw";
57-
pub const BASE64: &str = "base64";
58-
pub const CHECK: &str = "check";
59-
pub const STRICT: &str = "strict";
60-
pub const TEXT: &str = "text";
61-
pub const BINARY: &str = "binary";
62-
pub const STATUS: &str = "status";
63-
pub const WARN: &str = "warn";
64-
pub const IGNORE_MISSING: &str = "ignore-missing";
65-
pub const QUIET: &str = "quiet";
66-
pub const ZERO: &str = "zero";
67-
pub const DEBUG: &str = "debug";
68-
}
69-
7046
/// cksum has a bunch of legacy behavior. We handle this in this function to
7147
/// make sure they are self contained and "easier" to understand.
7248
///
@@ -101,14 +77,6 @@ fn maybe_sanitize_length(
10177
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
10278
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
10379

104-
let check = matches.get_flag(options::CHECK);
105-
106-
let ignore_missing = matches.get_flag(options::IGNORE_MISSING);
107-
let warn = matches.get_flag(options::WARN);
108-
let quiet = matches.get_flag(options::QUIET);
109-
let strict = matches.get_flag(options::STRICT);
110-
let status = matches.get_flag(options::STATUS);
111-
11280
let algo_cli = matches
11381
.get_one::<String>(options::ALGORITHM)
11482
.map(AlgoKind::from_cksum)
@@ -120,68 +88,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
12088

12189
let length = maybe_sanitize_length(algo_cli, input_length)?;
12290

123-
// clap provides the default value -. So we unwrap() safety.
124-
let files = matches
125-
.get_many::<OsString>(options::FILE)
126-
.unwrap()
127-
.map(|s| s.as_os_str());
128-
129-
if check {
130-
// cksum does not support '--check'ing legacy algorithms
131-
if algo_cli.is_some_and(AlgoKind::is_legacy) {
132-
return Err(ChecksumError::AlgorithmNotSupportedWithCheck.into());
133-
}
134-
135-
let text_flag = matches.get_flag(options::TEXT);
136-
let binary_flag = matches.get_flag(options::BINARY);
137-
let tag = matches.get_flag(options::TAG);
138-
139-
if tag || binary_flag || text_flag {
140-
return Err(ChecksumError::BinaryTextConflict.into());
141-
}
142-
143-
// Execute the checksum validation based on the presence of files or the use of stdin
144-
145-
let verbose = ChecksumVerbose::new(status, quiet, warn);
146-
let opts = ChecksumValidateOptions {
147-
ignore_missing,
148-
strict,
149-
verbose,
150-
};
151-
152-
return perform_checksum_validation(files, algo_cli, length, opts);
153-
}
154-
155-
// Not --check
91+
let output_format = OutputFormat::from_cksum(
92+
algo_cli.unwrap_or(AlgoKind::Crc),
93+
// Making TAG default at clap blocks --untagged
94+
/* tag */
95+
!matches.get_flag(options::UNTAGGED),
96+
/* binary */ matches.get_flag(options::BINARY),
97+
/* raw */ matches.get_flag(options::RAW),
98+
/* base64 */ matches.get_flag(options::BASE64),
99+
);
156100

157101
// Print hardware debug info if requested
158102
if matches.get_flag(options::DEBUG) {
159103
print_cpu_debug_info();
160104
}
161105

162-
// Set the default algorithm to CRC when not '--check'ing.
163-
let algo_kind = algo_cli.unwrap_or(AlgoKind::Crc);
164-
165-
let algo = SizedAlgoKind::from_unsized(algo_kind, length)?;
166-
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
167-
168-
let opts = ChecksumComputeOptions {
169-
algo_kind: algo,
170-
output_format: OutputFormat::from_cksum(
171-
algo_kind,
172-
// Making TAG default at clap blocks --untagged
173-
/* tag */
174-
!matches.get_flag(options::UNTAGGED),
175-
/* binary */ matches.get_flag(options::BINARY),
176-
/* raw */ matches.get_flag(options::RAW),
177-
/* base64 */ matches.get_flag(options::BASE64),
178-
),
179-
line_ending,
180-
};
181-
182-
perform_checksum_computation(opts, files)?;
183-
184-
Ok(())
106+
checksum_main(algo_cli, length, matches, output_format)
185107
}
186108

187109
pub fn uu_app() -> Command {

0 commit comments

Comments
 (0)