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>
54#include <sys/socket.h>
55#if defined(WINPR_HAVE_SYS_XATTR_H)
58#if defined(WINPR_HAVE_LINUX_MSDOS_FS_H)
59#include <linux/msdos_fs.h>
62#ifdef WINPR_HAVE_AIO_H
63#undef WINPR_HAVE_AIO_H
66#ifdef WINPR_HAVE_AIO_H
73#include <sys/statvfs.h>
76#include "../handle/handle.h"
78#include "../pipe/pipe.h"
79#include "../path/path.h"
186static wArrayList* HandleCreators;
188static pthread_once_t HandleCreatorsInitialized = PTHREAD_ONCE_INIT;
190#include "../comm/comm.h"
191#include "namedPipeClient.h"
194static DWORD FileAttributesFromStat(
const char* path,
const struct stat* fileStat);
197static BOOL FindDataFromStat(
const char* path,
const struct stat* fileStat,
199static void SetDosAttributesToXAttr(
const char* path, DWORD dwFileAttributes);
201static void HandleCreatorsInit(
void)
203 WINPR_ASSERT(HandleCreators ==
nullptr);
204 HandleCreators = ArrayList_New(TRUE);
212 ArrayList_Append(HandleCreators, GetNamedPipeClientHandleCreator());
215 ArrayList_Append(HandleCreators, serial);
216 ArrayList_Append(HandleCreators, GetFileHandleCreator());
219#ifdef WINPR_HAVE_AIO_H
221static BOOL g_AioSignalHandlerInstalled = FALSE;
223void AioSignalHandler(
int signum, siginfo_t* siginfo,
void* arg)
225 WLog_INFO(
"%d", signum);
228int InstallAioSignalHandler()
230 if (!g_AioSignalHandlerInstalled)
232 struct sigaction action;
233 sigemptyset(&action.sa_mask);
234 sigaddset(&action.sa_mask, SIGIO);
235 action.sa_flags = SA_SIGINFO;
236 action.sa_sigaction = (
void*)&AioSignalHandler;
237 sigaction(SIGIO, &action,
nullptr);
238 g_AioSignalHandlerInstalled = TRUE;
246#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
247HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
248 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
249 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
251 return winpr_CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
252 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
256HANDLE winpr_CreateFile(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
257 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
258 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
261 return INVALID_HANDLE_VALUE;
263 if (pthread_once(&HandleCreatorsInitialized, HandleCreatorsInit) != 0)
265 SetLastError(ERROR_DLL_INIT_FAILED);
266 return INVALID_HANDLE_VALUE;
269 if (HandleCreators ==
nullptr)
271 SetLastError(ERROR_DLL_INIT_FAILED);
272 return INVALID_HANDLE_VALUE;
275 ArrayList_Lock(HandleCreators);
277 for (
size_t i = 0; i <= ArrayList_Count(HandleCreators); i++)
279 const HANDLE_CREATOR* creator = ArrayList_GetItem(HandleCreators, i);
281 if (creator && creator->IsHandled(lpFileName))
284 creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
285 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
286 ArrayList_Unlock(HandleCreators);
291 ArrayList_Unlock(HandleCreators);
292 return INVALID_HANDLE_VALUE;
295HANDLE CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
296 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
297 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
299 HANDLE hdl =
nullptr;
302 char* lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName,
nullptr);
306 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
310 hdl = winpr_CreateFile(lpFileNameA, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
311 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
317#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
318BOOL DeleteFileA(LPCSTR lpFileName)
320 return winpr_DeleteFile(lpFileName);
324BOOL DeleteFileW(LPCWSTR lpFileName)
328 LPSTR lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName,
nullptr);
329 BOOL rc = winpr_DeleteFile(lpFileNameA);
334BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
340 if (hFile == INVALID_HANDLE_VALUE)
348 if (!lpNumberOfBytesRead && !lpOverlapped)
351 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
356 if (handle->ops->ReadFile)
357 return handle->ops->ReadFile(handle, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead,
360 WLog_ERR(TAG,
"ReadFile operation not implemented");
364BOOL ReadFileEx(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
365 LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
370 if (hFile == INVALID_HANDLE_VALUE)
373 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
378 if (handle->ops->ReadFileEx)
379 return handle->ops->ReadFileEx(handle, lpBuffer, nNumberOfBytesToRead, lpOverlapped,
380 lpCompletionRoutine);
382 WLog_ERR(TAG,
"ReadFileEx operation not implemented");
386BOOL ReadFileScatter(HANDLE hFile,
FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToRead,
392 if (hFile == INVALID_HANDLE_VALUE)
395 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
400 if (handle->ops->ReadFileScatter)
401 return handle->ops->ReadFileScatter(handle, aSegmentArray, nNumberOfBytesToRead, lpReserved,
404 WLog_ERR(TAG,
"ReadFileScatter operation not implemented");
408BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
409 LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped)
414 if (hFile == INVALID_HANDLE_VALUE)
417 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
422 if (handle->ops->WriteFile)
423 return handle->ops->WriteFile(handle, lpBuffer, nNumberOfBytesToWrite,
424 lpNumberOfBytesWritten, lpOverlapped);
426 WLog_ERR(TAG,
"WriteFile operation not implemented");
430BOOL WriteFileEx(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
431 LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
436 if (hFile == INVALID_HANDLE_VALUE)
439 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
444 if (handle->ops->WriteFileEx)
445 return handle->ops->WriteFileEx(handle, lpBuffer, nNumberOfBytesToWrite, lpOverlapped,
446 lpCompletionRoutine);
448 WLog_ERR(TAG,
"WriteFileEx operation not implemented");
453 DWORD nNumberOfBytesToWrite, LPDWORD lpReserved,
LPOVERLAPPED lpOverlapped)
458 if (hFile == INVALID_HANDLE_VALUE)
461 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
466 if (handle->ops->WriteFileGather)
467 return handle->ops->WriteFileGather(handle, aSegmentArray, nNumberOfBytesToWrite,
468 lpReserved, lpOverlapped);
470 WLog_ERR(TAG,
"WriteFileGather operation not implemented");
474BOOL FlushFileBuffers(HANDLE hFile)
479 if (hFile == INVALID_HANDLE_VALUE)
482 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
487 if (handle->ops->FlushFileBuffers)
488 return handle->ops->FlushFileBuffers(handle);
490 WLog_ERR(TAG,
"FlushFileBuffers operation not implemented");
494BOOL WINAPI GetFileAttributesExA(LPCSTR lpFileName,
495 WINPR_ATTR_UNUSED GET_FILEEX_INFO_LEVELS fInfoLevelId,
496 LPVOID lpFileInformation)
502 struct stat fileStat = WINPR_C_ARRAY_INIT;
503 if (stat(lpFileName, &fileStat) != 0)
507 if (!FindDataFromStat(lpFileName, &fileStat, &findFileData))
510 fd->dwFileAttributes = findFileData.dwFileAttributes;
511 fd->ftCreationTime = findFileData.ftCreationTime;
512 fd->ftLastAccessTime = findFileData.ftLastAccessTime;
513 fd->ftLastWriteTime = findFileData.ftLastWriteTime;
514 fd->nFileSizeHigh = findFileData.nFileSizeHigh;
515 fd->nFileSizeLow = findFileData.nFileSizeLow;
519BOOL WINAPI GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
520 LPVOID lpFileInformation)
525 LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName,
nullptr);
529 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
533 ret = GetFileAttributesExA(lpCFileName, fInfoLevelId, lpFileInformation);
538DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName)
540 struct stat fileStat = WINPR_C_ARRAY_INIT;
541 if (stat(lpFileName, &fileStat) != 0)
542 return INVALID_FILE_ATTRIBUTES;
544 return FileAttributesFromStat(lpFileName, &fileStat);
547DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
552 LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName,
nullptr);
555 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
559 ret = GetFileAttributesA(lpCFileName);
569 if (hFile == INVALID_HANDLE_VALUE)
572 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
577 if (handle->ops->GetFileInformationByHandle)
578 return handle->ops->GetFileInformationByHandle(handle, lpFileInformation);
580 WLog_ERR(TAG,
"GetFileInformationByHandle operation not implemented");
584static char* append(
char* buffer,
size_t size,
const char* append)
586 (void)winpr_str_append(append, buffer, size,
"|");
591static const char* flagsToStr(
char* buffer,
size_t size, DWORD flags)
593 char strflags[32] = WINPR_C_ARRAY_INIT;
594 if (flags & FILE_ATTRIBUTE_READONLY)
595 append(buffer, size,
"FILE_ATTRIBUTE_READONLY");
596 if (flags & FILE_ATTRIBUTE_HIDDEN)
597 append(buffer, size,
"FILE_ATTRIBUTE_HIDDEN");
598 if (flags & FILE_ATTRIBUTE_SYSTEM)
599 append(buffer, size,
"FILE_ATTRIBUTE_SYSTEM");
600 if (flags & FILE_ATTRIBUTE_DIRECTORY)
601 append(buffer, size,
"FILE_ATTRIBUTE_DIRECTORY");
602 if (flags & FILE_ATTRIBUTE_ARCHIVE)
603 append(buffer, size,
"FILE_ATTRIBUTE_ARCHIVE");
604 if (flags & FILE_ATTRIBUTE_DEVICE)
605 append(buffer, size,
"FILE_ATTRIBUTE_DEVICE");
606 if (flags & FILE_ATTRIBUTE_NORMAL)
607 append(buffer, size,
"FILE_ATTRIBUTE_NORMAL");
608 if (flags & FILE_ATTRIBUTE_TEMPORARY)
609 append(buffer, size,
"FILE_ATTRIBUTE_TEMPORARY");
610 if (flags & FILE_ATTRIBUTE_SPARSE_FILE)
611 append(buffer, size,
"FILE_ATTRIBUTE_SPARSE_FILE");
612 if (flags & FILE_ATTRIBUTE_REPARSE_POINT)
613 append(buffer, size,
"FILE_ATTRIBUTE_REPARSE_POINT");
614 if (flags & FILE_ATTRIBUTE_COMPRESSED)
615 append(buffer, size,
"FILE_ATTRIBUTE_COMPRESSED");
616 if (flags & FILE_ATTRIBUTE_OFFLINE)
617 append(buffer, size,
"FILE_ATTRIBUTE_OFFLINE");
618 if (flags & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
619 append(buffer, size,
"FILE_ATTRIBUTE_NOT_CONTENT_INDEXED");
620 if (flags & FILE_ATTRIBUTE_ENCRYPTED)
621 append(buffer, size,
"FILE_ATTRIBUTE_ENCRYPTED");
622 if (flags & FILE_ATTRIBUTE_VIRTUAL)
623 append(buffer, size,
"FILE_ATTRIBUTE_VIRTUAL");
625 (void)_snprintf(strflags,
sizeof(strflags),
" [0x%08" PRIx32
"]", flags);
626 winpr_str_append(strflags, buffer, size,
nullptr);
630BOOL SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes)
633#ifdef WINPR_HAVE_FCNTL_H
634 const uint32_t mask = ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_NORMAL |
635 FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
636 if (dwFileAttributes & mask)
638 char buffer[8192] = WINPR_C_ARRAY_INIT;
639 const char* flags = flagsToStr(buffer,
sizeof(buffer), dwFileAttributes & mask);
640 WLog_WARN(TAG,
"Unsupported flags %s, ignoring!", flags);
643 SetDosAttributesToXAttr(lpFileName, dwFileAttributes);
645 int fd = open(lpFileName, O_RDONLY);
649 struct stat st = WINPR_C_ARRAY_INIT;
650 if (fstat(fd, &st) != 0)
653 if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
655 st.st_mode &= WINPR_ASSERTING_INT_CAST(mode_t, (mode_t)(~(S_IWUSR | S_IWGRP | S_IWOTH)));
659 st.st_mode |= S_IWUSR;
662 if (fchmod(fd, st.st_mode) != 0)
672BOOL SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
679 char* lpCFileName = ConvertWCharToUtf8Alloc(lpFileName,
nullptr);
682 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
686 ret = SetFileAttributesA(lpCFileName, dwFileAttributes);
691BOOL SetEndOfFile(HANDLE hFile)
696 if (hFile == INVALID_HANDLE_VALUE)
699 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
704 if (handle->ops->SetEndOfFile)
705 return handle->ops->SetEndOfFile(handle);
707 WLog_ERR(TAG,
"SetEndOfFile operation not implemented");
711DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
716 if (hFile == INVALID_HANDLE_VALUE)
719 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
724 if (handle->ops->GetFileSize)
725 return handle->ops->GetFileSize(handle, lpFileSizeHigh);
727 WLog_ERR(TAG,
"GetFileSize operation not implemented");
731DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh,
737 if (hFile == INVALID_HANDLE_VALUE)
740 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
745 if (handle->ops->SetFilePointer)
746 return handle->ops->SetFilePointer(handle, lDistanceToMove, lpDistanceToMoveHigh,
749 WLog_ERR(TAG,
"SetFilePointer operation not implemented");
753BOOL SetFilePointerEx(HANDLE hFile,
LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer,
759 if (hFile == INVALID_HANDLE_VALUE)
762 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
767 if (handle->ops->SetFilePointerEx)
768 return handle->ops->SetFilePointerEx(handle, liDistanceToMove, lpNewFilePointer,
771 WLog_ERR(TAG,
"SetFilePointerEx operation not implemented");
775BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
776 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
781 if (hFile == INVALID_HANDLE_VALUE)
784 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
789 if (handle->ops->LockFile)
790 return handle->ops->LockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
791 nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
793 WLog_ERR(TAG,
"LockFile operation not implemented");
797BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved, DWORD nNumberOfBytesToLockLow,
798 DWORD nNumberOfBytesToLockHigh,
LPOVERLAPPED lpOverlapped)
803 if (hFile == INVALID_HANDLE_VALUE)
806 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
811 if (handle->ops->LockFileEx)
812 return handle->ops->LockFileEx(handle, dwFlags, dwReserved, nNumberOfBytesToLockLow,
813 nNumberOfBytesToLockHigh, lpOverlapped);
815 WLog_ERR(TAG,
"LockFileEx operation not implemented");
819BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
820 DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
825 if (hFile == INVALID_HANDLE_VALUE)
828 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
833 if (handle->ops->UnlockFile)
834 return handle->ops->UnlockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
835 nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
837 WLog_ERR(TAG,
"UnLockFile operation not implemented");
841BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow,
842 DWORD nNumberOfBytesToUnlockHigh,
LPOVERLAPPED lpOverlapped)
847 if (hFile == INVALID_HANDLE_VALUE)
850 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
855 if (handle->ops->UnlockFileEx)
856 return handle->ops->UnlockFileEx(handle, dwReserved, nNumberOfBytesToUnlockLow,
857 nNumberOfBytesToUnlockHigh, lpOverlapped);
859 WLog_ERR(TAG,
"UnLockFileEx operation not implemented");
863BOOL WINAPI SetFileTime(HANDLE hFile,
const FILETIME* lpCreationTime,
869 if (hFile == INVALID_HANDLE_VALUE)
872 if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
877 if (handle->ops->SetFileTime)
878 return handle->ops->SetFileTime(handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
880 WLog_ERR(TAG,
"operation not implemented");
892static const char file_search_magic[] =
"file_srch_magic";
894WINPR_ATTR_MALLOC(FindClose, 1)
895static WIN32_FILE_SEARCH* file_search_new(const
char* name,
size_t namelen, const
char* pattern,
898 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)calloc(1,
sizeof(WIN32_FILE_SEARCH));
901 WINPR_ASSERT(
sizeof(file_search_magic) ==
sizeof(pFileSearch->magic));
902 memcpy(pFileSearch->magic, file_search_magic,
sizeof(pFileSearch->magic));
904 pFileSearch->lpPath = strndup(name, namelen);
905 pFileSearch->lpPattern = strndup(pattern, patternlen);
906 if (!pFileSearch->lpPath || !pFileSearch->lpPattern)
909 pFileSearch->pDir = opendir(pFileSearch->lpPath);
910 if (!pFileSearch->pDir)
916 struct stat fileStat = WINPR_C_ARRAY_INIT;
917 if (stat(name, &fileStat) == 0)
919 if (S_ISDIR(fileStat.st_mode))
921 pFileSearch->pDir = opendir(name);
922 if (pFileSearch->pDir)
924 free(pFileSearch->lpPath);
925 free(pFileSearch->lpPattern);
926 pFileSearch->lpPath = _strdup(name);
927 pFileSearch->lpPattern = _strdup(
"*");
928 if (!pFileSearch->lpPath || !pFileSearch->lpPattern)
930 closedir(pFileSearch->pDir);
931 pFileSearch->pDir =
nullptr;
938 char buffer[128] = WINPR_C_ARRAY_INIT;
939 const DWORD err = map_posix_err(errno);
940 WLog_DBG(TAG,
"stat failed with %s [%d] -> %s",
941 winpr_strerror(errno, buffer,
sizeof(buffer)), errno,
942 Win32ErrorCode2Tag(err & 0xFFFF));
946 if (!pFileSearch->pDir)
951 WINPR_PRAGMA_DIAG_PUSH
952 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
953 FindClose(pFileSearch);
954 WINPR_PRAGMA_DIAG_POP
959static BOOL is_valid_file_search_handle(HANDLE handle)
961 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)handle;
964 if (pFileSearch == INVALID_HANDLE_VALUE)
966 if (strncmp(file_search_magic, pFileSearch->magic,
sizeof(file_search_magic)) != 0)
972static DWORD GetDosAttributesFromXAttr(WINPR_ATTR_UNUSED
const char* path)
974#if defined(WINPR_HAVE_SYS_XATTR_H) || defined(WINPR_HAVE_LINUX_MSDOS_FS_H)
975 DWORD dwFileAttributes = 0;
976 uint32_t intAttr = 0;
978 const DWORD supportedMask = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
979 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY |
980 FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_NORMAL;
982#if defined(WINPR_HAVE_SYS_XATTR_H)
983 length = getxattr(path,
"system.ntfs_attrib_be", &intAttr,
sizeof(intAttr));
986 intAttr = ntohl(intAttr);
987 dwFileAttributes = intAttr;
991#if defined(WINPR_HAVE_SYS_XATTR_H)
992 if ((dwFileAttributes & supportedMask) == 0)
994 length = getxattr(path,
"user.cifs.dosattrib", &intAttr,
sizeof(intAttr));
996 dwFileAttributes = intAttr;
1000 if ((dwFileAttributes & supportedMask) == 0)
1002#if defined(WINPR_HAVE_LINUX_MSDOS_FS_H)
1003 int fd = open(path, O_RDONLY | O_CLOEXEC);
1006 const int rc = ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &intAttr);
1009 char buffer[64] = WINPR_C_ARRAY_INIT;
1010 WLog_DBG(TAG,
"ioctl(%d, FAT_IOCTL_GET_ATTRIBUTES) failed with %s", fd,
1011 winpr_strerror(errno, buffer,
sizeof(buffer)));
1014 dwFileAttributes = intAttr;
1016 const int crc = close(fd);
1019 char buffer[64] = WINPR_C_ARRAY_INIT;
1020 WLog_DBG(TAG,
"close(%d) failed with %s", fd,
1021 winpr_strerror(errno, buffer,
sizeof(buffer)));
1027#if defined(WINPR_HAVE_SYS_XATTR_H)
1028 if ((dwFileAttributes & supportedMask) == 0)
1031 dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1033 char attrValue[11] = WINPR_C_ARRAY_INIT;
1034 length = getxattr(path,
"user.DOSATTRIB", attrValue,
sizeof(attrValue) - 1);
1038 const unsigned long val = strtoul(attrValue,
nullptr, 0);
1040 dwFileAttributes = WINPR_ASSERTING_INT_CAST(DWORD, val);
1044 if ((dwFileAttributes & supportedMask) == 0)
1047 dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1051 return dwFileAttributes;
1057static void SetDosAttributesToXAttr(
const char* path, DWORD dwFileAttributes)
1059#if defined(WINPR_HAVE_SYS_XATTR_H) || defined(WINPR_HAVE_LINUX_MSDOS_FS_H)
1061 dwFileAttributes & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
1062 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_NORMAL);
1063 uint32_t intAttrBE = htonl(intAttr);
1065#if defined(WINPR_HAVE_SYS_XATTR_H)
1066 if (setxattr(path,
"system.ntfs_attrib_be", &intAttrBE,
sizeof(intAttrBE), 0) >= 0)
1068 WLog_DBG(TAG,
"Set NTFS attribute xattr for %s", path);
1073 ssize_t length = getxattr(path,
"user.cifs.dosattrib",
nullptr, 0);
1076 if (setxattr(path,
"user.cifs.dosattrib", &intAttrBE,
sizeof(intAttrBE), 0) >= 0)
1078 WLog_DBG(TAG,
"Set CIFS DOS attribute xattr for %s", path);
1085#if defined(WINPR_HAVE_LINUX_MSDOS_FS_H)
1086 int fd = open(path, O_RDONLY | O_CLOEXEC);
1089 if (ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &intAttr) != -1)
1092 WLog_DBG(TAG,
"Set FAT attribute for %s", path);
1099#if defined(WINPR_HAVE_SYS_XATTR_H)
1103 if (intAttr == FILE_ATTRIBUTE_ARCHIVE)
1105 const int rc = removexattr(path,
"user.DOSATTRIB");
1108 char buffer[128] = WINPR_C_ARRAY_INIT;
1109 WLog_DBG(TAG,
"removexattr(%s) failed with %s", path,
1110 winpr_strerror(errno, buffer,
sizeof(buffer)));
1115 char attrValue[11] = WINPR_C_ARRAY_INIT;
1116 const int written = snprintf(attrValue,
sizeof(attrValue),
"0x%x", intAttr);
1120 if (setxattr(path,
"user.DOSATTRIB", attrValue, strlen(attrValue), 0) < 0)
1121 WLog_DBG(TAG,
"Failed to set DOS attribute xattr for %s", path);
1123 WLog_DBG(TAG,
"Set DOS attribute xattr for %s", path);
1129static DWORD FileAttributesFromStat(
const char* path,
const struct stat* fileStat)
1131 DWORD dwFileAttributes = GetDosAttributesFromXAttr(path);
1133 if (S_ISDIR(fileStat->st_mode))
1134 dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
1136 if (dwFileAttributes == 0)
1137 dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
1139 const char* lastSep = strrchr(path,
'/');
1142 const char* name = lastSep + 1;
1143 const size_t namelen = strlen(name);
1145 if ((namelen > 1) && (name[0] ==
'.') && (name[1] !=
'.'))
1146 dwFileAttributes |= FILE_ATTRIBUTE_HIDDEN;
1149 if (!(fileStat->st_mode & S_IWUSR))
1150 dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
1152 return dwFileAttributes;
1156static BOOL FindDataFromStat(
const char* path,
const struct stat* fileStat,
1160 lpFindFileData->dwFileAttributes = FileAttributesFromStat(path, fileStat);
1162#ifdef _DARWIN_FEATURE_64_BIT_INODE
1163 ft = STAT_TIME_TO_FILETIME(fileStat->st_birthtime);
1165 ft = STAT_TIME_TO_FILETIME(fileStat->st_ctime);
1167 lpFindFileData->ftCreationTime.dwHighDateTime = (ft) >> 32ULL;
1168 lpFindFileData->ftCreationTime.dwLowDateTime = ft & 0xFFFFFFFF;
1169 ft = STAT_TIME_TO_FILETIME(fileStat->st_mtime);
1170 lpFindFileData->ftLastWriteTime.dwHighDateTime = (ft) >> 32ULL;
1171 lpFindFileData->ftLastWriteTime.dwLowDateTime = ft & 0xFFFFFFFF;
1172 ft = STAT_TIME_TO_FILETIME(fileStat->st_atime);
1173 lpFindFileData->ftLastAccessTime.dwHighDateTime = (ft) >> 32ULL;
1174 lpFindFileData->ftLastAccessTime.dwLowDateTime = ft & 0xFFFFFFFF;
1175 lpFindFileData->nFileSizeHigh = ((UINT64)fileStat->st_size) >> 32ULL;
1176 lpFindFileData->nFileSizeLow = fileStat->st_size & 0xFFFFFFFF;
1182 if (!lpFindFileData || !lpFileName)
1184 SetLastError(ERROR_BAD_ARGUMENTS);
1185 return INVALID_HANDLE_VALUE;
1189 *lpFindFileData = empty;
1191 WIN32_FILE_SEARCH* pFileSearch =
nullptr;
1192 size_t patternlen = 0;
1193 const size_t flen = strlen(lpFileName);
1194 const char sep = PathGetSeparatorA(PATH_STYLE_NATIVE);
1195 const char* ptr = strrchr(lpFileName, sep);
1198 patternlen = strlen(ptr + 1);
1199 if (patternlen == 0)
1202 pFileSearch = file_search_new(lpFileName, flen - patternlen, ptr + 1, patternlen);
1205 return INVALID_HANDLE_VALUE;
1207 if (FindNextFileA((HANDLE)pFileSearch, lpFindFileData))
1208 return (HANDLE)pFileSearch;
1211 FindClose(pFileSearch);
1212 return INVALID_HANDLE_VALUE;
1219 if (!lpFindFileDataA || !lpFindFileDataW)
1222 lpFindFileDataW->dwFileAttributes = lpFindFileDataA->dwFileAttributes;
1223 lpFindFileDataW->ftCreationTime = lpFindFileDataA->ftCreationTime;
1224 lpFindFileDataW->ftLastAccessTime = lpFindFileDataA->ftLastAccessTime;
1225 lpFindFileDataW->ftLastWriteTime = lpFindFileDataA->ftLastWriteTime;
1226 lpFindFileDataW->nFileSizeHigh = lpFindFileDataA->nFileSizeHigh;
1227 lpFindFileDataW->nFileSizeLow = lpFindFileDataA->nFileSizeLow;
1228 lpFindFileDataW->dwReserved0 = lpFindFileDataA->dwReserved0;
1229 lpFindFileDataW->dwReserved1 = lpFindFileDataA->dwReserved1;
1231 if (ConvertUtf8NToWChar(lpFindFileDataA->cFileName, ARRAYSIZE(lpFindFileDataA->cFileName),
1232 lpFindFileDataW->cFileName, ARRAYSIZE(lpFindFileDataW->cFileName)) < 0)
1235 return ConvertUtf8NToWChar(lpFindFileDataA->cAlternateFileName,
1236 ARRAYSIZE(lpFindFileDataA->cAlternateFileName),
1237 lpFindFileDataW->cAlternateFileName,
1238 ARRAYSIZE(lpFindFileDataW->cAlternateFileName)) >= 0;
1243 LPSTR utfFileName =
nullptr;
1246 return INVALID_HANDLE_VALUE;
1252 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1253 return INVALID_HANDLE_VALUE;
1256 utfFileName = ConvertWCharToUtf8Alloc(lpFileName,
nullptr);
1259 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1261 return INVALID_HANDLE_VALUE;
1264 h = FindFirstFileA(utfFileName, fd);
1267 if (h != INVALID_HANDLE_VALUE)
1269 if (!ConvertFindDataAToW(fd, lpFindFileData))
1271 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1273 h = INVALID_HANDLE_VALUE;
1283HANDLE FindFirstFileExA(WINPR_ATTR_UNUSED LPCSTR lpFileName,
1284 WINPR_ATTR_UNUSED FINDEX_INFO_LEVELS fInfoLevelId,
1285 WINPR_ATTR_UNUSED LPVOID lpFindFileData,
1286 WINPR_ATTR_UNUSED FINDEX_SEARCH_OPS fSearchOp,
1287 WINPR_ATTR_UNUSED LPVOID lpSearchFilter,
1288 WINPR_ATTR_UNUSED DWORD dwAdditionalFlags)
1290 WLog_ERR(
"TODO",
"TODO: Implement");
1291 return INVALID_HANDLE_VALUE;
1294HANDLE FindFirstFileExW(WINPR_ATTR_UNUSED LPCWSTR lpFileName,
1295 WINPR_ATTR_UNUSED FINDEX_INFO_LEVELS fInfoLevelId,
1296 WINPR_ATTR_UNUSED LPVOID lpFindFileData,
1297 WINPR_ATTR_UNUSED FINDEX_SEARCH_OPS fSearchOp,
1298 WINPR_ATTR_UNUSED LPVOID lpSearchFilter,
1299 WINPR_ATTR_UNUSED DWORD dwAdditionalFlags)
1301 WLog_ERR(
"TODO",
"TODO: Implement");
1302 return INVALID_HANDLE_VALUE;
1307 if (!lpFindFileData)
1311 *lpFindFileData = empty;
1313 if (!is_valid_file_search_handle(hFindFile))
1316 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
1317 struct dirent* pDirent =
nullptr;
1319 while ((pDirent = readdir(pFileSearch->pDir)) !=
nullptr)
1321 if (FilePatternMatchA(pDirent->d_name, pFileSearch->lpPattern))
1323 BOOL success = FALSE;
1325 strncpy(lpFindFileData->cFileName, pDirent->d_name, MAX_PATH);
1326 const size_t namelen = strnlen(lpFindFileData->cFileName, MAX_PATH);
1327 size_t pathlen = strlen(pFileSearch->lpPath);
1328 char* fullpath = (
char*)malloc(pathlen + namelen + 2);
1330 if (fullpath ==
nullptr)
1332 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1336 memcpy(fullpath, pFileSearch->lpPath, pathlen);
1339 if (fullpath[pathlen - 1] !=
'/')
1340 fullpath[pathlen++] =
'/';
1341 memcpy(fullpath + pathlen, pDirent->d_name, namelen);
1342 fullpath[pathlen + namelen] = 0;
1344 char* canonical = winpr_PathCanonicalize(fullpath);
1350 struct stat fileStat = WINPR_C_ARRAY_INIT;
1351 if (stat(canonical, &fileStat) != 0)
1354 SetLastError(map_posix_err(errno));
1360 if (S_ISFIFO(fileStat.st_mode))
1366 success = FindDataFromStat(canonical, &fileStat, lpFindFileData);
1372 SetLastError(ERROR_NO_MORE_FILES);
1382 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1386 if (FindNextFileA(hFindFile, fd))
1388 if (!ConvertFindDataAToW(fd, lpFindFileData))
1390 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1403BOOL FindClose(HANDLE hFindFile)
1405 WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
1412#ifndef __clang_analyzer__
1413 if (!is_valid_file_search_handle(hFindFile))
1417 free(pFileSearch->lpPath);
1418 free(pFileSearch->lpPattern);
1420 if (pFileSearch->pDir)
1421 closedir(pFileSearch->pDir);
1428BOOL CreateDirectoryA(LPCSTR lpPathName,
1429 WINPR_ATTR_UNUSED LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1431 return mkdir(lpPathName, S_IRUSR | S_IWUSR | S_IXUSR) == 0;
1434BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1438 char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName,
nullptr);
1443 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1447 ret = CreateDirectoryA(utfPathName, lpSecurityAttributes);
1453#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
1454BOOL RemoveDirectoryA(LPCSTR lpPathName)
1456 return winpr_RemoveDirectory(lpPathName);
1460BOOL RemoveDirectoryW(LPCWSTR lpPathName)
1464 char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName,
nullptr);
1469 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1473 ret = winpr_RemoveDirectory(utfPathName);
1479#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
1480BOOL MoveFileExA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
1482 return winpr_MoveFileEx(lpExistingFileName, lpNewFileName, dwFlags);
1486BOOL MoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags)
1488 if (!lpExistingFileName || !lpNewFileName)
1491 LPSTR lpCExistingFileName = ConvertWCharToUtf8Alloc(lpExistingFileName,
nullptr);
1492 LPSTR lpCNewFileName = ConvertWCharToUtf8Alloc(lpNewFileName,
nullptr);
1495 if (!lpCExistingFileName || !lpCNewFileName)
1497 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1501 ret = winpr_MoveFileEx(lpCExistingFileName, lpCNewFileName, dwFlags);
1503 free(lpCNewFileName);
1504 free(lpCExistingFileName);
1508#if !defined(WITHOUT_WINPR_3x_DEPRECATED)
1509BOOL MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
1511 return winpr_MoveFileEx(lpExistingFileName, lpNewFileName, 0);
1515BOOL MoveFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
1517 return MoveFileExW(lpExistingFileName, lpNewFileName, 0);
1524int UnixChangeFileMode(
const char* filename,
int flags)
1530 fl |= (flags & 0x4000) ? S_ISUID : 0;
1531 fl |= (flags & 0x2000) ? S_ISGID : 0;
1532 fl |= (flags & 0x1000) ? S_ISVTX : 0;
1533 fl |= (flags & 0x0400) ? S_IRUSR : 0;
1534 fl |= (flags & 0x0200) ? S_IWUSR : 0;
1535 fl |= (flags & 0x0100) ? S_IXUSR : 0;
1536 fl |= (flags & 0x0040) ? S_IRGRP : 0;
1537 fl |= (flags & 0x0020) ? S_IWGRP : 0;
1538 fl |= (flags & 0x0010) ? S_IXGRP : 0;
1539 fl |= (flags & 0x0004) ? S_IROTH : 0;
1540 fl |= (flags & 0x0002) ? S_IWOTH : 0;
1541 fl |= (flags & 0x0001) ? S_IXOTH : 0;
1542 return chmod(filename, fl);
1545 WCHAR* wfl = ConvertUtf8ToWCharAlloc(filename,
nullptr);
1551 if (flags & ~(_S_IREAD | _S_IWRITE))
1552 WLog_WARN(TAG,
"Unsupported file mode %d for _wchmod", flags);
1554 rc = _wchmod(wfl, flags);
1560#if defined(_WIN32) || defined(_UWP)
1561HANDLE winpr_CreateFile(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
1562 LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
1563 DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
1565 WCHAR* filename = ConvertUtf8ToWCharAlloc(lpFileName,
nullptr);
1569 HANDLE hdl = CreateFileW(filename, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
1570 dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);