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 = winpr_CreateFile(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)
36 for (
size_t x = 0; x < 10; x++)
38 CHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
39 strncpy(FilePath, BasePath, ARRAYSIZE(FilePath));
41 CHAR name[64] = WINPR_C_ARRAY_INIT;
42 (void)_snprintf(name, ARRAYSIZE(name),
"%zd-TestFile%zd", level, x);
43 if (FAILED(NativePathCchAppendA(FilePath, PATHCCH_MAX_CCH, name)))
46 if (create_fileA(FilePath))
48 if (!ArrayList_Append(files, FilePath))
55static BOOL create_layout_directories(
size_t level,
size_t max_level,
const char* BasePath,
58 if (level >= max_level)
61 CHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
62 strncpy(FilePath, BasePath, ARRAYSIZE(FilePath));
63 if (FAILED(PathCchConvertStyleA(FilePath, ARRAYSIZE(FilePath), PATH_STYLE_NATIVE)))
65 if (!winpr_PathMakePath(FilePath,
nullptr))
67 ArrayList_Append(files, FilePath);
69 if (!create_layout_files(level + 1, BasePath, files))
72 for (
size_t x = 0; x < 10; x++)
74 CHAR CurFilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
75 strncpy(CurFilePath, FilePath, ARRAYSIZE(CurFilePath));
77 if (FAILED(PathCchConvertStyleA(CurFilePath, ARRAYSIZE(CurFilePath), PATH_STYLE_NATIVE)))
80 CHAR name[64] = WINPR_C_ARRAY_INIT;
81 (void)_snprintf(name, ARRAYSIZE(name),
"%zd-TestPath%zd", level, x);
82 if (FAILED(NativePathCchAppendA(CurFilePath, PATHCCH_MAX_CCH, name)))
85 if (!create_layout_directories(level + 1, max_level, CurFilePath, files))
91static BOOL create_layout(
const char* BasePath, wArrayList* files)
93 CHAR BasePathNative[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
94 memcpy(BasePathNative, BasePath,
sizeof(BasePathNative));
95 if (FAILED(PathCchConvertStyleA(BasePathNative, ARRAYSIZE(BasePathNative), PATH_STYLE_NATIVE)))
98 return create_layout_directories(0, 3, BasePathNative, files);
101static void cleanup_layout(
const char* BasePath)
103 winpr_RemoveDirectory_RecursiveA(BasePath);
106static BOOL find_first_file_success(
const char* FilePath)
110 HANDLE hFind = FindFirstFileA(FilePath, &FindData);
111 if (hFind == INVALID_HANDLE_VALUE)
113 printf(
"FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePath);
117 printf(
"FindFirstFile: %s\n", FindData.cFileName);
119 if (strcmp(FindData.cFileName, testFile1A) != 0)
121 printf(
"FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1A, FindData.cFileName);
126 if (hFind != INVALID_HANDLE_VALUE)
131static BOOL list_directory_dot(
const char* BasePath, wArrayList* files)
134 CHAR BasePathDot[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
135 memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot));
136 if (FAILED(PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE)))
138 if (FAILED(NativePathCchAppendA(BasePathDot, PATHCCH_MAX_CCH,
".")))
141 HANDLE hFind = FindFirstFileA(BasePathDot, &FindData);
142 if (hFind == INVALID_HANDLE_VALUE)
148 if (strcmp(FindData.cFileName,
".") != 0)
150 }
while (FindNextFile(hFind, &FindData));
161static BOOL list_directory_star(
const char* BasePath, wArrayList* files)
163 CHAR BasePathDot[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
164 memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot));
166 if (FAILED(PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE)))
169 if (FAILED(NativePathCchAppendA(BasePathDot, PATHCCH_MAX_CCH,
"*")))
173 HANDLE hFind = FindFirstFileA(BasePathDot, &FindData);
174 if (hFind == INVALID_HANDLE_VALUE)
178 size_t dotdotcount = 0;
181 if (strcmp(FindData.cFileName,
".") == 0)
183 else if (strcmp(FindData.cFileName,
"..") == 0)
187 }
while (FindNextFile(hFind, &FindData));
190 const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE);
192 const size_t baselen = strlen(BasePath);
193 const size_t total = ArrayList_Count(files);
194 for (
size_t x = 0; x < total; x++)
196 const char* path = ArrayList_GetItem(files, x);
197 const size_t pathlen = strlen(path);
198 if (pathlen < baselen)
200 const char* skip = &path[baselen];
203 const char* end = strrchr(skip, sep);
209 return (fcount == count);
212static BOOL find_first_file_fail(
const char* FilePath)
215 HANDLE hFind = FindFirstFileA(FilePath, &FindData);
216 if (hFind == INVALID_HANDLE_VALUE)
223static int TestFileFindFirstFileA(
const char* str)
227 printf(
"[%s] basepath: '%s'\n", __func__, str);
231 CHAR BasePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
233 strncpy(BasePath, str, ARRAYSIZE(BasePath));
235 const size_t length = strnlen(BasePath, PATHCCH_MAX_CCH - 1);
237 CHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
238 CopyMemory(FilePath, BasePath, length *
sizeof(CHAR));
240 if (FAILED(PathCchConvertStyleA(BasePath, length, PATH_STYLE_WINDOWS)))
243 wArrayList* files = ArrayList_New(FALSE);
246 wObject* obj = ArrayList_Object(files);
250 if (!create_layout(BasePath, files))
253 if (FAILED(NativePathCchAppendA(FilePath, PATHCCH_MAX_CCH, testFile1A)))
256 printf(
"Finding file: %s\n", FilePath);
258 if (!find_first_file_fail(FilePath))
261 if (!create_fileA(FilePath))
264 if (!find_first_file_success(FilePath))
267 CHAR BasePathInvalid[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
268 memcpy(BasePathInvalid, BasePath, ARRAYSIZE(BasePathInvalid));
269 if (FAILED(PathCchAddBackslashA(BasePathInvalid, PATHCCH_MAX_CCH)))
272 if (!find_first_file_fail(BasePathInvalid))
275 if (!list_directory_dot(BasePath, files))
278 if (!list_directory_star(BasePath, files))
283 winpr_DeleteFile(FilePath);
284 cleanup_layout(BasePath);
285 ArrayList_Free(files);
289WINPR_ATTR_FORMAT_ARG(1, 0)
290static
int printf1W(const
char* WINPR_FORMAT_ARG fmt, const WCHAR* arg1)
292 char* var1 = ConvertWCharToUtf8Alloc(arg1,
nullptr);
293 const int rc = printf(fmt, var1);
298WINPR_ATTR_FORMAT_ARG(1, 0)
299static
int printf2W(const
char* WINPR_FORMAT_ARG fmt, const WCHAR* arg1, const WCHAR* arg2)
301 char* var1 = ConvertWCharToUtf8Alloc(arg1,
nullptr);
302 char* var2 = ConvertWCharToUtf8Alloc(arg2,
nullptr);
303 const int rc = printf(fmt, var1, var2);
309static int TestFileFindFirstFileW(
const char* str)
311 WCHAR buffer[32] = WINPR_C_ARRAY_INIT;
312 const WCHAR* testFile1W = InitializeConstWCharFromUtf8(
"TestFile1W", buffer, ARRAYSIZE(buffer));
317 WCHAR BasePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
319 printf(
"[%s] basepath: '%s'\n", __func__, str);
320 (void)ConvertUtf8ToWChar(str, BasePath, ARRAYSIZE(BasePath));
322 const size_t length = _wcsnlen(BasePath, PATHCCH_MAX_CCH - 1);
324 WCHAR FilePath[PATHCCH_MAX_CCH] = WINPR_C_ARRAY_INIT;
325 CopyMemory(FilePath, BasePath, length *
sizeof(WCHAR));
327 HANDLE hFind = INVALID_HANDLE_VALUE;
328 if (FAILED(PathCchConvertStyleW(BasePath, length, PATH_STYLE_WINDOWS)))
330 if (FAILED(NativePathCchAppendW(FilePath, PATHCCH_MAX_CCH, testFile1W)))
333 if (!create_fileW(FilePath))
336 printf1W(
"Finding file: %s\n", FilePath);
339 hFind = FindFirstFileW(FilePath, &FindData);
341 if (hFind == INVALID_HANDLE_VALUE)
343 printf1W(
"FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePath);
347 printf1W(
"FindFirstFile: %s\n", FindData.cFileName);
349 if (_wcscmp(FindData.cFileName, testFile1W) != 0)
351 printf2W(
"FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1W,
358 DeleteFileW(FilePath);
363int TestFileFindFirstFile(
int argc,
char* argv[])
365 char* str = GetKnownSubPath(KNOWN_PATH_TEMP,
"TestFileFindFirstFile");
373 if (winpr_PathMakePath(str,
nullptr))
375 rc1 = TestFileFindFirstFileA(str);
376 rc2 = TestFileFindFirstFileW(str);
377 winpr_RemoveDirectory(str);
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew