Coverage Report

Created: 2024-05-20 06:11

/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
  if (!lpFileName)
237
0
    return INVALID_HANDLE_VALUE;
238
239
0
  if (pthread_once(&_HandleCreatorsInitialized, _HandleCreatorsInit) != 0)
240
0
  {
241
0
    SetLastError(ERROR_DLL_INIT_FAILED);
242
0
    return INVALID_HANDLE_VALUE;
243
0
  }
244
245
0
  if (_HandleCreators == NULL)
246
0
  {
247
0
    SetLastError(ERROR_DLL_INIT_FAILED);
248
0
    return INVALID_HANDLE_VALUE;
249
0
  }
250
251
0
  ArrayList_Lock(_HandleCreators);
252
253
0
  for (size_t i = 0; i <= ArrayList_Count(_HandleCreators); i++)
254
0
  {
255
0
    HANDLE_CREATOR* creator = ArrayList_GetItem(_HandleCreators, i);
256
257
0
    if (creator && creator->IsHandled(lpFileName))
258
0
    {
259
0
      HANDLE newHandle =
260
0
          creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
261
0
                               dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
262
0
      ArrayList_Unlock(_HandleCreators);
263
0
      return newHandle;
264
0
    }
265
0
  }
266
267
0
  ArrayList_Unlock(_HandleCreators);
268
0
  return INVALID_HANDLE_VALUE;
269
0
}
270
271
HANDLE CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
272
                   LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
273
                   DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
274
0
{
275
0
  HANDLE hdl = NULL;
276
0
  if (!lpFileName)
277
0
    return NULL;
278
0
  char* lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL);
279
280
0
  if (!lpFileNameA)
281
0
  {
282
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
283
0
    goto fail;
284
0
  }
285
286
0
  hdl = CreateFileA(lpFileNameA, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
287
0
                    dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
288
0
fail:
289
0
  free(lpFileNameA);
290
0
  return hdl;
291
0
}
292
293
BOOL DeleteFileA(LPCSTR lpFileName)
294
0
{
295
0
  int status = 0;
296
0
  status = unlink(lpFileName);
297
0
  return (status != -1) ? TRUE : FALSE;
298
0
}
299
300
BOOL DeleteFileW(LPCWSTR lpFileName)
301
0
{
302
0
  if (!lpFileName)
303
0
    return FALSE;
304
0
  LPSTR lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL);
305
0
  BOOL rc = FALSE;
306
307
0
  if (!lpFileNameA)
308
0
    goto fail;
309
310
0
  rc = DeleteFileA(lpFileNameA);
311
0
fail:
312
0
  free(lpFileNameA);
313
0
  return rc;
314
0
}
315
316
BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
317
              LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
