[winpr,file] do not assume order for directory listings

This commit is contained in:
Armin Novak 2023-07-23 10:30:09 +02:00 committed by Martin Fleisz
parent 4816a7d2a5
commit 62b6c98393
2 changed files with 25 additions and 34 deletions

View File

@ -315,33 +315,39 @@ static BOOL add_directory_entry_to_list(wClipboard* clipboard, const WCHAR* loca
}
static BOOL do_add_directory_contents_to_list(wClipboard* clipboard, const WCHAR* local_name,
const WCHAR* remote_name, HANDLE hFind,
const WCHAR* remote_name, WCHAR* namebuf,
wArrayList* files)
{
WINPR_ASSERT(clipboard);
WINPR_ASSERT(local_name);
WINPR_ASSERT(remote_name);
WINPR_ASSERT(files);
WINPR_ASSERT(namebuf);
WIN32_FIND_DATAW FindData = { 0 };
HANDLE hFind = FindFirstFileW(namebuf, &FindData);
if (INVALID_HANDLE_VALUE == hFind)
{
WLog_ERR(TAG, "FindFirstFile failed (%" PRIu32 ")", GetLastError());
return FALSE;
}
while (TRUE)
{
WIN32_FIND_DATAW FileData = { 0 };
BOOL bRet = FindNextFileW(hFind, &FileData);
if (!add_directory_entry_to_list(clipboard, local_name, remote_name, &FindData, files))
{
FindClose(hFind);
return FALSE;
}
BOOL bRet = FindNextFileW(hFind, &FindData);
if (!bRet)
{
FindClose(hFind);
if (ERROR_NO_MORE_FILES == GetLastError())
return TRUE;
WLog_WARN(TAG, "FindNextFile failed (%" PRIu32 ")", GetLastError());
return FALSE;
}
if (!add_directory_entry_to_list(clipboard, local_name, remote_name, &FileData, files))
return FALSE;
}
return TRUE;
@ -351,8 +357,6 @@ static BOOL add_directory_contents_to_list(wClipboard* clipboard, const WCHAR* l
const WCHAR* remote_name, wArrayList* files)
{
BOOL result = FALSE;
HANDLE hFind = NULL;
WIN32_FIND_DATAW FindData = { 0 };
const WCHAR wildcard[] = { '/', '*', '\0' };
WINPR_ASSERT(clipboard);
@ -368,18 +372,9 @@ static BOOL add_directory_contents_to_list(wClipboard* clipboard, const WCHAR* l
memcpy(namebuf, local_name, len * sizeof(WCHAR));
memcpy(&namebuf[len], wildcard, sizeof(wildcard));
hFind = FindFirstFileW(namebuf, &FindData);
result = do_add_directory_contents_to_list(clipboard, local_name, remote_name, namebuf, files);
free(namebuf);
if (hFind == INVALID_HANDLE_VALUE)
{
WLog_ERR(TAG, "FindFirstFile failed (%" PRIu32 ")", GetLastError());
return FALSE;
}
result = do_add_directory_contents_to_list(clipboard, local_name, remote_name, hFind, files);
FindClose(hFind);
return result;
}

View File

@ -955,7 +955,7 @@ HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
if (isDir)
{
pFileSearch->lpPath = _strdup(lpFileName);
pFileSearch->lpPattern = _strdup(".");
pFileSearch->lpPattern = _strdup("*");
}
else
{
@ -1034,7 +1034,6 @@ HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
else
name++;
pFileSearch->lpPattern[0] = '*';
sprintf_s(lpFindFileData->cFileName, ARRAYSIZE(lpFindFileData->cFileName), "%s", name);
}
@ -1125,30 +1124,26 @@ HANDLE FindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPV
BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
{
WIN32_FILE_SEARCH* pFileSearch;
struct stat fileStat;
char* fullpath;
size_t pathlen;
size_t namelen;
if (!hFindFile || !lpFindFileData)
return FALSE;
if (hFindFile == INVALID_HANDLE_VALUE)
return FALSE;
ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
const WIN32_FIND_DATAA empty = { 0 };
*lpFindFileData = empty;
WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
while ((pFileSearch->pDirent = readdir(pFileSearch->pDir)) != NULL)
{
if (FilePatternMatchA(pFileSearch->pDirent->d_name, pFileSearch->lpPattern))
{
BOOL success;
BOOL success = FALSE;
strncpy(lpFindFileData->cFileName, pFileSearch->pDirent->d_name, MAX_PATH);
namelen = strnlen(lpFindFileData->cFileName, MAX_PATH);
pathlen = strlen(pFileSearch->lpPath);
fullpath = (char*)malloc(pathlen + namelen + 2);
const size_t namelen = strnlen(lpFindFileData->cFileName, MAX_PATH);
size_t pathlen = strlen(pFileSearch->lpPath);
char* fullpath = (char*)malloc(pathlen + namelen + 2);
if (fullpath == NULL)
{
@ -1164,6 +1159,7 @@ BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
memcpy(fullpath + pathlen, pFileSearch->pDirent->d_name, namelen);
fullpath[pathlen + namelen] = 0;
struct stat fileStat = { 0 };
if (stat(fullpath, &fileStat) != 0)
{
free(fullpath);