1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/handle.h>
#include <winpr/file.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/collections.h>
#include <winpr/windows.h>
static const CHAR testFile1A[] = "TestFile1A";
static BOOL create_layout_files(size_t level, const char* BasePath, wArrayList* files)
{
for (size_t x = 0; x < 10; x++)
{
CHAR FilePath[PATHCCH_MAX_CCH] = { 0 };
strncpy(FilePath, BasePath, ARRAYSIZE(FilePath));
CHAR name[64] = { 0 };
_snprintf(name, ARRAYSIZE(name), "%zd-TestFile%zd", level, x);
NativePathCchAppendA(FilePath, PATHCCH_MAX_CCH, name);
HANDLE hdl =
CreateFileA(FilePath, GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hdl == INVALID_HANDLE_VALUE)
return FALSE;
ArrayList_Append(files, FilePath);
CloseHandle(hdl);
}
return TRUE;
}
static BOOL create_layout_directories(size_t level, size_t max_level, const char* BasePath,
wArrayList* files)
{
if (level >= max_level)
return TRUE;
CHAR FilePath[PATHCCH_MAX_CCH] = { 0 };
strncpy(FilePath, BasePath, ARRAYSIZE(FilePath));
PathCchConvertStyleA(FilePath, ARRAYSIZE(FilePath), PATH_STYLE_NATIVE);
if (!winpr_PathMakePath(FilePath, NULL))
return FALSE;
ArrayList_Append(files, FilePath);
if (!create_layout_files(level + 1, BasePath, files))
return FALSE;
for (size_t x = 0; x < 10; x++)
{
CHAR CurFilePath[PATHCCH_MAX_CCH] = { 0 };
strncpy(CurFilePath, FilePath, ARRAYSIZE(CurFilePath));
PathCchConvertStyleA(CurFilePath, ARRAYSIZE(CurFilePath), PATH_STYLE_NATIVE);
CHAR name[64] = { 0 };
_snprintf(name, ARRAYSIZE(name), "%zd-TestPath%zd", level, x);
NativePathCchAppendA(CurFilePath, PATHCCH_MAX_CCH, name);
if (!create_layout_directories(level + 1, max_level, CurFilePath, files))
return FALSE;
}
return TRUE;
}
static BOOL create_layout(const char* BasePath, wArrayList* files)
{
CHAR BasePathNative[PATHCCH_MAX_CCH] = { 0 };
memcpy(BasePathNative, BasePath, sizeof(BasePathNative));
PathCchConvertStyleA(BasePathNative, ARRAYSIZE(BasePathNative), PATH_STYLE_NATIVE);
return create_layout_directories(0, 3, BasePathNative, files);
}
static void cleanup_layout(const char* BasePath)
{
winpr_RemoveDirectory_RecursiveA(BasePath);
}
static BOOL find_first_file_success(const char* FilePath)
{
BOOL rc = FALSE;
WIN32_FIND_DATAA FindData = { 0 };
HANDLE hFind = FindFirstFileA(FilePath, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf("FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePath);
goto fail;
}
printf("FindFirstFile: %s", FindData.cFileName);
if (strcmp(FindData.cFileName, testFile1A) != 0)
{
printf("FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1A, FindData.cFileName);
goto fail;
}
rc = TRUE;
fail:
FindClose(hFind);
return rc;
}
static BOOL list_directory_dot(const char* BasePath, wArrayList* files)
{
BOOL rc = FALSE;
CHAR BasePathDot[PATHCCH_MAX_CCH] = { 0 };
memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot));
PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE);
NativePathCchAppendA(BasePathDot, PATHCCH_MAX_CCH, ".");
WIN32_FIND_DATAA FindData = { 0 };
HANDLE hFind = FindFirstFileA(BasePathDot, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
size_t count = 0;
do
{
count++;
if (strcmp(FindData.cFileName, ".") != 0)
goto fail;
} while (FindNextFile(hFind, &FindData));
rc = TRUE;
fail:
FindClose(hFind);
if (count != 1)
return FALSE;
return rc;
}
static BOOL list_directory_star(const char* BasePath, wArrayList* files)
{
CHAR BasePathDot[PATHCCH_MAX_CCH] = { 0 };
memcpy(BasePathDot, BasePath, ARRAYSIZE(BasePathDot));
PathCchConvertStyleA(BasePathDot, ARRAYSIZE(BasePathDot), PATH_STYLE_NATIVE);
NativePathCchAppendA(BasePathDot, PATHCCH_MAX_CCH, "*");
WIN32_FIND_DATAA FindData = { 0 };
HANDLE hFind = FindFirstFileA(BasePathDot, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
size_t count = 0;
size_t dotcount = 0;
size_t dotdotcount = 0;
do
{
if (strcmp(FindData.cFileName, ".") == 0)
dotcount++;
else if (strcmp(FindData.cFileName, "..") == 0)
dotdotcount++;
else
count++;
} while (FindNextFile(hFind, &FindData));
FindClose(hFind);
const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE);
size_t fcount = 0;
const size_t baselen = strlen(BasePath);
const size_t total = ArrayList_Count(files);
for (size_t x = 0; x < total; x++)
{
const char* path = ArrayList_GetItem(files, x);
const size_t pathlen = strlen(path);
if (pathlen < baselen)
continue;
const char* skip = &path[baselen];
if (*skip == sep)
skip++;
const char* end = strrchr(skip, sep);
if (end)
continue;
fcount++;
}
if (fcount != count)
return FALSE;
return TRUE;
}
static BOOL find_first_file_fail(const char* FilePath)
{
WIN32_FIND_DATAA FindData = { 0 };
HANDLE hFind = FindFirstFileA(FilePath, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
return TRUE;
FindClose(hFind);
return FALSE;
}
static int TestFileFindFirstFileA(const char* str)
{
int rc = -1;
if (!str)
return -1;
CHAR BasePath[PATHCCH_MAX_CCH] = { 0 };
strncpy(BasePath, str, ARRAYSIZE(BasePath));
const size_t length = strnlen(BasePath, PATHCCH_MAX_CCH - 1);
CHAR FilePath[PATHCCH_MAX_CCH] = { 0 };
CopyMemory(FilePath, BasePath, length * sizeof(CHAR));
PathCchConvertStyleA(BasePath, length, PATH_STYLE_WINDOWS);
wArrayList* files = ArrayList_New(FALSE);
if (!files)
return -3;
wObject* obj = ArrayList_Object(files);
obj->fnObjectFree = winpr_ObjectStringFree;
obj->fnObjectNew = winpr_ObjectStringClone;
if (!create_layout(BasePath, files))
return -1;
NativePathCchAppendA(FilePath, PATHCCH_MAX_CCH, testFile1A);
printf("Finding file: %s\n", FilePath);
if (!find_first_file_fail(FilePath))
goto fail;
HANDLE hdl =
CreateFileA(FilePath, GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hdl == INVALID_HANDLE_VALUE)
goto fail;
CloseHandle(hdl);
if (!find_first_file_success(FilePath))
goto fail;
CHAR BasePathInvalid[PATHCCH_MAX_CCH] = { 0 };
memcpy(BasePathInvalid, BasePath, ARRAYSIZE(BasePathInvalid));
PathCchAddBackslashA(BasePathInvalid, PATHCCH_MAX_CCH);
if (!find_first_file_fail(BasePathInvalid))
goto fail;
if (!list_directory_dot(BasePath, files))
goto fail;
if (!list_directory_star(BasePath, files))
goto fail;
rc = 0;
fail:
DeleteFileA(FilePath);
cleanup_layout(BasePath);
ArrayList_Free(files);
return rc;
}
static int TestFileFindFirstFileW(const char* str)
{
WCHAR buffer[32] = { 0 };
const WCHAR* testFile1W = InitializeConstWCharFromUtf8("TestFile1W", buffer, ARRAYSIZE(buffer));
int rc = -1;
if (!str)
return -1;
WCHAR BasePath[PATHCCH_MAX_CCH] = { 0 };
ConvertUtf8ToWChar(str, BasePath, ARRAYSIZE(BasePath));
const size_t length = _wcsnlen(BasePath, PATHCCH_MAX_CCH - 1);
WCHAR FilePath[PATHCCH_MAX_CCH] = { 0 };
CopyMemory(FilePath, BasePath, length * sizeof(WCHAR));
PathCchConvertStyleW(BasePath, length, PATH_STYLE_WINDOWS);
NativePathCchAppendW(FilePath, PATHCCH_MAX_CCH, testFile1W);
CHAR FilePathA[PATHCCH_MAX_CCH] = { 0 };
ConvertWCharNToUtf8(FilePath, ARRAYSIZE(FilePath), FilePathA, ARRAYSIZE(FilePathA));
printf("Finding file: %s\n", FilePathA);
WIN32_FIND_DATAW FindData = { 0 };
HANDLE hFind = FindFirstFileW(FilePath, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf("FindFirstFile failure: %s (INVALID_HANDLE_VALUE -1)\n", FilePathA);
goto fail;
}
CHAR cFileName[MAX_PATH] = { 0 };
ConvertWCharNToUtf8(FindData.cFileName, ARRAYSIZE(FindData.cFileName), cFileName,
ARRAYSIZE(cFileName));
printf("FindFirstFile: %s", cFileName);
if (_wcscmp(FindData.cFileName, testFile1W) != 0)
{
printf("FindFirstFile failure: Expected: %s, Actual: %s\n", testFile1A, cFileName);
goto fail;
}
rc = 0;
fail:
DeleteFileW(FilePath);
FindClose(hFind);
return rc;
}
int TestFileFindFirstFile(int argc, char* argv[])
{
char* str = GetKnownSubPath(KNOWN_PATH_TEMP, "TestFileFindFirstFile");
if (!str)
return -23;
cleanup_layout(str);
int rc1 = -1;
int rc2 = -1;
if (winpr_PathMakePath(str, NULL))
{
rc1 = TestFileFindFirstFileA(str);
rc2 = 0; // TestFileFindFirstFileW(str);
winpr_RemoveDirectory(str);
}
free(str);
return rc1 + rc2;
}
|