318
0
{
319
0
  ULONG Type = 0;
320
0
  WINPR_HANDLE* handle = NULL;
321
322
0
  if (hFile == INVALID_HANDLE_VALUE)
323
0
    return FALSE;
324
325
  /*
326
   * from http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
327
   * lpNumberOfBytesRead can be NULL only when the lpOverlapped parameter is not NULL.
328
   */
329
330
0
  if (!lpNumberOfBytesRead && !lpOverlapped)
331
0
    return FALSE;
332
333
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
334
0
    return FALSE;
335
336
0
  handle = (WINPR_HANDLE*)hFile;
337
338
0
  if (handle->ops->ReadFile)
339
0
    return handle->ops->ReadFile(handle, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead,
340
0
                                 lpOverlapped);
341
342
0
  WLog_ERR(TAG, "ReadFile operation not implemented");
343
0
  return FALSE;
344
0
}
345
346
BOOL ReadFileEx(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
347
                LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
348
0
{
349
0
  ULONG Type = 0;
350
0
  WINPR_HANDLE* handle = NULL;
351
352
0
  if (hFile == INVALID_HANDLE_VALUE)
353
0
    return FALSE;
354
355
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
356
0
    return FALSE;
357
358
0
  handle = (WINPR_HANDLE*)hFile;
359
360
0
  if (handle->ops->ReadFileEx)
361
0
    return handle->ops->ReadFileEx(handle, lpBuffer, nNumberOfBytesToRead, lpOverlapped,
362
0
                                   lpCompletionRoutine);
363
364
0
  WLog_ERR(TAG, "ReadFileEx operation not implemented");
365
0
  return FALSE;
366
0
}
367
368
BOOL ReadFileScatter(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToRead,
369
                     LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
370
0
{
371
0
  ULONG Type = 0;
372
0
  WINPR_HANDLE* handle = NULL;
373
374
0
  if (hFile == INVALID_HANDLE_VALUE)
375
0
    return FALSE;
376
377
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
378
0
    return FALSE;
379
380
0
  handle = (WINPR_HANDLE*)hFile;
381
382
0
  if (handle->ops->ReadFileScatter)
383
0
    return handle->ops->ReadFileScatter(handle, aSegmentArray, nNumberOfBytesToRead, lpReserved,
384
0
                                        lpOverlapped);
385
386
0
  WLog_ERR(TAG, "ReadFileScatter operation not implemented");
387
0
  return FALSE;
388
0
}
389
390
BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
391
               LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
392
0
{
393
0
  ULONG Type = 0;
394
0
  WINPR_HANDLE* handle = NULL;
395
396
0
  if (hFile == INVALID_HANDLE_VALUE)
397
0
    return FALSE;
398
399
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
400
0
    return FALSE;
401
402
0
  handle = (WINPR_HANDLE*)hFile;
403
404
0
  if (handle->ops->WriteFile)
405
0
    return handle->ops->WriteFile(handle, lpBuffer, nNumberOfBytesToWrite,
406
0
                                  lpNumberOfBytesWritten, lpOverlapped);
407
408
0
  WLog_ERR(TAG, "WriteFile operation not implemented");
409
0
  return FALSE;
410
0
}
411
412
BOOL WriteFileEx(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
413
                 LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
414
0
{
415
0
  ULONG Type = 0;
416
0
  WINPR_HANDLE* handle = NULL;
417
418
0
  if (hFile == INVALID_HANDLE_VALUE)
419
0
    return FALSE;
420
421
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
422
0
    return FALSE;
423
424
0
  handle = (WINPR_HANDLE*)hFile;
425
426
0
  if (handle->ops->WriteFileEx)
427
0
    return handle->ops->WriteFileEx(handle, lpBuffer, nNumberOfBytesToWrite, lpOverlapped,
428
0
                                    lpCompletionRoutine);
429
430
0
  WLog_ERR(TAG, "WriteFileEx operation not implemented");
431
0
  return FALSE;
432
0
}
433
434
BOOL WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[],
435
                     DWORD nNumberOfBytesToWrite, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
436
0
{
437
0
  ULONG Type = 0;
438
0
  WINPR_HANDLE* handle = NULL;
439
440
0
  if (hFile == INVALID_HANDLE_VALUE)
441
0
    return FALSE;
442
443
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
444
0
    return FALSE;
445
446
0
  handle = (WINPR_HANDLE*)hFile;
447
448
0
  if (handle->ops->WriteFileGather)
449
0
    return handle->ops->WriteFileGather(handle, aSegmentArray, nNumberOfBytesToWrite,
450
0
                                        lpReserved, lpOverlapped);
451
452
0
  WLog_ERR(TAG, "WriteFileGather operation not implemented");
453
0
  return FALSE;
454
0
}
455
456
BOOL FlushFileBuffers(HANDLE hFile)
457
0
{
458
0
  ULONG Type = 0;
459
0
  WINPR_HANDLE* handle = NULL;
460
461
0
  if (hFile == INVALID_HANDLE_VALUE)
462
0
    return FALSE;
463
464
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
465
0
    return FALSE;
466
467
0
  handle = (WINPR_HANDLE*)hFile;
468
469
0
  if (handle->ops->FlushFileBuffers)
470
0
    return handle->ops->FlushFileBuffers(handle);
471
472
0
  WLog_ERR(TAG, "FlushFileBuffers operation not implemented");
473
0
  return FALSE;
474
0
}
475
476
BOOL WINAPI GetFileAttributesExA(LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
477
                                 LPVOID lpFileInformation)
478
0
{
479
0
  LPWIN32_FILE_ATTRIBUTE_DATA fd = lpFileInformation;
480
0
  WIN32_FIND_DATAA findFileData;
481
0
  HANDLE hFind = NULL;
482
483
0
  if (!fd)
484
0
    return FALSE;
485
486
0
  if ((hFind = FindFirstFileA(lpFileName, &findFileData)) == INVALID_HANDLE_VALUE)
487
0
    return FALSE;
488
489
0
  FindClose(hFind);
490
0
  fd->dwFileAttributes = findFileData.dwFileAttributes;
491
0
  fd->ftCreationTime = findFileData.ftCreationTime;
492
0
  fd->ftLastAccessTime = findFileData.ftLastAccessTime;
493
0
  fd->ftLastWriteTime = findFileData.ftLastWriteTime;
494
0
  fd->nFileSizeHigh = findFileData.nFileSizeHigh;
495
0
  fd->nFileSizeLow = findFileData.nFileSizeLow;
496
0
  return TRUE;
497
0
}
498
499
BOOL WINAPI GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
500
                                 LPVOID lpFileInformation)
501
0
{
502
0
  BOOL ret = 0;
503
0
  if (!lpFileName)
504
0
    return FALSE;
505
0
  LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
506
507
0
  if (!lpCFileName)
508
0
  {
509
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
510
0
    return FALSE;
511
0
  }
512
513
0
  ret = GetFileAttributesExA(lpCFileName, fInfoLevelId, lpFileInformation);
514
0
  free(lpCFileName);
515
0
  return ret;
516
0
}
517
518
DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName)
519
0
{
520
0
  WIN32_FIND_DATAA findFileData;
521
0
  HANDLE hFind = NULL;
522
523
0
  if ((hFind = FindFirstFileA(lpFileName, &findFileData)) == INVALID_HANDLE_VALUE)
524
0
    return INVALID_FILE_ATTRIBUTES;
525
526
0
  FindClose(hFind);
527
0
  return findFileData.dwFileAttributes;
528
0
}
529
530
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
531
0
{
532
0
  DWORD ret = 0;
533
0
  if (!lpFileName)
534
0
    return FALSE;
535
0
  LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
536
0
  if (!lpCFileName)
537
0
  {
538
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
539
0
    return FALSE;
540
0
  }
541
542
0
  ret = GetFileAttributesA(lpCFileName);
543
0
  free(lpCFileName);
544
0
  return ret;
545
0
}
546
547
BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation)
548
0
{
549
0
  ULONG Type = 0;
550
0
  WINPR_HANDLE* handle = NULL;
551
552
0
  if (hFile == INVALID_HANDLE_VALUE)
553
0
    return FALSE;
554
555
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
556
0
    return FALSE;
557
558
0
  handle = (WINPR_HANDLE*)hFile;
559
560
0
  if (handle->ops->GetFileInformationByHandle)
561
0
    return handle->ops->GetFileInformationByHandle(handle, lpFileInformation);
562
563
0
  WLog_ERR(TAG, "GetFileInformationByHandle operation not implemented");
564
0
  return 0;
565
0
}
566
567
static char* append(char* buffer, size_t size, const char* append)
568
0
{
569
0
  winpr_str_append(append, buffer, size, "|");
570
0
  return buffer;
571
0
}
572
573
static const char* flagsToStr(char* buffer, size_t size, DWORD flags)
574
0
{
575
0
  char strflags[32] = { 0 };
576
0
  if (flags & FILE_ATTRIBUTE_READONLY)
577
0
    append(buffer, size, "FILE_ATTRIBUTE_READONLY");
578
0
  if (flags & FILE_ATTRIBUTE_HIDDEN)
579
0
    append(buffer, size, "FILE_ATTRIBUTE_HIDDEN");
580
0
  if (flags & FILE_ATTRIBUTE_SYSTEM)
581
0
    append(buffer, size, "FILE_ATTRIBUTE_SYSTEM");
582
0
  if (flags & FILE_ATTRIBUTE_DIRECTORY)
583
0
    append(buffer, size, "FILE_ATTRIBUTE_DIRECTORY");
584
0
  if (flags & FILE_ATTRIBUTE_ARCHIVE)
585
0
    append(buffer, size, "FILE_ATTRIBUTE_ARCHIVE");
586
0
  if (flags & FILE_ATTRIBUTE_DEVICE)
587
0
    append(buffer, size, "FILE_ATTRIBUTE_DEVICE");
588
0
  if (flags & FILE_ATTRIBUTE_NORMAL)
589
0
    append(buffer, size, "FILE_ATTRIBUTE_NORMAL");
590
0
  if (flags & FILE_ATTRIBUTE_TEMPORARY)
591
0
    append(buffer, size, "FILE_ATTRIBUTE_TEMPORARY");
592
0
  if (flags & FILE_ATTRIBUTE_SPARSE_FILE)
593
0
    append(buffer, size, "FILE_ATTRIBUTE_SPARSE_FILE");
594
0
  if (flags & FILE_ATTRIBUTE_REPARSE_POINT)
595
0
    append(buffer, size, "FILE_ATTRIBUTE_REPARSE_POINT");
596
0
  if (flags & FILE_ATTRIBUTE_COMPRESSED)
597
0
    append(buffer, size, "FILE_ATTRIBUTE_COMPRESSED");
598
0
  if (flags & FILE_ATTRIBUTE_OFFLINE)
599
0
    append(buffer, size, "FILE_ATTRIBUTE_OFFLINE");
600
0
  if (flags & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
601
0
    append(buffer, size, "FILE_ATTRIBUTE_NOT_CONTENT_INDEXED");
602
0
  if (flags & FILE_ATTRIBUTE_ENCRYPTED)
603
0
    append(buffer, size, "FILE_ATTRIBUTE_ENCRYPTED");
604
0
  if (flags & FILE_ATTRIBUTE_VIRTUAL)
605
0
    append(buffer, size, "FILE_ATTRIBUTE_VIRTUAL");
606
607
0
  _snprintf(strflags, sizeof(strflags), " [0x%08" PRIx32 "]", flags);
608
0
  winpr_str_append(strflags, buffer, size, NULL);
609
0
  return buffer;
610
0
}
611
612
BOOL SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes)
613
0
{
614
0
  struct stat st;
615
0
  int fd = 0;
616
0
  BOOL rc = FALSE;
617
618
0
  if (dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)
619
0
  {
620
0
    char buffer[8192] = { 0 };
621
0
    const char* flags =
622
0
        flagsToStr(buffer, sizeof(buffer), dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
623
0
    WLog_WARN(TAG, "Unsupported flags %s, ignoring!", flags);
624
0
  }
625
626
0
  fd = open(lpFileName, O_RDONLY);
627
0
  if (fd < 0)
628
0
    return FALSE;
629
630
0
  if (fstat(fd, &st) != 0)
631
0
    goto fail;
632
633
0
  if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
634
0
  {
635
0
    st.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
636
0
  }
637
0
  else
638
0
  {
639
0
    st.st_mode |= S_IWUSR;
640
0
  }
641
642
0
  if (fchmod(fd, st.st_mode) != 0)
643
0
    goto fail;
644
645
0
  rc = TRUE;
646
0
fail:
647
0
  close(fd);
648
0
  return rc;
649
0
}
650
651
BOOL SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
652
0
{
653
0
  BOOL ret = 0;
654
0
  LPSTR lpCFileName = NULL;
655
656
0
  if (!lpFileName)
657
0
    return FALSE;
658
659
0
  if (dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)
660
0
  {
661
0
    char buffer[8192] = { 0 };
662
0
    const char* flags =
663
0
        flagsToStr(buffer, sizeof(buffer), dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
664
0
    WLog_WARN(TAG, "Unsupported flags %s, ignoring!", flags);
665
0
  }
666
667
0
  lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
668
0
  if (!lpCFileName)
669
0
  {
670
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
671
0
    return FALSE;
672
0
  }
673
674
0
  ret = SetFileAttributesA(lpCFileName, dwFileAttributes);
675
0
  free(lpCFileName);
676
0
  return ret;
677
0
}
678
679
BOOL SetEndOfFile(HANDLE hFile)
680
0
{
681
0
  ULONG Type = 0;
682
0
  WINPR_HANDLE* handle = NULL;
683
684
0
  if (hFile == INVALID_HANDLE_VALUE)
685
0
    return FALSE;
686
687
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
688
0
    return FALSE;
689
690
0
  handle = (WINPR_HANDLE*)hFile;
691
692
0
  if (handle->ops->SetEndOfFile)
693
0
    return handle->ops->SetEndOfFile(handle);
694
695
0
  WLog_ERR(TAG, "SetEndOfFile operation not implemented");
696
0
  return FALSE;
697
0
}
698
699
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
700
0
{
701
0
  ULONG Type = 0;
702
0
  WINPR_HANDLE* handle = NULL;
703
704
0
  if (hFile == INVALID_HANDLE_VALUE)
705
0
    return FALSE;
706
707
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
708
0
    return FALSE;
709
710
0
  handle = (WINPR_HANDLE*)hFile;
711
712
0
  if (handle->ops->GetFileSize)
713
0
    return handle->ops->GetFileSize(handle, lpFileSizeHigh);
714
715
0
  WLog_ERR(TAG, "GetFileSize operation not implemented");
716
0
  return 0;
717
0
}
718
719
DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh,
720
                     DWORD dwMoveMethod)
721
0
{
722
0
  ULONG Type = 0;
723
0
  WINPR_HANDLE* handle = NULL;
724
725
0
  if (hFile == INVALID_HANDLE_VALUE)
726
0
    return FALSE;
727
728
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
729
0
    return FALSE;
730
731
0
  handle = (WINPR_HANDLE*)hFile;
732
733
0
  if (handle->ops->SetFilePointer)
734
0
    return handle->ops->SetFilePointer(handle, lDistanceToMove, lpDistanceToMoveHigh,
735
0
                                       dwMoveMethod);
736
737
0
  WLog_ERR(TAG, "SetFilePointer operation not implemented");
738
0
  return 0;
739
0
}
740
741
BOOL SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer,
742
                      DWORD dwMoveMethod)
