mirror of
https://github.com/znc/znc.git
synced 2026-05-04 04:22:37 +02:00
work around Win32 rename() issue using native API
This commit is contained in:
@@ -210,15 +210,9 @@ bool CFile::Move(const CString& sOldFileName, const CString& sNewFileName, bool
|
||||
return false;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
// Windows (or maybe cygwin?) sometimes fails to rename() file over an existing file
|
||||
// We don't want to remove the old file before moving new file over, for the case if the following rename() will fail for any reason
|
||||
CString sTempFile = sNewFileName
|
||||
+ CUtils::FormatTime(time(NULL), "_%Y%m%d-%H%M%S_", "UTC")
|
||||
+ CString::RandomString(10).MD5(); // this file shouldn't exist :)
|
||||
bool result = rename(sNewFileName.c_str(), sTempFile.c_str()) == 0;
|
||||
result &&= rename(sOldFileName.c_str(), sNewFileName.c_str()) == 0;
|
||||
result &&= Delete(sTempFile); // ok, renamed the file successfully, it's safe to remove backup of old file now
|
||||
return result;
|
||||
// rename() never overwrites files on Windows.
|
||||
DWORD dFlags = MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
|
||||
return (::MoveFileExA(sOldFileName.c_str(), sNewFileName.c_str(), dFlags) != 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user