Skip to content

Commit 74efafa

Browse files
committed
cksum family: Backport new errors for --binary, --text and --tag
1 parent 26498f5 commit 74efafa

File tree

7 files changed

+42
-85
lines changed

7 files changed

+42
-85
lines changed

src/uu/checksum_common/src/cli.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl ChecksumCommand for Command {
131131
.long(options::BINARY)
132132
.short('b')
133133
.hide(true)
134+
.overrides_with(options::TEXT)
134135
.action(ArgAction::SetTrue),
135136
)
136137
}
@@ -169,6 +170,7 @@ impl ChecksumCommand for Command {
169170
Arg::new(options::UNTAGGED)
170171
.long(options::UNTAGGED)
171172
.help(translate!("ck-common-help-untagged"))
173+
.overrides_with(options::TAG)
172174
.action(ArgAction::SetTrue),
173175
)
174176
}

src/uu/checksum_common/src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ pub fn standalone_with_length_main(
7373
.transpose()?
7474
.flatten();
7575

76-
let format = OutputFormat::from_standalone(std::env::args_os());
76+
//todo: deduplicate matches.get_flag
77+
let text = !matches.get_flag(options::BINARY);
78+
let tag = matches.get_flag(options::TAG);
79+
let format = OutputFormat::from_standalone(text, tag);
7780

7881
checksum_main(algo, length, matches, format?)
7982
}
@@ -82,8 +85,10 @@ pub fn standalone_with_length_main(
8285
pub fn standalone_main(algo: AlgoKind, cmd: Command, args: impl uucore::Args) -> UResult<()> {
8386
let matches = uucore::clap_localization::handle_clap_result(cmd, args)?;
8487
let algo = Some(algo);
85-
86-
let format = OutputFormat::from_standalone(std::env::args_os());
88+
//todo: deduplicate matches.get_flag
89+
let text = !matches.get_flag(options::BINARY);
90+
let tag = matches.get_flag(options::TAG);
91+
let format = OutputFormat::from_standalone(text, tag);
8792

8893
checksum_main(algo, None, matches, format?)
8994
}
@@ -154,24 +159,30 @@ pub fn checksum_main(
154159
let quiet = check_flag("quiet")?;
155160
let strict = check_flag("strict")?;
156161
let status = check_flag("status")?;
162+
let text_flag = matches.get_flag(options::TEXT);
163+
let binary_flag = matches.get_flag(options::BINARY);
164+
let tag = matches.get_flag(options::TAG);
157165

158166
// clap provides the default value -. So we unwrap() safety.
159167
let files = matches
160168
.get_many::<OsString>(options::FILE)
161169
.unwrap()
162170
.map(|s| s.as_os_str());
163171

172+
if text_flag && tag {
173+
return Err(ChecksumError::TextAfterTag.into());
174+
}
175+
164176
if check {
165177
// cksum does not support '--check'ing legacy algorithms
166178
if algo.is_some_and(AlgoKind::is_legacy) {
167179
return Err(ChecksumError::AlgorithmNotSupportedWithCheck.into());
168180
}
169-
170-
let text_flag = matches.get_flag(options::TEXT);
171-
let binary_flag = matches.get_flag(options::BINARY);
172-
let tag = matches.get_flag(options::TAG);
173-
174-
if tag || binary_flag || text_flag {
181+
// Maybe, we should just use clap
182+
if tag {
183+
return Err(ChecksumError::TagCheck.into());
184+
}
185+
if binary_flag || text_flag {
175186
return Err(ChecksumError::BinaryTextConflict.into());
176187
}
177188

src/uu/cksum/src/cksum.rs

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,48 +44,6 @@ fn print_cpu_debug_info() {
4444
}
4545
}
4646

47-
/// cksum has a bunch of legacy behavior. We handle this in this function to
48-
/// make sure they are self contained and "easier" to understand.
49-
///
50-
/// Returns a pair of boolean. The first one indicates if we should use tagged
51-
/// output format, the second one indicates if we should use the binary flag in
52-
/// the untagged case.
53-
fn handle_tag_text_binary_flags<S: AsRef<OsStr>>(
54-
args: impl Iterator<Item = S>,
55-
) -> UResult<(bool, bool)> {
56-
let mut tag = true;
57-
let mut binary = false;
58-
let mut text = false;
59-
60-
// --binary, --tag and --untagged are tight together: none of them
61-
// conflicts with each other but --tag will reset "binary" and "text" and
62-
// set "tag".
63-
64-
for arg in args {
65-
let arg = arg.as_ref();
66-
if arg == "-b" || arg == "--binary" {
67-
text = false;
68-
binary = true;
69-
} else if arg == "--text" {
70-
text = true;
71-
binary = false;
72-
} else if arg == "--tag" {
73-
tag = true;
74-
binary = false;
75-
text = false;
76-
} else if arg == "--untagged" {
77-
tag = false;
78-
}
79-
}
80-
81-
// Specifying --text without ever mentioning --untagged fails.
82-
if text && tag {
83-
return Err(ChecksumError::TextWithoutUntagged.into());
84-
}
85-
86-
Ok((tag, binary))
87-
}
88-
8947
/// Sanitize the `--length` argument depending on `--algorithm` and `--length`.
9048
fn maybe_sanitize_length(
9149
algo_cli: Option<AlgoKind>,
@@ -124,8 +82,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
12482
.map(String::as_str);
12583

12684
let length = maybe_sanitize_length(algo_cli, input_length)?;
85+
let tag = !matches.get_flag(options::UNTAGGED);
86+
let binary = matches.get_flag(options::BINARY);
87+
let text = matches.get_flag(options::TEXT);
12788

128-
let (tag, binary) = handle_tag_text_binary_flags(std::env::args_os())?;
89+
//Specifying --text without ever mentioning --untagged fails.
90+
if text && tag {
91+
return Err(ChecksumError::TextWithoutUntagged.into());
92+
}
12993

13094
let output_format = OutputFormat::from_cksum(
13195
algo_cli.unwrap_or(AlgoKind::Crc),

src/uucore/src/lib/features/checksum/compute.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// spell-checker:ignore bitlen
77

8-
use std::ffi::{OsStr, OsString};
8+
use std::ffi::OsStr;
99
use std::fs::File;
1010
use std::io::{self, BufReader, Read, Write};
1111
use std::path::Path;
@@ -140,27 +140,7 @@ impl OutputFormat {
140140
///
141141
/// Since standalone utils can't use the Raw or Legacy output format, it is
142142
/// decided only using the --tag, --binary and --text arguments.
143-
pub fn from_standalone(args: impl Iterator<Item = OsString>) -> UResult<Self> {
144-
let mut text = true;
145-
let mut tag = false;
146-
147-
for arg in args {
148-
if arg == "--" {
149-
break;
150-
} else if arg == "--tag" {
151-
tag = true;
152-
text = false;
153-
} else if arg == "--binary" || arg == "-b" {
154-
text = false;
155-
} else if arg == "--text" || arg == "-t" {
156-
// Finding a `--text` after `--tag` is an error.
157-
if tag {
158-
return Err(ChecksumError::TextAfterTag.into());
159-
}
160-
text = true;
161-
}
162-
}
163-
143+
pub fn from_standalone(text: bool, tag: bool) -> UResult<Self> {
164144
if tag {
165145
Ok(Self::Tagged(DigestFormat::Hexadecimal))
166146
} else {

src/uucore/src/lib/features/checksum/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ pub enum ChecksumError {
397397
BinaryTextConflict,
398398
#[error("--text mode is only supported with --untagged")]
399399
TextWithoutUntagged,
400+
#[error("the --tag option is meaningless when verifying checksums")]
401+
TagCheck,
400402
#[error("--tag does not support --text mode")]
401403
TextAfterTag,
402404
#[error("--check is not supported with --algorithm={{bsd,sysv,crc,crc32b}}")]

tests/by-util/test_cksum.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,13 +1010,13 @@ fn test_reset_binary() {
10101010

10111011
scene
10121012
.ucmd()
1013-
.arg("--binary") // should disappear because of the following option
1013+
.arg("--binary")
10141014
.arg("--tag")
10151015
.arg("--untagged")
10161016
.arg("--algorithm=md5")
10171017
.arg(at.subdir.join("f"))
10181018
.succeeds()
1019-
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e ");
1019+
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
10201020
}
10211021

10221022
#[test]
@@ -1038,7 +1038,6 @@ fn test_reset_binary_but_set() {
10381038
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
10391039
}
10401040

1041-
/// Test legacy behaviors with --tag, --untagged, --binary and --text
10421041
mod output_format {
10431042
use super::*;
10441043

@@ -1047,13 +1046,11 @@ mod output_format {
10471046
let (at, mut ucmd) = at_and_ucmd!();
10481047
at.touch("f");
10491048

1050-
ucmd.arg("--text") // should disappear because of the following option
1049+
ucmd.arg("--text")
10511050
.arg("--tag")
10521051
.args(&["-a", "md5"])
10531052
.arg(at.subdir.join("f"))
1054-
.succeeds()
1055-
// Tagged output is used
1056-
.stdout_contains("f) = d41d8cd98f00b204e9800998ecf8427e");
1053+
.fails();
10571054
}
10581055

10591056
#[test]
@@ -1179,7 +1176,7 @@ fn test_binary_file() {
11791176
.arg("--untagged")
11801177
.arg("lorem_ipsum.txt")
11811178
.succeeds()
1182-
.stdout_is("cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt\n");
1179+
.stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n");
11831180
}
11841181

11851182
#[test]
@@ -1227,9 +1224,7 @@ fn test_conflicting_options() {
12271224
.arg("md5")
12281225
.fails_with_code(1)
12291226
.no_stdout()
1230-
.stderr_contains(
1231-
"cksum: the --binary and --text options are meaningless when verifying checksums",
1232-
);
1227+
.stderr_contains("cksum: the --tag option is meaningless when verifying checksums");
12331228
}
12341229

12351230
#[test]

util/fetch-gnu.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ curl -L ${repo}/raw/refs/heads/master/tests/nproc/nproc-quota-systemd.sh > tests
1818
curl -L ${repo}/raw/refs/heads/master/tests/stty/bad-speed.sh > tests/stty/bad-speed.sh
1919
# Better support for single binary
2020
curl -L ${repo}/raw/refs/heads/master/tests/env/env.sh > tests/env/env.sh
21+
# Accurate errors on *sum with --binary --text and --tag. Freeze commit to avoid regression.
22+
curl -L ${repo}/raw/refs/heads/master/tests/cksum/cksum-a.sh > tests/cksum/cksum-a.sh
23+
curl -L https://raw.githubusercontent.com/coreutils/coreutils/9200747f65e3523f7b48184a331eec5a1225b435/tests/cksum/cksum-c.sh > tests/cksum/cksum-c.sh
2124
# Avoid incorrect PASS
2225
curl -L ${repo}/raw/refs/heads/master/tests/runcon/runcon-compute.sh > tests/runcon/runcon-compute.sh
2326
curl -L ${repo}/raw/refs/heads/master/tests/tac/tac-continue.sh > tests/tac/tac-continue.sh

0 commit comments

Comments
 (0)