Skip to content

Commit 48a9615

Browse files
committed
introduce checksum_common
1 parent 3c51c55 commit 48a9615

9 files changed

Lines changed: 490 additions & 0 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ uucore = { version = "0.5.0", package = "uucore", path = "src/uucore" }
407407
uucore_procs = { version = "0.5.0", package = "uucore_procs", path = "src/uucore_procs" }
408408
uu_ls = { version = "0.5.0", path = "src/uu/ls" }
409409
uu_base32 = { version = "0.5.0", path = "src/uu/base32" }
410+
uu_checksum_common = { version = "0.5.0", path = "src/uu/checksum_common" }
410411
uutests = { version = "0.5.0", package = "uutests", path = "tests/uutests" }
411412

412413
[dependencies]

fuzz/Cargo.lock

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

src/uu/checksum_common/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "uu_checksum_common"
3+
description = "Base for checksum utils"
4+
version.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
keywords.workspace = true
9+
categories.workspace = true
10+
edition.workspace = true
11+
readme.workspace = true
12+
13+
[lints]
14+
workspace = true
15+
16+
[lib]
17+
path = "src/lib.rs"
18+
19+
[dependencies]
20+
clap = { workspace = true }
21+
uucore = { workspace = true, features = [
22+
"checksum",
23+
"encoding",
24+
"sum",
25+
"hardware",
26+
] }
27+
fluent = { workspace = true }
28+
29+
[dev-dependencies]
30+
divan = { workspace = true }
31+
tempfile = { workspace = true }
32+
33+
# [[bench]]
34+
# name = "b2sum_bench"
35+
# harness = false

src/uu/checksum_common/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../LICENSE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ck-common-after-help = With no FILE or when FILE is -, read standard input
2+
3+
# checksum argument help messages
4+
ck-common-help-algorithm = select the digest type to use. See DIGEST below
5+
ck-common-help-untagged = create a reversed style checksum, without digest type
6+
ck-common-help-tag-default = create a BSD style checksum (default)
7+
ck-common-help-tag = create a BSD style checksum
8+
ck-common-help-text = read in text mode (default)
9+
ck-common-help-length = digest length in bits; must not exceed the max size and must be a multiple of 8 for blake2b; must be 224, 256, 384, or 512 for sha2 or sha3
10+
ck-common-help-check = read checksums from the FILEs and check them
11+
ck-common-help-base64 = emit base64-encoded digests, not hexadecimal
12+
ck-common-help-raw = emit a raw binary digest, not hexadecimal
13+
ck-common-help-zero = end each output line with NUL, not newline, and disable file name escaping
14+
ck-common-help-strict = exit non-zero for improperly formatted checksum lines
15+
ck-common-help-warn = warn about improperly formatted checksum lines
16+
ck-common-help-status = don't output anything, status code shows success
17+
ck-common-help-quiet = don't print OK for each successfully verified file
18+
ck-common-help-ignore-missing = don't fail or report status for missing files
19+
ck-common-help-debug = print CPU hardware capability detection info used by cksum
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ck-common-after-help = Sans FICHIER ou quand FICHER est -, lit l'entrée standard
2+
3+
# Messages d'aide d'arguments checksum
4+
ck-common-help-algorithm = sélectionner le type de condensé à utiliser. Voir DIGEST ci-dessous
5+
ck-common-help-untagged = créer une somme de contrôle de style inversé, sans type de condensé
6+
ck-common-help-tag-default = créer une somme de contrôle de style BSD (par défaut)
7+
ck-common-help-tag = créer une somme de contrôle de style BSD
8+
ck-common-help-text = lire en mode texte (par défaut)
9+
ck-common-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
10+
ck-common-help-raw = émettre un condensé binaire brut, pas hexadécimal
11+
ck-common-help-strict = sortir avec un code non-zéro pour les lignes de somme de contrôle mal formatées
12+
ck-common-help-check = lire les sommes de hachage des FICHIERs et les vérifier
13+
ck-common-help-base64 = émettre un condensé base64, pas hexadécimal
14+
ck-common-help-warn = avertir des lignes de somme de contrôle mal formatées
15+
ck-common-help-status = ne rien afficher, le code de statut indique le succès
16+
ck-common-help-quiet = ne pas afficher OK pour chaque fichier vérifié avec succès
17+
ck-common-help-ignore-missing = ne pas échouer ou signaler le statut pour les fichiers manquants
18+
ck-common-help-zero = terminer chaque ligne de sortie avec NUL, pas un saut de ligne, et désactiver l'échappement des noms de fichiers
19+
ck-common-help-debug = afficher les informations de débogage sur la détection de la prise en charge matérielle du processeur

