aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2018-09-26 09:49:02 +0200
committerDavid Schulz <david.schulz@qt.io>2018-09-26 10:58:11 +0000
commit8d609692b397f2fad522ec35a430f92bf691f44d (patch)
treebe0ca8ed62f8ddd058da1190e70f42d6e7d23bfc
parent4d7f825deb8767f23ab2eb3bd1adddea8c9770bb (diff)
Utils: Fix file saver for delete locked files
If we can not delete a file via the ReplaceFile operation, because we ca not get the DELETE access right we still can try to replace it contents. Task-number: QTCREATORBUG-7668 Change-Id: I8804133a0e118518307f33976b821d5b2fdc9b8d Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
-rw-r--r--src/libs/utils/savefile.cpp27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/libs/utils/savefile.cpp b/src/libs/utils/savefile.cpp
index 13b9db0d84..7f26761419 100644
--- a/src/libs/utils/savefile.cpp
+++ b/src/libs/utils/savefile.cpp
@@ -128,19 +128,32 @@ bool SaveFile::commit()
fileName().toStdWString().data(),
nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
if (!result) {
- const DWORD replaceErrorCode = GetLastError();
+ 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];
- FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- nullptr, replaceErrorCode,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- messageBuffer, sizeof(messageBuffer), nullptr);
- errorStr = QString::fromWCharArray(messageBuffer);
+ if (replaceErrorCode == ERROR_UNABLE_TO_REMOVE_REPLACED) {
+ // If we do not get the rights to remove the original final file we still might try
+ // to replace the file contents
+ result = MoveFileEx(fileName().toStdWString().data(),
+ finalFileName.toStdWString().data(),
+ MOVEFILE_COPY_ALLOWED
+ | MOVEFILE_REPLACE_EXISTING
+ | MOVEFILE_WRITE_THROUGH);
+ if (!result)
+ replaceErrorCode = GetLastError();
+ }
+ if (!result) {
+ wchar_t messageBuffer[256];
+ FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ nullptr, replaceErrorCode,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ messageBuffer, sizeof(messageBuffer), nullptr);
+ errorStr = QString::fromWCharArray(messageBuffer);
+ }
}
if (!result) {
remove();