Browse Source

Code enhancement for file checking

Add missing dwFileAttributes init to INVALID_FILE_ATTRIBUTES.

Close #15820
pull/15829/head
xomx 6 days ago committed by Don Ho
parent
commit
c021c86195
  1. 1
      PowerEditor/installer/nativeLang/english.xml
  2. 1
      PowerEditor/installer/nativeLang/english_customizable.xml
  3. 13
      PowerEditor/src/MISC/Common/Common.cpp
  4. 4
      PowerEditor/src/MISC/Common/Common.h
  5. 1
      PowerEditor/src/MISC/Common/FileInterface.cpp
  6. 2
      PowerEditor/src/Parameters.cpp
  7. 26
      PowerEditor/src/ScintillaComponent/Buffer.cpp
  8. 14
      PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.cpp
  9. 5
      PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.h

1
PowerEditor/installer/nativeLang/english.xml

@ -1552,6 +1552,7 @@ Press the OK button to open the Find dialog or set focus on it.
If you require the backward regex searching feature, consult the user manual for instructions on enabling it."/>
<PrintError title="0" message="Cannot start printer document."/><!-- Use title="0" to use Windows OS default translated "Error" title. -->
<FileToLoadSizeCheckFailed title="File to load size-check failed" message="Cannot obtain the file size before loading!"/>
</MessageBox>
<ClipboardHistory>
<PanelTitle name="Clipboard History"/>

1
PowerEditor/installer/nativeLang/english_customizable.xml

@ -1550,6 +1550,7 @@ Press the OK button to open the Find dialog or set focus on it.
If you require the backward regex searching feature, consult the user manual for instructions on enabling it."/>
<PrintError title="0" message="Cannot start printer document."/><!-- Use title="0" to use Windows OS default translated "Error" title. -->
<FileToLoadSizeCheckFailed title="File to load size-check failed" message="Cannot obtain the file size before loading!"/>
</MessageBox>
<ClipboardHistory>
<PanelTitle name="Clipboard History"/>

13
PowerEditor/src/MISC/Common/Common.cpp

@ -1778,7 +1778,7 @@ struct GetDiskFreeSpaceParamResult
{
std::wstring _dirPath;
ULARGE_INTEGER _freeBytesForUser {};
DWORD _result = FALSE;
BOOL _result = FALSE;
bool _isTimeoutReached = true;
GetDiskFreeSpaceParamResult(wstring dirPath) : _dirPath(dirPath) {};
@ -1792,7 +1792,7 @@ DWORD WINAPI getDiskFreeSpaceExWorker(void* data)
return ERROR_SUCCESS;
};
DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isTimeoutReached)
BOOL getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isTimeoutReached)
{
GetDiskFreeSpaceParamResult data(dirPath);
@ -1833,7 +1833,7 @@ struct GetAttrExParamResult
{
wstring _filePath;
WIN32_FILE_ATTRIBUTE_DATA _attributes{};
DWORD _result = FALSE;
BOOL _result = FALSE;
bool _isTimeoutReached = true;
GetAttrExParamResult(wstring filePath): _filePath(filePath) {
@ -1844,12 +1844,12 @@ struct GetAttrExParamResult
DWORD WINAPI getFileAttributesExWorker(void* data)
{
GetAttrExParamResult* inAndOut = static_cast<GetAttrExParamResult*>(data);
inAndOut->_result = ::GetFileAttributesEx(inAndOut->_filePath.c_str(), GetFileExInfoStandard, &(inAndOut->_attributes));
inAndOut->_result = ::GetFileAttributesExW(inAndOut->_filePath.c_str(), GetFileExInfoStandard, &(inAndOut->_attributes));
inAndOut->_isTimeoutReached = false;
return ERROR_SUCCESS;
};
DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isTimeoutReached)
BOOL getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isTimeoutReached)
{
GetAttrExParamResult data(filePath);
@ -1886,6 +1886,7 @@ DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBU
bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait, bool* isTimeoutReached)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
getFileAttributesExWithTimeout(filePath, &attributes, milliSec2wait, isTimeoutReached);
return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && !(attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
}
@ -1893,6 +1894,7 @@ bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait, bool* isTimeout
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait, bool* isTimeoutReached)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
getFileAttributesExWithTimeout(dirPath, &attributes, milliSec2wait, isTimeoutReached);
return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && (attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
}
@ -1900,6 +1902,7 @@ bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait, bool* isTim
bool doesPathExist(const wchar_t* path, DWORD milliSec2wait, bool* isTimeoutReached)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
getFileAttributesExWithTimeout(path, &attributes, milliSec2wait, isTimeoutReached);
return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES);
}

4
PowerEditor/src/MISC/Common/Common.h

@ -283,8 +283,8 @@ private:
};
DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
BOOL getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
BOOL getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr);

1
PowerEditor/src/MISC/Common/FileInterface.cpp

@ -31,6 +31,7 @@ Win32_IO_File::Win32_IO_File(const wchar_t *fname)
_path = converter.to_bytes(fn);
WIN32_FILE_ATTRIBUTE_DATA attributes_original{};
attributes_original.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
DWORD dispParam = CREATE_ALWAYS;
bool fileExists = false;
bool isTimeoutReached = false;

2
PowerEditor/src/Parameters.cpp

