Use std::filesystem::rename() instead of std::rename(). We rely on the destination to be overwritten if it exists, but std::rename()’s behavior is implementation-defined in this case.
This is a rebase of #20435 by vasild.
Use std::filesystem::rename() instead of std::rename(). We rely on the
destination to be overwritten if it exists, but std::rename()'s behavior
is implementation-defined in this case.
Approach ACK ee822d85d6de7db85416190cf843ad74147519cf.
We rely on the destination to be overwritten if it exists
Add a test for this assumption?
ACK ee822d85d6de7db85416190cf843ad74147519cf
Add a test for this assumption?
0commit 23074edf054845b1b74c43618cf7a8cc58aa5518 (HEAD -> pull/24308_1644480965_ee822d85d6__20435_rebased)
1Parent: ee822d85d6de7db85416190cf843ad74147519cf
2Author: Vasil Dimov <vd@FreeBSD.org>
3AuthorDate: Thu Feb 10 14:11:35 2022 +0100
4Commit: Vasil Dimov <vd@FreeBSD.org>
5CommitDate: Thu Feb 10 14:14:58 2022 +0100
6
7 test: add a unit test for RenameOver()
8
9diff --git a/src/test/fs_tests.cpp b/src/test/fs_tests.cpp
10index 1256395849..8a5f025e5f 100644
11--- a/src/test/fs_tests.cpp
12+++ b/src/test/fs_tests.cpp
13@@ -115,7 +115,40 @@ BOOST_AUTO_TEST_CASE(fsbridge_fstream)
14 BOOST_CHECK(p1 != p2);
15 BOOST_CHECK(p2 != p3);
16 BOOST_CHECK(p1 != p3);
17 }
18 }
19
20-BOOST_AUTO_TEST_SUITE_END()
21\ No newline at end of file
22+BOOST_AUTO_TEST_CASE(rename)
23+{
24+ const fs::path tmpfolder{m_args.GetDataDirBase()};
25+
26+ const fs::path path1{GetUniquePath(tmpfolder)};
27+ const fs::path path2{GetUniquePath(tmpfolder)};
28+
29+ const std::string path1_contents{"1111"};
30+ const std::string path2_contents{"2222"};
31+
32+ {
33+ std::ofstream file{path1};
34+ file << path1_contents;
35+ }
36+
37+ {
38+ std::ofstream file{path2};
39+ file << path2_contents;
40+ }
41+
42+ // Rename path1 -> path2.
43+ BOOST_CHECK(RenameOver(path1, path2));
44+
45+ BOOST_CHECK(!fs::exists(path1));
46+
47+ {
48+ std::ifstream file{path2};
49+ std::string contents;
50+ file >> contents;
51+ BOOST_CHECK_EQUAL(contents, path1_contents);
52+ }
53+}
54+
55+BOOST_AUTO_TEST_SUITE_END()
Concept ACK. Checked the relevant documentation that the behavior is what we want for RenameOver
:
Moves or renames the filesystem object identified by old_p to new_p as if by the POSIX rename
- https://en.cppreference.com/w/cpp/filesystem/rename
If the link named by the new argument exists, it shall be removed and old renamed to new.
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
Also, it is a nice cleanup, no more win32-specific code here.
It seems to break Windows builds:
Add a test for this assumption?
Unit test for RenameOver()
Btw, it fails for mingw build: https://cirrus-ci.com/task/6670169794674688