1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 03:26:18 +00:00

Merge pull request #6230 from sylvestre/hash-ignore-missing

hashsum: implement the ignore-missing option
This commit is contained in:
Sylvestre Ledru 2024-04-17 23:43:38 +02:00 committed by GitHub
commit 4e1dbcd3fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 2 deletions

View file

@ -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,
@ -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.
@ -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,
@ -359,6 +366,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
strict,
warn,
zero,
ignore_missing,
};
match matches.get_many::<OsString>("FILE") {
@ -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')
@ -564,6 +578,7 @@ fn uu_app(binary_name: &str) -> Command {
enum HashsumError {
InvalidRegex,
InvalidFormat,
IgnoreNotCheck,
}
impl Error for HashsumError {}
@ -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"
),
}
}
}
@ -705,6 +724,11 @@ 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",
@ -712,6 +736,7 @@ where
ck_filename
);
println!("{ck_filename}: FAILED open or read");
set_exit_code(1);
continue;
}
Ok(file) => file,

View file

@ -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!());
@ -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");
}