Skip to content

Commit 63c07e1

Browse files
author
Pat Hickey
authored
Add remove_file_or_symlink to DirExt and DirExtUtf8 (#149)
* Add `remove_file_or_symlink` to DirExt and DirExtUtf8
1 parent 480ff7d commit 63c07e1

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

cap-fs-ext/src/dir_ext.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ pub trait DirExt {
8181
fn open_dir_nofollow<P: AsRef<Path>>(&self, path: P) -> io::Result<Self>
8282
where
8383
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<()>;
8494
}
8595

8696
/// `fs_utf8` version of `DirExt`.
@@ -157,6 +167,16 @@ pub trait DirExtUtf8 {
157167
fn open_dir_nofollow<P: AsRef<str>>(&self, path: P) -> io::Result<Self>
158168
where
159169
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<()>;
160180
}
161181

162182
#[cfg(feature = "std")]
@@ -238,6 +258,19 @@ impl DirExt for cap_std::fs::Dir {
238258
Err(e) => Err(e),
239259
}
240260
}
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+
}
241274
}
242275

243276
#[cfg(feature = "async_std")]
@@ -319,6 +352,19 @@ impl DirExt for cap_async_std::fs::Dir {
319352
Err(e) => Err(e),
320353
}
321354
}
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+
}
322368
}
323369

324370
#[cfg(all(feature = "std", feature = "fs_utf8"))]
@@ -404,6 +450,19 @@ impl DirExtUtf8 for cap_std::fs_utf8::Dir {
404450
Err(e) => Err(e),
405451
}
406452
}
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+
}
407466
}
408467

409468
#[cfg(all(feature = "async_std", feature = "fs_utf8"))]
@@ -495,6 +554,19 @@ impl DirExtUtf8 for cap_async_std::fs_utf8::Dir {
495554
Err(e) => Err(e),
496555
}
497556
}
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+
}
498570
}
499571

500572
#[cfg(all(any(feature = "std", feature = "async_std"), feature = "fs_utf8"))]

0 commit comments

Comments
 (0)