743
0
{
744
0
  ULONG Type = 0;
745
0
  WINPR_HANDLE* handle = NULL;
746
747
0
  if (hFile == INVALID_HANDLE_VALUE)
748
0
    return FALSE;
749
750
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
751
0
    return FALSE;
752
753
0
  handle = (WINPR_HANDLE*)hFile;
754
755
0
  if (handle->ops->SetFilePointerEx)
756
0
    return handle->ops->SetFilePointerEx(handle, liDistanceToMove, lpNewFilePointer,
757
0
                                         dwMoveMethod);
758
759
0
  WLog_ERR(TAG, "SetFilePointerEx operation not implemented");
760
0
  return 0;
761
0
}
762
763
BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
764
              DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
765
0
{
766
0
  ULONG Type = 0;
767
0
  WINPR_HANDLE* handle = NULL;
768
769
0
  if (hFile == INVALID_HANDLE_VALUE)
770
0
    return FALSE;
771
772
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
773
0
    return FALSE;
774
775
0
  handle = (WINPR_HANDLE*)hFile;
776
777
0
  if (handle->ops->LockFile)
778
0
    return handle->ops->LockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
779
0
                                 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
780
781
0
  WLog_ERR(TAG, "LockFile operation not implemented");
782
0
  return FALSE;
783
0
}
784
785
BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved, DWORD nNumberOfBytesToLockLow,
786
                DWORD nNumberOfBytesToLockHigh, LPOVERLAPPED lpOverlapped)
787
0
{
788
0
  ULONG Type = 0;
789
0
  WINPR_HANDLE* handle = NULL;
790
791
0
  if (hFile == INVALID_HANDLE_VALUE)
792
0
    return FALSE;
793
794
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
795
0
    return FALSE;
796
797
0
  handle = (WINPR_HANDLE*)hFile;
798
799
0
  if (handle->ops->LockFileEx)
800
0
    return handle->ops->LockFileEx(handle, dwFlags, dwReserved, nNumberOfBytesToLockLow,
801
0
                                   nNumberOfBytesToLockHigh, lpOverlapped);
802
803
0
  WLog_ERR(TAG, "LockFileEx operation not implemented");
804
0
  return FALSE;
805
0
}
806
807
BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
808
                DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
809
0
{
810
0
  ULONG Type = 0;
811
0
  WINPR_HANDLE* handle = NULL;
812
813
0
  if (hFile == INVALID_HANDLE_VALUE)
814
0
    return FALSE;
815
816
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
817
0
    return FALSE;
818
819
0
  handle = (WINPR_HANDLE*)hFile;
820
821
0
  if (handle->ops->UnlockFile)
822
0
    return handle->ops->UnlockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
823
0
                                   nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
824
825
0
  WLog_ERR(TAG, "UnLockFile operation not implemented");
826
0
  return FALSE;
827
0
}
828
829
BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow,
830
                  DWORD nNumberOfBytesToUnlockHigh, LPOVERLAPPED lpOverlapped)
831
0
{
832
0
  ULONG Type = 0;
833
0
  WINPR_HANDLE* handle = NULL;
834
835
0
  if (hFile == INVALID_HANDLE_VALUE)
836
0
    return FALSE;
837
838
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
839
0
    return FALSE;
840
841
0
  handle = (WINPR_HANDLE*)hFile;
842
843
0
  if (handle->ops->UnlockFileEx)
844
0
    return handle->ops->UnlockFileEx(handle, dwReserved, nNumberOfBytesToUnlockLow,
845
0
                                     nNumberOfBytesToUnlockHigh, lpOverlapped);
846
847
0
  WLog_ERR(TAG, "UnLockFileEx operation not implemented");
848
0
  return FALSE;
849
0
}
850
851
BOOL WINAPI SetFileTime(HANDLE hFile, const FILETIME* lpCreationTime,
852
                        const FILETIME* lpLastAccessTime, const FILETIME* lpLastWriteTime)
853
0
{
854
0
  ULONG Type = 0;
855
0
  WINPR_HANDLE* handle = NULL;
856
857
0
  if (hFile == INVALID_HANDLE_VALUE)
858
0
    return FALSE;
859
860
0
  if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
861
0
    return FALSE;
862
863
0
  handle = (WINPR_HANDLE*)hFile;
864
865
0
  if (handle->ops->SetFileTime)
866
0
    return handle->ops->SetFileTime(handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
867
868
0
  WLog_ERR(TAG, "operation not implemented");
869
0
  return FALSE;
870
0
}
871
872
typedef struct
873
{
874
  char magic[16];
875
  LPSTR lpPath;
876
  LPSTR lpPattern;
877
  DIR* pDir;
878
} WIN32_FILE_SEARCH;
879
880
static const char file_search_magic[] = "file_srch_magic";
881
882
static WIN32_FILE_SEARCH* file_search_new(const char* name, size_t namelen, const char* pattern,
883
                                          size_t patternlen)
884
0
{
885
0
  WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)calloc(1, sizeof(WIN32_FILE_SEARCH));
886
0
  if (!pFileSearch)
887
0
    return NULL;
888
0
  strncpy(pFileSearch->magic, file_search_magic, sizeof(pFileSearch->magic) - 1);
889
890
0
  pFileSearch->lpPath = strndup(name, namelen);
891
0
  pFileSearch->lpPattern = strndup(pattern, patternlen);
892
0
  if (!pFileSearch->lpPath || !pFileSearch->lpPattern)
893
0
    goto fail;
894
895
0
  pFileSearch->pDir = opendir(pFileSearch->lpPath);
896
0
  if (!pFileSearch->pDir)
897
0
  {
898
    /* Work around for android:
899
     * parent directories are not accessible, so if we have a directory without pattern
900
     * try to open it directly and set pattern to '*'
901
     */
902
0
    struct stat fileStat = { 0 };
903
0
    if (stat(name, &fileStat) == 0)
904
0
    {
905
0
      if (S_ISDIR(fileStat.st_mode))
906
0
      {
907
0
        pFileSearch->pDir = opendir(name);
908
0
        if (pFileSearch->pDir)
909
0
        {
910
0
          free(pFileSearch->lpPath);
911
0
          free(pFileSearch->lpPattern);
912
0
          pFileSearch->lpPath = _strdup(name);
913
0
          pFileSearch->lpPattern = _strdup("*");
914
0
          if (!pFileSearch->lpPath || !pFileSearch->lpPattern)
915
0
          {
916
0
            closedir(pFileSearch->pDir);
917
0
            pFileSearch->pDir = NULL;
918
0
          }
919
0
        }
920
0
      }
921
0
    }
922
0
  }
923
0
  if (!pFileSearch->pDir)
924
0
    goto fail;
925
926
0
  return pFileSearch;
927
0
fail:
928
0
  WINPR_PRAGMA_DIAG_PUSH
929
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
930
0
  FindClose(pFileSearch);
931
0
  WINPR_PRAGMA_DIAG_POP
932
0
  return NULL;
933
0
}
934
935
static BOOL is_valid_file_search_handle(HANDLE handle)
936
0
{
937
0
  WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)handle;
938
0
  if (!pFileSearch)
939
0
    return FALSE;
940
0
  if (pFileSearch == INVALID_HANDLE_VALUE)
941
0
    return FALSE;
942
0
  if (strcmp(file_search_magic, pFileSearch->magic) != 0)
943
0
    return FALSE;
944
0
  return TRUE;
945
0
}
946
static BOOL FindDataFromStat(const char* path, const struct stat* fileStat,
947
                             LPWIN32_FIND_DATAA lpFindFileData)
