1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cksum: add binary and prepare the rest

This commit is contained in:
Sylvestre Ledru 2024-04-20 18:55:36 +02:00 committed by Ben Wiederhake
parent dc5342d115
commit 10d8fd6f98
2 changed files with 73 additions and 11 deletions

View file

@ -147,6 +147,7 @@ struct Options {
untagged: bool, untagged: bool,
length: Option<usize>, length: Option<usize>,
output_format: OutputFormat, output_format: OutputFormat,
binary: bool,
} }
/// Calculate checksum /// Calculate checksum
@ -170,6 +171,8 @@ where
let stdin_buf; let stdin_buf;
let file_buf; let file_buf;
let not_file = filename == OsStr::new("-"); let not_file = filename == OsStr::new("-");
// Handle the file input
let mut file = BufReader::new(if not_file { let mut file = BufReader::new(if not_file {
stdin_buf = stdin(); stdin_buf = stdin();
Box::new(stdin_buf) as Box<dyn Read> Box::new(stdin_buf) as Box<dyn Read>
@ -185,6 +188,7 @@ where
}; };
Box::new(file_buf) as Box<dyn Read> Box::new(file_buf) as Box<dyn Read>
}); });
let (sum_hex, sz) = digest_read(&mut options.digest, &mut file, options.output_bits) let (sum_hex, sz) = digest_read(&mut options.digest, &mut file, options.output_bits)
.map_err_context(|| "failed to read input".to_string())?; .map_err_context(|| "failed to read input".to_string())?;
if filename.is_dir() { if filename.is_dir() {
@ -250,7 +254,8 @@ where
} }
_ => { _ => {
if options.untagged { if options.untagged {
println!("{sum} {}", filename.display()); let prefix = if options.binary { "*" } else { " " };
println!("{sum} {prefix}{}", filename.display());
} else { } else {
println!( println!(
"{} ({}) = {sum}", "{} ({}) = {sum}",
@ -306,6 +311,10 @@ mod options {
pub const LENGTH: &str = "length"; pub const LENGTH: &str = "length";
pub const RAW: &str = "raw"; pub const RAW: &str = "raw";
pub const BASE64: &str = "base64"; pub const BASE64: &str = "base64";
pub const CHECK: &str = "check";
// for legacy compat reasons
pub const TEXT: &str = "text";
pub const BINARY: &str = "binary";
} }
#[uucore::main] #[uucore::main]
@ -375,6 +384,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
length, length,
untagged: matches.get_flag(options::UNTAGGED), untagged: matches.get_flag(options::UNTAGGED),
output_format, output_format,
binary: matches.get_flag(options::BINARY),
}; };
match matches.get_many::<String>(options::FILE) { match matches.get_many::<String>(options::FILE) {
@ -448,15 +458,42 @@ pub fn uu_app() -> Command {
.help("emit a raw binary digest, not hexadecimal") .help("emit a raw binary digest, not hexadecimal")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
/*.arg(
Arg::new(options::STRICT)
.long(options::STRICT)
.help("exit non-zero for improperly formatted checksum lines")
.action(ArgAction::SetTrue),
)*/
.arg(
Arg::new(options::CHECK)
.short('c')
.long(options::CHECK)
.help("read hashsums from the FILEs and check them")
.action(ArgAction::SetTrue)
.conflicts_with("tag"),
)
.arg( .arg(
Arg::new(options::BASE64) Arg::new(options::BASE64)
.long(options::BASE64) .long(options::BASE64)
.short('b')
.help("emit a base64 digest, not hexadecimal") .help("emit a base64 digest, not hexadecimal")
.action(ArgAction::SetTrue) .action(ArgAction::SetTrue)
// Even though this could easily just override an earlier '--raw', // Even though this could easily just override an earlier '--raw',
// GNU cksum does not permit these flags to be combined: // GNU cksum does not permit these flags to be combined:
.conflicts_with(options::RAW), .conflicts_with(options::RAW),
) )
.arg(
Arg::new(options::TEXT)
.long(options::TEXT)
.short('t')
.hide(true) // for legacy compatibility, no action
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::BINARY)
.long(options::BINARY)
.short('b')
.hide(true) // for legacy compatibility, no action
.action(ArgAction::SetTrue),
)
.after_help(AFTER_HELP) .after_help(AFTER_HELP)
} }

View file

@ -231,6 +231,16 @@ fn test_untagged_algorithm_single_file() {
} }
} }
#[test]
fn test_tag_short() {
new_ucmd!()
.arg("-t")
.arg("--algorithm=md5")
.arg("lorem_ipsum.txt")
.succeeds()
.stdout_is("MD5 (lorem_ipsum.txt) = cd724690f7dc61775dfac400a71f2caa\n");
}
#[test] #[test]
fn test_untagged_algorithm_after_tag() { fn test_untagged_algorithm_after_tag() {
new_ucmd!() new_ucmd!()
@ -379,9 +389,8 @@ fn test_base64_raw_conflicts() {
#[test] #[test]
fn test_base64_single_file() { fn test_base64_single_file() {
for algo in ALGOS { for algo in ALGOS {
for base64_option in ["--base64", "-b"] {
new_ucmd!() new_ucmd!()
.arg(base64_option) .arg("--base64")
.arg("lorem_ipsum.txt") .arg("lorem_ipsum.txt")
.arg(format!("--algorithm={algo}")) .arg(format!("--algorithm={algo}"))
.succeeds() .succeeds()
@ -389,7 +398,6 @@ fn test_base64_single_file() {
.stdout_is_fixture_bytes(format!("base64/{algo}_single_file.expected")); .stdout_is_fixture_bytes(format!("base64/{algo}_single_file.expected"));
} }
} }
}
#[test] #[test]
fn test_base64_multiple_files() { fn test_base64_multiple_files() {
new_ucmd!() new_ucmd!()
@ -435,6 +443,23 @@ fn test_all_algorithms_fail_on_folder() {
} }
} }
#[test]
fn test_binary_file() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
scene
.ucmd()
.arg("--untagged")
.arg("-b")
.arg("--algorithm=md5")
.arg(at.subdir.join("f"))
.succeeds()
.stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
}
#[test] #[test]
fn test_folder_and_file() { fn test_folder_and_file() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());