Skip to content

Commit 1d91b47

Browse files
committed
cksum family: Backport new errors for --binary, --text and --tag
1 parent 3035619 commit 1d91b47

File tree

6 files changed

+25
-62
lines changed

6 files changed

+25
-62
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,26 @@ pub fn checksum_main(
154154
let quiet = check_flag("quiet")?;
155155
let strict = check_flag("strict")?;
156156
let status = check_flag("status")?;
157+
let text_flag = matches.get_flag(options::TEXT);
158+
let binary_flag = matches.get_flag(options::BINARY);
159+
let tag = matches.get_flag(options::TAG);
157160

158161
// clap provides the default value -. So we unwrap() safety.
159162
let files = matches
160163
.get_many::<OsString>(options::FILE)
161164
.unwrap()
162165
.map(|s| s.as_os_str());
163166

167+
if text_flag && tag {
168+
return Err(ChecksumError::TextAfterTag.into());
169+
}
170+
164171
if check {
165172
// cksum does not support '--check'ing legacy algorithms
166173
if algo.is_some_and(AlgoKind::is_legacy) {
167174
return Err(ChecksumError::AlgorithmNotSupportedWithCheck.into());
168175
}
169176

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-
174177
if tag || binary_flag || text_flag {
175178
return Err(ChecksumError::BinaryTextConflict.into());
176179
}

src/uu/cksum/src/cksum.rs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

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

8-
use std::ffi::OsStr;
9-
108
use clap::Command;
119
use uu_checksum_common::{ChecksumCommand, checksum_main, default_checksum_app, options};
1210

@@ -42,48 +40,6 @@ fn print_cpu_debug_info() {
4240
}
4341
}
4442

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

12480
let length = maybe_sanitize_length(algo_cli, input_length)?;
81+
let tag = !matches.get_flag(options::UNTAGGED);
82+
let text = matches.get_flag(options::TEXT);
12583

126-
let (tag, binary) = handle_tag_text_binary_flags(std::env::args_os())?;
84+
//Specifying --text without ever mentioning --untagged fails.
85+
if text && tag {
86+
return Err(ChecksumError::TextWithoutUntagged.into());
87+
}
12788

12889
let output_format = OutputFormat::from_cksum(
12990
algo_cli.unwrap_or(AlgoKind::Crc),
13091
tag,
131-
binary,
92+
!text,
13293
/* raw */ matches.get_flag(options::RAW),
13394
/* base64 */ matches.get_flag(options::BASE64),
13495
);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl OutputFormat {
144144
let mut text = true;
145145
let mut tag = false;
146146

147+
// todo: switch to clap's matches.get_flag
147148
for arg in args {
148149
if arg == "--" {
149150
break;
@@ -153,10 +154,6 @@ impl OutputFormat {
153154
} else if arg == "--binary" || arg == "-b" {
154155
text = false;
155156
} 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-
}
160157
text = true;
161158
}
162159
}

tests/by-util/test_cksum.rs

Lines changed: 5 additions & 8 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]

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
22+
curl -L ${repo}/raw/refs/heads/master/tests/cksum/cksum-a.sh > tests/cksum/cksum-a.sh
23+
curl -L ${repo}/raw/refs/heads/master/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)