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

cksum: accept non-UTF-8 filenames

Fixes #6574.
This commit is contained in:
Ben Wiederhake 2024-07-16 01:25:29 +02:00
parent a55d9b359d
commit 244634b5c6

View file

@ -4,8 +4,9 @@
// file that was distributed with this source code. // file that was distributed with this source code.
// spell-checker:ignore (ToDO) fname, algo // spell-checker:ignore (ToDO) fname, algo
use clap::builder::ValueParser;
use clap::{crate_version, value_parser, Arg, ArgAction, Command}; use clap::{crate_version, value_parser, Arg, ArgAction, Command};
use std::ffi::OsStr; use std::ffi::{OsStr, OsString};
use std::fs::File; use std::fs::File;
use std::io::{self, stdin, stdout, BufReader, Read, Write}; use std::io::{self, stdin, stdout, BufReader, Read, Write};
use std::iter; use std::iter;
@ -209,7 +210,7 @@ fn prompt_asterisk(tag: bool, binary: bool, had_reset: bool) -> bool {
* Don't do it with clap because if it struggling with the --overrides_with * Don't do it with clap because if it struggling with the --overrides_with
* marking the value as set even if not present * marking the value as set even if not present
*/ */
fn had_reset(args: &[String]) -> bool { fn had_reset(args: &[OsString]) -> bool {
// Indices where "--binary" or "-b", "--tag", and "--untagged" are found // Indices where "--binary" or "-b", "--tag", and "--untagged" are found
let binary_index = args.iter().position(|x| x == "--binary" || x == "-b"); let binary_index = args.iter().position(|x| x == "--binary" || x == "-b");
let tag_index = args.iter().position(|x| x == "--tag"); let tag_index = args.iter().position(|x| x == "--tag");
@ -234,7 +235,7 @@ fn handle_tag_text_binary_flags(matches: &clap::ArgMatches) -> UResult<(bool, bo
let binary_flag: bool = matches.get_flag(options::BINARY); let binary_flag: bool = matches.get_flag(options::BINARY);
let args: Vec<String> = std::env::args().collect(); let args: Vec<OsString> = std::env::args_os().collect();
let had_reset = had_reset(&args); let had_reset = had_reset(&args);
let asterisk: bool = prompt_asterisk(tag, binary_flag, had_reset); let asterisk: bool = prompt_asterisk(tag, binary_flag, had_reset);
@ -298,7 +299,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// Execute the checksum validation based on the presence of files or the use of stdin // Execute the checksum validation based on the presence of files or the use of stdin
let files = matches.get_many::<String>(options::FILE).map_or_else( let files = matches.get_many::<OsString>(options::FILE).map_or_else(
|| iter::once(OsStr::new("-")).collect::<Vec<_>>(), || iter::once(OsStr::new("-")).collect::<Vec<_>>(),
|files| files.map(OsStr::new).collect::<Vec<_>>(), |files| files.map(OsStr::new).collect::<Vec<_>>(),
); );
@ -337,7 +338,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
asterisk, asterisk,
}; };
match matches.get_many::<String>(options::FILE) { match matches.get_many::<OsString>(options::FILE) {
Some(files) => cksum(opts, files.map(OsStr::new))?, Some(files) => cksum(opts, files.map(OsStr::new))?,
None => cksum(opts, iter::once(OsStr::new("-")))?, None => cksum(opts, iter::once(OsStr::new("-")))?,
}; };
@ -356,6 +357,7 @@ pub fn uu_app() -> Command {
Arg::new(options::FILE) Arg::new(options::FILE)
.hide(true) .hide(true)
.action(clap::ArgAction::Append) .action(clap::ArgAction::Append)
.value_parser(ValueParser::os_string())
.value_hint(clap::ValueHint::FilePath), .value_hint(clap::ValueHint::FilePath),
) )
.arg( .arg(
@ -469,61 +471,62 @@ mod tests {
use super::had_reset; use super::had_reset;
use crate::calculate_blake2b_length; use crate::calculate_blake2b_length;
use crate::prompt_asterisk; use crate::prompt_asterisk;
use std::ffi::OsString;
#[test] #[test]
fn test_had_reset() { fn test_had_reset() {
let args = ["--binary", "--tag", "--untagged"] let args = ["--binary", "--tag", "--untagged"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(had_reset(&args)); assert!(had_reset(&args));
let args = ["-b", "--tag", "--untagged"] let args = ["-b", "--tag", "--untagged"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(had_reset(&args)); assert!(had_reset(&args));
let args = ["-b", "--binary", "--tag", "--untagged"] let args = ["-b", "--binary", "--tag", "--untagged"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(had_reset(&args)); assert!(had_reset(&args));
let args = ["--untagged", "--tag", "--binary"] let args = ["--untagged", "--tag", "--binary"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(!had_reset(&args)); assert!(!had_reset(&args));
let args = ["--untagged", "--tag", "-b"] let args = ["--untagged", "--tag", "-b"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(!had_reset(&args)); assert!(!had_reset(&args));
let args = ["--binary", "--tag"] let args = ["--binary", "--tag"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(!had_reset(&args)); assert!(!had_reset(&args));
let args = ["--tag", "--untagged"] let args = ["--tag", "--untagged"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(!had_reset(&args)); assert!(!had_reset(&args));
let args = ["--text", "--untagged"] let args = ["--text", "--untagged"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(!had_reset(&args)); assert!(!had_reset(&args));
let args = ["--binary", "--untagged"] let args = ["--binary", "--untagged"]
.iter() .iter()
.map(|&s| s.to_string()) .map(|&s| s.into())
.collect::<Vec<String>>(); .collect::<Vec<OsString>>();
assert!(!had_reset(&args)); assert!(!had_reset(&args));
} }