0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

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.
This commit is contained in:
Vasil Dimov 2020-03-14 20:58:55 +01:00 committed by fanquake
parent 243a9c3925
commit ee822d85d6
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
2 changed files with 10 additions and 7 deletions

View file

@ -74,6 +74,7 @@
#include <memory>
#include <optional>
#include <string>
#include <system_error>
#include <thread>
#include <typeinfo>
@ -1061,13 +1062,9 @@ void ArgsManager::LogArgs() const
bool RenameOver(fs::path src, fs::path dest)
{
#ifdef WIN32
return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
MOVEFILE_REPLACE_EXISTING) != 0;
#else
int rc = std::rename(src.c_str(), dest.c_str());
return (rc == 0);
#endif /* WIN32 */
std::error_code error;
fs::rename(src, dest, error);
return !error;
}
/**

View file

@ -69,7 +69,13 @@ void DirectoryCommit(const fs::path &dirname);
bool TruncateFile(FILE *file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
/**
* Rename src to dest.
* @return true if the rename was successful.
*/
[[nodiscard]] bool RenameOver(fs::path src, fs::path dest);
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name);
bool DirIsWritable(const fs::path& directory);