Merge bitcoin/bitcoin#24331: util: Revert back MoveFileExW call for MinGW-w64

dc01cbc538 test: Add fs_tests/rename unit test (Hennadii Stepanov)
d4999d40b9 util: Revert back MoveFileExW call for MinGW-w64 (Hennadii Stepanov)

Pull request description:

  Unfortunately, bitcoin/bitcoin#24308 introduced a [regression](https://github.com/bitcoin/bitcoin/pull/24308#issuecomment-1037259386) for mingw builds.

  The root of the problem is a broken implementation of [`std::filesystem::rename`](https://en.cppreference.com/w/cpp/filesystem/rename). In particular, the expected behavior
  > If `old_p` is a non-directory file, then `new_p` must be ... existing non-directory file: `new_p` _is first deleted_...

  fails with the "File exists" error.

  This PR reverts back the `MoveFileExW` call, and adds the [suggested](https://github.com/bitcoin/bitcoin/pull/24308#pullrequestreview-878832906) unit test.

ACKs for top commit:
  vasild:
    ACK dc01cbc538

Tree-SHA512: c8e5a98844cfa32bec0ad67a1aaa58fe2efd0c5474d3e83490211985b110f83245758a742dcaa0a933a192ab66a7f11807e0c53ae69260b7dd02fc99f6d03849
This commit is contained in:
laanwj 2022-02-17 12:29:22 +01:00
commit df0825046a
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
2 changed files with 46 additions and 1 deletions

View File

@ -118,4 +118,38 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
}
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(rename)
{
const fs::path tmpfolder{m_args.GetDataDirBase()};
const fs::path path1{GetUniquePath(tmpfolder)};
const fs::path path2{GetUniquePath(tmpfolder)};
const std::string path1_contents{"1111"};
const std::string path2_contents{"2222"};
{
std::ofstream file{path1};
file << path1_contents;
}
{
std::ofstream file{path2};
file << path2_contents;
}
// Rename path1 -> path2.
BOOST_CHECK(RenameOver(path1, path2));
BOOST_CHECK(!fs::exists(path1));
{
std::ifstream file{path2};
std::string contents;
file >> contents;
BOOST_CHECK_EQUAL(contents, path1_contents);
}
fs::remove(path2);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1062,9 +1062,20 @@ void ArgsManager::LogArgs() const
bool RenameOver(fs::path src, fs::path dest)
{
#ifdef __MINGW64__
// This is a workaround for a bug in libstdc++ which
// implements std::filesystem::rename with _wrename function.
// This bug has been fixed in upstream:
// - GCC 10.3: 8dd1c1085587c9f8a21bb5e588dfe1e8cdbba79e
// - GCC 11.1: 1dfd95f0a0ca1d9e6cbc00e6cbfd1fa20a98f312
// For more details see the commits mentioned above.
return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
MOVEFILE_REPLACE_EXISTING) != 0;
#else
std::error_code error;
fs::rename(src, dest, error);
return !error;
#endif
}
/**