mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
cksum: fix error handling
This commit is contained in:
parent
9a6f5521b9
commit
d5cc3dd8ee
2 changed files with 54 additions and 12 deletions
|
@ -154,7 +154,7 @@ pub fn create_sha3(bits: Option<usize>) -> UResult<HashAlgorithm> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::comparison_chain)]
|
#[allow(clippy::comparison_chain)]
|
||||||
fn cksum_output(res: &ChecksumResult, ignore_missing: bool, status: bool) {
|
fn cksum_output(res: &ChecksumResult, status: bool) {
|
||||||
if res.bad_format == 1 {
|
if res.bad_format == 1 {
|
||||||
show_warning_caps!("{} line is improperly formatted", res.bad_format);
|
show_warning_caps!("{} line is improperly formatted", res.bad_format);
|
||||||
} else if res.bad_format > 1 {
|
} else if res.bad_format > 1 {
|
||||||
|
@ -168,14 +168,12 @@ fn cksum_output(res: &ChecksumResult, ignore_missing: bool, status: bool) {
|
||||||
show_warning_caps!("{} computed checksums did NOT match", res.failed_cksum);
|
show_warning_caps!("{} computed checksums did NOT match", res.failed_cksum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !ignore_missing {
|
|
||||||
if res.failed_open_file == 1 {
|
if res.failed_open_file == 1 {
|
||||||
show_warning_caps!("{} listed file could not be read", res.failed_open_file);
|
show_warning_caps!("{} listed file could not be read", res.failed_open_file);
|
||||||
} else if res.failed_open_file > 1 {
|
} else if res.failed_open_file > 1 {
|
||||||
show_warning_caps!("{} listed files could not be read", res.failed_open_file);
|
show_warning_caps!("{} listed files could not be read", res.failed_open_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm> {
|
pub fn detect_algo(algo: &str, length: Option<usize>) -> UResult<HashAlgorithm> {
|
||||||
match algo {
|
match algo {
|
||||||
|
@ -364,10 +362,16 @@ fn get_file_to_check(
|
||||||
if filename == "-" {
|
if filename == "-" {
|
||||||
Some(Box::new(stdin())) // Use stdin if "-" is specified in the checksum file
|
Some(Box::new(stdin())) // Use stdin if "-" is specified in the checksum file
|
||||||
} else {
|
} else {
|
||||||
|
let mut failed_open = || {
|
||||||
|
println!("{filename}: FAILED open or read");
|
||||||
|
res.failed_open_file += 1;
|
||||||
|
};
|
||||||
match File::open(filename) {
|
match File::open(filename) {
|
||||||
Ok(f) => {
|
Ok(f) => {
|
||||||
if f.metadata().ok()?.is_dir() {
|
if f.metadata().ok()?.is_dir() {
|
||||||
show!(USimpleError::new(1, format!("{filename}: Is a directory")));
|
show!(USimpleError::new(1, format!("{filename}: Is a directory")));
|
||||||
|
// also regarded as a failed open
|
||||||
|
failed_open();
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(Box::new(f))
|
Some(Box::new(f))
|
||||||
|
@ -377,9 +381,8 @@ fn get_file_to_check(
|
||||||
if !ignore_missing {
|
if !ignore_missing {
|
||||||
// yes, we have both stderr and stdout here
|
// yes, we have both stderr and stdout here
|
||||||
show!(err.map_err_context(|| filename.to_string()));
|
show!(err.map_err_context(|| filename.to_string()));
|
||||||
println!("{filename}: FAILED open or read");
|
failed_open();
|
||||||
}
|
}
|
||||||
res.failed_open_file += 1;
|
|
||||||
// we could not open the file but we want to continue
|
// we could not open the file but we want to continue
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -612,6 +615,9 @@ where
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if any incorrectly formatted line, show it
|
||||||
|
cksum_output(&res, status);
|
||||||
|
|
||||||
if ignore_missing && correct_format == 0 {
|
if ignore_missing && correct_format == 0 {
|
||||||
// we have only bad format
|
// we have only bad format
|
||||||
// and we had ignore-missing
|
// and we had ignore-missing
|
||||||
|
@ -633,9 +639,6 @@ where
|
||||||
if (res.failed_cksum > 0 || res.failed_open_file > 0) && !ignore_missing {
|
if (res.failed_cksum > 0 || res.failed_open_file > 0) && !ignore_missing {
|
||||||
set_exit_code(1);
|
set_exit_code(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if any incorrectly formatted line, show it
|
|
||||||
cksum_output(&res, ignore_missing, status);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1344,3 +1344,42 @@ fn test_check_comment_leading_space() {
|
||||||
.stdout_contains("foo: OK")
|
.stdout_contains("foo: OK")
|
||||||
.stderr_contains("WARNING: 1 line is improperly formatted");
|
.stderr_contains("WARNING: 1 line is improperly formatted");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This test checks alignment with GNU's error handling.
|
||||||
|
/// Windows has a different logic and is guarded by [`test_check_directory_error`].
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
#[test]
|
||||||
|
fn test_check_failed_to_read() {
|
||||||
|
// check `cksum`'s behavior when encountering directories or non existing files
|
||||||
|
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
|
let at = &scene.fixtures;
|
||||||
|
|
||||||
|
at.write(
|
||||||
|
"CHECKSUM",
|
||||||
|
"SHA1 (dir) = ffffffffffffffffffffffffffffffffffffffff\n\
|
||||||
|
SHA1 (not-file) = ffffffffffffffffffffffffffffffffffffffff\n",
|
||||||
|
);
|
||||||
|
at.mkdir("dir");
|
||||||
|
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.arg("--check")
|
||||||
|
.arg("CHECKSUM")
|
||||||
|
.fails()
|
||||||
|
.stdout_is(
|
||||||
|
"dir: FAILED open or read\n\
|
||||||
|
not-file: FAILED open or read\n",
|
||||||
|
)
|
||||||
|
.stderr_contains("cksum: WARNING: 2 listed files could not be read");
|
||||||
|
|
||||||
|
// check with `--ignore-missing`
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.arg("--check")
|
||||||
|
.arg("CHECKSUM")
|
||||||
|
.arg("--ignore-missing")
|
||||||
|
.fails()
|
||||||
|
.stdout_is("dir: FAILED open or read\n")
|
||||||
|
.stderr_contains("cksum: WARNING: 1 listed file could not be read");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue