Skip to content

Commit 559ecf9

Browse files
committed
cksum: transition to checksum_common
1 parent 57df915 commit 559ecf9

6 files changed

Lines changed: 35 additions & 267 deletions

File tree

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: 28 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55

66
// spell-checker:ignore (ToDO) fname, algo, bitlen
77

8-
use clap::builder::ValueParser;
9-
use clap::{Arg, ArgAction, Command};
10-
use std::ffi::{OsStr, 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-
};
8+
use std::ffi::OsStr;
9+
10+
use clap::Command;
11+
use uu_checksum_common::{ChecksumCommand, checksum_main, default_checksum_app, options};
12+
13+
use uucore::checksum::compute::OutputFormat;
1714
use uucore::checksum::{
18-
AlgoKind, ChecksumError, SUPPORTED_ALGORITHMS, SizedAlgoKind, calculate_blake2b_length_str,
19-
sanitize_sha2_sha3_length_str,
15+
AlgoKind, ChecksumError, calculate_blake2b_length_str, sanitize_sha2_sha3_length_str,
2016
};
2117
use uucore::error::UResult;
2218
use uucore::hardware::{HasHardwareFeatures as _, SimdPolicy};
23-
use uucore::line_ending::LineEnding;
24-
use uucore::{format_usage, show_error, translate};
19+
use uucore::{show_error, translate};
2520

2621
/// Print CPU hardware capability detection information to stderr
2722
/// This matches GNU cksum's --debug behavior
@@ -47,26 +42,6 @@ fn print_cpu_debug_info() {
4742
}
4843
}
4944

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-
7045
/// cksum has a bunch of legacy behavior. We handle this in this function to
7146
/// make sure they are self contained and "easier" to understand.
7247
///
@@ -137,22 +112,6 @@ fn maybe_sanitize_length(
137112
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
138113
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
139114

140-
let check = matches.get_flag(options::CHECK);
141-
142-
let check_flag = |flag| match (check, matches.get_flag(flag)) {
143-
(_, false) => Ok(false),
144-
(true, true) => Ok(true),
145-
(false, true) => Err(ChecksumError::CheckOnlyFlag(flag.into())),
146-
};
147-
148-
// Each of the following flags are only expected in --check mode.
149-
// If we encounter them otherwise, end with an error.
150-
let ignore_missing = check_flag(options::IGNORE_MISSING)?;
151-
let warn = check_flag(options::WARN)?;
152-
let quiet = check_flag(options::QUIET)?;
153-
let strict = check_flag(options::STRICT)?;
154-
let status = check_flag(options::STATUS)?;
155-
156115
let algo_cli = matches
157116
.get_one::<String>(options::ALGORITHM)
158117
.map(AlgoKind::from_cksum)
@@ -164,202 +123,36 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
164123

165124
let length = maybe_sanitize_length(algo_cli, input_length)?;
166125

167-
// clap provides the default value -. So we unwrap() safety.
168-
let files = matches
169-
.get_many::<OsString>(options::FILE)
170-
.unwrap()
171-
.map(|s| s.as_os_str());
172-
173-
if check {
174-
// cksum does not support '--check'ing legacy algorithms
175-
if algo_cli.is_some_and(AlgoKind::is_legacy) {
176-
return Err(ChecksumError::AlgorithmNotSupportedWithCheck.into());
177-
}
178-
179-
let text_flag = matches.get_flag(options::TEXT);
180-
let binary_flag = matches.get_flag(options::BINARY);
181-
let tag = matches.get_flag(options::TAG);
182-
183-
if tag || binary_flag || text_flag {
184-
return Err(ChecksumError::BinaryTextConflict.into());
185-
}
186-
187-
// Execute the checksum validation based on the presence of files or the use of stdin
188-
189-
let verbose = ChecksumVerbose::new(status, quiet, warn);
190-
let opts = ChecksumValidateOptions {
191-
ignore_missing,
192-
strict,
193-
verbose,
194-
};
195-
196-
return perform_checksum_validation(files, algo_cli, length, opts);
197-
}
198-
199-
// Not --check
200-
201-
// Print hardware debug info if requested
202-
if matches.get_flag(options::DEBUG) {
203-
print_cpu_debug_info();
204-
}
205-
206-
// Set the default algorithm to CRC when not '--check'ing.
207-
let algo_kind = algo_cli.unwrap_or(AlgoKind::Crc);
208-
209126
let (tag, binary) = handle_tag_text_binary_flags(std::env::args_os())?;
210127

211128
let output_format = OutputFormat::from_cksum(
212-
algo_kind,
129+
algo_cli.unwrap_or(AlgoKind::Crc),
213130
tag,
214131
binary,
215-
matches.get_flag(options::RAW),
216-
matches.get_flag(options::BASE64),
132+
/* raw */ matches.get_flag(options::RAW),
133+
/* base64 */ matches.get_flag(options::BASE64),
217134
);
218135

219-
let algo = SizedAlgoKind::from_unsized(algo_kind, length)?;
220-
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
221-
222-
let opts = ChecksumComputeOptions {
223-
algo_kind: algo,
224-
output_format,
225-
line_ending,
226-
};
227-
228-
perform_checksum_computation(opts, files)?;
136+
// Print hardware debug info if requested
137+
if matches.get_flag(options::DEBUG) {
138+
print_cpu_debug_info();
139+
}
229140

230-
Ok(())
141+
checksum_main(algo_cli, length, matches, output_format)
231142
}
232143

