Skip to content

Commit a0c2de5

Browse files
Adjusted behavior of filesystem::is_empty and filesystem::directory_iterator to work with empty volumes (#4311)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 80e0909 commit a0c2de5

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

stl/inc/filesystem

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,8 @@ namespace filesystem {
25052505
_EXPORT_STD enum class directory_options { none = 0, follow_directory_symlink = 1, skip_permission_denied = 2 };
25062506
_BITMASK_OPS(_EXPORT_STD, directory_options)
25072507

2508+
_EXPORT_STD _NODISCARD inline bool exists(const path& _Target, error_code& _Ec) noexcept;
2509+
25082510
struct _Dir_enum_impl {
25092511
_NODISCARD static __std_win_error _Advance_and_reset_if_no_more_files(shared_ptr<_Dir_enum_impl>& _Ptr) {
25102512
auto& _Impl = *_Ptr;
@@ -2543,6 +2545,7 @@ namespace filesystem {
25432545
return __std_win_error::_File_not_found;
25442546
}
25452547

2548+
const path _Original_path = _Path;
25462549
_Path /= L"*"sv;
25472550
auto _Error = _Dir._Open(_Path.c_str(), &_Data);
25482551
if (_Error == __std_win_error::_Success) {
@@ -2552,6 +2555,13 @@ namespace filesystem {
25522555
if (_Error == __std_win_error::_Access_denied
25532556
&& _Bitmask_includes_any(_Options_arg, directory_options::skip_permission_denied)) {
25542557
_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+
}
25552565
}
25562566

25572567
return _Error;

0 commit comments

Comments
 (0)