Fix saving files loosing Alternate Data Stream issue

Fix #1498, fix #8451, fix #8013, close #14039
pull/14177/head
Don Ho 2023-08-23 03:25:43 +02:00
parent 01ff1eb881
commit b068aa9033
2 changed files with 12 additions and 38 deletions

View File

@ -16,18 +16,10 @@
#include <locale>
#include <codecvt>
#include <shlwapi.h>
#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<std::codecvt_utf8_utf16<wchar_t>> 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<int_fast64_t>(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)
{

View File

@ -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 };
};