948
0
{
949
0
  UINT64 ft = 0;
950
0
  char* lastSep = NULL;
951
0
  lpFindFileData->dwFileAttributes = 0;
952
953
0
  if (S_ISDIR(fileStat->st_mode))
954
0
    lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
955
956
0
  if (lpFindFileData->dwFileAttributes == 0)
957
0
    lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
958
959
0
  lastSep = strrchr(path, '/');
960
961
0
  if (lastSep)
962
0
  {
963
0
    const char* name = lastSep + 1;
964
0
    const size_t namelen = strlen(name);
965
966
0
    if ((namelen > 1) && (name[0] == '.') && (name[1] != '.'))
967
0
      lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_HIDDEN;
968
0
  }
969
970
0
  if (!(fileStat->st_mode & S_IWUSR))
971
0
    lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
972
973
#ifdef _DARWIN_FEATURE_64_BIT_INODE
974
  ft = STAT_TIME_TO_FILETIME(fileStat->st_birthtime);
975
#else
976
0
  ft = STAT_TIME_TO_FILETIME(fileStat->st_ctime);
977
0
#endif
978
0
  lpFindFileData->ftCreationTime.dwHighDateTime = ((UINT64)ft) >> 32ULL;
979
0
  lpFindFileData->ftCreationTime.dwLowDateTime = ft & 0xFFFFFFFF;
980
0
  ft = STAT_TIME_TO_FILETIME(fileStat->st_mtime);
981
0
  lpFindFileData->ftLastWriteTime.dwHighDateTime = ((UINT64)ft) >> 32ULL;
982
0
  lpFindFileData->ftLastWriteTime.dwLowDateTime = ft & 0xFFFFFFFF;
983
0
  ft = STAT_TIME_TO_FILETIME(fileStat->st_atime);
984
0
  lpFindFileData->ftLastAccessTime.dwHighDateTime = ((UINT64)ft) >> 32ULL;
985
0
  lpFindFileData->ftLastAccessTime.dwLowDateTime = ft & 0xFFFFFFFF;
986
0
  lpFindFileData->nFileSizeHigh = ((UINT64)fileStat->st_size) >> 32ULL;
987
0
  lpFindFileData->nFileSizeLow = fileStat->st_size & 0xFFFFFFFF;
988
0
  return TRUE;
989
0
}
990
991
HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
992
0
{
993
0
  if (!lpFindFileData || !lpFileName)
994
0
  {
995
0
    SetLastError(ERROR_BAD_ARGUMENTS);
996
0
    return INVALID_HANDLE_VALUE;
997
0
  }
998
999
0
  const WIN32_FIND_DATAA empty = { 0 };
1000
0
  *lpFindFileData = empty;
1001
1002
0
  WIN32_FILE_SEARCH* pFileSearch = NULL;
1003
0
  size_t patternlen = 0;
1004
0
  const size_t flen = strlen(lpFileName);
1005
0
  const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE);
1006
0
  const char* ptr = strrchr(lpFileName, sep);
1007
0
  if (!ptr)
1008
0
    goto fail;
1009
0
  patternlen = strlen(ptr + 1);
1010
0
  if (patternlen == 0)
1011
0
    goto fail;
1012
1013
0
  pFileSearch = file_search_new(lpFileName, flen - patternlen, ptr + 1, patternlen);
1014
1015
0
  if (!pFileSearch)
1016
0
  {
1017
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1018
0
    return INVALID_HANDLE_VALUE;
1019
0
  }
1020
1021
0
  if (FindNextFileA((HANDLE)pFileSearch, lpFindFileData))
1022
0
    return (HANDLE)pFileSearch;
1023
1024
0
fail:
1025
0
  FindClose(pFileSearch);
1026
0
  return INVALID_HANDLE_VALUE;
1027
0
}
1028
1029
static BOOL ConvertFindDataAToW(LPWIN32_FIND_DATAA lpFindFileDataA,
1030
                                LPWIN32_FIND_DATAW lpFindFileDataW)
1031
0
{
1032
0
  if (!lpFindFileDataA || !lpFindFileDataW)
1033
0
    return FALSE;
1034
1035
0
  lpFindFileDataW->dwFileAttributes = lpFindFileDataA->dwFileAttributes;
1036
0
  lpFindFileDataW->ftCreationTime = lpFindFileDataA->ftCreationTime;
1037
0
  lpFindFileDataW->ftLastAccessTime = lpFindFileDataA->ftLastAccessTime;
1038
0
  lpFindFileDataW->ftLastWriteTime = lpFindFileDataA->ftLastWriteTime;
1039
0
  lpFindFileDataW->nFileSizeHigh = lpFindFileDataA->nFileSizeHigh;
1040
0
  lpFindFileDataW->nFileSizeLow = lpFindFileDataA->nFileSizeLow;
1041
0
  lpFindFileDataW->dwReserved0 = lpFindFileDataA->dwReserved0;
1042
0
  lpFindFileDataW->dwReserved1 = lpFindFileDataA->dwReserved1;
1043
1044
0
  if (ConvertUtf8NToWChar(lpFindFileDataA->cFileName, ARRAYSIZE(lpFindFileDataA->cFileName),
1045
0
                          lpFindFileDataW->cFileName, ARRAYSIZE(lpFindFileDataW->cFileName)) < 0)
1046
0
    return FALSE;
1047
1048
0
  return ConvertUtf8NToWChar(lpFindFileDataA->cAlternateFileName,
1049
0
                             ARRAYSIZE(lpFindFileDataA->cAlternateFileName),
1050
0
                             lpFindFileDataW->cAlternateFileName,
1051
0
                             ARRAYSIZE(lpFindFileDataW->cAlternateFileName)) >= 0;
1052
0
}
1053
1054
HANDLE FindFirstFileW(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData)
1055
0
{
1056
0
  LPSTR utfFileName = NULL;
1057
0
  HANDLE h = NULL;
1058
0
  if (!lpFileName)
1059
0
    return FALSE;
1060
0
  LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)calloc(1, sizeof(WIN32_FIND_DATAA));
1061
1062
0
  if (!fd)
1063
0
  {
1064
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1065
0
    return INVALID_HANDLE_VALUE;
1066
0
  }
1067
1068
0
  utfFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
1069
0
  if (!utfFileName)
1070
0
  {
1071
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1072
0
    free(fd);
1073
0
    return INVALID_HANDLE_VALUE;
1074
0
  }
1075
1076
0
  h = FindFirstFileA(utfFileName, fd);
1077
0
  free(utfFileName);
1078
1079
0
  if (h != INVALID_HANDLE_VALUE)
