Commit 29b79144 authored by Ivan Donchevskii's avatar Ivan Donchevskii

Utils: workaround for saving file opened by another application

...fixes file saving on Windows in specific circumstances.

File deletion on Windows is quite tricky. If you simply
remove the file opened by another app that shares it for
deletion the file might become semi-removed. It looks
like it still exists but it is in fact inaccessible for
any operations until the other app frees the handle.

To solve that case there is a ReplaceFile API call
which carefully deals with that case.

[Backstory] Oswald Buddenhagen insists that this fix is
rather a workaround and we should solve file access
problem in llvm.
For that purpose we have QTCREATORBUG-19404 and upstream

Task-number: QTCREATORBUG-15449
Change-Id: If37d484231b79d8e33822c795232dc31243c88c0
Reviewed-by: David Schulz's avatarDavid Schulz <>
parent 010fb417
...@@ -119,6 +119,24 @@ bool SaveFile::commit() ...@@ -119,6 +119,24 @@ bool SaveFile::commit()
QString finalFileName QString finalFileName
= FileUtils::resolveSymlinks(FileName::fromString(m_finalFileName)).toString(); = FileUtils::resolveSymlinks(FileName::fromString(m_finalFileName)).toString();
#ifdef Q_OS_WIN
// Release the file lock
bool replaceResult = ReplaceFile(finalFileName.toStdWString().data(),
nullptr, 0, nullptr, nullptr);
if (!replaceResult) {
wchar_t messageBuffer[256];
nullptr, GetLastError(),
messageBuffer, sizeof(messageBuffer), nullptr);
return replaceResult;
const QString backupName = finalFileName + '~'; const QString backupName = finalFileName + '~';
// Back up current file. // Back up current file.
...@@ -148,6 +166,7 @@ bool SaveFile::commit() ...@@ -148,6 +166,7 @@ bool SaveFile::commit()
QFile::remove(backupName); QFile::remove(backupName);
return result; return result;
} }
void SaveFile::initializeUmask() void SaveFile::initializeUmask()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment