about summary refs log tree commit diff
path: root/library/std/src/sys_common/fs.rs
diff options
context:
space:
mode:
authorbinarycat <binarycat@envs.net>2024-07-11 15:44:58 -0400
committerbinarycat <binarycat@envs.net>2024-08-22 14:18:42 -0400
commit736f773844e7ebf05ccb827c17b7ad9eb28aa295 (patch)
tree43d80bc940926d94ecc0299410d688d4677d3424 /library/std/src/sys_common/fs.rs
parent53676730146e38e4697b6204c2ee61a9fd6b7e51 (diff)
downloadrust-736f773844e7ebf05ccb827c17b7ad9eb28aa295.tar.gz
rust-736f773844e7ebf05ccb827c17b7ad9eb28aa295.zip
fix: fs::remove_dir_all: treat ENOENT as success
fixes #127576

windows implementation still needs some work
Diffstat (limited to 'library/std/src/sys_common/fs.rs')
-rw-r--r--library/std/src/sys_common/fs.rs21
1 files changed, 15 insertions, 6 deletions
diff --git a/library/std/src/sys_common/fs.rs b/library/std/src/sys_common/fs.rs
index acb6713cf1b..a25a7244660 100644
--- a/library/std/src/sys_common/fs.rs
+++ b/library/std/src/sys_common/fs.rs
@@ -3,6 +3,7 @@
 use crate::fs;
 use crate::io::{self, Error, ErrorKind};
 use crate::path::Path;
+use crate::sys_common::ignore_notfound;
 
 pub(crate) const NOT_FILE_ERROR: Error = io::const_io_error!(
     ErrorKind::InvalidInput,
@@ -32,14 +33,22 @@ pub fn remove_dir_all(path: &Path) -> io::Result<()> {
 
 fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
     for child in fs::read_dir(path)? {
-        let child = child?;
-        if child.file_type()?.is_dir() {
-            remove_dir_all_recursive(&child.path())?;
-        } else {
-            fs::remove_file(&child.path())?;
+        let result: io::Result<()> = try {
+            let child = child?;
+            if child.file_type()?.is_dir() {
+                remove_dir_all_recursive(&child.path())?;
+            } else {
+                fs::remove_file(&child.path())?;
+            }
+        };
+        // ignore internal NotFound errors to prevent race conditions
+        if let Err(err) = &result
+            && err.kind() != io::ErrorKind::NotFound
+        {
+            return result;
         }
     }
-    fs::remove_dir(path)
+    ignore_notfound(fs::remove_dir(path))
 }
 
 pub fn exists(path: &Path) -> io::Result<bool> {