1080
0
  {
1081
0
    if (!ConvertFindDataAToW(fd, lpFindFileData))
1082
0
    {
1083
0
      SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1084
0
      FindClose(h);
1085
0
      h = INVALID_HANDLE_VALUE;
1086
0
      goto out;
1087
0
    }
1088
0
  }
1089
1090
0
out:
1091
0
  free(fd);
1092
0
  return h;
1093
0
}
1094
1095
HANDLE FindFirstFileExA(LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
1096
                        FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1097
0
{
1098
0
  return INVALID_HANDLE_VALUE;
1099
0
}
1100
1101
HANDLE FindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
1102
                        FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
1103
0
{
1104
0
  return INVALID_HANDLE_VALUE;
1105
0
}
1106
1107
BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
1108
0
{
1109
0
  if (!lpFindFileData)
1110
0
    return FALSE;
1111
1112
0
  const WIN32_FIND_DATAA empty = { 0 };
1113
0
  *lpFindFileData = empty;
1114
1115
0
  if (!is_valid_file_search_handle(hFindFile))
1116
0
    return FALSE;
1117
1118
0
  WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
1119
0
  struct dirent* pDirent = NULL;
1120
0
  while ((pDirent = readdir(pFileSearch->pDir)) != NULL)
1121
0
  {
1122
0
    if (FilePatternMatchA(pDirent->d_name, pFileSearch->lpPattern))
1123
0
    {
1124
0
      BOOL success = FALSE;
1125
1126
0
      strncpy(lpFindFileData->cFileName, pDirent->d_name, MAX_PATH);
1127
0
      const size_t namelen = strnlen(lpFindFileData->cFileName, MAX_PATH);
1128
0
      size_t pathlen = strlen(pFileSearch->lpPath);
1129
0
      char* fullpath = (char*)malloc(pathlen + namelen + 2);
1130
1131
0
      if (fullpath == NULL)
1132
0
      {
1133
0
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1134
0
        return FALSE;
1135
0
      }
1136
1137
0
      memcpy(fullpath, pFileSearch->lpPath, pathlen);
1138
      /* Ensure path is terminated with a separator, but prevent
1139
       * duplicate separators */
1140
0
      if (fullpath[pathlen - 1] != '/')
1141
0
        fullpath[pathlen++] = '/';
1142
0
      memcpy(fullpath + pathlen, pDirent->d_name, namelen);
1143
0
      fullpath[pathlen + namelen] = 0;
1144
1145
0
      struct stat fileStat = { 0 };
1146
0
      if (stat(fullpath, &fileStat) != 0)
1147
0
      {
1148
0
        free(fullpath);
1149
0
        SetLastError(map_posix_err(errno));
1150
0
        errno = 0;
1151
0
        continue;
1152
0
      }
1153
1154
      /* Skip FIFO entries. */
1155
0
      if (S_ISFIFO(fileStat.st_mode))
1156
0
      {
1157
0
        free(fullpath);
1158
0
        continue;
1159
0
      }
1160
1161
0
      success = FindDataFromStat(fullpath, &fileStat, lpFindFileData);
1162
0
      free(fullpath);
1163
0
      return success;
1164
0
    }
1165
0
  }
1166
1167
0
  SetLastError(ERROR_NO_MORE_FILES);
1168
0
  return FALSE;
1169
0
}
1170
1171
BOOL FindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData)
1172
0
{
1173
0
  LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)calloc(1, sizeof(WIN32_FIND_DATAA));
1174
1175
0
  if (!fd)
1176
0
  {
1177
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1178
0
    return FALSE;
1179
0
  }
1180
1181
0
  if (FindNextFileA(hFindFile, fd))
1182
0
  {
1183
0
    if (!ConvertFindDataAToW(fd, lpFindFileData))
1184
0
    {
1185
0
      SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1186
0
      free(fd);
1187
0
      return FALSE;
1188
0
    }
1189
1190
0
    free(fd);
1191
0
    return TRUE;
1192
0
  }
1193
1194
0
  free(fd);
1195
0
  return FALSE;
1196
0
}
1197
1198
BOOL FindClose(HANDLE hFindFile)
1199
0
{
1200
0
  WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
1201
0
  if (!pFileSearch)
1202
0
    return FALSE;
1203
1204
    /* Since INVALID_HANDLE_VALUE != NULL the analyzer guesses that there
1205
     * is a initialized HANDLE that is not freed properly.
1206
     * Disable this return to stop confusing the analyzer. */
1207
0
#ifndef __clang_analyzer__
1208
0
  if (!is_valid_file_search_handle(hFindFile))
1209
0
    return FALSE;
1210
0
#endif
1211
1212
0
  free(pFileSearch->lpPath);
1213
0
  free(pFileSearch->lpPattern);
1214
1215
0
  if (pFileSearch->pDir)
1216
0
    closedir(pFileSearch->pDir);
1217
1218
0
  free(pFileSearch);
1219
0
  return TRUE;
1220
0
}
1221
1222
BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1223
0
{
1224
0
  if (!mkdir(lpPathName, S_IRUSR | S_IWUSR | S_IXUSR))
1225
0
    return TRUE;
1226
1227
0
  return FALSE;
1228
0
}
1229
1230
BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1231
0
{
1232
0
  if (!lpPathName)
1233
0
    return FALSE;
1234
0
  char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL);
1235
0
  BOOL ret = FALSE;
1236
1237
0
  if (!utfPathName)
