Skip to content

Commit

Permalink
Remove logs
Browse files Browse the repository at this point in the history
  • Loading branch information
donho committed Oct 9, 2024
1 parent 58862e1 commit e22972c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 90 deletions.
152 changes: 104 additions & 48 deletions PowerEditor/src/MISC/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1840,79 +1840,84 @@ bool doesPathExist(const wchar_t* path, DWORD milliSec2wait, bool* isNetWorkProb

//----------------------------------------------------

struct wfopenParamResult
struct GetDiskFreeSpaceParamResult
{
std::wstring _filePath;
FILE* _pFile = nullptr;
std::wstring _dirPath;
ULARGE_INTEGER _freeBytesForUser {};
DWORD _result = FALSE;
bool _isNetworkFailure = true;
wfopenParamResult(wstring filePath) : _filePath(filePath) {};
GetDiskFreeSpaceParamResult(wstring dirPath) : _dirPath(dirPath) {};
};

DWORD WINAPI wfopenWorker(void* data)
DWORD WINAPI getDiskFreeSpaceExWorker(void* data)
{
wfopenParamResult* inAndOut = static_cast<wfopenParamResult*>(data);
inAndOut->_pFile = _wfopen(inAndOut->_filePath.c_str(), L"rb");
GetDiskFreeSpaceParamResult* inAndOut = static_cast<GetDiskFreeSpaceParamResult*>(data);
inAndOut->_result = ::GetDiskFreeSpaceExW(inAndOut->_dirPath.c_str(), &(inAndOut->_freeBytesForUser), nullptr, nullptr);
inAndOut->_isNetworkFailure = false;
return ERROR_SUCCESS;
};

FILE* wfopenWaitSec(const wchar_t* filePath, DWORD milliSec2wait, bool* isNetWorkProblem)
DWORD getDiskFreeSpaceWaitSec(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isNetWorkProblem)
{
wfopenParamResult data(filePath);
GetDiskFreeSpaceParamResult data(dirPath);

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

// wait for our worker thread to complete or terminate it when the required timeout has elapsed
DWORD dwWaitStatus = ::WaitForSingleObject(hThread, milliSec2wait == 0 ? DEFAULT_MILLISEC : milliSec2wait);
switch (dwWaitStatus)
{
case WAIT_OBJECT_0: // Ok, the state of our worker thread is signaled, so it finished itself in the timeout given
// - nothing else to do here, except the thread handle closing later
break;
case WAIT_OBJECT_0: // Ok, the state of our worker thread is signaled, so it finished itself in the timeout given
// - nothing else to do here, except the thread handle closing later
break;

case WAIT_TIMEOUT: // the timeout interval elapsed, but the worker's state is still non-signaled
default: // any other dwWaitStatus is a BAD one here
// WAIT_FAILED or WAIT_ABANDONED
::TerminateThread(hThread, dwWaitStatus);
break;
case WAIT_TIMEOUT: // the timeout interval elapsed, but the worker's state is still non-signaled
default: // any other dwWaitStatus is a BAD one here
// WAIT_FAILED or WAIT_ABANDONED
::TerminateThread(hThread, dwWaitStatus);
break;
}
CloseHandle(hThread);

*freeBytesForUser = data._freeBytesForUser;

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

return data._pFile;
return data._result;
}


//----------------------------------------------------

struct GetDiskFreeSpaceParamResult
struct GetAttrExParamResult
{
std::wstring _dirPath;
ULARGE_INTEGER _freeBytesForUser {};
wstring _filePath;
WIN32_FILE_ATTRIBUTE_DATA _attributes{};
DWORD _result = FALSE;
bool _isNetworkFailure = true;
GetDiskFreeSpaceParamResult(wstring dirPath) : _dirPath(dirPath) {};
GetAttrExParamResult(wstring filePath): _filePath(filePath) {
_attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
}
};

DWORD WINAPI getDiskFreeSpaceExWorker(void* data)
DWORD WINAPI getFileAttributesExWorker(void* data)
{
GetDiskFreeSpaceParamResult* inAndOut = static_cast<GetDiskFreeSpaceParamResult*>(data);
inAndOut->_result = ::GetDiskFreeSpaceExW(inAndOut->_dirPath.c_str(), &(inAndOut->_freeBytesForUser), nullptr, nullptr);
GetAttrExParamResult* inAndOut = static_cast<GetAttrExParamResult*>(data);
inAndOut->_result = ::GetFileAttributesEx(inAndOut->_filePath.c_str(), GetFileExInfoStandard, &(inAndOut->_attributes));
inAndOut->_isNetworkFailure = false;
return ERROR_SUCCESS;
};

DWORD getDiskFreeSpaceWaitSec(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isNetWorkProblem)
DWORD getFileAttributesExWaitSec(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isNetWorkProblem)
{
GetDiskFreeSpaceParamResult data(dirPath);
GetAttrExParamResult data(filePath);

HANDLE hThread = ::CreateThread(NULL, 0, getDiskFreeSpaceExWorker, &data, 0, NULL);
HANDLE hThread = ::CreateThread(NULL, 0, getFileAttributesExWorker, &data, 0, NULL);
if (!hThread)
{
return FALSE;
Expand All @@ -1934,7 +1939,7 @@ DWORD getDiskFreeSpaceWaitSec(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesF
}
CloseHandle(hThread);

*freeBytesForUser = data._freeBytesForUser;
*fileAttr = data._attributes;

if (isNetWorkProblem != nullptr)
*isNetWorkProblem = data._isNetworkFailure;
Expand All @@ -1945,33 +1950,35 @@ DWORD getDiskFreeSpaceWaitSec(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesF

//----------------------------------------------------

struct GetAttrExParamResult
struct CreateFileParamResult
{
wstring _filePath;
WIN32_FILE_ATTRIBUTE_DATA _attributes{};
DWORD _result = FALSE;
HANDLE _hFile = INVALID_HANDLE_VALUE;
DWORD _accessParam = GENERIC_READ | GENERIC_WRITE;
DWORD _shareParam = FILE_SHARE_READ | FILE_SHARE_WRITE;
DWORD _dispParam = CREATE_ALWAYS;
DWORD _attribParam = FILE_ATTRIBUTE_NORMAL;
bool _isNetworkFailure = true;
GetAttrExParamResult(wstring filePath): _filePath(filePath) {
_attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES;
}
CreateFileParamResult(wstring filePath, DWORD accessParam, DWORD shareParam, DWORD dispParam, DWORD attribParam) :
_filePath(filePath), _accessParam(accessParam), _shareParam(shareParam), _dispParam(dispParam), _attribParam(attribParam) {};
};

DWORD WINAPI getFileAttributesExWorker(void* data)
DWORD WINAPI createFileWorker(void* data)
{
GetAttrExParamResult* inAndOut = static_cast<GetAttrExParamResult*>(data);
inAndOut->_result = ::GetFileAttributesEx(inAndOut->_filePath.c_str(), GetFileExInfoStandard, &(inAndOut->_attributes));
CreateFileParamResult* inAndOut = static_cast<CreateFileParamResult*>(data);
inAndOut->_hFile = ::CreateFileW(inAndOut->_filePath.c_str(), inAndOut->_accessParam, inAndOut->_shareParam, NULL, inAndOut->_dispParam, inAndOut->_attribParam, NULL);
inAndOut->_isNetworkFailure = false;
return ERROR_SUCCESS;
};

DWORD getFileAttributesExWaitSec(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isNetWorkProblem)
HANDLE createFileWaitSec(const wchar_t* filePath, DWORD accessParam, DWORD shareParam, DWORD dispParam, DWORD attribParam, DWORD milliSec2wait, bool* isNetWorkProblem)
{
GetAttrExParamResult data(filePath);
CreateFileParamResult data(filePath, accessParam, shareParam, dispParam, attribParam);

HANDLE hThread = ::CreateThread(NULL, 0, getFileAttributesExWorker, &data, 0, NULL);
HANDLE hThread = ::CreateThread(NULL, 0, createFileWorker, &data, 0, NULL);
if (!hThread)
{
return FALSE;
return INVALID_HANDLE_VALUE;
}

// wait for our worker thread to complete or terminate it when the required timeout has elapsed
Expand All @@ -1983,17 +1990,66 @@ DWORD getFileAttributesExWaitSec(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_D
break;

case WAIT_TIMEOUT: // the timeout interval elapsed, but the worker's state is still non-signaled
default: // any other dwWaitStatus is a BAD one here
// WAIT_FAILED or WAIT_ABANDONED
default: // Timeout reached, or WAIT_FAILED or WAIT_ABANDONED
// attempt to cancel the operation
::CancelIoEx(data._hFile, NULL);
::TerminateThread(hThread, dwWaitStatus);
break;
}
CloseHandle(hThread);

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

return data._hFile;
}

//----------------------------------------------------

struct wfopenParamResult
{
std::wstring _filePath;
FILE* _pFile = nullptr;
bool _isNetworkFailure = true;
wfopenParamResult(wstring filePath) : _filePath(filePath) {};
};

DWORD WINAPI wfopenWorker(void* data)
{
wfopenParamResult* inAndOut = static_cast<wfopenParamResult*>(data);
inAndOut->_pFile = _wfopen(inAndOut->_filePath.c_str(), L"rb");
inAndOut->_isNetworkFailure = false;
return ERROR_SUCCESS;
};

FILE* wfopenWithTimeout(const wchar_t* filePath, DWORD milliSec2wait, bool* isNetWorkProblem)
{
wfopenParamResult data(filePath);

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

// wait for our worker thread to complete or terminate it when the required timeout has elapsed
DWORD dwWaitStatus = ::WaitForSingleObject(hThread, milliSec2wait == 0 ? DEFAULT_MILLISEC : milliSec2wait);
switch (dwWaitStatus)
{
case WAIT_OBJECT_0: // Ok, the state of our worker thread is signaled, so it finished itself in the timeout given
// - nothing else to do here, except the thread handle closing later
break;

case WAIT_TIMEOUT: // the timeout interval elapsed, but the worker's state is still non-signaled
default: // any other dwWaitStatus is a BAD one here
// WAIT_FAILED or WAIT_ABANDONED
::TerminateThread(hThread, dwWaitStatus);
break;
}
CloseHandle(hThread);

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

return data._result;
return data._pFile;
}
3 changes: 2 additions & 1 deletion PowerEditor/src/MISC/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isNet
bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
bool doesPathExist(const wchar_t* path, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);

FILE* wfopenWaitSec(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
DWORD getDiskFreeSpaceWaitSec(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
DWORD getFileAttributesExWaitSec(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
HANDLE createFileWaitSec(const wchar_t* filePath, DWORD accessParam, DWORD shareParam, DWORD dispParam, DWORD attribParam, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
FILE* wfopenWithTimeout(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isNetWorkProblem = nullptr);
46 changes: 5 additions & 41 deletions PowerEditor/src/ScintillaComponent/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,42 +702,22 @@ BufferID FileManager::loadFile(const wchar_t* filename, Document doc, int encodi
//Get file size
int64_t fileSize = -1;
const wchar_t* pPath = filename;
std::wstring msg = pPath;
msg += L"\t BEFORE doesFileExist";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());
if (!doesFileExist(pPath))
{
pPath = backupFileName;
}
msg = pPath;
msg += L"\t AFTER doesFileExist";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());



if (pPath)
{
msg = pPath;
msg += L"\t BEFORE getFileAttributesExWaitSec";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());

WIN32_FILE_ATTRIBUTE_DATA attributes{};
if (getFileAttributesExWaitSec(pPath, &attributes) != FALSE)
{
msg = pPath;
msg += L"\t getFileAttributesExWaitSec OK - let's retrieve file length";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());

LARGE_INTEGER size{};
size.LowPart = attributes.nFileSizeLow;
size.HighPart = attributes.nFileSizeHigh;

fileSize = size.QuadPart;
}

msg = pPath;
msg += L"\t AFTER getFileAttributesExWaitSec";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());
}

// * the auto-completion feature will be disabled for large files
Expand Down Expand Up @@ -797,16 +777,8 @@ BufferID FileManager::loadFile(const wchar_t* filename, Document doc, int encodi
loadedFileFormat._eolFormat = EolType::unknown;
loadedFileFormat._language = L_TEXT;

msg = pPath;
msg += L"\t BEFORE loadFileData";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());

bool loadRes = loadFileData(doc, fileSize, backupFileName ? backupFileName : fullpath, data, &UnicodeConvertor, loadedFileFormat);

msg = pPath;
msg += L"\t AFTER loadFileData";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());

delete[] data;

if (loadRes)
Expand Down Expand Up @@ -1642,15 +1614,7 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f
}
}

wstring msg = filename;
msg += L"\t BEFORE _wfopen";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());

FILE* fp = wfopenWaitSec(filename, 5000);

msg = filename;
msg += L"\t AFTER _wfopen";
writeLog(L"c:\\tmp\\QQQ", msg.c_str());
FILE* fp = wfopenWithTimeout(filename);

if (!fp)
return false;
Expand Down Expand Up @@ -1687,8 +1651,8 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f
bool success = true;
EolType format = EolType::unknown;
int sciStatus = SC_STATUS_OK;
/*wchar_t szException[64] = {'\0'};
__try*/
wchar_t szException[64] = {'\0'};
__try
{
// First allocate enough memory for the whole file (this will reduce memory copy during loading)
_pscratchTilla->execute(SCI_ALLOCATE, WPARAM(bufferSizeRequested));
Expand Down Expand Up @@ -1776,7 +1740,7 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f
}
while (lenFile > 0);
}
/*__except (EXCEPTION_EXECUTE_HANDLER)
__except (EXCEPTION_EXECUTE_HANDLER)
{
switch (sciStatus)
{
Expand Down Expand Up @@ -1815,7 +1779,7 @@ bool FileManager::loadFileData(Document doc, int64_t fileSize, const wchar_t * f
szException);
}
success = false;
}*/
}

fclose(fp);

Expand Down

0 comments on commit e22972c

Please sign in to comment.