Skip to content

Commit

Permalink
cksum: correctly output non-utf8 filename
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWiederhake committed Jul 16, 2024
1 parent 24c8b99 commit ed05338
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 33 deletions.
96 changes: 63 additions & 33 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,52 +117,82 @@ where
};
// The BSD checksum output is 5 digit integer
let bsd_width = 5;
match (options.algo_name, not_file) {
(ALGORITHM_OPTIONS_SYSV, true) => println!(
"{} {}",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits)
let (before_filename, print_filename, after_filename) = match (options.algo_name, not_file)
{
(ALGORITHM_OPTIONS_SYSV, true) => (
format!(
"{} {}",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits)
),
false,
String::new(),
),
(ALGORITHM_OPTIONS_SYSV, false) => println!(
"{} {} {}",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits),
filename.display()
(ALGORITHM_OPTIONS_SYSV, false) => (
format!(
"{} {} ",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits)
),
true,
String::new(),
),
(ALGORITHM_OPTIONS_BSD, true) => println!(
"{:0bsd_width$} {:bsd_width$}",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits)
(ALGORITHM_OPTIONS_BSD, true) => (
format!(
"{:0bsd_width$} {:bsd_width$}",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits)
),
false,
String::new(),
),
(ALGORITHM_OPTIONS_BSD, false) => println!(
"{:0bsd_width$} {:bsd_width$} {}",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits),
filename.display()
(ALGORITHM_OPTIONS_BSD, false) => (
format!(
"{:0bsd_width$} {:bsd_width$} ",
sum.parse::<u16>().unwrap(),
div_ceil(sz, options.output_bits),
),
true,
String::new(),
),
(ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"),
(ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()),
(ALGORITHM_OPTIONS_CRC, true) => (format!("{sum} {sz}"), false, String::new()),
(ALGORITHM_OPTIONS_CRC, false) => (format!("{sum} {sz} "), true, String::new()),
(ALGORITHM_OPTIONS_BLAKE2B, _) if options.tag => {
if let Some(length) = options.length {
// Multiply by 8 here, as we want to print the length in bits.
println!("BLAKE2b-{} ({}) = {sum}", length * 8, filename.display());
} else {
println!("BLAKE2b ({}) = {sum}", filename.display());
}
(
if let Some(length) = options.length {
// Multiply by 8 here, as we want to print the length in bits.
format!("BLAKE2b-{} (", length * 8)
} else {
format!("BLAKE2b (")
},
true,
format!(") = {sum}"),
)
}
_ => {
if options.tag {
println!(
"{} ({}) = {sum}",
options.algo_name.to_ascii_uppercase(),
filename.display()
);
(

Check failure on line 174 in src/uu/cksum/src/cksum.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/cksum/src/cksum.rs', line:174; use `cargo fmt -- "src/uu/cksum/src/cksum.rs"`)

Check failure on line 174 in src/uu/cksum/src/cksum.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/cksum/src/cksum.rs', line:174; use `cargo fmt -- "src/uu/cksum/src/cksum.rs"`)
format!("{} (", options.algo_name.to_ascii_uppercase()),
true,
format!(") = {sum}")
)
} else {
let prefix = if options.asterisk { "*" } else { " " };
println!("{sum} {prefix}{}", filename.display());
(

Check failure on line 181 in src/uu/cksum/src/cksum.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/cksum/src/cksum.rs', line:181; use `cargo fmt -- "src/uu/cksum/src/cksum.rs"`)

Check failure on line 181 in src/uu/cksum/src/cksum.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/cksum/src/cksum.rs', line:181; use `cargo fmt -- "src/uu/cksum/src/cksum.rs"`)
format!("{sum} {prefix}"),
true,
String::new()
)
}
}
};
print!("{}", before_filename);
if print_filename {

Check failure on line 190 in src/uu/cksum/src/cksum.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/cksum/src/cksum.rs', line:190; use `cargo fmt -- "src/uu/cksum/src/cksum.rs"`)

Check failure on line 190 in src/uu/cksum/src/cksum.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/cksum/src/cksum.rs', line:190; use `cargo fmt -- "src/uu/cksum/src/cksum.rs"`)
// The filename might not be valid UTF-8, and filename.display() would mangle the names.
// Therefore, emit the bytes directly to stdout, without any attempt at encoding them.
stdout().write(filename.as_os_str().as_encoded_bytes()).expect("FIXME");
}
println!("{}", after_filename);
}

Ok(())
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

use crate::common::util::TestScenario;

#[cfg(unix)]
use std::ffi::OsString;
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;

const ALGOS: [&str; 11] = [
"sysv", "bsd", "crc", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "blake2b", "sm3",
];
Expand Down Expand Up @@ -1250,3 +1255,27 @@ fn test_several_files_error_mgmt() {
.stderr_contains("empty: no properly ")
.stderr_contains("incorrect: no properly ");
}

#[cfg(unix)]
#[test]
fn test_non_utf8_filename() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let filename: OsString = OsStringExt::from_vec(b"funky\xffname".to_vec());

at.touch(filename.clone());

scene
.ucmd()
.arg(filename.clone())
.succeeds()
.stdout_is_bytes(b"4294967295 0 funky\xffname\n")
.no_stderr();
scene
.ucmd()
.arg("-asha256")
.arg(filename)
.succeeds()
.stdout_is_bytes(b"SHA256 (funky\xffname) = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n")
.no_stderr();
}

0 comments on commit ed05338

Please sign in to comment.