From af3e60cc62a312af9d50146571e278614d1b0bc0 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Tue, 16 Jul 2024 01:25:29 +0200 Subject: [PATCH] cksum: accept non-UTF-8 filenames Fixes #6574. --- src/uu/cksum/src/cksum.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 0c807c8c481..12b0e79e509 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -4,8 +4,9 @@ // file that was distributed with this source code. // spell-checker:ignore (ToDO) fname, algo +use clap::builder::ValueParser; use clap::{crate_version, value_parser, Arg, ArgAction, Command}; -use std::ffi::OsStr; +use std::ffi::{OsStr, OsString}; use std::fs::File; use std::io::{self, stdin, stdout, BufReader, Read, Write}; 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 * 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 let binary_index = args.iter().position(|x| x == "--binary" || x == "-b"); 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 args: Vec = std::env::args().collect(); + let args: Vec = std::env::args_os().collect(); let had_reset = had_reset(&args); 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 - let files = matches.get_many::(options::FILE).map_or_else( + let files = matches.get_many::(options::FILE).map_or_else( || iter::once(OsStr::new("-")).collect::>(), |files| files.map(OsStr::new).collect::>(), ); @@ -337,7 +338,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { asterisk, }; - match matches.get_many::(options::FILE) { + match matches.get_many::(options::FILE) { Some(files) => cksum(opts, files.map(OsStr::new))?, None => cksum(opts, iter::once(OsStr::new("-")))?, }; @@ -356,6 +357,7 @@ pub fn uu_app() -> Command { Arg::new(options::FILE) .hide(true) .action(clap::ArgAction::Append) + .value_parser(ValueParser::os_string()) .value_hint(clap::ValueHint::FilePath), ) .arg(