@@ -9,7 +9,7 @@ use clap::{Arg, ArgAction, Command};
99use std:: ffi:: OsString ;
1010use std:: fs;
1111use std:: os:: unix:: fs:: { MetadataExt , PermissionsExt } ;
12- use std:: path:: Path ;
12+ use std:: path:: { Path , PathBuf } ;
1313use thiserror:: Error ;
1414use uucore:: display:: Quotable ;
1515use uucore:: error:: { ExitCode , UError , UResult , USimpleError , UUsageError , set_exit_code} ;
@@ -27,17 +27,17 @@ use uucore::translate;
2727#[ derive( Debug , Error ) ]
2828enum ChmodError {
2929 #[ error( "{}" , translate!( "chmod-error-cannot-stat" , "file" => _0. quote( ) ) ) ]
30- CannotStat ( String ) ,
30+ CannotStat ( PathBuf ) ,
3131 #[ error( "{}" , translate!( "chmod-error-dangling-symlink" , "file" => _0. quote( ) ) ) ]
32- DanglingSymlink ( String ) ,
32+ DanglingSymlink ( PathBuf ) ,
3333 #[ error( "{}" , translate!( "chmod-error-no-such-file" , "file" => _0. quote( ) ) ) ]
34- NoSuchFile ( String ) ,
34+ NoSuchFile ( PathBuf ) ,
3535 #[ error( "{}" , translate!( "chmod-error-preserve-root" , "file" => _0. quote( ) ) ) ]
36- PreserveRoot ( String ) ,
36+ PreserveRoot ( PathBuf ) ,
3737 #[ error( "{}" , translate!( "chmod-error-permission-denied" , "file" => _0. quote( ) ) ) ]
38- PermissionDenied ( String ) ,
39- #[ error( "{}" , translate!( "chmod-error-new-permissions" , "file" => _0. clone ( ) , "actual" => _1. clone( ) , "expected" => _2. clone( ) ) ) ]
40- NewPermissions ( String , String , String ) ,
38+ PermissionDenied ( PathBuf ) ,
39+ #[ error( "{}" , translate!( "chmod-error-new-permissions" , "file" => _0. maybe_quote ( ) , "actual" => _1. clone( ) , "expected" => _2. clone( ) ) ) ]
40+ NewPermissions ( PathBuf , String , String ) ,
4141}
4242
4343impl UError for ChmodError { }
@@ -123,7 +123,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
123123 Some ( fref) => match fs:: metadata ( fref) {
124124 Ok ( meta) => Some ( meta. mode ( ) & 0o7777 ) ,
125125 Err ( _) => {
126- return Err ( ChmodError :: CannotStat ( fref. to_string_lossy ( ) . to_string ( ) ) . into ( ) ) ;
126+ return Err ( ChmodError :: CannotStat ( fref. into ( ) ) . into ( ) ) ;
127127 }
128128 } ,
129129 None => None ,
@@ -384,22 +384,18 @@ impl Chmoder {
384384 }
385385
386386 if !self . quiet {
387- show ! ( ChmodError :: DanglingSymlink (
388- filename. to_string_lossy( ) . to_string( )
389- ) ) ;
387+ show ! ( ChmodError :: DanglingSymlink ( filename. into( ) ) ) ;
390388 set_exit_code ( 1 ) ;
391389 }
392390
393391 if self . verbose {
394392 println ! (
395393 "{}" ,
396- translate!( "chmod-verbose-failed-dangling" , "file" => filename. to_string_lossy ( ) . quote( ) )
394+ translate!( "chmod-verbose-failed-dangling" , "file" => filename. quote( ) )
397395 ) ;
398396 }
399397 } else if !self . quiet {
400- show ! ( ChmodError :: NoSuchFile (
401- filename. to_string_lossy( ) . to_string( )
402- ) ) ;
398+ show ! ( ChmodError :: NoSuchFile ( filename. into( ) ) ) ;
403399 }
404400 // GNU exits with exit code 1 even if -q or --quiet are passed
405401 // So we set the exit code, because it hasn't been set yet if `self.quiet` is true.
@@ -412,7 +408,7 @@ impl Chmoder {
412408 continue ;
413409 }
414410 if self . recursive && self . preserve_root && file == Path :: new ( "/" ) {
415- return Err ( ChmodError :: PreserveRoot ( "/" . to_string ( ) ) . into ( ) ) ;
411+ return Err ( ChmodError :: PreserveRoot ( "/" . into ( ) ) . into ( ) ) ;
416412 }
417413 if self . recursive {
418414 r = self . walk_dir_with_context ( file, true ) ;
@@ -474,10 +470,7 @@ impl Chmoder {
474470 Err ( err) => {
475471 // Handle permission denied errors with proper file path context
476472 if err. kind ( ) == std:: io:: ErrorKind :: PermissionDenied {
477- r = r. and ( Err ( ChmodError :: PermissionDenied (
478- file_path. to_string_lossy ( ) . to_string ( ) ,
479- )
480- . into ( ) ) ) ;
473+ r = r. and ( Err ( ChmodError :: PermissionDenied ( file_path. into ( ) ) . into ( ) ) ) ;
481474 } else {
482475 r = r. and ( Err ( err. into ( ) ) ) ;
483476 }
@@ -504,7 +497,7 @@ impl Chmoder {
504497 // Handle permission denied with proper file path context
505498 let e = dir_meta. unwrap_err ( ) ;
506499 let error = if e. kind ( ) == std:: io:: ErrorKind :: PermissionDenied {
507- ChmodError :: PermissionDenied ( entry_path. to_string_lossy ( ) . to_string ( ) ) . into ( )
500+ ChmodError :: PermissionDenied ( entry_path) . into ( )
508501 } else {
509502 e. into ( )
510503 } ;
@@ -530,10 +523,7 @@ impl Chmoder {
530523 }
531524 Err ( err) => {
532525 let error = if err. kind ( ) == std:: io:: ErrorKind :: PermissionDenied {
533- ChmodError :: PermissionDenied (
534- entry_path. to_string_lossy ( ) . to_string ( ) ,
535- )
536- . into ( )
526+ ChmodError :: PermissionDenied ( entry_path) . into ( )
537527 } else {
538528 err. into ( )
539529 } ;
@@ -599,9 +589,7 @@ impl Chmoder {
599589 new_mode
600590 ) ;
601591 }
602- return Err (
603- ChmodError :: PermissionDenied ( file_path. to_string_lossy ( ) . to_string ( ) ) . into ( ) ,
604- ) ;
592+ return Err ( ChmodError :: PermissionDenied ( file_path. into ( ) ) . into ( ) ) ;
605593 }
606594
607595 // Report the change using the helper method
@@ -638,11 +626,9 @@ impl Chmoder {
638626 }
639627 Ok ( ( ) ) // Skip dangling symlinks
640628 } else if err. kind ( ) == std:: io:: ErrorKind :: PermissionDenied {
641- // These two filenames would normally be conditionally
642- // quoted, but GNU's tests expect them to always be quoted
643- Err ( ChmodError :: PermissionDenied ( file. to_string_lossy ( ) . to_string ( ) ) . into ( ) )
629+ Err ( ChmodError :: PermissionDenied ( file. into ( ) ) . into ( ) )
644630 } else {
645- Err ( ChmodError :: CannotStat ( file. to_string_lossy ( ) . to_string ( ) ) . into ( ) )
631+ Err ( ChmodError :: CannotStat ( file. into ( ) ) . into ( ) )
646632 } ;
647633 }
648634 } ;
@@ -672,7 +658,7 @@ impl Chmoder {
672658 // if a permission would have been removed if umask was 0, but it wasn't because umask was not 0, print an error and fail
673659 if ( new_mode & !naively_expected_new_mode) != 0 {
674660 return Err ( ChmodError :: NewPermissions (
675- file. to_string_lossy ( ) . to_string ( ) ,
661+ file. into ( ) ,
676662 display_permissions_unix ( new_mode as mode_t , false ) ,
677663 display_permissions_unix ( naively_expected_new_mode as mode_t , false ) ,
678664 )
0 commit comments