@@ -3,7 +3,8 @@ use dua::{
33 traverse:: { EntryData , Tree , TreeIndex } ,
44 ByteFormat , Color , TraversalSorting , WalkOptions ,
55} ;
6- use failure:: Error ;
6+ use failure:: { Error , ResultExt } ;
7+ use itertools:: Itertools ;
78use jwalk:: { DirEntry , WalkDir } ;
89use petgraph:: prelude:: NodeIndex ;
910use pretty_assertions:: assert_eq;
@@ -13,7 +14,6 @@ use std::{
1314 ffi:: OsString ,
1415 fmt,
1516 fs:: { copy, create_dir_all, remove_dir, remove_file} ,
16- io,
1717 io:: ErrorKind ,
1818 path:: Path ,
1919 path:: PathBuf ,
@@ -351,7 +351,7 @@ impl Drop for WritableFixture {
351351 }
352352}
353353
354- fn delete_recursive ( path : impl AsRef < Path > ) -> Result < ( ) , io :: Error > {
354+ fn delete_recursive ( path : impl AsRef < Path > ) -> Result < ( ) , Error > {
355355 let mut files: Vec < _ > = Vec :: new ( ) ;
356356 let mut dirs: Vec < _ > = Vec :: new ( ) ;
357357
@@ -366,8 +366,17 @@ fn delete_recursive(path: impl AsRef<Path>) -> Result<(), io::Error> {
366366
367367 files
368368 . iter ( )
369- . map ( |f| remove_file ( f) )
370- . chain ( dirs. iter ( ) . map ( |d| remove_dir ( d) ) )
369+ . map ( |f| remove_file ( f) . map_err ( Error :: from) )
370+ . chain (
371+ dirs. iter ( )
372+ . sorted_by_key ( |p| p. components ( ) . count ( ) )
373+ . rev ( )
374+ . map ( |d| {
375+ remove_dir ( d)
376+ . with_context ( |_| format ! ( "Could not delete '{}'" , d. display( ) ) )
377+ . map_err ( Error :: from)
378+ } ) ,
379+ )
371380 . collect :: < Result < _ , _ > > ( )
372381}
373382
@@ -385,12 +394,9 @@ fn copy_recursive(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<(), Er
385394 } else {
386395 copy ( & entry_path, dst)
387396 . map ( |_| ( ) )
388- . or_else ( |e| {
389- if let ErrorKind :: AlreadyExists = e. kind ( ) {
390- Ok ( ( ) )
391- } else {
392- Err ( e)
393- }
397+ . or_else ( |e| match e. kind ( ) {
398+ ErrorKind :: AlreadyExists => Ok ( ( ) ) ,
399+ _ => Err ( e) ,
394400 } )
395401 . map_err ( Into :: into)
396402 }
0 commit comments