mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Made cksum return an error if used on a directory. (#5822)
* Made cksum to return an error if it is used on a directory regardless of the algorithm * Added one more test for cksum on folders and deleted an old one that expected it to succeed instead of fail * Made cksum work on more than one item if it fails and added a test for this case
This commit is contained in:
parent
7e3f4d8358
commit
563df4b79d
2 changed files with 49 additions and 25 deletions
|
@ -15,8 +15,8 @@ use std::io::{self, stdin, stdout, BufReader, Read, Write};
|
|||
use std::iter;
|
||||
use std::path::Path;
|
||||
use uucore::{
|
||||
error::{FromIo, UError, UResult},
|
||||
format_usage, help_about, help_section, help_usage,
|
||||
error::{FromIo, UError, UResult, USimpleError},
|
||||
format_usage, help_about, help_section, help_usage, show,
|
||||
sum::{
|
||||
div_ceil, Blake2b, Digest, DigestWriter, Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sm3,
|
||||
BSD, CRC, SYSV,
|
||||
|
@ -174,7 +174,13 @@ where
|
|||
});
|
||||
let (sum, sz) = digest_read(&mut options.digest, &mut file, options.output_bits)
|
||||
.map_err_context(|| "failed to read input".to_string())?;
|
||||
|
||||
if filename.is_dir() {
|
||||
show!(USimpleError::new(
|
||||
1,
|
||||
format!("{}: Is a directory", filename.display())
|
||||
));
|
||||
continue;
|
||||
}
|
||||
if options.raw {
|
||||
let bytes = match options.algo_name {
|
||||
ALGORITHM_OPTIONS_CRC => sum.parse::<u32>().unwrap().to_be_bytes().to_vec(),
|
||||
|
@ -214,13 +220,6 @@ 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());
|
||||
|
|
|
@ -80,18 +80,6 @@ fn test_nonexisting_file() {
|
|||
.stderr_contains(format!("cksum: {file_name}: No such file or directory"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_folder() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
|
||||
let folder_name = "a_folder";
|
||||
at.mkdir(folder_name);
|
||||
|
||||
ucmd.arg(folder_name)
|
||||
.succeeds()
|
||||
.stdout_only(format!("4294967295 0 {folder_name}\n"));
|
||||
}
|
||||
|
||||
// Make sure crc is correct for files larger than 32 bytes
|
||||
// but <128 bytes (1 fold pclmul) // spell-checker:disable-line
|
||||
#[test]
|
||||
|
@ -312,15 +300,52 @@ fn test_raw_multiple_files() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_blake2b_fail_on_directory() {
|
||||
fn test_fail_on_folder() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
|
||||
let folder_name = "a_folder";
|
||||
at.mkdir(folder_name);
|
||||
|
||||
ucmd.arg("--algorithm=blake2b")
|
||||
.arg(folder_name)
|
||||
ucmd.arg(folder_name)
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains(format!("cksum: {folder_name}: Is a directory"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_algorithms_fail_on_folder() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
|
||||
let at = &scene.fixtures;
|
||||
|
||||
let folder_name = "a_folder";
|
||||
at.mkdir(folder_name);
|
||||
|
||||
for algo in ALGOS {
|
||||
scene
|
||||
.ucmd()
|
||||
.arg(format!("--algorithm={algo}"))
|
||||
.arg(folder_name)
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains(format!("cksum: {folder_name}: Is a directory"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_folder_and_file() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
|
||||
let at = &scene.fixtures;
|
||||
|
||||
let folder_name = "a_folder";
|
||||
at.mkdir(folder_name);
|
||||
|
||||
scene
|
||||
.ucmd()
|
||||
.arg(folder_name)
|
||||
.arg("lorem_ipsum.txt")
|
||||
.fails()
|
||||
.stderr_contains(format!("cksum: {folder_name}: Is a directory"))
|
||||
.stdout_is_fixture("crc_single_file.expected");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue