Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d773b72

Browse files
committed
hashsum: don't fail on dirs
1 parent e2e5c76 commit d773b72

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/uu/hashsum/src/hashsum.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use clap::value_parser;
1111
use clap::{Arg, ArgMatches, Command};
1212
use std::ffi::{OsStr, OsString};
1313
use std::fs::File;
14-
use std::io::{BufReader, Read, stdin};
14+
use std::io::{BufReader, ErrorKind, Read, stdin};
1515
use std::iter;
1616
use std::num::ParseIntError;
1717
use std::path::Path;
@@ -559,13 +559,31 @@ where
559559
Box::new(file_buf) as Box<dyn Read>
560560
});
561561

562-
let (sum, _) = digest_reader(
562+
let sum = match digest_reader(
563563
&mut options.digest,
564564
&mut file,
565565
options.binary,
566566
options.output_bits,
567-
)
568-
.map_err_context(|| translate!("hashsum-error-failed-to-read-input"))?;
567+
) {
568+
Ok((sum, _)) => sum,
569+
Err(e) => match e.kind() {
570+
ErrorKind::IsADirectory => {
571+
eprintln!(
572+
"{}: {}: {e}",
573+
options.binary_name,
574+
filename.to_string_lossy()
575+
);
576+
err_found = Some(ChecksumError::Io(e));
577+
continue;
578+
}
579+
_ => {
580+
return Err(
581+
e.map_err_context(|| translate!("hashsum-error-failed-to-read-input"))
582+
);
583+
}
584+
},
585+
};
586+
569587
let (escaped_filename, prefix) = escape_filename(filename);
570588
if options.tag {
571589
if options.algoname == "blake2b" {

tests/by-util/test_hashsum.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,26 @@ fn test_check_directory_error() {
872872
.stderr_contains(err_msg);
873873
}
874874

875+
#[test]
876+
fn test_continue_after_directory_error() {
877+
let scene = TestScenario::new(util_name!());
878+
let at = &scene.fixtures;
879+
880+
at.mkdir("d");
881+
at.touch("file");
882+
#[cfg(not(windows))]
883+
let err_msg = "md5sum: d: Is a directory (os error 21)\n";
884+
#[cfg(windows)]
885+
let err_msg = "md5sum: d: Permission denied\n";
886+
scene
887+
.ccmd("md5sum")
888+
.arg("d")
889+
.arg("file")
890+
.fails()
891+
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e file")
892+
.stderr_contains(err_msg);
893+
}
894+
875895
#[test]
876896
fn test_check_quiet() {
877897
let scene = TestScenario::new(util_name!());

0 commit comments

Comments
 (0)