@@ -3585,23 +3585,33 @@ namespace filesystem {
35853585 }
35863586
35873587 // FUNCTION remove
3588- inline bool remove(const path& _Target) {
3588+ inline bool remove(const path& _Target, error_code& _Ec) noexcept {
35893589 // remove file _Target (even if it is a directory); returns whether the file was removed
35903590 // note !exists(_Target) is not an error, and merely returns false
3591- const auto _Result = __std_fs_remove(_Target.c_str());
3592- if (_Result._Error != __std_win_error::_Success) {
3593- _Throw_fs_error("remove", _Result._Error, _Target);
3591+ auto _Result = __std_fs_remove(_Target.c_str());
3592+ if (_Result._Error == __std_win_error::_Access_denied) {
3593+ // Try to remove the read-only attribute and try again
3594+ const auto _Perm_change_err = __std_fs_change_permissions(_Target.c_str(), false, false);
3595+ if (_Perm_change_err != __std_win_error::_Success) {
3596+ _Ec = _Make_ec(_Perm_change_err);
3597+ return false;
3598+ }
3599+ _Result = __std_fs_remove(_Target.c_str());
35943600 }
3595-
3601+ _Ec = _Make_ec(_Result._Error);
35963602 return _Result._Removed;
35973603 }
35983604
3599- inline bool remove(const path& _Target, error_code& _Ec) noexcept {
3605+ inline bool remove(const path& _Target) {
36003606 // remove file _Target (even if it is a directory); returns whether the file was removed
36013607 // note !exists(_Target) is not an error, and merely returns false
3602- const auto _Result = __std_fs_remove(_Target.c_str());
3603- _Ec = _Make_ec(_Result._Error);
3604- return _Result._Removed;
3608+ error_code _Ec;
3609+ const auto _Result = _STD filesystem::remove(_Target, _Ec);
3610+ if (static_cast<__std_win_error>(_Ec.value()) != __std_win_error::_Success) {
3611+ _Throw_fs_error("remove", static_cast<__std_win_error>(_Ec.value()), _Target);
3612+ }
3613+
3614+ return _Result;
36053615 }
36063616
36073617 // FUNCTION rename
0 commit comments