@@ -317,9 +317,27 @@ fn bin_main() -> BinResult<()> {
317317 let mut stdio_tmp;
318318 let mut print_terminal_err = false ;
319319 let out: & mut dyn io:: Write = match output_path {
320- DestPath :: Path ( p) => {
321- file_tmp = File :: create ( p)
322- . map_err ( |e| format ! ( "Can't write to {}: {e}" , p. display( ) ) ) ?;
320+ DestPath :: Path ( path) => {
321+ file_tmp = File :: create ( path)
322+ . map_err ( |err| {
323+ let mut msg = format ! ( "Can't write to \" {}\" : {err}" , path. display( ) ) ;
324+ let canon = path. canonicalize ( ) ;
325+ if let Some ( parent) = canon. as_deref ( ) . unwrap_or ( path) . parent ( ) {
326+ if parent. as_os_str ( ) != "" {
327+ use std:: fmt:: Write ;
328+ match parent. try_exists ( ) {
329+ Ok ( true ) => { } ,
330+ Ok ( false ) => {
331+ let _ = write ! ( & mut msg, " (directory \" {}\" doesn't exist)" , parent. display( ) ) ;
332+ } ,
333+ Err ( err) => {
334+ let _ = write ! ( & mut msg, " (directory \" {}\" is not accessible: {err})" , parent. display( ) ) ;
335+ } ,
336+ }
337+ }
338+ }
339+ msg
340+ } ) ?;
323341 & mut file_tmp
324342 } ,
325343 DestPath :: Stdout => {
@@ -455,18 +473,34 @@ fn check_if_paths_exist(paths: &[PathBuf]) -> BinResult<()> {
455473 if path. as_os_str ( ) == "-" && paths. len ( ) == 1 {
456474 break ;
457475 }
458- if !path. exists ( ) {
459- let mut msg = format ! ( "Unable to find the input file: \" {}\" " , path. display( ) ) ;
460- if path. to_str ( ) . map_or ( false , |p| p. contains ( [ '*' , '?' , '[' ] ) ) {
461- msg += "\n The pattern did not match any files." ;
462- } else if path. extension ( ) == Some ( "gif" . as_ref ( ) ) {
463- msg = format ! ( "Did you mean to use -o \" {}\" to specify it as the output file instead?" , path. display( ) ) ;
464- } else if path. is_relative ( ) {
465- use std:: fmt:: Write ;
466- write ! ( & mut msg, " (searched in \" {}\" )" , env:: current_dir( ) ?. display( ) ) ?;
476+ let mut msg = match path. try_exists ( ) {
477+ Ok ( true ) => continue ,
478+ Ok ( false ) => format ! ( "Unable to find the input file: \" {}\" " , path. display( ) ) ,
479+ Err ( err) => format ! ( "Unable to access the input file \" {}\" : {err}" , path. display( ) ) ,
480+ } ;
481+ let canon = path. canonicalize ( ) ;
482+ if let Some ( parent) = canon. as_deref ( ) . unwrap_or ( path) . parent ( ) {
483+ if parent. as_os_str ( ) != "" {
484+ if let Ok ( false ) = path. try_exists ( ) {
485+ use std:: fmt:: Write ;
486+ if msg. len ( ) > 80 {
487+ msg. push ( '\n' ) ;
488+ }
489+ write ! ( & mut msg, " (directory \" {}\" doesn't exist either)" , parent. display( ) ) ?;
490+
491+ }
467492 }
468- return Err ( msg. into ( ) )
469493 }
494+ if path. to_str ( ) . map_or ( false , |p| p. contains ( [ '*' , '?' , '[' ] ) ) {
495+ msg += "\n The wildcard pattern did not match any files." ;
496+ } else if path. is_relative ( ) {
497+ use std:: fmt:: Write ;
498+ write ! ( & mut msg, " (searched in \" {}\" )" , env:: current_dir( ) ?. display( ) ) ?;
499+ }
500+ if path. extension ( ) == Some ( "gif" . as_ref ( ) ) {
501+ msg = format ! ( "\n Did you mean to use -o \" {}\" to specify it as the output file instead?" , path. display( ) ) ;
502+ }
503+ return Err ( msg. into ( ) )
470504 }
471505 Ok ( ( ) )
472506}
0 commit comments