@ -1270,7 +1270,7 @@ bool NppParameters::load()
if (doesFileExist(langs_xml_path.c_str()))
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
if (GetFileAttributesEx(langs_xml_path.c_str(), GetFileExInfoStandard, &attributes) != 0)
{
if (attributes.nFileSizeLow == 0 && attributes.nFileSizeHigh == 0)

26
PowerEditor/src/ScintillaComponent/Buffer.cpp

@ -142,7 +142,8 @@ void Buffer::updateTimeStamp()
{
FILETIME timeStampLive {};
WIN32_FILE_ATTRIBUTE_DATA attributes{};
if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes) != FALSE)
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes))
{
timeStampLive = attributes.ftLastWriteTime;
}
@ -260,6 +261,7 @@ bool Buffer::checkFileState() // returns true if the status has been changed (it
return false;
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
NppParameters& nppParam = NppParameters::getInstance();
bool fileExists = doesFileExist(_fullPathName.c_str());
@ -311,7 +313,7 @@ bool Buffer::checkFileState() // returns true if the status has been changed (it
isOK = true;
}
}
else if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes) != FALSE)
else if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes))
{
int mask = 0; //status always 'changes', even if from modified to modified
bool isFileReadOnly = attributes.dwFileAttributes & FILE_ATTRIBUTE_READONLY;
@ -381,6 +383,7 @@ bool Buffer::checkFileState() // returns true if the status has been changed (it
void Buffer::reload()
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
if (GetFileAttributesEx(_fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0)
{
_timeStamp = attributes.ftLastWriteTime;
@ -395,6 +398,7 @@ int64_t Buffer::getFileLength() const
return -1;
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
if (GetFileAttributesEx(_fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0)
{
LARGE_INTEGER size{};
@ -428,6 +432,7 @@ wstring Buffer::getTimeString(FILETIME rawtime) const
wstring Buffer::getFileTime(fileTimeType ftt) const
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
if (GetFileAttributesEx(_currentStatus == DOC_UNNAMED ? _backupFileName.c_str() : _fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0)
{
FILETIME rawtime;
@ -710,7 +715,8 @@ BufferID FileManager::loadFile(const wchar_t* filename, Document doc, int encodi
if (pPath)
{
WIN32_FILE_ATTRIBUTE_DATA attributes{};
if (getFileAttributesExWithTimeout(pPath, &attributes) != FALSE)
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
if (getFileAttributesExWithTimeout(pPath, &attributes))
{
LARGE_INTEGER size{};
size.LowPart = attributes.nFileSizeLow;
@ -720,6 +726,18 @@ BufferID FileManager::loadFile(const wchar_t* filename, Document doc, int encodi
}
}
if (fileSize == -1)
{
// we cannot continue (or Scintilla will throw SC_STATUS_FAILURE in the loadFileData later)
NativeLangSpeaker* pNativeSpeaker = NppParameters::getInstance().getNativeLangSpeaker();
pNativeSpeaker->messageBox("FileToLoadSizeCheckFailed",
_pNotepadPlus->_pEditView->getHSelf(),
L"Cannot obtain the file size before loading!",
L"File to load size-check failed",
MB_OK | MB_APPLMODAL);
return BUFFER_INVALID;
}
// * the auto-completion feature will be disabled for large files
// * the session snapshots and periodic backups feature will be disabled for large files
// * the backups on save feature will be disabled for large files
@ -845,6 +863,7 @@ bool FileManager::reloadBuffer(BufferID id)
//Get file size
int64_t fileSize = 0;
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
getFileAttributesExWithTimeout(buf->getFullPathName(), &attributes);
if (attributes.dwFileAttributes == INVALID_FILE_ATTRIBUTES)
{
@ -1209,6 +1228,7 @@ SavingStatus FileManager::saveBuffer(BufferID id, const wchar_t* filename, bool
}
WIN32_FILE_ATTRIBUTE_DATA attributes{};
attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
getFileAttributesExWithTimeout(fullpath, &attributes);
if (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && !(attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{

14
PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.cpp

@ -2,17 +2,21 @@
BOOL CReadFileChanges::DetectChanges() {
WIN32_FILE_ATTRIBUTE_DATA fInfo;
BOOL CReadFileChanges::DetectChanges()
{
WIN32_FILE_ATTRIBUTE_DATA fInfo{};
fInfo.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
BOOL rValue = FALSE;
::GetFileAttributesEx(_szFile, GetFileExInfoStandard, &fInfo);
if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_SIZE) && (fInfo.nFileSizeHigh != _lastFileInfo.nFileSizeHigh || fInfo.nFileSizeLow != _lastFileInfo.nFileSizeLow)) {
if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_SIZE) && (fInfo.nFileSizeHigh != _lastFileInfo.nFileSizeHigh || fInfo.nFileSizeLow != _lastFileInfo.nFileSizeLow))
{
rValue = TRUE;
}
if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_LAST_WRITE) && (fInfo.ftLastWriteTime.dwHighDateTime != _lastFileInfo.ftLastWriteTime.dwHighDateTime || fInfo.ftLastWriteTime.dwLowDateTime != _lastFileInfo.ftLastWriteTime.dwLowDateTime)) {
if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_LAST_WRITE) && (fInfo.ftLastWriteTime.dwHighDateTime != _lastFileInfo.ftLastWriteTime.dwHighDateTime || fInfo.ftLastWriteTime.dwLowDateTime != _lastFileInfo.ftLastWriteTime.dwLowDateTime))
{
rValue = TRUE;
}

5
PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.h

@ -14,7 +14,9 @@
class CReadFileChanges
{
public:
CReadFileChanges() {};
CReadFileChanges() {
_lastFileInfo.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
};
~CReadFileChanges() {};
void AddFile(LPCTSTR szDirectory, DWORD dwNotifyFilter);
BOOL DetectChanges();
@ -24,6 +26,5 @@ private:
LPCTSTR _szFile = nullptr;
DWORD _dwNotifyFilter = 0;
WIN32_FILE_ATTRIBUTE_DATA _lastFileInfo = {};
};

Loading…
Cancel
Save