Skip to content

Commit c9776e7

Browse files
committed
cksum: fix error handling
1 parent 9a6f552 commit c9776e7

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub fn create_sha3(bits: Option<usize>) -> UResult<HashAlgorithm> {
154154
}
155155

156156
#[allow(clippy::comparison_chain)]
157-
fn cksum_output(res: &ChecksumResult, ignore_missing: bool, status: bool) {
157+
fn cksum_output(res: &ChecksumResult, status: bool) {
158158
if res.bad_format == 1 {
159159
show_warning_caps!("{} line is improperly formatted", res.bad_format);
160160
} else if res.bad_format > 1 {
@@ -168,12 +168,10 @@ fn cksum_output(res: &ChecksumResult, ignore_missing: bool, status: bool) {
168168
show_warning_caps!("{} computed checksums did NOT match", res.failed_cksum);
169169
}
170170
}
171-
if !ignore_missing {
172-
if res.failed_open_file == 1 {
173-
show_warning_caps!("{} listed file could not be read", res.failed_open_file);
174-
} else if res.failed_open_file > 1 {
175-
show_warning_caps!("{} listed files could not be read", res.failed_open_file);
176-
}
171+
if res.failed_open_file == 1 {
172+
show_warning_caps!("{} listed file could not be read", res.failed_open_file);
173+
} else if res.failed_open_file > 1 {
174+
show_warning_caps!("{} listed files could not be read", res.failed_open_file);
177175
}
178176
}
179177

@@ -364,10 +362,16 @@ fn get_file_to_check(
364362
if filename == "-" {
365363
Some(Box::new(stdin())) // Use stdin if "-" is specified in the checksum file
366364
} else {
365+
let mut failed_open = || {
366+
println!("{filename}: FAILED open or read");
367+
res.failed_open_file += 1;
368+
};
367369
match File::open(filename) {
368370
Ok(f) => {
369371
if f.metadata().ok()?.is_dir() {
370372
show!(USimpleError::new(1, format!("{filename}: Is a directory")));
373+
// also regarded as a failed open
374+
failed_open();
371375
None
372376
} else {
373377
Some(Box::new(f))
@@ -377,9 +381,8 @@ fn get_file_to_check(
377381
if !ignore_missing {
378382
// yes, we have both stderr and stdout here
379383
show!(err.map_err_context(|| filename.to_string()));
380-
println!("{filename}: FAILED open or read");
384+
failed_open();
381385
}
382-
res.failed_open_file += 1;
383386
// we could not open the file but we want to continue
384387
None
385388
}
@@ -612,6 +615,9 @@ where
612615
return Ok(());
613616
}
614617

618+
// if any incorrectly formatted line, show it
619+
cksum_output(&res, status);
620+
615621
if ignore_missing && correct_format == 0 {
616622
// we have only bad format
617623
// and we had ignore-missing
@@ -633,9 +639,6 @@ where
633639
if (res.failed_cksum > 0 || res.failed_open_file > 0) && !ignore_missing {
634640
set_exit_code(1);
635641
}
636-
637-
// if any incorrectly formatted line, show it
638-
cksum_output(&res, ignore_missing, status);
639642
}
640643

641644
Ok(())

tests/by-util/test_cksum.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,3 +1344,39 @@ fn test_check_comment_leading_space() {
13441344
.stdout_contains("foo: OK")
13451345
.stderr_contains("WARNING: 1 line is improperly formatted");
13461346
}
1347+
1348+
#[test]
1349+
fn test_check_error_handling() {
1350+
// check `cksum`'s behavior when encountering directories or non existing files
1351+
1352+
let scene = TestScenario::new(util_name!());
1353+
let at = &scene.fixtures;
1354+
1355+
at.write(
1356+
"CHECKSUM",
1357+
"SHA1 (dir) = ffffffffffffffffffffffffffffffffffffffff\n\
1358+
SHA1 (not-file) = ffffffffffffffffffffffffffffffffffffffff\n",
1359+
);
1360+
at.mkdir("dir");
1361+
1362+
scene
1363+
.ucmd()
1364+
.arg("--check")
1365+
.arg("CHECKSUM")
1366+
.fails()
1367+
.stdout_contains(
1368+
"dir: FAILED open or read\n\
1369+
not-file: FAILED open or read\n",
1370+
)
1371+
.stderr_contains("cksum: WARNING: 2 listed files could not be read");
1372+
1373+
// check with `--ignore-missing`
1374+
scene
1375+
.ucmd()
1376+
.arg("--check")
1377+
.arg("CHECKSUM")
1378+
.arg("--ignore-missing")
1379+
.fails()
1380+
.stdout_contains("dir: FAILED open or read\n")
1381+
.stderr_contains("cksum: WARNING: 1 listed file could not be read");
1382+
}

0 commit comments

Comments
 (0)