From d9c5688635ae78ad04b739fb59bb7868f1903890 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Thu, 17 Mar 2022 19:57:05 +0100 Subject: [PATCH] Fix file saving critical bug under Symantec encryption desktop Fix #11339, close #11403 --- PowerEditor/src/MISC/Common/Common.cpp | 3 ++- PowerEditor/src/MISC/Common/FileInterface.cpp | 19 +++++++++++++++++-- PowerEditor/src/MISC/Common/FileInterface.h | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index 6f3a09da0..b4291b0d1 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -127,7 +127,7 @@ void writeLog(const TCHAR *logFileName, const char *log2write) const DWORD accessParam{ GENERIC_READ | GENERIC_WRITE }; const DWORD shareParam{ FILE_SHARE_READ | FILE_SHARE_WRITE }; const DWORD dispParam{ OPEN_ALWAYS }; // Open existing file for writing without destroying it or create new - const DWORD attribParam{ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH }; + const DWORD attribParam{ FILE_ATTRIBUTE_NORMAL }; HANDLE hFile = ::CreateFileW(logFileName, accessParam, shareParam, NULL, dispParam, attribParam, NULL); if (hFile != INVALID_HANDLE_VALUE) @@ -149,6 +149,7 @@ void writeLog(const TCHAR *logFileName, const char *log2write) ::WriteFile(hFile, log2writeStr.c_str(), static_cast(log2writeStr.length()), &bytes_written, NULL); ::FlushFileBuffers(hFile); + ::CloseHandle(hFile); } } diff --git a/PowerEditor/src/MISC/Common/FileInterface.cpp b/PowerEditor/src/MISC/Common/FileInterface.cpp index 44dec3ac4..4915b6eee 100644 --- a/PowerEditor/src/MISC/Common/FileInterface.cpp +++ b/PowerEditor/src/MISC/Common/FileInterface.cpp @@ -57,9 +57,11 @@ void Win32_IO_File::close() { if (isOpened()) { + DWORD flushError = NOERROR; if (_written) { - ::FlushFileBuffers(_hFile); + if (!::FlushFileBuffers(_hFile)) + flushError = ::GetLastError(); } ::CloseHandle(_hFile); @@ -74,7 +76,20 @@ void Win32_IO_File::close() generic_string nppIssueLog = nppParam.getUserPath(); pathAppend(nppIssueLog, issueFn); - std::string msg = _path; + + std::string msg; + if (flushError != NOERROR) + { + LPSTR messageBuffer = nullptr; + FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, flushError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, nullptr); + msg += messageBuffer; + + //Free the buffer. + LocalFree(messageBuffer); + msg += "\n"; + } + msg += _path; msg += " is closed."; writeLog(nppIssueLog.c_str(), msg.c_str()); } diff --git a/PowerEditor/src/MISC/Common/FileInterface.h b/PowerEditor/src/MISC/Common/FileInterface.h index b191082fc..b713ae7ed 100644 --- a/PowerEditor/src/MISC/Common/FileInterface.h +++ b/PowerEditor/src/MISC/Common/FileInterface.h @@ -59,5 +59,5 @@ private: const DWORD _accessParam { GENERIC_READ | GENERIC_WRITE }; const DWORD _shareParam { FILE_SHARE_READ | FILE_SHARE_WRITE }; const DWORD _dispParam { CREATE_ALWAYS }; - const DWORD _attribParam { FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH }; + const DWORD _attribParam { FILE_ATTRIBUTE_NORMAL }; };