/src/FreeRDP/winpr/libwinpr/file/generic.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * File Functions |
4 | | * |
5 | | * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2014 Hewlett-Packard Development Company, L.P. |
7 | | * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com> |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <winpr/config.h> |
23 | | |
24 | | #include <winpr/crt.h> |
25 | | #include <winpr/string.h> |
26 | | #include <winpr/path.h> |
27 | | #include <winpr/file.h> |
28 | | |
29 | | #ifdef WINPR_HAVE_UNISTD_H |
30 | | #include <unistd.h> |
31 | | #endif |
32 | | |
33 | | #ifdef WINPR_HAVE_FCNTL_H |
34 | | #include <fcntl.h> |
35 | | #endif |
36 | | |
37 | | #include "../log.h" |
38 | | #define TAG WINPR_TAG("file") |
39 | | |
40 | | #ifdef _WIN32 |
41 | | #include <io.h> |
42 | | #include <sys/stat.h> |
43 | | #else |
44 | | #include <winpr/assert.h> |
45 | | #include <pthread.h> |
46 | | #include <dirent.h> |
47 | | #include <libgen.h> |
48 | | #include <errno.h> |
49 | | |
50 | | #include <sys/un.h> |
51 | | #include <sys/stat.h> |
52 | | #include <sys/socket.h> |
53 | | |
54 | | #ifdef WINPR_HAVE_AIO_H |
55 | | #undef WINPR_HAVE_AIO_H /* disable for now, incomplete */ |
56 | | #endif |
57 | | |
58 | | #ifdef WINPR_HAVE_AIO_H |
59 | | #include <aio.h> |
60 | | #endif |
61 | | |
62 | | #ifdef ANDROID |
63 | | #include <sys/vfs.h> |
64 | | #else |
65 | | #include <sys/statvfs.h> |
66 | | #endif |
67 | | |
68 | | #include "../handle/handle.h" |
69 | | |
70 | | #include "../pipe/pipe.h" |
71 | | |
72 | | #include "file.h" |
73 | | |
74 | | /** |
75 | | * api-ms-win-core-file-l1-2-0.dll: |
76 | | * |
77 | | * CreateFileA |
78 | | * CreateFileW |
79 | | * CreateFile2 |
80 | | * DeleteFileA |
81 | | * DeleteFileW |
82 | | * CreateDirectoryA |
83 | | * CreateDirectoryW |
84 | | * RemoveDirectoryA |
85 | | * RemoveDirectoryW |
86 | | * CompareFileTime |
87 | | * DefineDosDeviceW |
88 | | * DeleteVolumeMountPointW |
89 | | * FileTimeToLocalFileTime |
90 | | * LocalFileTimeToFileTime |
91 | | * FindClose |
92 | | * FindCloseChangeNotification |
93 | | * FindFirstChangeNotificationA |
94 | | * FindFirstChangeNotificationW |
95 | | * FindFirstFileA |
96 | | * FindFirstFileExA |
97 | | * FindFirstFileExW |
98 | | * FindFirstFileW |
99 | | * FindFirstVolumeW |
100 | | * FindNextChangeNotification |
101 | | * FindNextFileA |
102 | | * FindNextFileW |
103 | | * FindNextVolumeW |
104 | | * FindVolumeClose |
105 | | * GetDiskFreeSpaceA |
106 | | * GetDiskFreeSpaceExA |
107 | | * GetDiskFreeSpaceExW |
108 | | * GetDiskFreeSpaceW |
109 | | * GetDriveTypeA |
110 | | * GetDriveTypeW |
111 | | * GetFileAttributesA |
112 | | * GetFileAttributesExA |
113 | | * GetFileAttributesExW |
114 | | * GetFileAttributesW |
115 | | * GetFileInformationByHandle |
116 | | * GetFileSize |
117 | | * GetFileSizeEx |
118 | | * GetFileTime |
119 | | * GetFileType |
120 | | * GetFinalPathNameByHandleA |
121 | | * GetFinalPathNameByHandleW |
122 | | * GetFullPathNameA |
123 | | * GetFullPathNameW |
124 | | * GetLogicalDrives |
125 | | * GetLogicalDriveStringsW |
126 | | * GetLongPathNameA |
127 | | * GetLongPathNameW |
128 | | * GetShortPathNameW |
129 | | * GetTempFileNameW |
130 | | * GetTempPathW |
131 | | * GetVolumeInformationByHandleW |
132 | | * GetVolumeInformationW |
133 | | * GetVolumeNameForVolumeMountPointW |
134 | | * GetVolumePathNamesForVolumeNameW |
135 | | * GetVolumePathNameW |
136 | | * QueryDosDeviceW |
137 | | * SetFileAttributesA |
138 | | * SetFileAttributesW |
139 | | * SetFileTime |
140 | | * SetFileValidData |
141 | | * SetFileInformationByHandle |
142 | | * ReadFile |
143 | | * ReadFileEx |
144 | | * ReadFileScatter |
145 | | * WriteFile |
146 | | * WriteFileEx |
147 | | * WriteFileGather |
148 | | * FlushFileBuffers |
149 | | * SetEndOfFile |
150 | | * SetFilePointer |
151 | | * SetFilePointerEx |
152 | | * LockFile |
153 | | * LockFileEx |
154 | | * UnlockFile |
155 | | * UnlockFileEx |
156 | | */ |
157 | | |
158 | | /** |
159 | | * File System Behavior in the Microsoft Windows Environment: |
160 | | * http://download.microsoft.com/download/4/3/8/43889780-8d45-4b2e-9d3a-c696a890309f/File%20System%20Behavior%20Overview.pdf |
161 | | */ |
162 | | |
163 | | /** |
164 | | * Asynchronous I/O - The GNU C Library: |
165 | | * http://www.gnu.org/software/libc/manual/html_node/Asynchronous-I_002fO.html |
166 | | */ |
167 | | |
168 | | /** |
169 | | * aio.h - asynchronous input and output: |
170 | | * http://pubs.opengroup.org/onlinepubs/009695399/basedefs/aio.h.html |
171 | | */ |
172 | | |
173 | | /** |
174 | | * Asynchronous I/O User Guide: |
175 | | * http://code.google.com/p/kernel/wiki/AIOUserGuide |
176 | | */ |
177 | | static wArrayList* _HandleCreators; |
178 | | |
179 | | static pthread_once_t _HandleCreatorsInitialized = PTHREAD_ONCE_INIT; |
180 | | |
181 | | extern HANDLE_CREATOR* GetNamedPipeClientHandleCreator(void); |
182 | | |
183 | | #if defined __linux__ && !defined ANDROID |
184 | | #include "../comm/comm.h" |
185 | | #endif /* __linux__ && !defined ANDROID */ |
186 | | |
187 | | static void _HandleCreatorsInit(void) |
188 | 0 | { |
189 | 0 | WINPR_ASSERT(_HandleCreators == NULL); |
190 | 0 | _HandleCreators = ArrayList_New(TRUE); |
191 | |
|
192 | 0 | if (!_HandleCreators) |
193 | 0 | return; |
194 | | |
195 | | /* |
196 | | * Register all file handle creators. |
197 | | */ |
198 | 0 | ArrayList_Append(_HandleCreators, GetNamedPipeClientHandleCreator()); |
199 | 0 | #if defined __linux__ && !defined ANDROID |
200 | 0 | ArrayList_Append(_HandleCreators, GetCommHandleCreator()); |
201 | 0 | #endif /* __linux__ && !defined ANDROID */ |
202 | 0 | ArrayList_Append(_HandleCreators, GetFileHandleCreator()); |
203 | 0 | } |
204 | | |
205 | | #ifdef WINPR_HAVE_AIO_H |
206 | | |
207 | | static BOOL g_AioSignalHandlerInstalled = FALSE; |
208 | | |
209 | | void AioSignalHandler(int signum, siginfo_t* siginfo, void* arg) |
210 | | { |
211 | | WLog_INFO("%d", signum); |
212 | | } |
213 | | |
214 | | int InstallAioSignalHandler() |
215 | | { |
216 | | if (!g_AioSignalHandlerInstalled) |
217 | | { |
218 | | struct sigaction action; |
219 | | sigemptyset(&action.sa_mask); |
220 | | sigaddset(&action.sa_mask, SIGIO); |
221 | | action.sa_flags = SA_SIGINFO; |
222 | | action.sa_sigaction = (void*)&AioSignalHandler; |
223 | | sigaction(SIGIO, &action, NULL); |
224 | | g_AioSignalHandlerInstalled = TRUE; |
225 | | } |
226 | | |
227 | | return 0; |
228 | | } |
229 | | |
230 | | #endif /* WINPR_HAVE_AIO_H */ |
231 | | |
232 | | HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, |
233 | | LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, |
234 | | DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) |
235 | 0 | { |
236 | 0 | size_t i; |
237 | |
|
238 | 0 | if (!lpFileName) |
239 | 0 | return INVALID_HANDLE_VALUE; |
240 | | |
241 | 0 | if (pthread_once(&_HandleCreatorsInitialized, _HandleCreatorsInit) != 0) |
242 | 0 | { |
243 | 0 | SetLastError(ERROR_DLL_INIT_FAILED); |
244 | 0 | return INVALID_HANDLE_VALUE; |
245 | 0 | } |
246 | | |
247 | 0 | if (_HandleCreators == NULL) |
248 | 0 | { |
249 | 0 | SetLastError(ERROR_DLL_INIT_FAILED); |
250 | 0 | return INVALID_HANDLE_VALUE; |
251 | 0 | } |
252 | | |
253 | 0 | ArrayList_Lock(_HandleCreators); |
254 | |
|
255 | 0 | for (i = 0; i <= ArrayList_Count(_HandleCreators); i++) |
256 | 0 | { |
257 | 0 | HANDLE_CREATOR* creator = ArrayList_GetItem(_HandleCreators, i); |
258 | |
|
259 | 0 | if (creator && creator->IsHandled(lpFileName)) |
260 | 0 | { |
261 | 0 | HANDLE newHandle = |
262 | 0 | creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, |
263 | 0 | dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); |
264 | 0 | ArrayList_Unlock(_HandleCreators); |
265 | 0 | return newHandle; |
266 | 0 | } |
267 | 0 | } |
268 | | |
269 | 0 | ArrayList_Unlock(_HandleCreators); |
270 | 0 | return INVALID_HANDLE_VALUE; |
271 | 0 | } |
272 | | |
273 | | HANDLE CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, |
274 | | LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, |
275 | | DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) |
276 | 0 | { |
277 | 0 | HANDLE hdl = NULL; |
278 | 0 | if (!lpFileName) |
279 | 0 | return NULL; |
280 | 0 | char* lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL); |
281 | |
|
282 | 0 | if (!lpFileNameA) |
283 | 0 | { |
284 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
285 | 0 | goto fail; |
286 | 0 | } |
287 | | |
288 | 0 | hdl = CreateFileA(lpFileNameA, dwDesiredAccess, dwShareMode, lpSecurityAttributes, |
289 | 0 | dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); |
290 | 0 | fail: |
291 | 0 | free(lpFileNameA); |
292 | 0 | return hdl; |
293 | 0 | } |
294 | | |
295 | | BOOL DeleteFileA(LPCSTR lpFileName) |
296 | 0 | { |
297 | 0 | int status; |
298 | 0 | status = unlink(lpFileName); |
299 | 0 | return (status != -1) ? TRUE : FALSE; |
300 | 0 | } |
301 | | |
302 | | BOOL DeleteFileW(LPCWSTR lpFileName) |
303 | 0 | { |
304 | 0 | if (!lpFileName) |
305 | 0 | return FALSE; |
306 | 0 | LPSTR lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL); |
307 | 0 | BOOL rc = FALSE; |
308 | |
|
309 | 0 | if (!lpFileNameA) |
310 | 0 | goto fail; |
311 | | |
312 | 0 | rc = DeleteFileA(lpFileNameA); |
313 | 0 | fail: |
314 | 0 | free(lpFileNameA); |
315 | 0 | return rc; |
316 | 0 | } |
317 | | |
318 | | BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, |
319 | | LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped) |
320 | 0 | { |
321 | 0 | ULONG Type; |
322 | 0 | WINPR_HANDLE* handle; |
323 | |
|
324 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
325 | 0 | return FALSE; |
326 | | |
327 | | /* |
328 | | * from http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx |
329 | | * lpNumberOfBytesRead can be NULL only when the lpOverlapped parameter is not NULL. |
330 | | */ |
331 | | |
332 | 0 | if (!lpNumberOfBytesRead && !lpOverlapped) |
333 | 0 | return FALSE; |
334 | | |
335 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
336 | 0 | return FALSE; |
337 | | |
338 | 0 | handle = (WINPR_HANDLE*)hFile; |
339 | |
|
340 | 0 | if (handle->ops->ReadFile) |
341 | 0 | return handle->ops->ReadFile(handle, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, |
342 | 0 | lpOverlapped); |
343 | | |
344 | 0 | WLog_ERR(TAG, "ReadFile operation not implemented"); |
345 | 0 | return FALSE; |
346 | 0 | } |
347 | | |
348 | | BOOL ReadFileEx(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, |
349 | | LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) |
350 | 0 | { |
351 | 0 | ULONG Type; |
352 | 0 | WINPR_HANDLE* handle; |
353 | |
|
354 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
355 | 0 | return FALSE; |
356 | | |
357 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
358 | 0 | return FALSE; |
359 | | |
360 | 0 | handle = (WINPR_HANDLE*)hFile; |
361 | |
|
362 | 0 | if (handle->ops->ReadFileEx) |
363 | 0 | return handle->ops->ReadFileEx(handle, lpBuffer, nNumberOfBytesToRead, lpOverlapped, |
364 | 0 | lpCompletionRoutine); |
365 | | |
366 | 0 | WLog_ERR(TAG, "ReadFileEx operation not implemented"); |
367 | 0 | return FALSE; |
368 | 0 | } |
369 | | |
370 | | BOOL ReadFileScatter(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToRead, |
371 | | LPDWORD lpReserved, LPOVERLAPPED lpOverlapped) |
372 | 0 | { |
373 | 0 | ULONG Type; |
374 | 0 | WINPR_HANDLE* handle; |
375 | |
|
376 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
377 | 0 | return FALSE; |
378 | | |
379 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
380 | 0 | return FALSE; |
381 | | |
382 | 0 | handle = (WINPR_HANDLE*)hFile; |
383 | |
|
384 | 0 | if (handle->ops->ReadFileScatter) |
385 | 0 | return handle->ops->ReadFileScatter(handle, aSegmentArray, nNumberOfBytesToRead, lpReserved, |
386 | 0 | lpOverlapped); |
387 | | |
388 | 0 | WLog_ERR(TAG, "ReadFileScatter operation not implemented"); |
389 | 0 | return FALSE; |
390 | 0 | } |
391 | | |
392 | | BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, |
393 | | LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) |
394 | 0 | { |
395 | 0 | ULONG Type; |
396 | 0 | WINPR_HANDLE* handle; |
397 | |
|
398 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
399 | 0 | return FALSE; |
400 | | |
401 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
402 | 0 | return FALSE; |
403 | | |
404 | 0 | handle = (WINPR_HANDLE*)hFile; |
405 | |
|
406 | 0 | if (handle->ops->WriteFile) |
407 | 0 | return handle->ops->WriteFile(handle, lpBuffer, nNumberOfBytesToWrite, |
408 | 0 | lpNumberOfBytesWritten, lpOverlapped); |
409 | | |
410 | 0 | WLog_ERR(TAG, "WriteFile operation not implemented"); |
411 | 0 | return FALSE; |
412 | 0 | } |
413 | | |
414 | | BOOL WriteFileEx(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, |
415 | | LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) |
416 | 0 | { |
417 | 0 | ULONG Type; |
418 | 0 | WINPR_HANDLE* handle; |
419 | |
|
420 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
421 | 0 | return FALSE; |
422 | | |
423 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
424 | 0 | return FALSE; |
425 | | |
426 | 0 | handle = (WINPR_HANDLE*)hFile; |
427 | |
|
428 | 0 | if (handle->ops->WriteFileEx) |
429 | 0 | return handle->ops->WriteFileEx(handle, lpBuffer, nNumberOfBytesToWrite, lpOverlapped, |
430 | 0 | lpCompletionRoutine); |
431 | | |
432 | 0 | WLog_ERR(TAG, "WriteFileEx operation not implemented"); |
433 | 0 | return FALSE; |
434 | 0 | } |
435 | | |
436 | | BOOL WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], |
437 | | DWORD nNumberOfBytesToWrite, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped) |
438 | 0 | { |
439 | 0 | ULONG Type; |
440 | 0 | WINPR_HANDLE* handle; |
441 | |
|
442 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
443 | 0 | return FALSE; |
444 | | |
445 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
446 | 0 | return FALSE; |
447 | | |
448 | 0 | handle = (WINPR_HANDLE*)hFile; |
449 | |
|
450 | 0 | if (handle->ops->WriteFileGather) |
451 | 0 | return handle->ops->WriteFileGather(handle, aSegmentArray, nNumberOfBytesToWrite, |
452 | 0 | lpReserved, lpOverlapped); |
453 | | |
454 | 0 | WLog_ERR(TAG, "WriteFileGather operation not implemented"); |
455 | 0 | return FALSE; |
456 | 0 | } |
457 | | |
458 | | BOOL FlushFileBuffers(HANDLE hFile) |
459 | 0 | { |
460 | 0 | ULONG Type; |
461 | 0 | WINPR_HANDLE* handle; |
462 | |
|
463 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
464 | 0 | return FALSE; |
465 | | |
466 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
467 | 0 | return FALSE; |
468 | | |
469 | 0 | handle = (WINPR_HANDLE*)hFile; |
470 | |
|
471 | 0 | if (handle->ops->FlushFileBuffers) |
472 | 0 | return handle->ops->FlushFileBuffers(handle); |
473 | | |
474 | 0 | WLog_ERR(TAG, "FlushFileBuffers operation not implemented"); |
475 | 0 | return FALSE; |
476 | 0 | } |
477 | | |
478 | | BOOL WINAPI GetFileAttributesExA(LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, |
479 | | LPVOID lpFileInformation) |
480 | 0 | { |
481 | 0 | LPWIN32_FILE_ATTRIBUTE_DATA fd = lpFileInformation; |
482 | 0 | WIN32_FIND_DATAA findFileData; |
483 | 0 | HANDLE hFind; |
484 | |
|
485 | 0 | if (!fd) |
486 | 0 | return FALSE; |
487 | | |
488 | 0 | if ((hFind = FindFirstFileA(lpFileName, &findFileData)) == INVALID_HANDLE_VALUE) |
489 | 0 | return FALSE; |
490 | | |
491 | 0 | FindClose(hFind); |
492 | 0 | fd->dwFileAttributes = findFileData.dwFileAttributes; |
493 | 0 | fd->ftCreationTime = findFileData.ftCreationTime; |
494 | 0 | fd->ftLastAccessTime = findFileData.ftLastAccessTime; |
495 | 0 | fd->ftLastWriteTime = findFileData.ftLastWriteTime; |
496 | 0 | fd->nFileSizeHigh = findFileData.nFileSizeHigh; |
497 | 0 | fd->nFileSizeLow = findFileData.nFileSizeLow; |
498 | 0 | return TRUE; |
499 | 0 | } |
500 | | |
501 | | BOOL WINAPI GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, |
502 | | LPVOID lpFileInformation) |
503 | 0 | { |
504 | 0 | BOOL ret; |
505 | 0 | if (!lpFileName) |
506 | 0 | return FALSE; |
507 | 0 | LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL); |
508 | |
|
509 | 0 | if (!lpCFileName) |
510 | 0 | { |
511 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
512 | 0 | return FALSE; |
513 | 0 | } |
514 | | |
515 | 0 | ret = GetFileAttributesExA(lpCFileName, fInfoLevelId, lpFileInformation); |
516 | 0 | free(lpCFileName); |
517 | 0 | return ret; |
518 | 0 | } |
519 | | |
520 | | DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName) |
521 | 0 | { |
522 | 0 | WIN32_FIND_DATAA findFileData; |
523 | 0 | HANDLE hFind; |
524 | |
|
525 | 0 | if ((hFind = FindFirstFileA(lpFileName, &findFileData)) == INVALID_HANDLE_VALUE) |
526 | 0 | return INVALID_FILE_ATTRIBUTES; |
527 | | |
528 | 0 | FindClose(hFind); |
529 | 0 | return findFileData.dwFileAttributes; |
530 | 0 | } |
531 | | |
532 | | DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName) |
533 | 0 | { |
534 | 0 | DWORD ret; |
535 | 0 | if (!lpFileName) |
536 | 0 | return FALSE; |
537 | 0 | LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL); |
538 | 0 | if (!lpCFileName) |
539 | 0 | { |
540 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
541 | 0 | return FALSE; |
542 | 0 | } |
543 | | |
544 | 0 | ret = GetFileAttributesA(lpCFileName); |
545 | 0 | free(lpCFileName); |
546 | 0 | return ret; |
547 | 0 | } |
548 | | |
549 | | BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation) |
550 | 0 | { |
551 | 0 | ULONG Type; |
552 | 0 | WINPR_HANDLE* handle; |
553 | |
|
554 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
555 | 0 | return FALSE; |
556 | | |
557 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
558 | 0 | return FALSE; |
559 | | |
560 | 0 | handle = (WINPR_HANDLE*)hFile; |
561 | |
|
562 | 0 | if (handle->ops->GetFileInformationByHandle) |
563 | 0 | return handle->ops->GetFileInformationByHandle(handle, lpFileInformation); |
564 | | |
565 | 0 | WLog_ERR(TAG, "GetFileInformationByHandle operation not implemented"); |
566 | 0 | return 0; |
567 | 0 | } |
568 | | |
569 | | static char* append(char* buffer, size_t size, const char* append) |
570 | 0 | { |
571 | 0 | winpr_str_append(append, buffer, size, "|"); |
572 | 0 | return buffer; |
573 | 0 | } |
574 | | |
575 | | static const char* flagsToStr(char* buffer, size_t size, DWORD flags) |
576 | 0 | { |
577 | 0 | char strflags[32] = { 0 }; |
578 | 0 | if (flags & FILE_ATTRIBUTE_READONLY) |
579 | 0 | append(buffer, size, "FILE_ATTRIBUTE_READONLY"); |
580 | 0 | if (flags & FILE_ATTRIBUTE_HIDDEN) |
581 | 0 | append(buffer, size, "FILE_ATTRIBUTE_HIDDEN"); |
582 | 0 | if (flags & FILE_ATTRIBUTE_SYSTEM) |
583 | 0 | append(buffer, size, "FILE_ATTRIBUTE_SYSTEM"); |
584 | 0 | if (flags & FILE_ATTRIBUTE_DIRECTORY) |
585 | 0 | append(buffer, size, "FILE_ATTRIBUTE_DIRECTORY"); |
586 | 0 | if (flags & FILE_ATTRIBUTE_ARCHIVE) |
587 | 0 | append(buffer, size, "FILE_ATTRIBUTE_ARCHIVE"); |
588 | 0 | if (flags & FILE_ATTRIBUTE_DEVICE) |
589 | 0 | append(buffer, size, "FILE_ATTRIBUTE_DEVICE"); |
590 | 0 | if (flags & FILE_ATTRIBUTE_NORMAL) |
591 | 0 | append(buffer, size, "FILE_ATTRIBUTE_NORMAL"); |
592 | 0 | if (flags & FILE_ATTRIBUTE_TEMPORARY) |
593 | 0 | append(buffer, size, "FILE_ATTRIBUTE_TEMPORARY"); |
594 | 0 | if (flags & FILE_ATTRIBUTE_SPARSE_FILE) |
595 | 0 | append(buffer, size, "FILE_ATTRIBUTE_SPARSE_FILE"); |
596 | 0 | if (flags & FILE_ATTRIBUTE_REPARSE_POINT) |
597 | 0 | append(buffer, size, "FILE_ATTRIBUTE_REPARSE_POINT"); |
598 | 0 | if (flags & FILE_ATTRIBUTE_COMPRESSED) |
599 | 0 | append(buffer, size, "FILE_ATTRIBUTE_COMPRESSED"); |
600 | 0 | if (flags & FILE_ATTRIBUTE_OFFLINE) |
601 | 0 | append(buffer, size, "FILE_ATTRIBUTE_OFFLINE"); |
602 | 0 | if (flags & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) |
603 | 0 | append(buffer, size, "FILE_ATTRIBUTE_NOT_CONTENT_INDEXED"); |
604 | 0 | if (flags & FILE_ATTRIBUTE_ENCRYPTED) |
605 | 0 | append(buffer, size, "FILE_ATTRIBUTE_ENCRYPTED"); |
606 | 0 | if (flags & FILE_ATTRIBUTE_VIRTUAL) |
607 | 0 | append(buffer, size, "FILE_ATTRIBUTE_VIRTUAL"); |
608 | |
|
609 | 0 | _snprintf(strflags, sizeof(strflags), " [0x%08" PRIx32 "]", flags); |
610 | 0 | winpr_str_append(strflags, buffer, size, NULL); |
611 | 0 | return buffer; |
612 | 0 | } |
613 | | |
614 | | BOOL SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes) |
615 | 0 | { |
616 | 0 | struct stat st; |
617 | 0 | int fd; |
618 | 0 | BOOL rc = FALSE; |
619 | |
|
620 | 0 | if (dwFileAttributes & ~FILE_ATTRIBUTE_READONLY) |
621 | 0 | { |
622 | 0 | char buffer[8192] = { 0 }; |
623 | 0 | const char* flags = |
624 | 0 | flagsToStr(buffer, sizeof(buffer), dwFileAttributes & ~FILE_ATTRIBUTE_READONLY); |
625 | 0 | WLog_WARN(TAG, "Unsupported flags %s, ignoring!", flags); |
626 | 0 | } |
627 | |
|
628 | 0 | fd = open(lpFileName, O_RDONLY); |
629 | 0 | if (fd < 0) |
630 | 0 | return FALSE; |
631 | | |
632 | 0 | if (fstat(fd, &st) != 0) |
633 | 0 | goto fail; |
634 | | |
635 | 0 | if (dwFileAttributes & FILE_ATTRIBUTE_READONLY) |
636 | 0 | { |
637 | 0 | st.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); |
638 | 0 | } |
639 | 0 | else |
640 | 0 | { |
641 | 0 | st.st_mode |= S_IWUSR; |
642 | 0 | } |
643 | |
|
644 | 0 | if (fchmod(fd, st.st_mode) != 0) |
645 | 0 | goto fail; |
646 | | |
647 | 0 | rc = TRUE; |
648 | 0 | fail: |
649 | 0 | close(fd); |
650 | 0 | return rc; |
651 | 0 | } |
652 | | |
653 | | BOOL SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes) |
654 | 0 | { |
655 | 0 | BOOL ret; |
656 | 0 | LPSTR lpCFileName; |
657 | |
|
658 | 0 | if (!lpFileName) |
659 | 0 | return FALSE; |
660 | | |
661 | 0 | if (dwFileAttributes & ~FILE_ATTRIBUTE_READONLY) |
662 | 0 | { |
663 | 0 | char buffer[8192] = { 0 }; |
664 | 0 | const char* flags = |
665 | 0 | flagsToStr(buffer, sizeof(buffer), dwFileAttributes & ~FILE_ATTRIBUTE_READONLY); |
666 | 0 | WLog_WARN(TAG, "Unsupported flags %s, ignoring!", flags); |
667 | 0 | } |
668 | |
|
669 | 0 | lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL); |
670 | 0 | if (!lpCFileName) |
671 | 0 | { |
672 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
673 | 0 | return FALSE; |
674 | 0 | } |
675 | | |
676 | 0 | ret = SetFileAttributesA(lpCFileName, dwFileAttributes); |
677 | 0 | free(lpCFileName); |
678 | 0 | return ret; |
679 | 0 | } |
680 | | |
681 | | BOOL SetEndOfFile(HANDLE hFile) |
682 | 0 | { |
683 | 0 | ULONG Type; |
684 | 0 | WINPR_HANDLE* handle; |
685 | |
|
686 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
687 | 0 | return FALSE; |
688 | | |
689 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
690 | 0 | return FALSE; |
691 | | |
692 | 0 | handle = (WINPR_HANDLE*)hFile; |
693 | |
|
694 | 0 | if (handle->ops->SetEndOfFile) |
695 | 0 | return handle->ops->SetEndOfFile(handle); |
696 | | |
697 | 0 | WLog_ERR(TAG, "SetEndOfFile operation not implemented"); |
698 | 0 | return FALSE; |
699 | 0 | } |
700 | | |
701 | | DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) |
702 | 0 | { |
703 | 0 | ULONG Type; |
704 | 0 | WINPR_HANDLE* handle; |
705 | |
|
706 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
707 | 0 | return FALSE; |
708 | | |
709 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
710 | 0 | return FALSE; |
711 | | |
712 | 0 | handle = (WINPR_HANDLE*)hFile; |
713 | |
|
714 | 0 | if (handle->ops->GetFileSize) |
715 | 0 | return handle->ops->GetFileSize(handle, lpFileSizeHigh); |
716 | | |
717 | 0 | WLog_ERR(TAG, "GetFileSize operation not implemented"); |
718 | 0 | return 0; |
719 | 0 | } |
720 | | |
721 | | DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, |
722 | | DWORD dwMoveMethod) |
723 | 0 | { |
724 | 0 | ULONG Type; |
725 | 0 | WINPR_HANDLE* handle; |
726 | |
|
727 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
728 | 0 | return FALSE; |
729 | | |
730 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
731 | 0 | return FALSE; |
732 | | |
733 | 0 | handle = (WINPR_HANDLE*)hFile; |
734 | |
|
735 | 0 | if (handle->ops->SetFilePointer) |
736 | 0 | return handle->ops->SetFilePointer(handle, lDistanceToMove, lpDistanceToMoveHigh, |
737 | 0 | dwMoveMethod); |
738 | | |
739 | 0 | WLog_ERR(TAG, "SetFilePointer operation not implemented"); |
740 | 0 | return 0; |
741 | 0 | } |
742 | | |
743 | | BOOL SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer, |
744 | | DWORD dwMoveMethod) |
745 | 0 | { |
746 | 0 | ULONG Type; |
747 | 0 | WINPR_HANDLE* handle; |
748 | |
|
749 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
750 | 0 | return FALSE; |
751 | | |
752 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
753 | 0 | return FALSE; |
754 | | |
755 | 0 | handle = (WINPR_HANDLE*)hFile; |
756 | |
|
757 | 0 | if (handle->ops->SetFilePointerEx) |
758 | 0 | return handle->ops->SetFilePointerEx(handle, liDistanceToMove, lpNewFilePointer, |
759 | 0 | dwMoveMethod); |
760 | | |
761 | 0 | WLog_ERR(TAG, "SetFilePointerEx operation not implemented"); |
762 | 0 | return 0; |
763 | 0 | } |
764 | | |
765 | | BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh, |
766 | | DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh) |
767 | 0 | { |
768 | 0 | ULONG Type; |
769 | 0 | WINPR_HANDLE* handle; |
770 | |
|
771 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
772 | 0 | return FALSE; |
773 | | |
774 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
775 | 0 | return FALSE; |
776 | | |
777 | 0 | handle = (WINPR_HANDLE*)hFile; |
778 | |
|
779 | 0 | if (handle->ops->LockFile) |
780 | 0 | return handle->ops->LockFile(handle, dwFileOffsetLow, dwFileOffsetHigh, |
781 | 0 | nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh); |
782 | | |
783 | 0 | WLog_ERR(TAG, "LockFile operation not implemented"); |
784 | 0 | return FALSE; |
785 | 0 | } |
786 | | |
787 | | BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved, DWORD nNumberOfBytesToLockLow, |
788 | | DWORD nNumberOfBytesToLockHigh, LPOVERLAPPED lpOverlapped) |
789 | 0 | { |
790 | 0 | ULONG Type; |
791 | 0 | WINPR_HANDLE* handle; |
792 | |
|
793 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
794 | 0 | return FALSE; |
795 | | |
796 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
797 | 0 | return FALSE; |
798 | | |
799 | 0 | handle = (WINPR_HANDLE*)hFile; |
800 | |
|
801 | 0 | if (handle->ops->LockFileEx) |
802 | 0 | return handle->ops->LockFileEx(handle, dwFlags, dwReserved, nNumberOfBytesToLockLow, |
803 | 0 | nNumberOfBytesToLockHigh, lpOverlapped); |
804 | | |
805 | 0 | WLog_ERR(TAG, "LockFileEx operation not implemented"); |
806 | 0 | return FALSE; |
807 | 0 | } |
808 | | |
809 | | BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh, |
810 | | DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh) |
811 | 0 | { |
812 | 0 | ULONG Type; |
813 | 0 | WINPR_HANDLE* handle; |
814 | |
|
815 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
816 | 0 | return FALSE; |
817 | | |
818 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
819 | 0 | return FALSE; |
820 | | |
821 | 0 | handle = (WINPR_HANDLE*)hFile; |
822 | |
|
823 | 0 | if (handle->ops->UnlockFile) |
824 | 0 | return handle->ops->UnlockFile(handle, dwFileOffsetLow, dwFileOffsetHigh, |
825 | 0 | nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh); |
826 | | |
827 | 0 | WLog_ERR(TAG, "UnLockFile operation not implemented"); |
828 | 0 | return FALSE; |
829 | 0 | } |
830 | | |
831 | | BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow, |
832 | | DWORD nNumberOfBytesToUnlockHigh, LPOVERLAPPED lpOverlapped) |
833 | 0 | { |
834 | 0 | ULONG Type; |
835 | 0 | WINPR_HANDLE* handle; |
836 | |
|
837 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
838 | 0 | return FALSE; |
839 | | |
840 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
841 | 0 | return FALSE; |
842 | | |
843 | 0 | handle = (WINPR_HANDLE*)hFile; |
844 | |
|
845 | 0 | if (handle->ops->UnlockFileEx) |
846 | 0 | return handle->ops->UnlockFileEx(handle, dwReserved, nNumberOfBytesToUnlockLow, |
847 | 0 | nNumberOfBytesToUnlockHigh, lpOverlapped); |
848 | | |
849 | 0 | WLog_ERR(TAG, "UnLockFileEx operation not implemented"); |
850 | 0 | return FALSE; |
851 | 0 | } |
852 | | |
853 | | BOOL WINAPI SetFileTime(HANDLE hFile, const FILETIME* lpCreationTime, |
854 | | const FILETIME* lpLastAccessTime, const FILETIME* lpLastWriteTime) |
855 | 0 | { |
856 | 0 | ULONG Type; |
857 | 0 | WINPR_HANDLE* handle; |
858 | |
|
859 | 0 | if (hFile == INVALID_HANDLE_VALUE) |
860 | 0 | return FALSE; |
861 | | |
862 | 0 | if (!winpr_Handle_GetInfo(hFile, &Type, &handle)) |
863 | 0 | return FALSE; |
864 | | |
865 | 0 | handle = (WINPR_HANDLE*)hFile; |
866 | |
|
867 | 0 | if (handle->ops->SetFileTime) |
868 | 0 | return handle->ops->SetFileTime(handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime); |
869 | | |
870 | 0 | WLog_ERR(TAG, "operation not implemented"); |
871 | 0 | return FALSE; |
872 | 0 | } |
873 | | |
874 | | typedef struct |
875 | | { |
876 | | char magic[16]; |
877 | | LPSTR lpPath; |
878 | | LPSTR lpPattern; |
879 | | DIR* pDir; |
880 | | } WIN32_FILE_SEARCH; |
881 | | |
882 | | static const char file_search_magic[] = "file_srch_magic"; |
883 | | |
884 | | static WIN32_FILE_SEARCH* file_search_new(const char* name, size_t namelen, const char* pattern, |
885 | | size_t patternlen) |
886 | 0 | { |
887 | 0 | WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)calloc(1, sizeof(WIN32_FILE_SEARCH)); |
888 | 0 | if (!pFileSearch) |
889 | 0 | return NULL; |
890 | 0 | strncpy(pFileSearch->magic, file_search_magic, sizeof(pFileSearch->magic)); |
891 | |
|
892 | 0 | pFileSearch->lpPath = strndup(name, namelen); |
893 | 0 | pFileSearch->lpPattern = strndup(pattern, patternlen); |
894 | 0 | if (!pFileSearch->lpPath || !pFileSearch->lpPattern) |
895 | 0 | goto fail; |
896 | | |
897 | 0 | pFileSearch->pDir = opendir(pFileSearch->lpPath); |
898 | 0 | if (!pFileSearch->pDir) |
899 | 0 | { |
900 | | /* Work around for android: |
901 | | * parent directories are not accessible, so if we have a directory without pattern |
902 | | * try to open it directly and set pattern to '*' |
903 | | */ |
904 | 0 | struct stat fileStat = { 0 }; |
905 | 0 | if (stat(name, &fileStat) == 0) |
906 | 0 | { |
907 | 0 | if (S_ISDIR(fileStat.st_mode)) |
908 | 0 | { |
909 | 0 | pFileSearch->pDir = opendir(name); |
910 | 0 | if (pFileSearch->pDir) |
911 | 0 | { |
912 | 0 | free(pFileSearch->lpPath); |
913 | 0 | free(pFileSearch->lpPattern); |
914 | 0 | pFileSearch->lpPath = _strdup(name); |
915 | 0 | pFileSearch->lpPattern = _strdup("*"); |
916 | 0 | if (!pFileSearch->lpPath || !pFileSearch->lpPattern) |
917 | 0 | { |
918 | 0 | closedir(pFileSearch->pDir); |
919 | 0 | pFileSearch->pDir = NULL; |
920 | 0 | } |
921 | 0 | } |
922 | 0 | } |
923 | 0 | } |
924 | 0 | } |
925 | 0 | if (!pFileSearch->pDir) |
926 | 0 | goto fail; |
927 | | |
928 | 0 | return pFileSearch; |
929 | 0 | fail: |
930 | 0 | FindClose(pFileSearch); |
931 | 0 | return NULL; |
932 | 0 | } |
933 | | |
934 | | static BOOL is_valid_file_search_handle(HANDLE handle) |
935 | 0 | { |
936 | 0 | WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)handle; |
937 | 0 | if (!pFileSearch) |
938 | 0 | return FALSE; |
939 | 0 | if (pFileSearch == INVALID_HANDLE_VALUE) |
940 | 0 | return FALSE; |
941 | 0 | if (strcmp(file_search_magic, pFileSearch->magic) != 0) |
942 | 0 | return FALSE; |
943 | 0 | return TRUE; |
944 | 0 | } |
945 | | static BOOL FindDataFromStat(const char* path, const struct stat* fileStat, |
946 | | LPWIN32_FIND_DATAA lpFindFileData) |
947 | 0 | { |
948 | 0 | UINT64 ft; |
949 | 0 | char* lastSep; |
950 | 0 | lpFindFileData->dwFileAttributes = 0; |
951 | |
|
952 | 0 | if (S_ISDIR(fileStat->st_mode)) |
953 | 0 | lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY; |
954 | |
|
955 | 0 | if (lpFindFileData->dwFileAttributes == 0) |
956 | 0 | lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE; |
957 | |
|
958 | 0 | lastSep = strrchr(path, '/'); |
959 | |
|
960 | 0 | if (lastSep) |
961 | 0 | { |
962 | 0 | const char* name = lastSep + 1; |
963 | 0 | const size_t namelen = strlen(name); |
964 | |
|
965 | 0 | if ((namelen > 1) && (name[0] == '.') && (name[1] != '.')) |
966 | 0 | lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_HIDDEN; |
967 | 0 | } |
968 | |
|
969 | 0 | if (!(fileStat->st_mode & S_IWUSR)) |
970 | 0 | lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_READONLY; |
971 | |
|
972 | | #ifdef _DARWIN_FEATURE_64_BIT_INODE |
973 | | ft = STAT_TIME_TO_FILETIME(fileStat->st_birthtime); |
974 | | #else |
975 | 0 | ft = STAT_TIME_TO_FILETIME(fileStat->st_ctime); |
976 | 0 | #endif |
977 | 0 | lpFindFileData->ftCreationTime.dwHighDateTime = ((UINT64)ft) >> 32ULL; |
978 | 0 | lpFindFileData->ftCreationTime.dwLowDateTime = ft & 0xFFFFFFFF; |
979 | 0 | ft = STAT_TIME_TO_FILETIME(fileStat->st_mtime); |
980 | 0 | lpFindFileData->ftLastWriteTime.dwHighDateTime = ((UINT64)ft) >> 32ULL; |
981 | 0 | lpFindFileData->ftLastWriteTime.dwLowDateTime = ft & 0xFFFFFFFF; |
982 | 0 | ft = STAT_TIME_TO_FILETIME(fileStat->st_atime); |
983 | 0 | lpFindFileData->ftLastAccessTime.dwHighDateTime = ((UINT64)ft) >> 32ULL; |
984 | 0 | lpFindFileData->ftLastAccessTime.dwLowDateTime = ft & 0xFFFFFFFF; |
985 | 0 | lpFindFileData->nFileSizeHigh = ((UINT64)fileStat->st_size) >> 32ULL; |
986 | 0 | lpFindFileData->nFileSizeLow = fileStat->st_size & 0xFFFFFFFF; |
987 | 0 | return TRUE; |
988 | 0 | } |
989 | | |
990 | | HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData) |
991 | 0 | { |
992 | 0 | if (!lpFindFileData || !lpFileName) |
993 | 0 | { |
994 | 0 | SetLastError(ERROR_BAD_ARGUMENTS); |
995 | 0 | return INVALID_HANDLE_VALUE; |
996 | 0 | } |
997 | | |
998 | 0 | const WIN32_FIND_DATAA empty = { 0 }; |
999 | 0 | *lpFindFileData = empty; |
1000 | |
|
1001 | 0 | WIN32_FILE_SEARCH* pFileSearch = NULL; |
1002 | 0 | size_t patternlen = 0; |
1003 | 0 | const size_t flen = strlen(lpFileName); |
1004 | 0 | const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE); |
1005 | 0 | const char* ptr = strrchr(lpFileName, sep); |
1006 | 0 | if (!ptr) |
1007 | 0 | goto fail; |
1008 | 0 | patternlen = strlen(ptr + 1); |
1009 | 0 | if (patternlen == 0) |
1010 | 0 | goto fail; |
1011 | | |
1012 | 0 | pFileSearch = file_search_new(lpFileName, flen - patternlen, ptr + 1, patternlen); |
1013 | |
|
1014 | 0 | if (!pFileSearch) |
1015 | 0 | { |
1016 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1017 | 0 | return INVALID_HANDLE_VALUE; |
1018 | 0 | } |
1019 | | |
1020 | 0 | if (FindNextFileA((HANDLE)pFileSearch, lpFindFileData)) |
1021 | 0 | return (HANDLE)pFileSearch; |
1022 | | |
1023 | 0 | fail: |
1024 | 0 | FindClose(pFileSearch); |
1025 | 0 | return INVALID_HANDLE_VALUE; |
1026 | 0 | } |
1027 | | |
1028 | | static BOOL ConvertFindDataAToW(LPWIN32_FIND_DATAA lpFindFileDataA, |
1029 | | LPWIN32_FIND_DATAW lpFindFileDataW) |
1030 | 0 | { |
1031 | 0 | if (!lpFindFileDataA || !lpFindFileDataW) |
1032 | 0 | return FALSE; |
1033 | | |
1034 | 0 | lpFindFileDataW->dwFileAttributes = lpFindFileDataA->dwFileAttributes; |
1035 | 0 | lpFindFileDataW->ftCreationTime = lpFindFileDataA->ftCreationTime; |
1036 | 0 | lpFindFileDataW->ftLastAccessTime = lpFindFileDataA->ftLastAccessTime; |
1037 | 0 | lpFindFileDataW->ftLastWriteTime = lpFindFileDataA->ftLastWriteTime; |
1038 | 0 | lpFindFileDataW->nFileSizeHigh = lpFindFileDataA->nFileSizeHigh; |
1039 | 0 | lpFindFileDataW->nFileSizeLow = lpFindFileDataA->nFileSizeLow; |
1040 | 0 | lpFindFileDataW->dwReserved0 = lpFindFileDataA->dwReserved0; |
1041 | 0 | lpFindFileDataW->dwReserved1 = lpFindFileDataA->dwReserved1; |
1042 | |
|
1043 | 0 | if (ConvertUtf8NToWChar(lpFindFileDataA->cFileName, ARRAYSIZE(lpFindFileDataA->cFileName), |
1044 | 0 | lpFindFileDataW->cFileName, ARRAYSIZE(lpFindFileDataW->cFileName)) < 0) |
1045 | 0 | return FALSE; |
1046 | | |
1047 | 0 | return ConvertUtf8NToWChar(lpFindFileDataA->cAlternateFileName, |
1048 | 0 | ARRAYSIZE(lpFindFileDataA->cAlternateFileName), |
1049 | 0 | lpFindFileDataW->cAlternateFileName, |
1050 | 0 | ARRAYSIZE(lpFindFileDataW->cAlternateFileName)) >= 0; |
1051 | 0 | } |
1052 | | |
1053 | | HANDLE FindFirstFileW(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData) |
1054 | 0 | { |
1055 | 0 | LPSTR utfFileName = NULL; |
1056 | 0 | HANDLE h; |
1057 | 0 | if (!lpFileName) |
1058 | 0 | return FALSE; |
1059 | 0 | LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)calloc(1, sizeof(WIN32_FIND_DATAA)); |
1060 | |
|
1061 | 0 | if (!fd) |
1062 | 0 | { |
1063 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1064 | 0 | return INVALID_HANDLE_VALUE; |
1065 | 0 | } |
1066 | | |
1067 | 0 | utfFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL); |
1068 | 0 | if (!utfFileName) |
1069 | 0 | { |
1070 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1071 | 0 | free(fd); |
1072 | 0 | return INVALID_HANDLE_VALUE; |
1073 | 0 | } |
1074 | | |
1075 | 0 | h = FindFirstFileA(utfFileName, fd); |
1076 | 0 | free(utfFileName); |
1077 | |
|
1078 | 0 | if (h != INVALID_HANDLE_VALUE) |
1079 | 0 | { |
1080 | 0 | if (!ConvertFindDataAToW(fd, lpFindFileData)) |
1081 | 0 | { |
1082 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1083 | 0 | FindClose(h); |
1084 | 0 | h = INVALID_HANDLE_VALUE; |
1085 | 0 | goto out; |
1086 | 0 | } |
1087 | 0 | } |
1088 | | |
1089 | 0 | out: |
1090 | 0 | free(fd); |
1091 | 0 | return h; |
1092 | 0 | } |
1093 | | |
1094 | | HANDLE FindFirstFileExA(LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData, |
1095 | | FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags) |
1096 | 0 | { |
1097 | 0 | return INVALID_HANDLE_VALUE; |
1098 | 0 | } |
1099 | | |
1100 | | HANDLE FindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData, |
1101 | | FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags) |
1102 | 0 | { |
1103 | 0 | return INVALID_HANDLE_VALUE; |
1104 | 0 | } |
1105 | | |
1106 | | BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData) |
1107 | 0 | { |
1108 | 0 | if (!lpFindFileData) |
1109 | 0 | return FALSE; |
1110 | | |
1111 | 0 | const WIN32_FIND_DATAA empty = { 0 }; |
1112 | 0 | *lpFindFileData = empty; |
1113 | |
|
1114 | 0 | if (!is_valid_file_search_handle(hFindFile)) |
1115 | 0 | return FALSE; |
1116 | | |
1117 | 0 | WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile; |
1118 | 0 | struct dirent* pDirent = NULL; |
1119 | 0 | while ((pDirent = readdir(pFileSearch->pDir)) != NULL) |
1120 | 0 | { |
1121 | 0 | if (FilePatternMatchA(pDirent->d_name, pFileSearch->lpPattern)) |
1122 | 0 | { |
1123 | 0 | BOOL success = FALSE; |
1124 | |
|
1125 | 0 | strncpy(lpFindFileData->cFileName, pDirent->d_name, MAX_PATH); |
1126 | 0 | const size_t namelen = strnlen(lpFindFileData->cFileName, MAX_PATH); |
1127 | 0 | size_t pathlen = strlen(pFileSearch->lpPath); |
1128 | 0 | char* fullpath = (char*)malloc(pathlen + namelen + 2); |
1129 | |
|
1130 | 0 | if (fullpath == NULL) |
1131 | 0 | { |
1132 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1133 | 0 | return FALSE; |
1134 | 0 | } |
1135 | | |
1136 | 0 | memcpy(fullpath, pFileSearch->lpPath, pathlen); |
1137 | | /* Ensure path is terminated with a separator, but prevent |
1138 | | * duplicate separators */ |
1139 | 0 | if (fullpath[pathlen - 1] != '/') |
1140 | 0 | fullpath[pathlen++] = '/'; |
1141 | 0 | memcpy(fullpath + pathlen, pDirent->d_name, namelen); |
1142 | 0 | fullpath[pathlen + namelen] = 0; |
1143 | |
|
1144 | 0 | struct stat fileStat = { 0 }; |
1145 | 0 | if (stat(fullpath, &fileStat) != 0) |
1146 | 0 | { |
1147 | 0 | free(fullpath); |
1148 | 0 | SetLastError(map_posix_err(errno)); |
1149 | 0 | errno = 0; |
1150 | 0 | continue; |
1151 | 0 | } |
1152 | | |
1153 | | /* Skip FIFO entries. */ |
1154 | 0 | if (S_ISFIFO(fileStat.st_mode)) |
1155 | 0 | { |
1156 | 0 | free(fullpath); |
1157 | 0 | continue; |
1158 | 0 | } |
1159 | | |
1160 | 0 | success = FindDataFromStat(fullpath, &fileStat, lpFindFileData); |
1161 | 0 | free(fullpath); |
1162 | 0 | return success; |
1163 | 0 | } |
1164 | 0 | } |
1165 | | |
1166 | 0 | SetLastError(ERROR_NO_MORE_FILES); |
1167 | 0 | return FALSE; |
1168 | 0 | } |
1169 | | |
1170 | | BOOL FindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData) |
1171 | 0 | { |
1172 | 0 | LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)calloc(1, sizeof(WIN32_FIND_DATAA)); |
1173 | |
|
1174 | 0 | if (!fd) |
1175 | 0 | { |
1176 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1177 | 0 | return FALSE; |
1178 | 0 | } |
1179 | | |
1180 | 0 | if (FindNextFileA(hFindFile, fd)) |
1181 | 0 | { |
1182 | 0 | if (!ConvertFindDataAToW(fd, lpFindFileData)) |
1183 | 0 | { |
1184 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1185 | 0 | free(fd); |
1186 | 0 | return FALSE; |
1187 | 0 | } |
1188 | | |
1189 | 0 | free(fd); |
1190 | 0 | return TRUE; |
1191 | 0 | } |
1192 | | |
1193 | 0 | free(fd); |
1194 | 0 | return FALSE; |
1195 | 0 | } |
1196 | | |
1197 | | BOOL FindClose(HANDLE hFindFile) |
1198 | 0 | { |
1199 | 0 | WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile; |
1200 | | |
1201 | | /* Since INVALID_HANDLE_VALUE != NULL the analyzer guesses that there |
1202 | | * is a initialized HANDLE that is not freed properly. |
1203 | | * Disable this return to stop confusing the analyzer. */ |
1204 | 0 | #ifndef __clang_analyzer__ |
1205 | 0 | if (!is_valid_file_search_handle(hFindFile)) |
1206 | 0 | return FALSE; |
1207 | 0 | #endif |
1208 | | |
1209 | 0 | free(pFileSearch->lpPath); |
1210 | 0 | free(pFileSearch->lpPattern); |
1211 | |
|
1212 | 0 | if (pFileSearch->pDir) |
1213 | 0 | closedir(pFileSearch->pDir); |
1214 | |
|
1215 | 0 | free(pFileSearch); |
1216 | 0 | return TRUE; |
1217 | 0 | } |
1218 | | |
1219 | | BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) |
1220 | 0 | { |
1221 | 0 | if (!mkdir(lpPathName, S_IRUSR | S_IWUSR | S_IXUSR)) |
1222 | 0 | return TRUE; |
1223 | | |
1224 | 0 | return FALSE; |
1225 | 0 | } |
1226 | | |
1227 | | BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) |
1228 | 0 | { |
1229 | 0 | if (!lpPathName) |
1230 | 0 | return FALSE; |
1231 | 0 | char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL); |
1232 | 0 | BOOL ret = FALSE; |
1233 | |
|
1234 | 0 | if (!utfPathName) |
1235 | 0 | { |
1236 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1237 | 0 | goto fail; |
1238 | 0 | } |
1239 | | |
1240 | 0 | ret = CreateDirectoryA(utfPathName, lpSecurityAttributes); |
1241 | 0 | fail: |
1242 | 0 | free(utfPathName); |
1243 | 0 | return ret; |
1244 | 0 | } |
1245 | | |
1246 | | BOOL RemoveDirectoryA(LPCSTR lpPathName) |
1247 | 0 | { |
1248 | 0 | int ret = rmdir(lpPathName); |
1249 | |
|
1250 | 0 | if (ret != 0) |
1251 | 0 | SetLastError(map_posix_err(errno)); |
1252 | 0 | else |
1253 | 0 | SetLastError(STATUS_SUCCESS); |
1254 | |
|
1255 | 0 | return ret == 0; |
1256 | 0 | } |
1257 | | |
1258 | | BOOL RemoveDirectoryW(LPCWSTR lpPathName) |
1259 | 0 | { |
1260 | 0 | if (!lpPathName) |
1261 | 0 | return FALSE; |
1262 | 0 | char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL); |
1263 | 0 | BOOL ret = FALSE; |
1264 | |
|
1265 | 0 | if (!utfPathName) |
1266 | 0 | { |
1267 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1268 | 0 | goto fail; |
1269 | 0 | } |
1270 | | |
1271 | 0 | ret = RemoveDirectoryA(utfPathName); |
1272 | 0 | fail: |
1273 | 0 | free(utfPathName); |
1274 | 0 | return ret; |
1275 | 0 | } |
1276 | | |
1277 | | BOOL MoveFileExA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags) |
1278 | 0 | { |
1279 | 0 | struct stat st; |
1280 | 0 | int ret; |
1281 | 0 | ret = stat(lpNewFileName, &st); |
1282 | |
|
1283 | 0 | if ((dwFlags & MOVEFILE_REPLACE_EXISTING) == 0) |
1284 | 0 | { |
1285 | 0 | if (ret == 0) |
1286 | 0 | { |
1287 | 0 | SetLastError(ERROR_ALREADY_EXISTS); |
1288 | 0 | return FALSE; |
1289 | 0 | } |
1290 | 0 | } |
1291 | 0 | else |
1292 | 0 | { |
1293 | 0 | if (ret == 0 && (st.st_mode & S_IWUSR) == 0) |
1294 | 0 | { |
1295 | 0 | SetLastError(ERROR_ACCESS_DENIED); |
1296 | 0 | return FALSE; |
1297 | 0 | } |
1298 | 0 | } |
1299 | | |
1300 | 0 | ret = rename(lpExistingFileName, lpNewFileName); |
1301 | |
|
1302 | 0 | if (ret != 0) |
1303 | 0 | SetLastError(map_posix_err(errno)); |
1304 | |
|
1305 | 0 | return ret == 0; |
1306 | 0 | } |
1307 | | |
1308 | | BOOL MoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags) |
1309 | 0 | { |
1310 | 0 | if (!lpExistingFileName || !lpNewFileName) |
1311 | 0 | return FALSE; |
1312 | | |
1313 | 0 | LPSTR lpCExistingFileName = ConvertWCharToUtf8Alloc(lpExistingFileName, NULL); |
1314 | 0 | LPSTR lpCNewFileName = ConvertWCharToUtf8Alloc(lpNewFileName, NULL); |
1315 | 0 | BOOL ret = FALSE; |
1316 | |
|
1317 | 0 | if (!lpCExistingFileName || !lpCNewFileName) |
1318 | 0 | { |
1319 | 0 | SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
1320 | 0 | goto fail; |
1321 | 0 | } |
1322 | | |
1323 | 0 | ret = MoveFileExA(lpCExistingFileName, lpCNewFileName, dwFlags); |
1324 | 0 | fail: |
1325 | 0 | free(lpCNewFileName); |
1326 | 0 | free(lpCExistingFileName); |
1327 | 0 | return ret; |
1328 | 0 | } |
1329 | | |
1330 | | BOOL MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName) |
1331 | 0 | { |
1332 | 0 | return MoveFileExA(lpExistingFileName, lpNewFileName, 0); |
1333 | 0 | } |
1334 | | |
1335 | | BOOL MoveFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName) |
1336 | 0 | { |
1337 | 0 | return MoveFileExW(lpExistingFileName, lpNewFileName, 0); |
1338 | 0 | } |
1339 | | |
1340 | | #endif |
1341 | | |
1342 | | /* Extended API */ |
1343 | | |
1344 | | int UnixChangeFileMode(const char* filename, int flags) |
1345 | 0 | { |
1346 | 0 | if (!filename) |
1347 | 0 | return -1; |
1348 | 0 | #ifndef _WIN32 |
1349 | 0 | mode_t fl = 0; |
1350 | 0 | fl |= (flags & 0x4000) ? S_ISUID : 0; |
1351 | 0 | fl |= (flags & 0x2000) ? S_ISGID : 0; |
1352 | 0 | fl |= (flags & 0x1000) ? S_ISVTX : 0; |
1353 | 0 | fl |= (flags & 0x0400) ? S_IRUSR : 0; |
1354 | 0 | fl |= (flags & 0x0200) ? S_IWUSR : 0; |
1355 | 0 | fl |= (flags & 0x0100) ? S_IXUSR : 0; |
1356 | 0 | fl |= (flags & 0x0040) ? S_IRGRP : 0; |
1357 | 0 | fl |= (flags & 0x0020) ? S_IWGRP : 0; |
1358 | 0 | fl |= (flags & 0x0010) ? S_IXGRP : 0; |
1359 | 0 | fl |= (flags & 0x0004) ? S_IROTH : 0; |
1360 | 0 | fl |= (flags & 0x0002) ? S_IWOTH : 0; |
1361 | 0 | fl |= (flags & 0x0001) ? S_IXOTH : 0; |
1362 | 0 | return chmod(filename, fl); |
1363 | | #else |
1364 | | int rc; |
1365 | | WCHAR* wfl = ConvertUtf8ToWCharAlloc(filename, NULL); |
1366 | | |
1367 | | if (!wfl) |
1368 | | return -1; |
1369 | | |
1370 | | /* Check for unsupported flags. */ |
1371 | | if (flags & ~(_S_IREAD | _S_IWRITE)) |
1372 | | WLog_WARN(TAG, "Unsupported file mode %d for _wchmod", flags); |
1373 | | |
1374 | | rc = _wchmod(wfl, flags); |
1375 | | free(wfl); |
1376 | | return rc; |
1377 | | #endif |
1378 | 0 | } |