src/uu/checksum_common/src/cli.rs

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use clap::{Arg, ArgAction, Command};
7+
use uucore::{checksum::SUPPORTED_ALGORITHMS, translate};
8+
9+
/// List of all options that can be encountered in checksum utils
10+
pub mod options {
11+
// cksum-specific
12+
pub const ALGORITHM: &str = "algorithm";
13+
pub const DEBUG: &str = "debug";
14+
15+
// positional arg
16+
pub const FILE: &str = "file";
17+
18+
pub const UNTAGGED: &str = "untagged";
19+
pub const TAG: &str = "tag";
20+
pub const LENGTH: &str = "length";
21+
pub const RAW: &str = "raw";
22+
pub const BASE64: &str = "base64";
23+
pub const CHECK: &str = "check";
24+
pub const TEXT: &str = "text";
25+
pub const BINARY: &str = "binary";
26+
pub const ZERO: &str = "zero";
27+
28+
// check-specific
29+
pub const STRICT: &str = "strict";
30+
pub const STATUS: &str = "status";
31+
pub const WARN: &str = "warn";
32+
pub const IGNORE_MISSING: &str = "ignore-missing";
33+
pub const QUIET: &str = "quiet";
34+
}
35+
36+
/// `ChecksumCommand` is a convenience trait to more easily declare checksum
37+
/// CLI interfaces with
38+
pub trait ChecksumCommand {
39+
fn with_algo(self) -> Self;
40+
41+
fn with_length(self) -> Self;
42+
43+
fn with_check(self) -> Self;
44+
45+
fn with_binary(self) -> Self;
46+
47+
fn with_text(self, default: bool) -> Self;
48+
49+
fn with_tag(self, default: bool) -> Self;
50+
51+
fn with_untagged(self) -> Self;
52+
53+
fn with_raw(self) -> Self;
54+
55+
fn with_base64(self) -> Self;
56+
57+
fn with_zero(self) -> Self;
58+
59+
fn with_debug(self) -> Self;
60+
}
61+
62+
impl ChecksumCommand for Command {
63+
fn with_algo(self) -> Self {
64+
self.arg(
65+
Arg::new(options::ALGORITHM)
66+
.long(options::ALGORITHM)
67+
.short('a')
68+
.help(translate!("ck-common-help-algorithm"))
69+
.value_name("ALGORITHM")
70+
.value_parser(SUPPORTED_ALGORITHMS),
71+
)
72+
}
73+
74+
fn with_length(self) -> Self {
75+
self.arg(
76+
Arg::new(options::LENGTH)
77+
.long(options::LENGTH)
78+
.short('l')
79+
.help(translate!("ck-common-help-length"))
80+
.action(ArgAction::Set),
81+
)
82+
}
83+
84+
fn with_check(self) -> Self {
85+
self.arg(
86+
Arg::new(options::CHECK)
87+
.short('c')
88+
.long(options::CHECK)
89+
.help(translate!("ck-common-help-check"))
90+
.action(ArgAction::SetTrue),
91+
)
92+
.arg(
93+
Arg::new(options::WARN)
94+
.short('w')
95+
.long("warn")
96+
.help(translate!("ck-common-help-warn"))
97+
.action(ArgAction::SetTrue)
98+
.overrides_with_all([options::STATUS, options::QUIET])
99+
.requires(options::CHECK),
100+
)
101+
.arg(
102+
Arg::new(options::STATUS)
103+
.long("status")
104+
.help(translate!("ck-common-help-status"))
105+
.action(ArgAction::SetTrue)
106+
.overrides_with_all([options::WARN, options::QUIET])
107+
.requires(options::CHECK),
108+
)
109+
.arg(
110+
Arg::new(options::QUIET)
111+
.long(options::QUIET)
112+
.help(translate!("ck-common-help-quiet"))
113+
.action(ArgAction::SetTrue)
114+
.overrides_with_all([options::WARN, options::STATUS])
115+
.requires(options::CHECK),
116+
)
117+
.arg(
118+
Arg::new(options::IGNORE_MISSING)
119+
.long(options::IGNORE_MISSING)
120+
.help(translate!("ck-common-help-ignore-missing"))
121+
.action(ArgAction::SetTrue)
122+
.requires(options::CHECK),
123+
)
124+
.arg(
125+
Arg::new(options::STRICT)
126+
.long(options::STRICT)
127+
.help(translate!("ck-common-help-strict"))
128+
.action(ArgAction::SetTrue)
129+
.requires(options::CHECK),
130+
)
131+
}
132+
133+
fn with_binary(self) -> Self {
134+
self.arg(
135+
Arg::new(options::BINARY)
136+
.long(options::BINARY)
137+
.short('b')
138+
.hide(true)
139+
.action(ArgAction::SetTrue)
140+
.overrides_with(options::TEXT)
141+
.requires(options::UNTAGGED),
142+
)
143+
}
144+
145+
fn with_text(self, default: bool) -> Self {
146+
let mut arg = Arg::new(options::TEXT)
147+
.long(options::TEXT)
148+
.short('t')
149+
.action(ArgAction::SetTrue);
150+
if default {
151+
arg = arg.help(translate!("ck-common-help-text"));
152+
} else {
153+
arg = arg.hide(true);
154+
}
155+
self.arg(arg)
156+
}
157+
158+
fn with_tag(self, default: bool) -> Self {
159+
self.arg(
160+
Arg::new(options::TAG)
161+
.long(options::TAG)
162+
.help(if default {
163+
translate!("ck-common-help-tag-default")
164+
} else {
165+
translate!("ck-common-help-tag")
166+
})
167+
.action(ArgAction::SetTrue)
168+
.overrides_with(options::UNTAGGED)
169+
.overrides_with(options::BINARY)
170+
.overrides_with(options::TEXT),
171+
)
172+
}
173+
174+
fn with_untagged(self) -> Self {
175+
self.arg(
176+
Arg::new(options::UNTAGGED)
177+
.long(options::UNTAGGED)
178+
.help(translate!("ck-common-help-untagged"))
179+
.action(ArgAction::SetTrue)
180+
.overrides_with(options::TAG),
181+
)
182+
}
183+
184+
fn with_raw(self) -> Self {
185+
self.arg(
186+
Arg::new(options::RAW)
187+
.long(options::RAW)
188+
.help(translate!("ck-common-help-raw"))
189+
.action(ArgAction::SetTrue),
190+
)
191+
}
192+
193+
fn with_base64(self) -> Self {
194+
self.arg(
195+
Arg::new(options::BASE64)
196+
.long(options::BASE64)
197+
.help(translate!("ck-common-help-base64"))
198+
.action(ArgAction::SetTrue)
199+
// Even though this could easily just override an earlier '--raw',
200+
// GNU cksum does not permit these flags to be combined:
201+
.conflicts_with(options::RAW),
202+
)
203+
}
204+
205+
fn with_zero(self) -> Self {
206+
self.arg(
207+
Arg::new(options::ZERO)
208+
.long(options::ZERO)
209+
.short('z')
210+
.help(translate!("ck-common-help-zero"))
211+
.action(ArgAction::SetTrue),
212+
)
213+
}
214+
215+
fn with_debug(self) -> Self {
216+
self.arg(
217+
Arg::new(options::DEBUG)
218+
.long(options::DEBUG)
219+
.help(translate!("ck-common-help-debug"))
220+
.action(ArgAction::SetTrue),
221+
)
222+
}
223+
}

0 commit comments

Comments
 (0)