about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorHTGAzureX1212. <39023054+HTGAzureX1212@users.noreply.github.com>2024-01-27 12:28:28 +0800
committerHTGAzureX1212. <39023054+HTGAzureX1212@users.noreply.github.com>2024-01-27 12:28:28 +0800
commite26f213050f451ec3e9c6a3132bc0d5be5123737 (patch)
tree991b59d5f35a7194e4a1dd5d4cfbcdad990632c9 /library/std/src/sys
parent2241d1618960a2d192b2874e925fa51afeb8a83b (diff)
make modifications as per reviews
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/pal/windows/fs.rs46
1 files changed, 25 insertions, 21 deletions
diff --git a/library/std/src/sys/pal/windows/fs.rs b/library/std/src/sys/pal/windows/fs.rs
index 38835db7649..1a8151479f3 100644
--- a/library/std/src/sys/pal/windows/fs.rs
+++ b/library/std/src/sys/pal/windows/fs.rs
@@ -1069,26 +1069,6 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
         let mut wfd = mem::zeroed();
         let find_handle = c::FindFirstFileW(path.as_ptr(), &mut wfd);
 
-        // The status `ERROR_FILE_NOT_FOUND` is returned by the `FindFirstFileW` function
-        // if no matching files can be found, but not necessarily that the path to find the
-        // files in does not exist.
-        //
-        // Hence, a check for whether the path to search in exists is added when the last
-        // os error returned by Windows is `ERROR_FILE_NOT_FOUND` to handle this scenario.
-        // If that is the case, an empty `ReadDir` iterator is returned as it returns `None`
-        // in the initial `.next()` invocation because `ERROR_NO_MORE_FILES` would have been
-        // returned by the `FindNextFileW` function.
-        //
-        // See issue #120040: https://github.com/rust-lang/rust/issues/120040.
-        let last_error = Error::last_os_error();
-        if last_error.raw_os_error().unwrap() == c::ERROR_FILE_NOT_FOUND as i32 && p.exists() {
-            return Ok(ReadDir {
-                handle: FindNextFileHandle(find_handle),
-                root: Arc::new(root),
-                first: None,
-            });
-        }
-
         if find_handle != c::INVALID_HANDLE_VALUE {
             Ok(ReadDir {
                 handle: FindNextFileHandle(find_handle),
@@ -1096,7 +1076,31 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
                 first: Some(wfd),
             })
         } else {
-            Err(last_error)
+            // The status `ERROR_FILE_NOT_FOUND` is returned by the `FindFirstFileW` function
+            // if no matching files can be found, but not necessarily that the path to find the
+            // files in does not exist.
+            //
+            // Hence, a check for whether the path to search in exists is added when the last
+            // os error returned by Windows is `ERROR_FILE_NOT_FOUND` to handle this scenario.
+            // If that is the case, an empty `ReadDir` iterator is returned as it returns `None`
+            // in the initial `.next()` invocation because `ERROR_NO_MORE_FILES` would have been
+            // returned by the `FindNextFileW` function.
+            //
+            // See issue #120040: https://github.com/rust-lang/rust/issues/120040.
+            let last_error = api::get_last_error();
+            if last_error.code == c::ERROR_FILE_NOT_FOUND {
+                return Ok(ReadDir {
+                    handle: FindNextFileHandle(find_handle),
+                    root: Arc::new(root),
+                    first: None,
+                });
+            }
+
+            // Just return the error constructed from the raw OS error if the above is not the case.
+            //
+            // Note: `ERROR_PATH_NOT_FOUND` would have been returned by the `FindFirstFileW` function
+            // when the path to search in does not exist in the first place.
+            Err(Error::from_raw_os_error(last_error.code as i32));
         }
     }
 }