@@ -81,6 +81,16 @@ pub trait DirExt {
81
81
fn open_dir_nofollow < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < Self >
82
82
where
83
83
Self : Sized ;
84
+
85
+ /// Removes a file or symlink from a filesystem.
86
+ ///
87
+ /// Removal of symlinks has different behavior under Windows - if a symlink points
88
+ /// to a directory, it cannot be removed with the remove_file operation. This
89
+ /// method will remove files and all symlinks.
90
+ ///
91
+ /// On Windows, if a file or symlink does not exist at this path, but an empty directory does
92
+ /// exist, this function will remove the directory.
93
+ fn remove_file_or_symlink < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > ;
84
94
}
85
95
86
96
/// `fs_utf8` version of `DirExt`.
@@ -157,6 +167,16 @@ pub trait DirExtUtf8 {
157
167
fn open_dir_nofollow < P : AsRef < str > > ( & self , path : P ) -> io:: Result < Self >
158
168
where
159
169
Self : Sized ;
170
+
171
+ /// Removes a file or symlink from a filesystem.
172
+ ///
173
+ /// Removal of symlinks has different behavior under Windows - if a symlink points
174
+ /// to a directory, it cannot be removed with the remove_file operation. This
175
+ /// method will remove files and all symlinks.
176
+ ///
177
+ /// On Windows, ff a file or symlink does not exist at this path, but an empty directory does
178
+ /// exist, this function will remove the directory.
179
+ fn remove_file_or_symlink < P : AsRef < str > > ( & self , path : P ) -> io:: Result < ( ) > ;
160
180
}
161
181
162
182
#[ cfg( feature = "std" ) ]
@@ -238,6 +258,19 @@ impl DirExt for cap_std::fs::Dir {
238
258
Err ( e) => Err ( e) ,
239
259
}
240
260
}
261
+
262
+ #[ cfg( not( windows) ) ]
263
+ #[ inline]
264
+ fn remove_file_or_symlink < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
265
+ self . remove_file ( path. as_ref ( ) )
266
+ }
267
+
268
+ #[ cfg( windows) ]
269
+ #[ inline]
270
+ fn remove_file_or_symlink < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
271
+ self . remove_file ( path. as_ref ( ) )
272
+ . or_else ( |e| self . remove_dir ( path. as_ref ( ) ) . map_err ( |_| e) )
273
+ }
241
274
}
242
275
243
276
#[ cfg( feature = "async_std" ) ]
@@ -319,6 +352,19 @@ impl DirExt for cap_async_std::fs::Dir {
319
352
Err ( e) => Err ( e) ,
320
353
}
321
354
}
355
+
356
+ #[ cfg( not( windows) ) ]
357
+ #[ inline]
358
+ fn remove_file_or_symlink < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
359
+ self . remove_file ( path. as_ref ( ) )
360
+ }
361
+
362
+ #[ cfg( windows) ]
363
+ #[ inline]
364
+ fn remove_file_or_symlink < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
365
+ self . remove_file ( path. as_ref ( ) )
366
+ . or_else ( |e| self . remove_dir ( path. as_ref ( ) ) . map_err ( |_| e) )
367
+ }
322
368
}
323
369
324
370
#[ cfg( all( feature = "std" , feature = "fs_utf8" ) ) ]
@@ -404,6 +450,19 @@ impl DirExtUtf8 for cap_std::fs_utf8::Dir {
404
450
Err ( e) => Err ( e) ,
405
451
}
406
452
}
453
+
454
+ #[ cfg( not( windows) ) ]
455
+ #[ inline]
456
+ fn remove_file_or_symlink < P : AsRef < str > > ( & self , path : P ) -> io:: Result < ( ) > {
457
+ self . remove_file ( path. as_ref ( ) )
458
+ }
459
+
460
+ #[ cfg( windows) ]
461
+ #[ inline]
462
+ fn remove_file_or_symlink < P : AsRef < str > > ( & self , path : P ) -> io:: Result < ( ) > {
463
+ self . remove_file ( path. as_ref ( ) )
464
+ . or_else ( |e| self . remove_dir ( path. as_ref ( ) ) . map_err ( |_| e) )
465
+ }
407
466
}
408
467
409
468
#[ cfg( all( feature = "async_std" , feature = "fs_utf8" ) ) ]
@@ -495,6 +554,19 @@ impl DirExtUtf8 for cap_async_std::fs_utf8::Dir {
495
554
Err ( e) => Err ( e) ,
496
555
}
497
556
}
557
+
558
+ #[ cfg( not( windows) ) ]
559
+ #[ inline]
560
+ fn remove_file_or_symlink < P : AsRef < str > > ( & self , path : P ) -> io:: Result < ( ) > {
561
+ self . remove_file ( path. as_ref ( ) )
562
+ }
563
+
564
+ #[ cfg( windows) ]
565
+ #[ inline]
566
+ fn remove_file_or_symlink < P : AsRef < str > > ( & self , path : P ) -> io:: Result < ( ) > {
567
+ self . remove_file ( path. as_ref ( ) )
568
+ . or_else ( |e| self . remove_dir ( path. as_ref ( ) ) . map_err ( |_| e) )
569
+ }
498
570
}
499
571
500
572
#[ cfg( all( any( feature = "std" , feature = "async_std" ) , feature = "fs_utf8" ) ) ]
0 commit comments