Merge pull request #6499 from sylvestre/b2sum2

hashsum/b2sum: when the checksum file is untagged, detect the size
This commit is contained in:
Daniel Hofstetter 2024-06-28 10:44:14 +02:00 committed by GitHub
commit 4b0090b323
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 65 additions and 25 deletions

View file

@ -207,16 +207,9 @@ 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 {
@ -224,22 +217,6 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
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");
@ -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,
//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())),

View file

@ -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,
@ -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)

View file

@ -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);
}