Utils: fix savefile in case we save new file (Windows)

Previous fix accidently broke saving files that do not
exist yet. They need to call rename instead of ReplaceFile.

Change-Id: Ida47845a59c1f2fe26a39dc593ab0f83fae9d18b
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
This commit is contained in:
Ivan Donchevskii
2017-12-18 15:42:51 +01:00
parent 3006a13869
commit bc4efa662c

View File

@@ -124,19 +124,31 @@ bool SaveFile::commit()
#ifdef Q_OS_WIN
// Release the file lock
m_tempFile.reset();
bool replaceResult = ReplaceFile(finalFileName.toStdWString().data(),
bool result = ReplaceFile(finalFileName.toStdWString().data(),
fileName().toStdWString().data(),
nullptr, 0, nullptr, nullptr);
if (!replaceResult) {
if (!result) {
const DWORD replaceErrorCode = GetLastError();
QString errorStr;
if (!QFile::exists(finalFileName)) {
// Replace failed because finalFileName does not exist, try rename.
if (!(result = rename(finalFileName)))
errorStr = errorString();
} else {
wchar_t messageBuffer[256];
size_t size = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, GetLastError(),
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, replaceErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
messageBuffer, sizeof(messageBuffer), nullptr);
setErrorString(QString::fromWCharArray(messageBuffer));
remove();
errorStr = QString::fromWCharArray(messageBuffer);
}
return replaceResult;
if (!result) {
remove();
setErrorString(errorStr);
}
}
return result;
#else
const QString backupName = finalFileName + '~';