Skip to content

Commit 3269513

Browse files
committed
fix issue 120040
1 parent 69db514 commit 3269513

File tree

1 file changed

+21
-0
lines changed
  • library/std/src/sys/pal/windows

1 file changed

+21
-0
lines changed

library/std/src/sys/pal/windows/fs.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,27 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
10681068
unsafe {
10691069
let mut wfd = mem::zeroed();
10701070
let find_handle = c::FindFirstFileW(path.as_ptr(), &mut wfd);
1071+
1072+
// The status `ERROR_FILE_NOT_FOUND` is returned by the `FindFirstFileW` function
1073+
// if no matching files can be found, but not necessarily that the path to find the
1074+
// files in does not exist.
1075+
//
1076+
// Hence, a check for whether the path to search in exists is added when the last
1077+
// os error returned by Windows is `ERROR_FILE_NOT_FOUND` to handle this scenario.
1078+
// If that is the case, an empty `ReadDir` iterator is returned as it returns `None`
1079+
// in the initial `.next()` invocation because `ERROR_NO_MORE_FILES` would have been
1080+
// returned by the `FindNextFileW` function.
1081+
//
1082+
// See issue #120040: https://github.com/rust-lang/rust/issues/120040.
1083+
let last_error = Error::last_os_error();
1084+
if last_error.raw_os_error().unwrap() == c::ERROR_FILE_NOT_FOUND && p.exists() {
1085+
return Ok(ReadDir {
1086+
handle: FindNextFileHandle(file_handle),
1087+
root: Arc::new(root),
1088+
first: None,
1089+
});
1090+
}
1091+
10711092
if find_handle != c::INVALID_HANDLE_VALUE {
10721093
Ok(ReadDir {
10731094
handle: FindNextFileHandle(find_handle),

0 commit comments

Comments
 (0)