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