Skip to content

Commit 7abdd91

Browse files
committed
hashsum: Fix length processing to fix last GNU test
1 parent bed7012 commit 7abdd91

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/uu/hashsum/src/hashsum.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ use std::num::ParseIntError;
1111
use std::path::Path;
1212

1313
use clap::builder::ValueParser;
14-
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
14+
use clap::{Arg, ArgAction, ArgMatches, Command};
1515

1616
use uucore::checksum::compute::{
1717
ChecksumComputeOptions, figure_out_output_format, perform_checksum_computation,
1818
};
1919
use uucore::checksum::validate::{
2020
ChecksumValidateOptions, ChecksumVerbose, perform_checksum_validation,
2121
};
22-
use uucore::checksum::{AlgoKind, ChecksumError, SizedAlgoKind, calculate_blake2b_length};
22+
use uucore::checksum::{AlgoKind, ChecksumError, SizedAlgoKind, calculate_blake2b_length_str};
2323
use uucore::error::UResult;
2424
use uucore::line_ending::LineEnding;
2525
use uucore::{format_usage, translate};
@@ -139,14 +139,14 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
139139
// least somewhat better from a user's perspective.
140140
let matches = uucore::clap_localization::handle_clap_result(command, args)?;
141141

142-
let input_length: Option<&usize> = if binary_name == "b2sum" {
143-
matches.get_one::<usize>(options::LENGTH)
142+
let input_length: Option<&String> = if binary_name == "b2sum" {
143+
matches.get_one::<String>(options::LENGTH)
144144
} else {
145145
None
146146
};
147147

148148
let length = match input_length {
149-
Some(length) => calculate_blake2b_length(*length)?,
149+
Some(length) => calculate_blake2b_length_str(length)?,
150150
None => None,
151151
};
152152

@@ -378,7 +378,6 @@ fn uu_app_opt_length(command: Command) -> Command {
378378
command.arg(
379379
Arg::new(options::LENGTH)
380380
.long(options::LENGTH)
381-
.value_parser(value_parser!(usize))
382381
.short('l')
383382
.help(translate!("hashsum-help-length"))
384383
.overrides_with(options::LENGTH)

src/uucore/src/lib/features/checksum/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ impl SizedAlgoKind {
289289
}
290290
// [`calculate_blake2b_length`] expects a length in bits but we
291291
// have a length in bytes.
292-
(ak::Blake2b, Some(l)) => Ok(Self::Blake2b(calculate_blake2b_length(8 * l)?)),
292+
(ak::Blake2b, Some(l)) => Ok(Self::Blake2b(calculate_blake2b_length_str(
293+
&(8 * l).to_string(),
294+
)?)),
293295
(ak::Blake2b, None) => Ok(Self::Blake2b(None)),
294296

295297
(ak::Sha224, None) => Ok(Self::Sha2(ShaLength::Len224)),
@@ -442,11 +444,6 @@ pub fn digest_reader<T: Read>(
442444
Ok((digest.result(), output_size))
443445
}
444446

445-
/// Calculates the length of the digest.
446-
pub fn calculate_blake2b_length(bit_length: usize) -> UResult<Option<usize>> {
447-
calculate_blake2b_length_str(bit_length.to_string().as_str())
448-
}
449-
450447
/// Calculates the length of the digest.
451448
pub fn calculate_blake2b_length_str(bit_length: &str) -> UResult<Option<usize>> {
452449
// Blake2b's length is parsed in an u64.
@@ -596,10 +593,10 @@ mod tests {
596593

597594
#[test]
598595
fn test_calculate_blake2b_length() {
599-
assert_eq!(calculate_blake2b_length(0).unwrap(), None);
600-
assert!(calculate_blake2b_length(10).is_err());
601-
assert!(calculate_blake2b_length(520).is_err());
602-
assert_eq!(calculate_blake2b_length(512).unwrap(), None);
603-
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
596+
assert_eq!(calculate_blake2b_length_str("0").unwrap(), None);
597+
assert!(calculate_blake2b_length_str("10").is_err());
598+
assert!(calculate_blake2b_length_str("520").is_err());
599+
assert_eq!(calculate_blake2b_length_str("512").unwrap(), None);
600+
assert_eq!(calculate_blake2b_length_str("256").unwrap(), Some(32));
604601
}
605602
}

0 commit comments

Comments
 (0)