From d46e5df3d3fe4ee6c76cf0a9b41408c6e3000714 Mon Sep 17 00:00:00 2001 From: David Matos Date: Wed, 19 Oct 2022 20:09:56 +0200 Subject: [PATCH 1/2] hashsum: warn on file not found rather than fail --- src/uu/hashsum/src/hashsum.rs | 31 +++++++++++++++++++++++++------ tests/by-util/test_hashsum.rs | 20 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/uu/hashsum/src/hashsum.rs b/src/uu/hashsum/src/hashsum.rs index e2e90434339..828436595b6 100644 --- a/src/uu/hashsum/src/hashsum.rs +++ b/src/uu/hashsum/src/hashsum.rs @@ -498,7 +498,8 @@ where I: Iterator, { let mut bad_format = 0; - let mut failed = 0; + let mut failed_cksum = 0; + let mut failed_open_file = 0; let binary_marker = if options.binary { "*" } else { " " }; for filename in files { let filename = Path::new(filename); @@ -574,8 +575,19 @@ where } }, }; - let f = File::open(ck_filename) - .map_err_context(|| "failed to open file".to_string())?; + let f = match File::open(ck_filename) { + Err(_) => { + failed_open_file += 1; + println!( + "{}: {}: No such file or directory", + uucore::util_name(), + ck_filename + ); + println!("{}: FAILED open or read", ck_filename); + continue; + } + Ok(file) => file, + }; let mut ckf = BufReader::new(Box::new(f) as Box); let real_sum = digest_reader( &mut options.digest, @@ -602,7 +614,7 @@ where if !options.status { println!("{}: FAILED", ck_filename); } - failed += 1; + failed_cksum += 1; } } } else { @@ -628,8 +640,15 @@ where Ordering::Greater => show_warning!("{} lines are improperly formatted", bad_format), _ => {} }; - if failed > 0 { - show_warning!("{} computed checksum did NOT match", failed); + if failed_cksum > 0 { + show_warning!("{} computed checksum did NOT match", failed_cksum); + } + match failed_open_file.cmp(&1) { + Ordering::Equal => show_warning!("{} listed file could not be read", failed_open_file), + Ordering::Greater => { + show_warning!("{} listed files could not be read", failed_open_file); + } + _ => {} } } diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 5fdc6e648fa..dea958befc4 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -117,6 +117,26 @@ fn test_check_sha1() { .stderr_is(""); } +#[test] +fn test_check_file_not_found_warning() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("testf", "foobar\n"); + at.write( + "testf.sha1", + "988881adc9fc3655077dc2d4d757d480b5ea0e11 testf\n", + ); + at.remove("testf"); + scene + .ccmd("sha1sum") + .arg("-c") + .arg(at.subdir.join("testf.sha1")) + .succeeds() + .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"); +} + #[test] fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); From 49c435a25bce22c5bd102abb3b1129192d4e4ad4 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 20 Oct 2022 07:46:38 +0200 Subject: [PATCH 2/2] ignore a word --- tests/by-util/test_hashsum.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index dea958befc4..f30eb42cea7 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -1,5 +1,5 @@ use crate::common::util::*; -// spell-checker:ignore checkfile, nonames, testf +// spell-checker:ignore checkfile, nonames, testf, ntestf macro_rules! get_hash( ($str:expr) => ( $str.split(' ').collect::>()[0]