diff options
| author | HTGAzureX1212. <39023054+HTGAzureX1212@users.noreply.github.com> | 2024-01-26 20:15:30 +0800 |
|---|---|---|
| committer | HTGAzureX1212. <39023054+HTGAzureX1212@users.noreply.github.com> | 2024-01-26 20:15:30 +0800 |
| commit | 3269513eb0189946c40a67d98724b40321e6e9f3 (patch) | |
| tree | 0ef4ee888f411670e250d31833a0fe1add0b82db | |
| parent | 69db514ed9238bb11f5d2c576fe26020e3b99a52 (diff) | |
| download | rust-3269513eb0189946c40a67d98724b40321e6e9f3.tar.gz rust-3269513eb0189946c40a67d98724b40321e6e9f3.zip | |
fix issue 120040
| -rw-r--r-- | library/std/src/sys/pal/windows/fs.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/library/std/src/sys/pal/windows/fs.rs b/library/std/src/sys/pal/windows/fs.rs index 42484543686..06a08ea22eb 100644 --- a/library/std/src/sys/pal/windows/fs.rs +++ b/library/std/src/sys/pal/windows/fs.rs @@ -1068,6 +1068,27 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> { unsafe { 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 && p.exists() { + return Ok(ReadDir { + handle: FindNextFileHandle(file_handle), + root: Arc::new(root), + first: None, + }); + } + if find_handle != c::INVALID_HANDLE_VALUE { Ok(ReadDir { handle: FindNextFileHandle(find_handle), |
