22#include <winpr/config.h>
25#include <winpr/wlog.h>
26#include <winpr/string.h>
27#include <winpr/path.h>
28#include <winpr/file.h>
30#ifdef WINPR_HAVE_UNISTD_H
34#ifdef WINPR_HAVE_FCNTL_H
39#define TAG WINPR_TAG("file")
45#include <winpr/assert.h>
53#include <sys/socket.h>
55#ifdef WINPR_HAVE_AIO_H
56#undef WINPR_HAVE_AIO_H
59#ifdef WINPR_HAVE_AIO_H
66#include <sys/statvfs.h>
69#include "../handle/handle.h"
71#include "../pipe/pipe.h"
178static wArrayList* HandleCreators;
180static pthread_once_t HandleCreatorsInitialized = PTHREAD_ONCE_INIT;
182#include "../comm/comm.h"
183#include "namedPipeClient.h"
185static void HandleCreatorsInit(
void)
187 WINPR_ASSERT(HandleCreators == NULL);
188 HandleCreators = ArrayList_New(TRUE);
196 ArrayList_Append(HandleCreators, GetNamedPipeClientHandleCreator());
199 ArrayList_Append(HandleCreators, serial);
200 ArrayList_Append(HandleCreators, GetFileHandleCreator());
203#ifdef WINPR_HAVE_AIO_H
205static BOOL g_AioSignalHandlerInstalled = FALSE;
207void AioSignalHandler(
int signum, siginfo_t* siginfo,
void* arg)
209 WLog_INFO(
"%d", signum);
212int InstallAioSignalHandler()
214 if (!g_AioSignalHandlerInstalled)
216 struct sigaction action;
217 sigemptyset(&action.sa_mask);
218 sigaddset(&action.sa_mask, SIGIO);
219 action.sa_flags = SA_SIGINFO;
220 action.sa_sigaction = (
void*)&AioSignalHandler;
221 sigaction(SIGIO, &action, NULL);
222 g_AioSignalHandlerInstalled = TRUE;
230HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
231 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
232 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
234 return winpr_CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
235 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
238HANDLE winpr_CreateFile(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
239 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
240 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
243 return INVALID_HANDLE_VALUE;
245 if (pthread_once(&HandleCreatorsInitialized, HandleCreatorsInit) != 0)
247 SetLastError(ERROR_DLL_INIT_FAILED);
248 return INVALID_HANDLE_VALUE;
251 if (HandleCreators == NULL)
253 SetLastError(ERROR_DLL_INIT_FAILED);
254 return INVALID_HANDLE_VALUE;
257 ArrayList_Lock(HandleCreators);
259 for (
size_t i = 0; i <= ArrayList_Count(HandleCreators); i++)
261 const HANDLE_CREATOR* creator = ArrayList_GetItem(HandleCreators, i);
263 if (creator && creator->IsHandled(lpFileName))
266 creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
267 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
268 ArrayList_Unlock(HandleCreators);
273 ArrayList_Unlock(HandleCreators);
274 return INVALID_HANDLE_VALUE;
277HANDLE CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
278 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
279 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
284 char* lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL);
288 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
292 hdl = winpr_CreateFile(lpFileNameA, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
293 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
299BOOL DeleteFileA(LPCSTR lpFileName)
301 return winpr_DeleteFile(lpFileName);
304BOOL DeleteFileW(LPCWSTR lpFileName)
308 LPSTR lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL);
309 BOOL rc = winpr_DeleteFile(lpFileNameA);
314BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
320 if (hFile == INVALID_HANDLE_VALUE)
328 if (!lpNumberOfBytesRead && !lpOverlapped)
331 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
336 if (handle->ops->ReadFile)
337 return handle->ops->ReadFile(handle, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead,
340 WLog_ERR(TAG,
"ReadFile operation not implemented");
344BOOL ReadFileEx(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
345 LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
350 if (hFile == INVALID_HANDLE_VALUE)
353 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
358 if (handle->ops->ReadFileEx)
359 return handle->ops->ReadFileEx(handle, lpBuffer, nNumberOfBytesToRead, lpOverlapped,
360 lpCompletionRoutine);
362 WLog_ERR(TAG,
"ReadFileEx operation not implemented");
366BOOL ReadFileScatter(HANDLE hFile,
FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToRead,
372 if (hFile == INVALID_HANDLE_VALUE)
375 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
380 if (handle->ops->ReadFileScatter)
381 return handle->ops->ReadFileScatter(handle, aSegmentArray, nNumberOfBytesToRead, lpReserved,
384 WLog_ERR(TAG,
"ReadFileScatter operation not implemented");
388BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
389 LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped)
394 if (hFile == INVALID_HANDLE_VALUE)
397 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
402 if (handle->ops->WriteFile)
403 return handle->ops->WriteFile(handle, lpBuffer, nNumberOfBytesToWrite,
404 lpNumberOfBytesWritten, lpOverlapped);
406 WLog_ERR(TAG,
"WriteFile operation not implemented");
410BOOL WriteFileEx(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
411 LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
416 if (hFile == INVALID_HANDLE_VALUE)
419 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
424 if (handle->ops->WriteFileEx)
425 return handle->ops->WriteFileEx(handle, lpBuffer, nNumberOfBytesToWrite, lpOverlapped,
426 lpCompletionRoutine);
428 WLog_ERR(TAG,
"WriteFileEx operation not implemented");
433 DWORD nNumberOfBytesToWrite, LPDWORD lpReserved,
LPOVERLAPPED lpOverlapped)
438 if (hFile == INVALID_HANDLE_VALUE)
441 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
446 if (handle->ops->WriteFileGather)
447 return handle->ops->WriteFileGather(handle, aSegmentArray, nNumberOfBytesToWrite,
448 lpReserved, lpOverlapped);
450 WLog_ERR(TAG,
"WriteFileGather operation not implemented");
454BOOL FlushFileBuffers(HANDLE hFile)
459 if (hFile == INVALID_HANDLE_VALUE)
462 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
467 if (handle->ops->FlushFileBuffers)
468 return handle->ops->FlushFileBuffers(handle);
470 WLog_ERR(TAG,
"FlushFileBuffers operation not implemented");
474BOOL WINAPI GetFileAttributesExA(LPCSTR lpFileName,
475 WINPR_ATTR_UNUSED GET_FILEEX_INFO_LEVELS fInfoLevelId,
476 LPVOID lpFileInformation)
484 HANDLE hFind = FindFirstFileA(lpFileName, &findFileData);
485 if (hFind == INVALID_HANDLE_VALUE)
489 fd->dwFileAttributes = findFileData.dwFileAttributes;
490 fd->ftCreationTime = findFileData.ftCreationTime;
491 fd->ftLastAccessTime = findFileData.ftLastAccessTime;
492 fd->ftLastWriteTime = findFileData.ftLastWriteTime;
493 fd->nFileSizeHigh = findFileData.nFileSizeHigh;
494 fd->nFileSizeLow = findFileData.nFileSizeLow;
498BOOL WINAPI GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
499 LPVOID lpFileInformation)
504 LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
508 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
512 ret = GetFileAttributesExA(lpCFileName, fInfoLevelId, lpFileInformation);
517DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName)
520 HANDLE hFind = FindFirstFileA(lpFileName, &findFileData);
522 if (hFind == INVALID_HANDLE_VALUE)
523 return INVALID_FILE_ATTRIBUTES;
526 return findFileData.dwFileAttributes;
529DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
534 LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
537 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
541 ret = GetFileAttributesA(lpCFileName);
551 if (hFile == INVALID_HANDLE_VALUE)
554 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
559 if (handle->ops->GetFileInformationByHandle)
560 return handle->ops->GetFileInformationByHandle(handle, lpFileInformation);
562 WLog_ERR(TAG,
"GetFileInformationByHandle operation not implemented");
566static char* append(
char* buffer,
size_t size,
const char* append)
568 winpr_str_append(append, buffer, size,
"|");
572static const char* flagsToStr(
char* buffer,
size_t size, DWORD flags)
574 char strflags[32] = { 0 };
575 if (flags & FILE_ATTRIBUTE_READONLY)
576 append(buffer, size,
"FILE_ATTRIBUTE_READONLY");
577 if (flags & FILE_ATTRIBUTE_HIDDEN)
578 append(buffer, size,
"FILE_ATTRIBUTE_HIDDEN");
579 if (flags & FILE_ATTRIBUTE_SYSTEM)
580 append(buffer, size,
"FILE_ATTRIBUTE_SYSTEM");
581 if (flags & FILE_ATTRIBUTE_DIRECTORY)
582 append(buffer, size,
"FILE_ATTRIBUTE_DIRECTORY");
583 if (flags & FILE_ATTRIBUTE_ARCHIVE)
584 append(buffer, size,
"FILE_ATTRIBUTE_ARCHIVE");
585 if (flags & FILE_ATTRIBUTE_DEVICE)
586 append(buffer, size,
"FILE_ATTRIBUTE_DEVICE");
587 if (flags & FILE_ATTRIBUTE_NORMAL)
588 append(buffer, size,
"FILE_ATTRIBUTE_NORMAL");
589 if (flags & FILE_ATTRIBUTE_TEMPORARY)
590 append(buffer, size,
"FILE_ATTRIBUTE_TEMPORARY");
591 if (flags & FILE_ATTRIBUTE_SPARSE_FILE)
592 append(buffer, size,
"FILE_ATTRIBUTE_SPARSE_FILE");
593 if (flags & FILE_ATTRIBUTE_REPARSE_POINT)
594 append(buffer, size,
"FILE_ATTRIBUTE_REPARSE_POINT");
595 if (flags & FILE_ATTRIBUTE_COMPRESSED)
596 append(buffer, size,
"FILE_ATTRIBUTE_COMPRESSED");
597 if (flags & FILE_ATTRIBUTE_OFFLINE)
598 append(buffer, size,
"FILE_ATTRIBUTE_OFFLINE");
599 if (flags & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
600 append(buffer, size,
"FILE_ATTRIBUTE_NOT_CONTENT_INDEXED");
601 if (flags & FILE_ATTRIBUTE_ENCRYPTED)
602 append(buffer, size,
"FILE_ATTRIBUTE_ENCRYPTED");
603 if (flags & FILE_ATTRIBUTE_VIRTUAL)
604 append(buffer, size,
"FILE_ATTRIBUTE_VIRTUAL");
606 (void)_snprintf(strflags,
sizeof(strflags),
" [0x%08" PRIx32
"]", flags);
607 winpr_str_append(strflags, buffer, size, NULL);
611BOOL SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes)
614#ifdef WINPR_HAVE_FCNTL_H
615 const uint32_t mask = ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_NORMAL);
616 if (dwFileAttributes & mask)
618 char buffer[8192] = { 0 };
619 const char* flags = flagsToStr(buffer,
sizeof(buffer), dwFileAttributes & mask);
620 WLog_WARN(TAG,
"Unsupported flags %s, ignoring!", flags);
623 int fd = open(lpFileName, O_RDONLY);
627 struct stat st = { 0 };
628 if (fstat(fd, &st) != 0)
631 if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
633 st.st_mode &= WINPR_ASSERTING_INT_CAST(mode_t, (mode_t)(~(S_IWUSR | S_IWGRP | S_IWOTH)));
637 st.st_mode |= S_IWUSR;
640 if (fchmod(fd, st.st_mode) != 0)
650BOOL SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
657 char* lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
660 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
664 ret = SetFileAttributesA(lpCFileName, dwFileAttributes);
669BOOL SetEndOfFile(HANDLE hFile)
674 if (hFile == INVALID_HANDLE_VALUE)
677 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
682 if (handle->ops->SetEndOfFile)
683 return handle->ops->SetEndOfFile(handle);
685 WLog_ERR(TAG,
"SetEndOfFile operation not implemented");
689DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
694 if (hFile == INVALID_HANDLE_VALUE)
697 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
702 if (handle->ops->GetFileSize)
703 return handle->ops->GetFileSize(handle, lpFileSizeHigh);
705 WLog_ERR(TAG,
"GetFileSize operation not implemented");
709DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh,
715 if (hFile == INVALID_HANDLE_VALUE)
718 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
723 if (handle->ops->SetFilePointer)
724 return handle->ops->SetFilePointer(handle, lDistanceToMove, lpDistanceToMoveHigh,
727 WLog_ERR(TAG,
"SetFilePointer operation not implemented");
731BOOL SetFilePointerEx(HANDLE hFile,
LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer,
737 if (hFile == INVALID_HANDLE_VALUE)
740 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
745 if (handle->ops->SetFilePointerEx)
746 return handle->ops->SetFilePointerEx(handle, liDistanceToMove, lpNewFilePointer,
749 WLog_ERR(TAG,
"SetFilePointerEx operation not implemented");
753BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
754 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
759 if (hFile == INVALID_HANDLE_VALUE)
762 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
767 if (handle->ops->LockFile)
768 return handle->ops->LockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
769 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
771 WLog_ERR(TAG,
"LockFile operation not implemented");
775BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved, DWORD nNumberOfBytesToLockLow,
776 DWORD nNumberOfBytesToLockHigh,
LPOVERLAPPED lpOverlapped)
781 if (hFile == INVALID_HANDLE_VALUE)
784 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
789 if (handle->ops->LockFileEx)
790 return handle->ops->LockFileEx(handle, dwFlags, dwReserved, nNumberOfBytesToLockLow,
791 nNumberOfBytesToLockHigh, lpOverlapped);
793 WLog_ERR(TAG,
"LockFileEx operation not implemented");
797BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
798 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
803 if (hFile == INVALID_HANDLE_VALUE)
806 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
811 if (handle->ops->UnlockFile)
812 return handle->ops->UnlockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
813 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
815 WLog_ERR(TAG,
"UnLockFile operation not implemented");
819BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow,
820 DWORD nNumberOfBytesToUnlockHigh,
LPOVERLAPPED lpOverlapped)
825 if (hFile == INVALID_HANDLE_VALUE)
828 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
833 if (handle->ops->UnlockFileEx)
834 return handle->ops->UnlockFileEx(handle, dwReserved, nNumberOfBytesToUnlockLow,
835 nNumberOfBytesToUnlockHigh, lpOverlapped);
837 WLog_ERR(TAG,
"UnLockFileEx operation not implemented");
841BOOL WINAPI SetFileTime(HANDLE hFile,
const FILETIME* lpCreationTime,
847 if (hFile == INVALID_HANDLE_VALUE)
850 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
855 if (handle->ops->SetFileTime)
856 return handle->ops->SetFileTime(handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
858 WLog_ERR(TAG,
"operation not implemented");
870static const char file_search_magic[] =
"file_srch_magic";
872WINPR_ATTR_MALLOC(FindClose, 1)
873static WIN32_FILE_SEARCH* file_search_new(const
char* name,
size_t namelen, const
char* pattern,
876 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)calloc(1,
sizeof(WIN32_FILE_SEARCH));
879 WINPR_ASSERT(
sizeof(file_search_magic) ==
sizeof(pFileSearch->magic));
880 memcpy(pFileSearch->magic, file_search_magic,
sizeof(pFileSearch->magic));
882 pFileSearch->lpPath = strndup(name, namelen);
883 pFileSearch->lpPattern = strndup(pattern, patternlen);
884 if (!pFileSearch->lpPath || !pFileSearch->lpPattern)
887 pFileSearch->pDir = opendir(pFileSearch->lpPath);
888 if (!pFileSearch->pDir)
894 struct stat fileStat = { 0 };
895 if (stat(name, &fileStat) == 0)
897 if (S_ISDIR(fileStat.st_mode))
899 pFileSearch->pDir = opendir(name);
900 if (pFileSearch->pDir)
902 free(pFileSearch->lpPath);
903 free(pFileSearch->lpPattern);
904 pFileSearch->lpPath = _strdup(name);
905 pFileSearch->lpPattern = _strdup(
"*");
906 if (!pFileSearch->lpPath || !pFileSearch->lpPattern)
908 closedir(pFileSearch->pDir);
909 pFileSearch->pDir = NULL;
915 if (!pFileSearch->pDir)
920 WINPR_PRAGMA_DIAG_PUSH
921 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
922 FindClose(pFileSearch);
923 WINPR_PRAGMA_DIAG_POP
927static BOOL is_valid_file_search_handle(HANDLE handle)
929 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)handle;
932 if (pFileSearch == INVALID_HANDLE_VALUE)
934 if (strncmp(file_search_magic, pFileSearch->magic,
sizeof(file_search_magic)) != 0)
938static BOOL FindDataFromStat(
const char* path,
const struct stat* fileStat,
942 char* lastSep = NULL;
943 lpFindFileData->dwFileAttributes = 0;
945 if (S_ISDIR(fileStat->st_mode))
946 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
948 if (lpFindFileData->dwFileAttributes == 0)
949 lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
951 lastSep = strrchr(path,
'/');
955 const char* name = lastSep + 1;
956 const size_t namelen = strlen(name);
958 if ((namelen > 1) && (name[0] ==
'.') && (name[1] !=
'.'))
959 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_HIDDEN;
962 if (!(fileStat->st_mode & S_IWUSR))
963 lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
965#ifdef _DARWIN_FEATURE_64_BIT_INODE
966 ft = STAT_TIME_TO_FILETIME(fileStat->st_birthtime);
968 ft = STAT_TIME_TO_FILETIME(fileStat->st_ctime);
970 lpFindFileData->ftCreationTime.dwHighDateTime = (ft) >> 32ULL;
971 lpFindFileData->ftCreationTime.dwLowDateTime = ft & 0xFFFFFFFF;
972 ft = STAT_TIME_TO_FILETIME(fileStat->st_mtime);
973 lpFindFileData->ftLastWriteTime.dwHighDateTime = (ft) >> 32ULL;
974 lpFindFileData->ftLastWriteTime.dwLowDateTime = ft & 0xFFFFFFFF;
975 ft = STAT_TIME_TO_FILETIME(fileStat->st_atime);
976 lpFindFileData->ftLastAccessTime.dwHighDateTime = (ft) >> 32ULL;
977 lpFindFileData->ftLastAccessTime.dwLowDateTime = ft & 0xFFFFFFFF;
978 lpFindFileData->nFileSizeHigh = ((UINT64)fileStat->st_size) >> 32ULL;
979 lpFindFileData->nFileSizeLow = fileStat->st_size & 0xFFFFFFFF;
985 if (!lpFindFileData || !lpFileName)
987 SetLastError(ERROR_BAD_ARGUMENTS);
988 return INVALID_HANDLE_VALUE;
992 *lpFindFileData = empty;
994 WIN32_FILE_SEARCH* pFileSearch = NULL;
995 size_t patternlen = 0;
996 const size_t flen = strlen(lpFileName);
997 const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE);
998 const char* ptr = strrchr(lpFileName, sep);
1001 patternlen = strlen(ptr + 1);
1002 if (patternlen == 0)
1005 pFileSearch = file_search_new(lpFileName, flen - patternlen, ptr + 1, patternlen);
1009 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1010 return INVALID_HANDLE_VALUE;
1013 if (FindNextFileA((HANDLE)pFileSearch, lpFindFileData))
1014 return (HANDLE)pFileSearch;
1017 FindClose(pFileSearch);
1018 return INVALID_HANDLE_VALUE;
1024 if (!lpFindFileDataA || !lpFindFileDataW)
1027 lpFindFileDataW->dwFileAttributes = lpFindFileDataA->dwFileAttributes;
1028 lpFindFileDataW->ftCreationTime = lpFindFileDataA->ftCreationTime;
1029 lpFindFileDataW->ftLastAccessTime = lpFindFileDataA->ftLastAccessTime;
1030 lpFindFileDataW->ftLastWriteTime = lpFindFileDataA->ftLastWriteTime;
1031 lpFindFileDataW->nFileSizeHigh = lpFindFileDataA->nFileSizeHigh;
1032 lpFindFileDataW->nFileSizeLow = lpFindFileDataA->nFileSizeLow;
1033 lpFindFileDataW->dwReserved0 = lpFindFileDataA->dwReserved0;
1034 lpFindFileDataW->dwReserved1 = lpFindFileDataA->dwReserved1;
1036 if (ConvertUtf8NToWChar(lpFindFileDataA->cFileName, ARRAYSIZE(lpFindFileDataA->cFileName),
1037 lpFindFileDataW->cFileName, ARRAYSIZE(lpFindFileDataW->cFileName)) < 0)
1040 return ConvertUtf8NToWChar(lpFindFileDataA->cAlternateFileName,
1041 ARRAYSIZE(lpFindFileDataA->cAlternateFileName),
1042 lpFindFileDataW->cAlternateFileName,
1043 ARRAYSIZE(lpFindFileDataW->cAlternateFileName)) >= 0;
1048 LPSTR utfFileName = NULL;
1051 return INVALID_HANDLE_VALUE;
1057 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1058 return INVALID_HANDLE_VALUE;
1061 utfFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
1064 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1066 return INVALID_HANDLE_VALUE;
1069 h = FindFirstFileA(utfFileName, fd);
1072 if (h != INVALID_HANDLE_VALUE)
1074 if (!ConvertFindDataAToW(fd, lpFindFileData))
1076 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1078 h = INVALID_HANDLE_VALUE;
1088HANDLE FindFirstFileExA(WINPR_ATTR_UNUSED LPCSTR lpFileName,
1089 WINPR_ATTR_UNUSED FINDEX_INFO_LEVELS fInfoLevelId,
1090 WINPR_ATTR_UNUSED LPVOID lpFindFileData,
1091 WINPR_ATTR_UNUSED FINDEX_SEARCH_OPS fSearchOp,
1092 WINPR_ATTR_UNUSED LPVOID lpSearchFilter,
1093 WINPR_ATTR_UNUSED DWORD dwAdditionalFlags)
1095 WLog_ERR(
"TODO",
"TODO: Implement");
1096 return INVALID_HANDLE_VALUE;
1099HANDLE FindFirstFileExW(WINPR_ATTR_UNUSED LPCWSTR lpFileName,
1100 WINPR_ATTR_UNUSED FINDEX_INFO_LEVELS fInfoLevelId,
1101 WINPR_ATTR_UNUSED LPVOID lpFindFileData,
1102 WINPR_ATTR_UNUSED FINDEX_SEARCH_OPS fSearchOp,
1103 WINPR_ATTR_UNUSED LPVOID lpSearchFilter,
1104 WINPR_ATTR_UNUSED DWORD dwAdditionalFlags)
1106 WLog_ERR(
"TODO",
"TODO: Implement");
1107 return INVALID_HANDLE_VALUE;
1112 if (!lpFindFileData)
1116 *lpFindFileData = empty;
1118 if (!is_valid_file_search_handle(hFindFile))
1121 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
1122 struct dirent* pDirent = NULL;
1124 while ((pDirent = readdir(pFileSearch->pDir)) != NULL)
1126 if (FilePatternMatchA(pDirent->d_name, pFileSearch->lpPattern))
1128 BOOL success = FALSE;
1130 strncpy(lpFindFileData->cFileName, pDirent->d_name, MAX_PATH);
1131 const size_t namelen = strnlen(lpFindFileData->cFileName, MAX_PATH);
1132 size_t pathlen = strlen(pFileSearch->lpPath);
1133 char* fullpath = (
char*)malloc(pathlen + namelen + 2);
1135 if (fullpath == NULL)
1137 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1141 memcpy(fullpath, pFileSearch->lpPath, pathlen);
1144 if (fullpath[pathlen - 1] !=
'/')
1145 fullpath[pathlen++] =
'/';
1146 memcpy(fullpath + pathlen, pDirent->d_name, namelen);
1147 fullpath[pathlen + namelen] = 0;
1149 struct stat fileStat = { 0 };
1150 if (stat(fullpath, &fileStat) != 0)
1153 SetLastError(map_posix_err(errno));
1159 if (S_ISFIFO(fileStat.st_mode))
1165 success = FindDataFromStat(fullpath, &fileStat, lpFindFileData);
1171 SetLastError(ERROR_NO_MORE_FILES);
1181 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1185 if (FindNextFileA(hFindFile, fd))
1187 if (!ConvertFindDataAToW(fd, lpFindFileData))
1189 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1202BOOL FindClose(HANDLE hFindFile)
1204 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
1211#ifndef __clang_analyzer__
1212 if (!is_valid_file_search_handle(hFindFile))
1216 free(pFileSearch->lpPath);
1217 free(pFileSearch->lpPattern);
1219 if (pFileSearch->pDir)
1220 closedir(pFileSearch->pDir);
1227BOOL CreateDirectoryA(LPCSTR lpPathName,
1228 WINPR_ATTR_UNUSED LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1230 if (!mkdir(lpPathName, S_IRUSR | S_IWUSR | S_IXUSR))
1236BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1240 char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL);
1245 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1249 ret = CreateDirectoryA(utfPathName, lpSecurityAttributes);
1255BOOL RemoveDirectoryA(LPCSTR lpPathName)
1257 return winpr_RemoveDirectory(lpPathName);
1260BOOL RemoveDirectoryW(LPCWSTR lpPathName)
1264 char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL);
1269 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1273 ret = winpr_RemoveDirectory(utfPathName);
1279BOOL MoveFileExA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
1281 return winpr_MoveFileEx(lpExistingFileName, lpNewFileName, dwFlags);
1284BOOL MoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags)
1286 if (!lpExistingFileName || !lpNewFileName)
1289 LPSTR lpCExistingFileName = ConvertWCharToUtf8Alloc(lpExistingFileName, NULL);
1290 LPSTR lpCNewFileName = ConvertWCharToUtf8Alloc(lpNewFileName, NULL);
1293 if (!lpCExistingFileName || !lpCNewFileName)
1295 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1299 ret = winpr_MoveFileEx(lpCExistingFileName, lpCNewFileName, dwFlags);
1301 free(lpCNewFileName);
1302 free(lpCExistingFileName);
1306BOOL MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
1308 return winpr_MoveFileEx(lpExistingFileName, lpNewFileName, 0);
1311BOOL MoveFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
1313 return MoveFileExW(lpExistingFileName, lpNewFileName, 0);
1320int UnixChangeFileMode(
const char* filename,
int flags)
1326 fl |= (flags & 0x4000) ? S_ISUID : 0;
1327 fl |= (flags & 0x2000) ? S_ISGID : 0;
1328 fl |= (flags & 0x1000) ? S_ISVTX : 0;
1329 fl |= (flags & 0x0400) ? S_IRUSR : 0;
1330 fl |= (flags & 0x0200) ? S_IWUSR : 0;
1331 fl |= (flags & 0x0100) ? S_IXUSR : 0;
1332 fl |= (flags & 0x0040) ? S_IRGRP : 0;
1333 fl |= (flags & 0x0020) ? S_IWGRP : 0;
1334 fl |= (flags & 0x0010) ? S_IXGRP : 0;
1335 fl |= (flags & 0x0004) ? S_IROTH : 0;
1336 fl |= (flags & 0x0002) ? S_IWOTH : 0;
1337 fl |= (flags & 0x0001) ? S_IXOTH : 0;
1338 return chmod(filename, fl);
1341 WCHAR* wfl = ConvertUtf8ToWCharAlloc(filename, NULL);
1347 if (flags & ~(_S_IREAD | _S_IWRITE))
1348 WLog_WARN(TAG,
"Unsupported file mode %d for _wchmod", flags);
1350 rc = _wchmod(wfl, flags);
1356#if defined(_WIN32) || defined(_UWP)
1357HANDLE winpr_CreateFile(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
1358 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
1359 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
1361 char* filename = ConvertUtf8ToWCharAlloc(lpFileName, NULL);
1365 HANDLE hdl = CreateFileW(filename, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
1366 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);