Skip to content

Commit ee822d8

Browse files
vasildfanquake
authored andcommitted
util: use stronger-guarantee rename method
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.
1 parent 243a9c3 commit ee822d8

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/util/system.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include <memory>
7575
#include <optional>
7676
#include <string>
77+
#include <system_error>
7778
#include <thread>
7879
#include <typeinfo>
7980

@@ -1061,13 +1062,9 @@ void ArgsManager::LogArgs() const
10611062

10621063
bool RenameOver(fs::path src, fs::path dest)
10631064
{
1064-
#ifdef WIN32
1065-
return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
1066-
MOVEFILE_REPLACE_EXISTING) != 0;
1067-
#else
1068-
int rc = std::rename(src.c_str(), dest.c_str());
1069-
return (rc == 0);
1070-
#endif /* WIN32 */
1065+
std::error_code error;
1066+
fs::rename(src, dest, error);
1067+
return !error;
10711068
}
10721069

10731070
/**

src/util/system.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ void DirectoryCommit(const fs::path &dirname);
6969
bool TruncateFile(FILE *file, unsigned int length);
7070
int RaiseFileDescriptorLimit(int nMinFD);
7171
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
72+
73+
/**
74+
* Rename src to dest.
75+
* @return true if the rename was successful.
76+
*/
7277
[[nodiscard]] bool RenameOver(fs::path src, fs::path dest);
78+
7379
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
7480
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name);
7581
bool DirIsWritable(const fs::path& directory);

0 commit comments

Comments
 (0)