Skip to content

Commit

Permalink
Remove static but add TerminateThread
Browse files Browse the repository at this point in the history
  • Loading branch information
donho committed Sep 29, 2024
1 parent 1037b2a commit 8a44235
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
85 changes: 65 additions & 20 deletions PowerEditor/src/MISC/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1764,60 +1764,105 @@ bool Version::isCompatibleTo(const Version& from, const Version& to) const
return false;
}


/*
bool doesFileExist(const wchar_t* filePath)
{
DWORD dwAttrib = ::GetFileAttributesW(filePath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
*/

struct GetAttrParamResult {
std::wstring _filePath;
DWORD _fileAttr;
DWORD _fileAttr = INVALID_FILE_ATTRIBUTES;
bool _isNetworkFailure = true;
};

DWORD WINAPI getFileAttributesWorker(void* data)
{
GetAttrParamResult* inAndOut = static_cast<GetAttrParamResult*>(data);
inAndOut->_fileAttr = ::GetFileAttributesW(inAndOut->_filePath.c_str());
inAndOut->_isNetworkFailure = false;
return TRUE;
};

DWORD getFileAttrWaitFewSec(const wchar_t* filePath)
DWORD getFileAttrWaitFewSec(const wchar_t* filePath, DWORD milleSec2wait, bool* isNetWorkProblem)
{
static GetAttrParamResult data;
GetAttrParamResult data;
data._fileAttr = INVALID_FILE_ATTRIBUTES;
data._filePath = filePath;
data._isNetworkFailure = true;

HANDLE hThread = ::CreateThread(NULL, 0, getFileAttributesWorker, &data, 0, NULL);
if (!hThread)
{
return false;
}

// Wait for 3 sec
::WaitForSingleObject(hThread, 3000);
// Wait for few sec
::WaitForSingleObject(hThread, milleSec2wait == 0 ? 1000 : milleSec2wait);
TerminateThread(hThread, 2);

CloseHandle(hThread);

if (isNetWorkProblem != nullptr)
*isNetWorkProblem = data._isNetworkFailure;

return data._fileAttr;
};

bool doesFileExist(const wchar_t* filePath)
bool doesFileExist(const wchar_t* filePath, DWORD milleSec2wait, bool* isNetWorkProblem)
{
DWORD attr = getFileAttrWaitFewSec(filePath);
DWORD attr = getFileAttrWaitFewSec(filePath, milleSec2wait, isNetWorkProblem);
return (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY));
}

bool doesDirectoryExist(const wchar_t* dirPath)
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milleSec2wait, bool* isNetWorkProblem)
{
DWORD attr = getFileAttrWaitFewSec(dirPath);
DWORD attr = getFileAttrWaitFewSec(dirPath, milleSec2wait, isNetWorkProblem);
return (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY));
}

bool doesPathExist(const wchar_t* path)
bool doesPathExist(const wchar_t* path, DWORD milleSec2wait, bool* isNetWorkProblem)
{
DWORD attr = getFileAttrWaitFewSec(path);
DWORD attr = getFileAttrWaitFewSec(path, milleSec2wait, isNetWorkProblem);
return (attr != INVALID_FILE_ATTRIBUTES);
}


struct GetDiskFreeSpaceParamResult
{
std::wstring _dirPath;
ULARGE_INTEGER _freeBytesForUser {};
DWORD _result = FALSE;
bool _isNetworkFailure = true;
};

DWORD WINAPI getDiskFreeSpaceExWorker(void* data)
{
GetDiskFreeSpaceParamResult* inAndOut = static_cast<GetDiskFreeSpaceParamResult*>(data);
inAndOut->_result = ::GetDiskFreeSpaceExW(inAndOut->_dirPath.c_str(), &(inAndOut->_freeBytesForUser), nullptr, nullptr);
inAndOut->_isNetworkFailure = false;
return TRUE;
};


