Skip to content

Commit 57df915

Browse files
committed
introduce checksum_common
1 parent da5da3b commit 57df915

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
@@ -404,6 +404,7 @@ uucore = { version = "0.5.0", package = "uucore", path = "src/uucore" }
404404
uucore_procs = { version = "0.5.0", package = "uucore_procs", path = "src/uucore_procs" }
405405
uu_ls = { version = "0.5.0", path = "src/uu/ls" }
406406
uu_base32 = { version = "0.5.0", path = "src/uu/base32" }
407+
uu_checksum_common = { version = "0.5.0", path = "src/uu/checksum_common" }
407408
uutests = { version = "0.5.0", package = "uutests", path = "tests/uutests" }
408409

409410
[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: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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_and_opts(self) -> Self;
44+
45+
fn with_binary(self) -> Self;
46+
47+
fn with_text(self, is_default: bool) -> Self;
48+
49+
fn with_tag(self, is_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_and_opts(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+
)
100+
.arg(
101+
Arg::new(options::STATUS)
102+
.long("status")
103+
.help(translate!("ck-common-help-status"))
104+
.action(ArgAction::SetTrue)
105+
.overrides_with_all([options::WARN, options::QUIET]),
106+
)
107+
.arg(
108+
Arg::new(options::QUIET)
109+
.long(options::QUIET)
110+
.help(translate!("ck-common-help-quiet"))
111+
.action(ArgAction::SetTrue)
112+
.overrides_with_all([options::STATUS, options::WARN]),
113+
)
114+
.arg(
115+
Arg::new(options::IGNORE_MISSING)
116+
.long(options::IGNORE_MISSING)
117+
.help(translate!("ck-common-help-ignore-missing"))
118+
.action(ArgAction::SetTrue),
119+
)
120+
.arg(
121+
Arg::new(options::STRICT)
122+
.long(options::STRICT)
123+
.help(translate!("ck-common-help-strict"))
124+
.action(ArgAction::SetTrue),
125+
)
126+
}
127+
128+
fn with_binary(self) -> Self {
129+
self.arg(
130+
Arg::new(options::BINARY)
131+
.long(options::BINARY)
132+
.short('b')
133+
.hide(true)
134+
.action(ArgAction::SetTrue),
135+
)
136+
}
137+
138+
fn with_text(self, is_default: bool) -> Self {
139+
let mut arg = Arg::new(options::TEXT)
140+
.long(options::TEXT)
141+
.short('t')
142+
.action(ArgAction::SetTrue);
143+
144+
arg = if is_default {
145+
arg.help(translate!("ck-common-help-text"))
146+
} else {
147+
arg.hide(true)
148+
};
149+
150+
self.arg(arg)
151+
}
152+
153+
fn with_tag(self, default: bool) -> Self {
154+
let mut arg = Arg::new(options::TAG)
155+
.long(options::TAG)
156+
.action(ArgAction::SetTrue);
157+
158+
arg = if default {
159+
arg.help(translate!("ck-common-help-tag-default"))
160+
} else {
161+
arg.help(translate!("ck-common-help-tag"))
162+
};
163+
164+
self.arg(arg)
165+
}
166+
167+
fn with_untagged(self) -> Self {
168+
self.arg(
169+
Arg::new(options::UNTAGGED)
170+
.long(options::UNTAGGED)
171+
.help(translate!("ck-common-help-untagged"))
172+
.action(ArgAction::SetTrue),
173+
)
174+
}
175+
176+
fn with_raw(self) -> Self {
177+
self.arg(
178+
Arg::new(options::RAW)
179+
.long(options::RAW)
180+
.help(translate!("ck-common-help-raw"))
181+
.action(ArgAction::SetTrue),
182+
)
183+
}
184+
185+
fn with_base64(self) -> Self {
186+
self.arg(
187+
Arg::new(options::BASE64)
188+
.long(options::BASE64)
189+
.help(translate!("ck-common-help-base64"))
190+
.action(ArgAction::SetTrue)
191+
// Even though this could easily just override an earlier '--raw',
192+
// GNU cksum does not permit these flags to be combined:
193+
.conflicts_with(options::RAW),
194+
)
195+
}
196+
197+
fn with_zero(self) -> Self {
198+
self.arg(
199+
Arg::new(options::ZERO)
200+
.long(options::ZERO)
201+
.short('z')
202+
.help(translate!("ck-common-help-zero"))
203+
.action(ArgAction::SetTrue),
204+
)
205+
}
206+
207+
fn with_debug(self) -> Self {
208+
self.arg(
209+
Arg::new(options::DEBUG)
210+
.long(options::DEBUG)
211+
.help(translate!("ck-common-help-debug"))
212+
.action(ArgAction::SetTrue),
213+
)
214+
}
215+
}

0 commit comments

Comments
 (0)