Merge pull request #6815 from RenjiSann/cksum-zero

cksum: Implement -z/--zero
This commit is contained in:
Sylvestre Ledru 2024-10-24 08:29:34 +02:00 committed by GitHub
commit c9f8ef50d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 2 deletions

View file

@ -19,7 +19,9 @@ use uucore::checksum::{
use uucore::{
encoding,
error::{FromIo, UResult, USimpleError},
format_usage, help_about, help_section, help_usage, os_str_as_bytes, show,
format_usage, help_about, help_section, help_usage,
line_ending::LineEnding,
os_str_as_bytes, show,
sum::{div_ceil, Digest},
};
@ -42,6 +44,7 @@ struct Options {
length: Option<usize>,
output_format: OutputFormat,
asterisk: bool, // if we display an asterisk or not (--binary/--text)
line_ending: LineEnding,
}
/// Calculate checksum
@ -173,7 +176,7 @@ where
// Therefore, emit the bytes directly to stdout, without any attempt at encoding them.
let _dropped_result = stdout().write_all(os_str_as_bytes(filename.as_os_str())?);
}
println!("{after_filename}");
print!("{after_filename}{}", options.line_ending);
}
Ok(())
@ -195,6 +198,7 @@ mod options {
pub const WARN: &str = "warn";
pub const IGNORE_MISSING: &str = "ignore-missing";
pub const QUIET: &str = "quiet";
pub const ZERO: &str = "zero";
}
/// Determines whether to prompt an asterisk (*) in the output.
@ -330,6 +334,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let (tag, asterisk) = handle_tag_text_binary_flags(&matches)?;
let algo = detect_algo(algo_name, length)?;
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
let output_format = if matches.get_flag(options::RAW) {
OutputFormat::Raw
@ -347,6 +352,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
tag,
output_format,
asterisk,
line_ending,
};
match matches.get_many::<OsString>(options::FILE) {
@ -474,6 +480,15 @@ pub fn uu_app() -> Command {
.help("don't fail or report status for missing files")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::ZERO)
.long(options::ZERO)
.short('z')
.help(
"end each output line with NUL, not newline,\n and disable file name escaping",
)
.action(ArgAction::SetTrue),
)
.after_help(AFTER_HELP)
}

View file

@ -1383,3 +1383,22 @@ fn test_check_failed_to_read() {
.stdout_is("dir: FAILED open or read\n")
.stderr_contains("cksum: WARNING: 1 listed file could not be read");
}
#[test]
fn test_zero_multiple_file() {
new_ucmd!()
.arg("-z")
.arg("alice_in_wonderland.txt")
.arg("lorem_ipsum.txt")
.succeeds()
.stdout_is_fixture("zero_multiple_file.expected");
}
#[test]
fn test_zero_single_file() {
new_ucmd!()
.arg("--zero")
.arg("alice_in_wonderland.txt")
.succeeds()
.stdout_is_fixture("zero_single_file.expected");
}

Binary file not shown.

Binary file not shown.