DWORD getDiskFreeSpaceWaitFewSec(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milleSec2wait, bool* isNetWorkProblem)
{
GetDiskFreeSpaceParamResult data;
data._dirPath = dirPath;
data._freeBytesForUser = {};
data._result = FALSE;
data._isNetworkFailure = true;

HANDLE hThread = ::CreateThread(NULL, 0, getDiskFreeSpaceExWorker, &data, 0, NULL);
if (!hThread)
{
return false;
}

// Wait for 1 sec
::WaitForSingleObject(hThread, milleSec2wait == 0 ? 1000 : milleSec2wait);
TerminateThread(hThread, 2);

CloseHandle(hThread);

*freeBytesForUser = data._freeBytesForUser;

if (isNetWorkProblem != nullptr)
*isNetWorkProblem = data._isNetworkFailure;

return data._result;
};
8 changes: 5 additions & 3 deletions PowerEditor/src/MISC/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ class Version final
unsigned long _build = 0;
};

bool doesFileExist(const wchar_t* filePath);
bool doesDirectoryExist(const wchar_t* dirPath);
bool doesPathExist(const wchar_t* path);
bool doesFileExist(const wchar_t* filePath, DWORD milleSec2wait = 0, bool* isNetWorkProblem = nullptr);
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milleSec2wait = 0, bool* isNetWorkProblem = nullptr);
bool doesPathExist(const wchar_t* path, DWORD milleSec2wait = 0, bool* isNetWorkProblem = nullptr);

DWORD getDiskFreeSpaceWaitFewSec(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milleSec2wait = 0, bool* isNetWorkProblem = nullptr);
3 changes: 2 additions & 1 deletion PowerEditor/src/Notepad_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2579,7 +2579,8 @@ void Notepad_plus::checkDocState()

bool isCurrentDirty = curBuf->isDirty();
bool isSeveralDirty = isCurrentDirty;
bool isFileExisting = doesFileExist(curBuf->getFullPathName());
bool isNetworkProblem;
bool isFileExisting = doesFileExist(curBuf->getFullPathName(), 1000, &isNetworkProblem);
if (!isCurrentDirty)
{
for (size_t i = 0; i < MainFileManager.getNbBuffers(); ++i)
Expand Down
6 changes: 4 additions & 2 deletions PowerEditor/src/ScintillaComponent/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,8 @@ SavingStatus FileManager::saveBuffer(BufferID id, const wchar_t* filename, bool
const wchar_t* currentBufFilePath = buffer->getFullPathName();
ULARGE_INTEGER freeBytesForUser;

BOOL getFreeSpaceRes = ::GetDiskFreeSpaceExW(dirDest, &freeBytesForUser, nullptr, nullptr);
if (getFreeSpaceRes != FALSE)
BOOL getFreeSpaceSuccessful = getDiskFreeSpaceWaitFewSec(dirDest, &freeBytesForUser);
if (getFreeSpaceSuccessful)
{
int64_t fileSize = buffer->getFileLength();
if (fileSize >= 0 && lstrcmp(fullpath, currentBufFilePath) == 0) // if file to save does exist, and it's an operation "Save" but not "Save As"
Expand Down Expand Up @@ -1305,6 +1305,8 @@ SavingStatus FileManager::saveBuffer(BufferID id, const wchar_t* filename, bool
}
else
{
//if (buffer->networkFailed)
//return SavingStatus::NetworkProblem;
return SavingStatus::SaveOpenFailed;
}
}
Expand Down
3 changes: 2 additions & 1 deletion PowerEditor/src/ScintillaComponent/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ enum SavingStatus {
SaveOK = 0,
SaveOpenFailed = 1,
SaveWritingFailed = 2,
NotEnoughRoom = 3
NotEnoughRoom = 3,
NetworkProblem = 4
};

struct BufferViewInfo {
Expand Down

0 comments on commit 8a44235

Please sign in to comment.