From d80e989dbafefcbf5718a4f91ee4743993ae681c Mon Sep 17 00:00:00 2001 From: Ingmar Runge Date: Thu, 6 Feb 2014 18:06:29 +0100 Subject: [PATCH] work around Win32 rename() issue using native API --- src/FileUtils.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/FileUtils.cpp b/src/FileUtils.cpp index ead69583..7224a497 100644 --- a/src/FileUtils.cpp +++ b/src/FileUtils.cpp @@ -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 }