@@ -15,7 +15,7 @@ use std::os::unix::ffi::OsStrExt;
1515use std:: path:: MAIN_SEPARATOR ;
1616use std:: path:: { Path , PathBuf } ;
1717use uucore:: display:: Quotable ;
18- use uucore:: error:: { UResult , USimpleError , UUsageError } ;
18+ use uucore:: error:: { FromIo , UResult , USimpleError , UUsageError } ;
1919use uucore:: {
2020 format_usage, help_about, help_section, help_usage, os_str_as_bytes, prompt_yes, show_error,
2121} ;
@@ -428,49 +428,35 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
428428 had_err
429429}
430430
431+ /// Remove the given directory, asking the user for permission if necessary.
432+ ///
433+ /// Returns true if it has encountered an error.
431434fn remove_dir ( path : & Path , options : & Options ) -> bool {
432- if prompt_dir ( path, options) {
433- if let Ok ( mut read_dir) = fs:: read_dir ( path) {
434- if options. dir || options. recursive {
435- if read_dir. next ( ) . is_none ( ) {
436- match fs:: remove_dir ( path) {
437- Ok ( _) => {
438- if options. verbose {
439- println ! ( "removed directory {}" , normalize( path) . quote( ) ) ;
440- }
441- }
442- Err ( e) => {
443- if e. kind ( ) == std:: io:: ErrorKind :: PermissionDenied {
444- // GNU compatibility (rm/fail-eacces.sh)
445- show_error ! (
446- "cannot remove {}: {}" ,
447- path. quote( ) ,
448- "Permission denied"
449- ) ;
450- } else {
451- show_error ! ( "cannot remove {}: {}" , path. quote( ) , e) ;
452- }
453- return true ;
454- }
455- }
456- } else {
457- // directory can be read but is not empty
458- show_error ! ( "cannot remove {}: Directory not empty" , path. quote( ) ) ;
459- return true ;
460- }
461- } else {
462- // called to remove a symlink_dir (windows) without "-r"/"-R" or "-d"
463- show_error ! ( "cannot remove {}: Is a directory" , path. quote( ) ) ;
464- return true ;
435+ // Ask the user for permission.
436+ if !prompt_dir ( path, options) {
437+ return false ;
438+ }
439+
440+ // Called to remove a symlink_dir (windows) without "-r"/"-R" or "-d".
441+ if !options. dir && !options. recursive {
442+ show_error ! ( "cannot remove {}: Is a directory" , path. quote( ) ) ;
443+ return true ;
444+ }
445+
446+ // Try to remove the directory.
447+ match fs:: remove_dir ( path) {
448+ Ok ( _) => {
449+ if options. verbose {
450+ println ! ( "removed directory {}" , normalize( path) . quote( ) ) ;
465451 }
466- } else {
467- // GNU's rm shows this message if directory is empty but not readable
468- show_error ! ( "cannot remove {}: Directory not empty" , path. quote( ) ) ;
469- return true ;
452+ false
453+ }
454+ Err ( e) => {
455+ let e = e. map_err_context ( || format ! ( "cannot remove {}" , path. quote( ) ) ) ;
456+ show_error ! ( "{e}" ) ;
457+ true
470458 }
471459 }
472-
473- false
474460}
475461
476462fn remove_file ( path : & Path , options : & Options ) -> bool {
0 commit comments