233144
pub fn uu_app() -> Command {
234-
Command::new(uucore::util_name())
235-
.version(uucore::crate_version!())
236-
.help_template(uucore::localized_help_template(uucore::util_name()))
237-
.about(translate!("cksum-about"))
238-
.override_usage(format_usage(&translate!("cksum-usage")))
239-
.infer_long_args(true)
240-
.args_override_self(true)
241-
.arg(
242-
Arg::new(options::FILE)
243-
.hide(true)
244-
.action(ArgAction::Append)
245-
.value_parser(ValueParser::os_string())
246-
.default_value("-")
247-
.hide_default_value(true)
248-
.value_hint(clap::ValueHint::FilePath),
249-
)
250-
.arg(
251-
Arg::new(options::ALGORITHM)
252-
.long(options::ALGORITHM)
253-
.short('a')
254-
.help(translate!("cksum-help-algorithm"))
255-
.value_name("ALGORITHM")
256-
.value_parser(SUPPORTED_ALGORITHMS),
257-
)
258-
.arg(
259-
Arg::new(options::UNTAGGED)
260-
.long(options::UNTAGGED)
261-
.help(translate!("cksum-help-untagged"))
262-
.action(ArgAction::SetTrue)
263-
.overrides_with(options::TAG),
264-
)
265-
.arg(
266-
Arg::new(options::TAG)
267-
.long(options::TAG)
268-
.help(translate!("cksum-help-tag"))
269-
.action(ArgAction::SetTrue)
270-
.overrides_with(options::UNTAGGED),
271-
)
272-
.arg(
273-
Arg::new(options::LENGTH)
274-
.long(options::LENGTH)
275-
.short('l')
276-
.help(translate!("cksum-help-length"))
277-
.action(ArgAction::Set),
278-
)
279-
.arg(
280-
Arg::new(options::RAW)
281-
.long(options::RAW)
282-
.help(translate!("cksum-help-raw"))
283-
.action(ArgAction::SetTrue),
284-
)
285-
.arg(
286-
Arg::new(options::STRICT)
287-
.long(options::STRICT)
288-
.help(translate!("cksum-help-strict"))
289-
.action(ArgAction::SetTrue),
290-
)
291-
.arg(
292-
Arg::new(options::CHECK)
293-
.short('c')
294-
.long(options::CHECK)
295-
.help(translate!("cksum-help-check"))
296-
.action(ArgAction::SetTrue),
297-
)
298-
.arg(
299-
Arg::new(options::BASE64)
300-
.long(options::BASE64)
301-
.help(translate!("cksum-help-base64"))
302-
.action(ArgAction::SetTrue)
303-
// Even though this could easily just override an earlier '--raw',
304-
// GNU cksum does not permit these flags to be combined:
305-
.conflicts_with(options::RAW),
306-
)
307-
.arg(
308-
Arg::new(options::TEXT)
309-
.long(options::TEXT)
310-
.short('t')
311-
.hide(true)
312-
.overrides_with(options::BINARY)
313-
.action(ArgAction::SetTrue),
314-
)
315-
.arg(
316-
Arg::new(options::BINARY)
317-
.long(options::BINARY)
318-
.short('b')
319-
.hide(true)
320-
.overrides_with(options::TEXT)
321-
.action(ArgAction::SetTrue),
322-
)
323-
.arg(
324-
Arg::new(options::WARN)
325-
.short('w')
326-
.long("warn")
327-
.help(translate!("cksum-help-warn"))
328-
.action(ArgAction::SetTrue)
329-
.overrides_with_all([options::STATUS, options::QUIET]),
330-
)
331-
.arg(
332-
Arg::new(options::STATUS)
333-
.long("status")
334-
.help(translate!("cksum-help-status"))
335-
.action(ArgAction::SetTrue)
336-
.overrides_with_all([options::WARN, options::QUIET]),
337-
)
338-
.arg(
339-
Arg::new(options::QUIET)
340-
.long(options::QUIET)
341-
.help(translate!("cksum-help-quiet"))
342-
.action(ArgAction::SetTrue)
343-
.overrides_with_all([options::WARN, options::STATUS]),
344-
)
345-
.arg(
346-
Arg::new(options::IGNORE_MISSING)
347-
.long(options::IGNORE_MISSING)
348-
.help(translate!("cksum-help-ignore-missing"))
349-
.action(ArgAction::SetTrue),
350-
)
351-
.arg(
352-
Arg::new(options::ZERO)
353-
.long(options::ZERO)
354-
.short('z')
355-
.help(translate!("cksum-help-zero"))
356-
.action(ArgAction::SetTrue),
357-
)
358-
.arg(
359-
Arg::new(options::DEBUG)
360-
.long(options::DEBUG)
361-
.help(translate!("cksum-help-debug"))
362-
.action(ArgAction::SetTrue),
363-
)
145+
default_checksum_app(translate!("cksum-about"), translate!("cksum-usage"))
146+
.with_algo()
147+
.with_untagged()
148+
.with_tag(true)
149+
.with_length()
150+
.with_raw()
151+
.with_check_and_opts()
152+
.with_base64()
153+
.with_text(false)
154+
.with_binary()
155+
.with_zero()
156+
.with_debug()
364157
.after_help(translate!("cksum-after-help"))
365158
}

src/uucore/src/lib/mods/locale.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ fn create_bundle(
156156
// Then, try to load utility-specific strings from the utility's locale directory
157157
try_add_resource_from(get_locales_dir(util_name).ok());
158158

159+
// checksum binaries also require fluent files from the checksum_common crate
160+
if ["cksum"].contains(&util_name) {
161+
try_add_resource_from(get_locales_dir("checksum_common").ok());
162+
}
163+
159164
// If we have at least one resource, return the bundle
160165
if bundle.has_message("common-error") || bundle.has_message(&format!("{util_name}-about")) {
161166
Ok(bundle)

0 commit comments

Comments
 (0)