Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 20 additions & 23 deletions src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,39 +207,16 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
binary_flag_default
};
let check = matches.get_flag("check");
let tag = matches.get_flag("tag");
let nonames = *matches
.try_get_one("no-names")
.unwrap_or(None)
.unwrap_or(&false);
let status = matches.get_flag("status");
let quiet = matches.get_flag("quiet") || status;
//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(ChecksumError::IgnoreNotCheck.into());
}

let opts = Options {
algoname: algo.name,
digest: (algo.create_fn)(),
output_bits: algo.bits,
binary,
//check,
tag,
nonames,
//status,
//quiet,
//strict,
//warn,
zero,
//ignore_missing,
};

if check {
let text_flag = matches.get_flag("text");
let binary_flag = matches.get_flag("binary");
Expand Down Expand Up @@ -270,6 +247,26 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
);
}

let nonames = *matches
.try_get_one("no-names")
.unwrap_or(None)
.unwrap_or(&false);
let zero = matches.get_flag("zero");

let opts = Options {
algoname: algo.name,
digest: (algo.create_fn)(),
output_bits: algo.bits,
binary,
tag: matches.get_flag("tag"),
nonames,
//status,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix/remove these comments after it lands

//quiet,
//warn,
zero,
//ignore_missing,
};

// Show the hashsum of the input
match matches.get_many::<OsString>(options::FILE) {
Some(files) => hashsum(opts, files.map(|f| f.as_os_str())),
Expand Down
11 changes: 9 additions & 2 deletions src/uucore/src/lib/features/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,6 @@ where
get_expected_checksum(filename_to_check, &caps, &chosen_regex)?;

// If the algo_name is provided, we use it, otherwise we try to detect it

let (algo_name, length) = if is_algo_based_format {
identify_algo_name_and_length(
&caps,
Expand All @@ -513,7 +512,15 @@ where
.unwrap_or((String::new(), None))
} else if let Some(a) = algo_name_input {
// When a specific algorithm name is input, use it and use the provided bits
(a.to_lowercase(), length_input)
// except when dealing with blake2b, where we will detect the length
if algo_name_input == Some(ALGORITHM_OPTIONS_BLAKE2B) {
// division by 2 converts the length of the Blake2b checksum from hexadecimal
// characters to bytes, as each byte is represented by two hexadecimal characters.
let length = Some(expected_checksum.len() / 2);
(ALGORITHM_OPTIONS_BLAKE2B.to_string(), length)
} else {
(a.to_lowercase(), length_input)
}
} else {
// Default case if no algorithm is specified and non-algo based format is matched
(String::new(), None)
Expand Down
36 changes: 36 additions & 0 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,3 +890,39 @@ fn test_star_to_start() {
.succeeds()
.stdout_only("f: OK\n");
}

#[test]
fn test_check_b2sum_strict_check() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");

let checksums = [
"2e f\n",
"e4a6a0577479b2b4 f\n",
"cae66941d9efbd404e4d88758ea67670 f\n",
"246c0442cd564aced8145b8b60f1370aa7 f\n",
"0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 f\n",
"4ded8c5fc8b12f3273f877ca585a44ad6503249a2b345d6d9c0e67d85bcb700db4178c0303e93b8f4ad758b8e2c9fd8b3d0c28e585f1928334bb77d36782e8 f\n",
"786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce f\n",
];

at.write("ck", &checksums.join(""));

let output = "f: OK\n".to_string().repeat(checksums.len());

scene
.ccmd("b2sum")
.arg("-c")
.arg(at.subdir.join("ck"))
.succeeds()
.stdout_only(&output);

scene
.ccmd("b2sum")
.arg("--strict")
.arg("-c")
.arg(at.subdir.join("ck"))
.succeeds()
.stdout_only(&output);
}