diff --git a/PowerEditor/src/MISC/Common/FileInterface.cpp b/PowerEditor/src/MISC/Common/FileInterface.cpp index c5062adb8..0305e632f 100644 --- a/PowerEditor/src/MISC/Common/FileInterface.cpp +++ b/PowerEditor/src/MISC/Common/FileInterface.cpp @@ -16,18 +16,10 @@ #include #include +#include #include "FileInterface.h" #include "Parameters.h" -Win32_IO_File::Win32_IO_File(const char *fname) -{ - if (fname) - { - _path = fname; - _hFile = ::CreateFileA(fname, _accessParam, _shareParam, NULL, _dispParam, _attribParam, NULL); - } -} - Win32_IO_File::Win32_IO_File(const wchar_t *fname) { @@ -36,7 +28,17 @@ Win32_IO_File::Win32_IO_File(const wchar_t *fname) std::wstring fn = fname; std::wstring_convert> converter; _path = converter.to_bytes(fn); - _hFile = ::CreateFileW(fname, _accessParam, _shareParam, NULL, _dispParam, _attribParam, NULL); + + DWORD dispParam = ::PathFileExistsW(fname) ? TRUNCATE_EXISTING : CREATE_ALWAYS; + _hFile = ::CreateFileW(fname, _accessParam, _shareParam, NULL, dispParam, _attribParam, NULL); + + // Race condition management: + // If file didn't exist while calling PathFileExistsW, but before calling CreateFileW, file is created: use CREATE_ALWAYS is OK + // If file did exist while calling PathFileExistsW, but before calling CreateFileW, file is deleted: use TRUNCATE_EXISTING will cause the error + if (_hFile == INVALID_HANDLE_VALUE && ::GetLastError() == ERROR_FILE_NOT_FOUND) + { + _hFile = ::CreateFileW(fname, _accessParam, _shareParam, NULL, CREATE_ALWAYS, _attribParam, NULL); + } NppParameters& nppParam = NppParameters::getInstance(); if (nppParam.isEndSessionStarted() && nppParam.doNppLogNulContentCorruptionIssue()) @@ -104,31 +106,6 @@ void Win32_IO_File::close() } } -/* -int_fast64_t Win32_IO_File::getSize() -{ - LARGE_INTEGER r; - r.QuadPart = -1; - - if (isOpened()) - ::GetFileSizeEx(_hFile, &r); - - return static_cast(r.QuadPart); -} - -unsigned long Win32_IO_File::read(void *rbuf, unsigned long buf_size) -{ - if (!isOpened() || (rbuf == nullptr) || (buf_size == 0)) - return 0; - - DWORD bytes_read = 0; - - if (::ReadFile(_hFile, rbuf, buf_size, &bytes_read, NULL) == FALSE) - return 0; - - return bytes_read; -} -*/ bool Win32_IO_File::write(const void *wbuf, size_t buf_size) { diff --git a/PowerEditor/src/MISC/Common/FileInterface.h b/PowerEditor/src/MISC/Common/FileInterface.h index 04f98ae07..52d1d9d92 100644 --- a/PowerEditor/src/MISC/Common/FileInterface.h +++ b/PowerEditor/src/MISC/Common/FileInterface.h @@ -42,8 +42,6 @@ public: }; void close(); - //int_fast64_t getSize(); - //unsigned long read(void *rbuf, unsigned long buf_size); bool write(const void *wbuf, size_t buf_size); @@ -58,6 +56,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 }; };