diff --git a/stl/inc/filesystem b/stl/inc/filesystem index 17192a3ba60..6985c604925 100644 --- a/stl/inc/filesystem +++ b/stl/inc/filesystem @@ -2111,12 +2111,14 @@ namespace filesystem { directory_entry(const directory_entry&) = default; directory_entry(directory_entry&&) noexcept = default; explicit directory_entry(const filesystem::path& _Path_arg) : _Cached_data{}, _Path(_Path_arg) { - (void) _Refresh(_Cached_data, _Path); + refresh(); } directory_entry(const filesystem::path& _Path_arg, error_code& _Ec) : _Cached_data{}, _Path(_Path_arg) { - _Ec.clear(); - (void) _Refresh(_Cached_data, _Path); + refresh(_Ec); + if (_Ec) { + _Path.clear(); + } } ~directory_entry() = default; @@ -2128,35 +2130,39 @@ namespace filesystem { // [fs.dir.entry.mods], modifiers void assign(const filesystem::path& _Path_arg) { _Path.assign(_Path_arg); - (void) _Refresh(_Cached_data, _Path); + refresh(); } void assign(const filesystem::path& _Path_arg, error_code& _Ec) { _Ec.clear(); // for exception safety _Path.assign(_Path_arg); - (void) _Refresh(_Cached_data, _Path); + refresh(_Ec); } void replace_filename(const filesystem::path& _Path_arg) { _Path.replace_filename(_Path_arg); - (void) _Refresh(_Cached_data, _Path); + refresh(); } void replace_filename(const filesystem::path& _Path_arg, error_code& _Ec) { _Ec.clear(); // for exception safety _Path.replace_filename(_Path_arg); - (void) _Refresh(_Cached_data, _Path); + refresh(_Ec); } void refresh() { const auto _Error = _Refresh(_Cached_data, _Path); - if (_Error != __std_win_error::_Success) { + if (_Error != __std_win_error::_Success && !__std_is_file_not_found(_Error)) { _Throw_fs_error("directory_entry::refresh", _Error, _Path); } } void refresh(error_code& _Ec) noexcept { - _Ec = _Make_ec(_Refresh(_Cached_data, _Path)); + _Ec.clear(); + const auto _Error = _Refresh(_Cached_data, _Path); + if (_Error != __std_win_error::_Success && !__std_is_file_not_found(_Error)) { + _Ec = _Make_ec(_Error); + } } #if _HAS_CXX20 diff --git a/tests/std/tests/P0218R1_filesystem/test.cpp b/tests/std/tests/P0218R1_filesystem/test.cpp index edf331db6a9..9140c01a10d 100644 --- a/tests/std/tests/P0218R1_filesystem/test.cpp +++ b/tests/std/tests/P0218R1_filesystem/test.cpp @@ -996,7 +996,11 @@ void test_directory_entry() { EXPECT(!nonexistentEntryEc.exists()); EXPECT(good(ec)); - EXPECT(throws_filesystem_error([&] { nonexistentEntryEc.refresh(); }, "directory_entry::refresh", nonexistent)); + // Also test GH-232 ": directory_entry(const path& p, error_code& ec) does not return error code" + nonexistentEntry.refresh(); + + nonexistentEntryEc.refresh(ec); + EXPECT(good(ec)); } directory_entry goodEntry(filePath, ec);