Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/uucore/src/lib/features/checksum/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ fn get_file_to_check(
Ok(Box::new(f))
}
}
Err(err) => {
Err(err) if err.kind() == io::ErrorKind::NotFound => {
if !opts.ignore_missing {
// yes, we have both stderr and stdout here
print_error(err);
Expand All @@ -622,6 +622,13 @@ fn get_file_to_check(
// we could not open the file but we want to continue
Err(LineCheckError::FileNotFound)
}
Err(err) => {
// The file exists but cannot be opened. `--ignore-missing` does
// not cover this, so report it whether or not it is set.
print_error(err);
failed_open();
Err(LineCheckError::CantOpenFile)
}
}
}
}
Expand Down Expand Up @@ -1001,8 +1008,9 @@ fn process_checksum_file(
return Err(FileCheckError::Failed);
}

// If a file was missing, return an error unless we explicitly ignore it.
if res.failed_open_file > 0 && !opts.ignore_missing {
// Files we could not read are a failure. Missing files are only counted
// when they are not ignored, so this needs no further guard.
if res.failed_open_file > 0 {
return Err(FileCheckError::Failed);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,36 @@ fn test_check_failed_to_read() {
.stderr_contains("cksum: WARNING: 1 listed file could not be read");
}

#[test]
#[cfg(not(windows))]
fn test_check_ignore_missing_unopenable_file() {
// `--ignore-missing` excuses a file that is absent, not one that is
// present but cannot be opened. The latter is still a failed read.

let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.write("f", "data\n");
at.write(
"CHECKSUM",
"SHA1 (f) = c5d84736ba451747dd5f0eb9d17e104f3697ef47\n\
SHA1 (f/sub) = ffffffffffffffffffffffffffffffffffffffff\n\
SHA1 (not-file) = ffffffffffffffffffffffffffffffffffffffff\n",
);

scene
.ucmd()
.arg("--check")
.arg("--ignore-missing")
.arg("CHECKSUM")
.fails()
.stdout_is(
"f: OK\n\
f/sub: FAILED open or read\n",
)
.stderr_contains("cksum: WARNING: 1 listed file could not be read");
}

#[test]
fn test_zero_multiple_file() {
new_ucmd!()
Expand Down
Loading