@@ -2505,6 +2505,8 @@ namespace filesystem {
2505
2505
_EXPORT_STD enum class directory_options { none = 0, follow_directory_symlink = 1, skip_permission_denied = 2 };
2506
2506
_BITMASK_OPS(_EXPORT_STD, directory_options)
2507
2507
2508
+ _EXPORT_STD _NODISCARD inline bool exists(const path& _Target, error_code& _Ec) noexcept;
2509
+
2508
2510
struct _Dir_enum_impl {
2509
2511
_NODISCARD static __std_win_error _Advance_and_reset_if_no_more_files(shared_ptr<_Dir_enum_impl>& _Ptr) {
2510
2512
auto& _Impl = *_Ptr;
@@ -2543,6 +2545,7 @@ namespace filesystem {
2543
2545
return __std_win_error::_File_not_found;
2544
2546
}
2545
2547
2548
+ const path _Original_path = _Path;
2546
2549
_Path /= L"*"sv;
2547
2550
auto _Error = _Dir._Open(_Path.c_str(), &_Data);
2548
2551
if (_Error == __std_win_error::_Success) {
@@ -2552,6 +2555,13 @@ namespace filesystem {
2552
2555
if (_Error == __std_win_error::_Access_denied
2553
2556
&& _Bitmask_includes_any(_Options_arg, directory_options::skip_permission_denied)) {
2554
2557
_Error = __std_win_error::_No_more_files;
2558
+ } else if (_Error == __std_win_error::_File_not_found) {
2559
+ error_code _Ignored; // When exists() returns true, that implies that the error_code is successful.
2560
+ // When exists() returns false, we don't want to interfere with _Open_dir()'s behavior,
2561
+ // as it's going to return __std_win_error::_File_not_found.
2562
+ if (_STD filesystem::exists(_Original_path, _Ignored)) {
2563
+ _Error = __std_win_error::_No_more_files; // Handle empty volumes, see GH-4291
2564
+ }
2555
2565
}
2556
2566
2557
2567
return _Error;
0 commit comments