@@ -18,7 +18,9 @@ use std::{
1818
1919use crate :: {
2020 error:: { FromIo , UError , UResult , USimpleError } ,
21- os_str_as_bytes, os_str_from_bytes, read_os_string_lines, show, show_error, show_warning_caps,
21+ os_str_as_bytes, os_str_from_bytes,
22+ quoting_style:: { QuotingStyle , locale_aware_escape_name} ,
23+ read_os_string_lines, show, show_error, show_warning_caps,
2224 sum:: {
2325 Blake2b , Blake3 , Bsd , CRC32B , Crc , Digest , DigestWriter , Md5 , Sha1 , Sha3_224 , Sha3_256 ,
2426 Sha3_384 , Sha3_512 , Sha224 , Sha256 , Sha384 , Sha512 , Shake128 , Shake256 , Sm3 , SysV ,
@@ -734,7 +736,7 @@ fn get_file_to_check(
734736 opts : ChecksumOptions ,
735737) -> Result < Box < dyn Read > , LineCheckError > {
736738 let filename_bytes = os_str_as_bytes ( filename) . expect ( "UTF-8 error" ) ;
737- let filename_lossy = String :: from_utf8_lossy ( filename_bytes ) ;
739+
738740 if filename == "-" {
739741 Ok ( Box :: new ( stdin ( ) ) ) // Use stdin if "-" is specified in the checksum file
740742 } else {
@@ -747,15 +749,23 @@ fn get_file_to_check(
747749 opts. verbose ,
748750 ) ;
749751 } ;
752+ let print_error = |err : io:: Error | {
753+ show ! ( err. map_err_context( || {
754+ locale_aware_escape_name( filename, QuotingStyle :: SHELL_ESCAPE )
755+ // This is non destructive thanks to the escaping
756+ . to_string_lossy( )
757+ . to_string( )
758+ } ) ) ;
759+ } ;
750760 match File :: open ( filename) {
751761 Ok ( f) => {
752762 if f. metadata ( )
753763 . map_err ( |_| LineCheckError :: CantOpenFile ) ?
754764 . is_dir ( )
755765 {
756- show ! ( USimpleError :: new(
757- 1 ,
758- format! ( "{filename_lossy}: Is a directory")
766+ print_error ( io :: Error :: new (
767+ io :: ErrorKind :: IsADirectory ,
768+ " Is a directory",
759769 ) ) ;
760770 // also regarded as a failed open
761771 failed_open ( ) ;
@@ -767,7 +777,7 @@ fn get_file_to_check(
767777 Err ( err) => {
768778 if !opts. ignore_missing {
769779 // yes, we have both stderr and stdout here
770- show ! ( err. map_err_context ( || filename_lossy . to_string ( ) ) ) ;
780+ print_error ( err) ;
771781 failed_open ( ) ;
772782 }
773783 // we could not open the file but we want to continue
0 commit comments