1238
0
  {
1239
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1240
0
    goto fail;
1241
0
  }
1242
1243
0
  ret = CreateDirectoryA(utfPathName, lpSecurityAttributes);
1244
0
fail:
1245
0
  free(utfPathName);
1246
0
  return ret;
1247
0
}
1248
1249
BOOL RemoveDirectoryA(LPCSTR lpPathName)
1250
0
{
1251
0
  int ret = rmdir(lpPathName);
1252
1253
0
  if (ret != 0)
1254
0
    SetLastError(map_posix_err(errno));
1255
0
  else
1256
0
    SetLastError(STATUS_SUCCESS);
1257
1258
0
  return ret == 0;
1259
0
}
1260
1261
BOOL RemoveDirectoryW(LPCWSTR lpPathName)
1262
0
{
1263
0
  if (!lpPathName)
1264
0
    return FALSE;
1265
0
  char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL);
1266
0
  BOOL ret = FALSE;
1267
1268
0
  if (!utfPathName)
1269
0
  {
1270
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1271
0
    goto fail;
1272
0
  }
1273
1274
0
  ret = RemoveDirectoryA(utfPathName);
1275
0
fail:
1276
0
  free(utfPathName);
1277
0
  return ret;
1278
0
}
1279
1280
BOOL MoveFileExA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
1281
0
{
1282
0
  struct stat st;
1283
0
  int ret = 0;
1284
0
  ret = stat(lpNewFileName, &st);
1285
1286
0
  if ((dwFlags & MOVEFILE_REPLACE_EXISTING) == 0)
1287
0
  {
1288
0
    if (ret == 0)
1289
0
    {
1290
0
      SetLastError(ERROR_ALREADY_EXISTS);
1291
0
      return FALSE;
1292
0
    }
1293
0
  }
1294
0
  else
1295
0
  {
1296
0
    if (ret == 0 && (st.st_mode & S_IWUSR) == 0)
1297
0
    {
1298
0
      SetLastError(ERROR_ACCESS_DENIED);
1299
0
      return FALSE;
1300
0
    }
1301
0
  }
1302
1303
0
  ret = rename(lpExistingFileName, lpNewFileName);
1304
1305
0
  if (ret != 0)
1306
0
    SetLastError(map_posix_err(errno));
1307
1308
0
  return ret == 0;
1309
0
}
1310
1311
BOOL MoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags)
1312
0
{
1313
0
  if (!lpExistingFileName || !lpNewFileName)
1314
0
    return FALSE;
1315
1316
0
  LPSTR lpCExistingFileName = ConvertWCharToUtf8Alloc(lpExistingFileName, NULL);
1317
0
  LPSTR lpCNewFileName = ConvertWCharToUtf8Alloc(lpNewFileName, NULL);
1318
0
  BOOL ret = FALSE;
1319
1320
0
  if (!lpCExistingFileName || !lpCNewFileName)
1321
0
  {
1322
0
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1323
0
    goto fail;
1324
0
  }
1325
1326
0
  ret = MoveFileExA(lpCExistingFileName, lpCNewFileName, dwFlags);
1327
0
fail:
1328
0
  free(lpCNewFileName);
1329
0
  free(lpCExistingFileName);
1330
0
  return ret;
1331
0
}
1332
1333
BOOL MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
1334
0
{
1335
0
  return MoveFileExA(lpExistingFileName, lpNewFileName, 0);
1336
0
}
1337
1338
BOOL MoveFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
1339
0
{
1340
0
  return MoveFileExW(lpExistingFileName, lpNewFileName, 0);
1341
0
}
1342
1343
#endif
1344
1345
/* Extended API */
1346
1347
int UnixChangeFileMode(const char* filename, int flags)
1348
0
{
1349
0
  if (!filename)
1350
0
    return -1;
1351
0
#ifndef _WIN32
1352
0
  mode_t fl = 0;
1353
0
  fl |= (flags & 0x4000) ? S_ISUID : 0;
1354
0
  fl |= (flags & 0x2000) ? S_ISGID : 0;
1355
0
  fl |= (flags & 0x1000) ? S_ISVTX : 0;
1356
0
  fl |= (flags & 0x0400) ? S_IRUSR : 0;
1357
0
  fl |= (flags & 0x0200) ? S_IWUSR : 0;
1358
0
  fl |= (flags & 0x0100) ? S_IXUSR : 0;
1359
0
  fl |= (flags & 0x0040) ? S_IRGRP : 0;
1360
0
  fl |= (flags & 0x0020) ? S_IWGRP : 0;
1361
0
  fl |= (flags & 0x0010) ? S_IXGRP : 0;
1362
0
  fl |= (flags & 0x0004) ? S_IROTH : 0;
1363
0
  fl |= (flags & 0x0002) ? S_IWOTH : 0;
1364
0
  fl |= (flags & 0x0001) ? S_IXOTH : 0;
1365
0
  return chmod(filename, fl);
1366
#else
1367
  int rc;
1368
  WCHAR* wfl = ConvertUtf8ToWCharAlloc(filename, NULL);
1369
1370
  if (!wfl)
1371
    return -1;
1372
1373
  /* Check for unsupported flags. */
1374
  if (flags & ~(_S_IREAD | _S_IWRITE))
1375
    WLog_WARN(TAG, "Unsupported file mode %d for _wchmod", flags);
1376
1377
  rc = _wchmod(wfl, flags);
1378
  free(wfl);
1379
  return rc;
1380
#endif
1381
0
}