4#include <winpr/handle.h>
7#include <winpr/tchar.h>
8#include <winpr/collections.h>
9#include <winpr/windows.h>
11static const CHAR testFile1A[] =
"TestFile1A";
13static BOOL create_fileA(
const char* FilePath)
15 HANDLE hdl = CreateFileA(FilePath, GENERIC_ALL, 0,
nullptr, CREATE_ALWAYS,
16 FILE_ATTRIBUTE_NORMAL,
nullptr);
17 if (hdl == INVALID_HANDLE_VALUE)
19 (void)CloseHandle(hdl);
23static BOOL create_fileW(
const WCHAR* FilePath)
25 HANDLE hdl = CreateFileW(FilePath, GENERIC_ALL, 0,
nullptr, CREATE_ALWAYS,
26 FILE_ATTRIBUTE_NORMAL,
nullptr);
27 if (hdl == INVALID_HANDLE_VALUE)
29 (void)CloseHandle(hdl);
33static BOOL create_layout_files(
size_t level,
const char* BasePath, wArrayList* files)
35 for (
size_t x = 0; x < 10; x++)
37 CHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
38 strncpy(FilePath, BasePath, ARRAYSIZE(FilePath));
40 CHAR name[64] = WINPR_C_ARRAY_INIT;
41 (void)_snprintf(name, ARRAYSIZE(name),
"%zd-TestFile%zd", level, x);
42 NativePathCchAppendA(FilePath, PATHCCH_MAX_CCH, name);
44 if (create_fileA(FilePath))
45 ArrayList_Append(files, FilePath);
50static BOOL create_layout_directories(
size_t level,
size_t max_level,
const char* BasePath,
53 if (level >= max_level)
56 CHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
57 strncpy(FilePath, BasePath, ARRAYSIZE(FilePath));
58 PathCchConvertStyleA(FilePath, ARRAYSIZE(FilePath), PATH_STYLE_NATIVE);
59 if (!winpr_PathMakePath(FilePath,
nullptr))
61 ArrayList_Append(files, FilePath);
63 if (!create_layout_files(level + 1, BasePath, files))
66 for (
size_t x = 0; x < 10; x++)
68 CHAR CurFilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
69 strncpy(CurFilePath, FilePath, ARRAYSIZE(CurFilePath));
71 PathCchConvertStyleA(CurFilePath, ARRAYSIZE(CurFilePath), PATH_STYLE_NATIVE);
73 CHAR name[64] = WINPR_C_ARRAY_INIT;
74 (void)_snprintf(name, ARRAYSIZE(name),
"%zd-TestPath%zd", level, x);
75 NativePathCchAppendA(CurFilePath, PATHCCH_MAX_CCH, name);
77 if (!create_layout_directories(level + 1, max_level, CurFilePath, files))
83static BOOL create_layout(
const char* BasePath, wArrayList* files)
85 CHAR BasePathNative[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
86 memcpy(BasePathNative, BasePath,
sizeof(BasePathNative));
87 PathCchConvertStyleA(BasePathNative, ARRAYSIZE(BasePathNative), PATH_STYLE_NATIVE);
89 return create_layout_directories(0, 3, BasePathNative, files);
92static void cleanup_layout(
const char* BasePath)
94 winpr_RemoveDirectory_RecursiveA(BasePath);
97static BOOL find_first_file_success(
const char* FilePath)
101 HANDLE hFind = FindFirstFileA(FilePath, &FindData);
102 if (hFind == INVALID_HANDLE_VALUE)
104 printf(
"FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePath);
108 printf(
"FindFirstFile: %s\n", FindData.cFileName);
110 if (strcmp(FindData.cFileName, testFile1A) != 0)
112 printf(
"FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1A, FindData.cFileName);
117 if (hFind != INVALID_HANDLE_VALUE)
122static BOOL list_directory_dot(
const char* BasePath, wArrayList* files)
125 CHAR BasePathDot[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
126 memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot));
127 PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE);
128 NativePathCchAppendA(BasePathDot, PATHCCH_MAX_CCH,
".");
130 HANDLE hFind = FindFirstFileA(BasePathDot, &FindData);
131 if (hFind == INVALID_HANDLE_VALUE)
137 if (strcmp(FindData.cFileName,
".") != 0)
139 }
while (FindNextFile(hFind, &FindData));
150static BOOL list_directory_star(
const char* BasePath, wArrayList* files)
152 CHAR BasePathDot[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
153 memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot));
154 PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE);
155 NativePathCchAppendA(BasePathDot, PATHCCH_MAX_CCH,
"*");
157 HANDLE hFind = FindFirstFileA(BasePathDot, &FindData);
158 if (hFind == INVALID_HANDLE_VALUE)
162 size_t dotdotcount = 0;
165 if (strcmp(FindData.cFileName,
".") == 0)
167 else if (strcmp(FindData.cFileName,
"..") == 0)
171 }
while (FindNextFile(hFind, &FindData));
174 const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE);
176 const size_t baselen = strlen(BasePath);
177 const size_t total = ArrayList_Count(files);
178 for (
size_t x = 0; x < total; x++)
180 const char* path = ArrayList_GetItem(files, x);
181 const size_t pathlen = strlen(path);
182 if (pathlen < baselen)
184 const char* skip = &path[baselen];
187 const char* end = strrchr(skip, sep);
193 return (fcount == count);
196static BOOL find_first_file_fail(
const char* FilePath)
199 HANDLE hFind = FindFirstFileA(FilePath, &FindData);
200 if (hFind == INVALID_HANDLE_VALUE)
207static int TestFileFindFirstFileA(
const char* str)
211 printf(
"[%s] basepath: '%s'\n", __func__, str);
215 CHAR BasePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
217 strncpy(BasePath, str, ARRAYSIZE(BasePath));
219 const size_t length = strnlen(BasePath, PATHCCH_MAX_CCH - 1);
221 CHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
222 CopyMemory(FilePath, BasePath, length *
sizeof(CHAR));
224 PathCchConvertStyleA(BasePath, length, PATH_STYLE_WINDOWS);
226 wArrayList* files = ArrayList_New(FALSE);
229 wObject* obj = ArrayList_Object(files);
233 if (!create_layout(BasePath, files))
236 NativePathCchAppendA(FilePath, PATHCCH_MAX_CCH, testFile1A);
238 printf(
"Finding file: %s\n", FilePath);
240 if (!find_first_file_fail(FilePath))
243 if (!create_fileA(FilePath))
246 if (!find_first_file_success(FilePath))
249 CHAR BasePathInvalid[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
250 memcpy(BasePathInvalid, BasePath, ARRAYSIZE(BasePathInvalid));
251 PathCchAddBackslashA(BasePathInvalid, PATHCCH_MAX_CCH);
253 if (!find_first_file_fail(BasePathInvalid))
256 if (!list_directory_dot(BasePath, files))
259 if (!list_directory_star(BasePath, files))
264 winpr_DeleteFile(FilePath);
265 cleanup_layout(BasePath);
266 ArrayList_Free(files);
270WINPR_ATTR_FORMAT_ARG(1, 0)
271static
int printf1W(const
char* WINPR_FORMAT_ARG fmt, const WCHAR* arg1)
273 char* var1 = ConvertWCharToUtf8Alloc(arg1,
nullptr);
274 const int rc = printf(fmt, var1);
279WINPR_ATTR_FORMAT_ARG(1, 0)
280static
int printf2W(const
char* WINPR_FORMAT_ARG fmt, const WCHAR* arg1, const WCHAR* arg2)
282 char* var1 = ConvertWCharToUtf8Alloc(arg1,
nullptr);
283 char* var2 = ConvertWCharToUtf8Alloc(arg2,
nullptr);
284 const int rc = printf(fmt, var1, var2);
290static int TestFileFindFirstFileW(
const char* str)
292 WCHAR buffer[32] = WINPR_C_ARRAY_INIT;
293 const WCHAR* testFile1W = InitializeConstWCharFromUtf8(
"TestFile1W", buffer, ARRAYSIZE(buffer));
298 WCHAR BasePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
300 printf(
"[%s] basepath: '%s'\n", __func__, str);
301 (void)ConvertUtf8ToWChar(str, BasePath, ARRAYSIZE(BasePath));
303 const size_t length = _wcsnlen(BasePath, PATHCCH_MAX_CCH - 1);
305 WCHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
306 CopyMemory(FilePath, BasePath, length *
sizeof(WCHAR));
308 PathCchConvertStyleW(BasePath, length, PATH_STYLE_WINDOWS);
309 NativePathCchAppendW(FilePath, PATHCCH_MAX_CCH, testFile1W);
311 HANDLE hFind = INVALID_HANDLE_VALUE;
312 if (!create_fileW(FilePath))
315 printf1W(
"Finding file: %s\n", FilePath);
318 hFind = FindFirstFileW(FilePath, &FindData);
320 if (hFind == INVALID_HANDLE_VALUE)
322 printf1W(
"FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePath);
326 printf1W(
"FindFirstFile: %s\n", FindData.cFileName);
328 if (_wcscmp(FindData.cFileName, testFile1W) != 0)
330 printf2W(
"FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1W,
337 DeleteFileW(FilePath);
342int TestFileFindFirstFile(
int argc,
char* argv[])
344 char* str = GetKnownSubPath(KNOWN_PATH_TEMP,
"TestFileFindFirstFile");
352 if (winpr_PathMakePath(str,
nullptr))
354 rc1 = TestFileFindFirstFileA(str);
355 rc2 = TestFileFindFirstFileW(str);
356 winpr_RemoveDirectory(str);
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
OBJECT_NEW_FN fnObjectNew