@@ -16,7 +16,7 @@ use std::os::unix::fs::PermissionsExt;
1616use std:: path:: MAIN_SEPARATOR ;
1717use std:: path:: { Path , PathBuf } ;
1818use uucore:: display:: Quotable ;
19- use uucore:: error:: { UResult , USimpleError , UUsageError } ;
19+ use uucore:: error:: { FromIo , UResult , USimpleError , UUsageError } ;
2020use uucore:: {
2121 format_usage, help_about, help_section, help_usage, os_str_as_bytes, prompt_yes, show_error,
2222} ;
@@ -455,48 +455,29 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
455455}
456456
457457fn remove_dir ( path : & Path , options : & Options ) -> bool {
458- if prompt_dir ( path, options) {
459- if let Ok ( mut read_dir) = fs:: read_dir ( path) {
460- if options. dir || options. recursive {
461- if read_dir. next ( ) . is_none ( ) {
462- match fs:: remove_dir ( path) {
463- Ok ( _) => {
464- if options. verbose {
465- println ! ( "removed directory {}" , normalize( path) . quote( ) ) ;
466- }
467- }
468- Err ( e) => {
469- if e. kind ( ) == std:: io:: ErrorKind :: PermissionDenied {
470- // GNU compatibility (rm/fail-eacces.sh)
471- show_error ! (
472- "cannot remove {}: {}" ,
473- path. quote( ) ,
474- "Permission denied"
475- ) ;
476- } else {
477- show_error ! ( "cannot remove {}: {}" , path. quote( ) , e) ;
478- }
479- return true ;
480- }
481- }
482- } else {
483- // directory can be read but is not empty
484- show_error ! ( "cannot remove {}: Directory not empty" , path. quote( ) ) ;
485- return true ;
486- }
487- } else {
488- // called to remove a symlink_dir (windows) without "-r"/"-R" or "-d"
489- show_error ! ( "cannot remove {}: Is a directory" , path. quote( ) ) ;
490- return true ;
458+ if !prompt_dir ( path, options) {
459+ return false ;
460+ }
461+
462+ if !options. dir && !options. recursive {
463+ // called to remove a symlink_dir (windows) without "-r"/"-R" or "-d"
464+ show_error ! ( "cannot remove {}: Is a directory" , path. quote( ) ) ;
465+ return true ;
466+ }
467+
468+ match fs:: remove_dir ( path) {
469+ Ok ( _) => {
470+ if options. verbose {
471+ println ! ( "removed directory {}" , normalize( path) . quote( ) ) ;
491472 }
492- } else {
493- // GNU's rm shows this message if directory is empty but not readable
494- show_error ! ( "cannot remove {}: Directory not empty" , path. quote( ) ) ;
495- return true ;
473+ false
474+ }
475+ Err ( e) => {
476+ let e = e. map_err_context ( || format ! ( "cannot remove {}" , path. quote( ) ) ) ;
477+ show_error ! ( "{e}" ) ;
478+ true
496479 }
497480 }
498-
499- false
500481}
501482
502483fn remove_file ( path : & Path , options : & Options ) -> bool {
0 commit comments