Skip to content

Commit

Permalink
Merge pull request #5804 from Ato2207/cksum_error
Browse files Browse the repository at this point in the history
Made cksum return an error if the algorithm blake2b is used on a directory.
  • Loading branch information
cakebaker authored Jan 8, 2024
2 parents f1499d0 + 0bfd4bb commit 8e83b34
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ where
(ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"),
(ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()),
(ALGORITHM_OPTIONS_BLAKE2B, _) if !options.untagged => {
if filename.is_dir() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("{}: Is a directory", filename.display()),
)
.into());
}
if let Some(length) = options.length {
// Multiply by 8 here, as we want to print the length in bits.
println!("BLAKE2b-{} ({}) = {sum}", length * 8, filename.display());
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,17 @@ fn test_length_is_zero() {
.no_stderr()
.stdout_is_fixture("length_is_zero.expected");
}

#[test]
fn test_blake2b_fail_on_directory() {
let (at, mut ucmd) = at_and_ucmd!();

let folder_name = "a_folder";
at.mkdir(folder_name);

ucmd.arg("--algorithm=blake2b")
.arg(folder_name)
.fails()
.no_stdout()
.stderr_contains(format!("cksum: {folder_name}: Is a directory"));
}

0 comments on commit 8e83b34

Please sign in to comment.