Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashsum: implement the ignore-missing option #6230

Merged
merged 2 commits into from
Apr 17, 2024
Merged
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
27 changes: 26 additions & 1 deletion src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::iter;
use std::num::ParseIntError;
use std::path::Path;
use uucore::error::USimpleError;
use uucore::error::{FromIo, UError, UResult};
use uucore::error::{set_exit_code, FromIo, UError, UResult};
use uucore::sum::{
Blake2b, Blake3, Digest, DigestWriter, Md5, Sha1, Sha224, Sha256, Sha384, Sha3_224, Sha3_256,
Sha3_384, Sha3_512, Sha512, Shake128, Shake256,
Expand All @@ -46,6 +46,7 @@ struct Options {
warn: bool,
output_bits: usize,
zero: bool,
ignore_missing: bool,
}

/// Creates a Blake2b hasher instance based on the specified length argument.
Expand Down Expand Up @@ -345,6 +346,12 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
let strict = matches.get_flag("strict");
let warn = matches.get_flag("warn") && !status;
let zero = matches.get_flag("zero");
let ignore_missing = matches.get_flag("ignore-missing");

if ignore_missing && !check {
// --ignore-missing needs -c
return Err(HashsumError::IgnoreNotCheck.into());
}

let opts = Options {
algoname: name,
Expand All @@ -359,6 +366,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
strict,
warn,
zero,
ignore_missing,
};

match matches.get_many::<OsString>("FILE") {
Expand Down Expand Up @@ -431,6 +439,12 @@ pub fn uu_app_common() -> Command {
.help("exit non-zero for improperly formatted checksum lines")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("ignore-missing")
.long("ignore-missing")
.help("don't fail or report status for missing files")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("warn")
.short('w')
Expand Down Expand Up @@ -564,6 +578,7 @@ fn uu_app(binary_name: &str) -> Command {
enum HashsumError {
InvalidRegex,
InvalidFormat,
IgnoreNotCheck,
}

impl Error for HashsumError {}
Expand All @@ -574,6 +589,10 @@ impl std::fmt::Display for HashsumError {
match self {
Self::InvalidRegex => write!(f, "invalid regular expression"),
Self::InvalidFormat => Ok(()),
Self::IgnoreNotCheck => write!(
f,
"the --ignore-missing option is meaningful only when verifying checksums"
),
}
}
}
Expand Down Expand Up @@ -705,13 +724,19 @@ where
let (ck_filename_unescaped, prefix) = unescape_filename(&ck_filename);
let f = match File::open(ck_filename_unescaped) {
Err(_) => {
if options.ignore_missing {
// No need to show or return an error.
continue;
}

failed_open_file += 1;
println!(
"{}: {}: No such file or directory",
uucore::util_name(),
ck_filename
);
println!("{ck_filename}: FAILED open or read");
set_exit_code(1);
continue;
}
Ok(file) => file,
Expand Down
36 changes: 35 additions & 1 deletion tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,40 @@ fn test_check_sha1() {
.stderr_is("");
}

#[test]
fn test_check_md5_ignore_missing() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.write("testf", "foobar\n");
at.write(
"testf.sha1",
"14758f1afd44c09b7992073ccf00b43d testf\n14758f1afd44c09b7992073ccf00b43d testf2\n",
);
scene
.ccmd("md5sum")
.arg("-c")
.arg(at.subdir.join("testf.sha1"))
.fails()
.stdout_contains("testf2: FAILED open or read");

scene
.ccmd("md5sum")
.arg("-c")
.arg("--ignore-missing")
.arg(at.subdir.join("testf.sha1"))
.succeeds()
.stdout_is("testf: OK\n")
.stderr_is("");

scene
.ccmd("md5sum")
.arg("--ignore-missing")
.arg(at.subdir.join("testf.sha1"))
.fails()
.stderr_contains("the --ignore-missing option is meaningful only when verifying checksums");
}

#[test]
fn test_check_b2sum_length_option_0() {
let scene = TestScenario::new(util_name!());
Expand Down Expand Up @@ -208,7 +242,7 @@ fn test_check_file_not_found_warning() {
.ccmd("sha1sum")
.arg("-c")
.arg(at.subdir.join("testf.sha1"))
.succeeds()
.fails()
.stdout_is("sha1sum: testf: No such file or directory\ntestf: FAILED open or read\n")
.stderr_is("sha1sum: warning: 1 listed file could not be read\n");
}
Expand Down
Loading