@@ -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 ) ,
37- #[ 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 ) ,
36+ PreserveRoot ( PathBuf ) ,
37+ #[ error( "{}" , translate!( "chmod-error-permission-denied" , "file" => _0. maybe_quote ( ) ) ) ]
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 } ;
@@ -584,9 +577,7 @@ impl Chmoder {
584577 new_mode
585578 ) ;
586579 }
587- return Err (
588- ChmodError :: PermissionDenied ( file_path. to_string_lossy ( ) . to_string ( ) ) . into ( ) ,
589- ) ;
580+ return Err ( ChmodError :: PermissionDenied ( file_path. into ( ) ) . into ( ) ) ;
590581 }
591582
592583 // Report the change using the helper method
@@ -625,9 +616,9 @@ impl Chmoder {
625616 } else if err. kind ( ) == std:: io:: ErrorKind :: PermissionDenied {
626617 // These two filenames would normally be conditionally
627618 // quoted, but GNU's tests expect them to always be quoted
628- Err ( ChmodError :: PermissionDenied ( file. to_string_lossy ( ) . to_string ( ) ) . into ( ) )
619+ Err ( ChmodError :: PermissionDenied ( file. into ( ) ) . into ( ) )
629620 } else {
630- Err ( ChmodError :: CannotStat ( file. to_string_lossy ( ) . to_string ( ) ) . into ( ) )
621+ Err ( ChmodError :: CannotStat ( file. into ( ) ) . into ( ) )
631622 } ;
632623 }
633624 } ;
@@ -657,7 +648,7 @@ impl Chmoder {
657648 // 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
658649 if ( new_mode & !naively_expected_new_mode) != 0 {
659650 return Err ( ChmodError :: NewPermissions (
660- file. to_string_lossy ( ) . to_string ( ) ,
651+ file. into ( ) ,
661652 display_permissions_unix ( new_mode as mode_t , false ) ,
662653 display_permissions_unix ( naively_expected_new_mode as mode_t , false ) ,
663654 )
0 commit comments