2012-09-24 23:58:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* File Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2014-04-18 19:18:08 +04:00
|
|
|
* Copyright 2014 Hewlett-Packard Development Company, L.P.
|
2016-12-01 00:47:06 +03:00
|
|
|
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
|
2012-09-24 23:58:33 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-09-24 23:58:33 +04:00
|
|
|
|
2012-10-19 00:58:27 +04:00
|
|
|
#include <winpr/crt.h>
|
2022-06-03 11:25:30 +03:00
|
|
|
#include <winpr/string.h>
|
2013-07-30 18:57:54 +04:00
|
|
|
#include <winpr/path.h>
|
2012-09-24 23:58:33 +04:00
|
|
|
#include <winpr/file.h>
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_UNISTD_H
|
2012-10-08 04:25:50 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_FCNTL_H
|
2012-11-03 04:58:58 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("file")
|
2015-09-09 16:48:59 +03:00
|
|
|
|
2016-02-26 14:53:23 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#else
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2015-09-09 16:48:59 +03:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <dirent.h>
|
2016-12-01 00:47:06 +03:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <errno.h>
|
2015-09-09 16:48:59 +03:00
|
|
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_AIO_H
|
|
|
|
#undef WINPR_HAVE_AIO_H /* disable for now, incomplete */
|
2015-09-09 16:48:59 +03:00
|
|
|
#endif
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_AIO_H
|
2015-09-09 16:48:59 +03:00
|
|
|
#include <aio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#else
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
|
|
|
#include "../pipe/pipe.h"
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
#include "file.h"
|
|
|
|
|
2012-09-24 23:58:33 +04:00
|
|
|
/**
|
|
|
|
* api-ms-win-core-file-l1-2-0.dll:
|
2014-06-19 14:03:36 +04:00
|
|
|
*
|
2012-09-24 23:58:33 +04:00
|
|
|
* CreateFileA
|
|
|
|
* CreateFileW
|
2012-09-27 00:25:23 +04:00
|
|
|
* CreateFile2
|
2012-09-24 23:58:33 +04:00
|
|
|
* DeleteFileA
|
|
|
|
* DeleteFileW
|
2012-09-27 00:25:23 +04:00
|
|
|
* CreateDirectoryA
|
|
|
|
* CreateDirectoryW
|
|
|
|
* RemoveDirectoryA
|
|
|
|
* RemoveDirectoryW
|
|
|
|
* CompareFileTime
|
|
|
|
* DefineDosDeviceW
|
2012-09-24 23:58:33 +04:00
|
|
|
* DeleteVolumeMountPointW
|
|
|
|
* FileTimeToLocalFileTime
|
2012-09-27 00:25:23 +04:00
|
|
|
* LocalFileTimeToFileTime
|
2012-09-24 23:58:33 +04:00
|
|
|
* FindClose
|
|
|
|
* FindCloseChangeNotification
|
|
|
|
* FindFirstChangeNotificationA
|
|
|
|
* FindFirstChangeNotificationW
|
|
|
|
* FindFirstFileA
|
|
|
|
* FindFirstFileExA
|
|
|
|
* FindFirstFileExW
|
|
|
|
* FindFirstFileW
|
|
|
|
* FindFirstVolumeW
|
|
|
|
* FindNextChangeNotification
|
|
|
|
* FindNextFileA
|
|
|
|
* FindNextFileW
|
|
|
|
* FindNextVolumeW
|
|
|
|
* FindVolumeClose
|
|
|
|
* GetDiskFreeSpaceA
|
|
|
|
* GetDiskFreeSpaceExA
|
|
|
|
* GetDiskFreeSpaceExW
|
|
|
|
* GetDiskFreeSpaceW
|
|
|
|
* GetDriveTypeA
|
|
|
|
* GetDriveTypeW
|
|
|
|
* GetFileAttributesA
|
|
|
|
* GetFileAttributesExA
|
|
|
|
* GetFileAttributesExW
|
|
|
|
* GetFileAttributesW
|
|
|
|
* GetFileInformationByHandle
|
|
|
|
* GetFileSize
|
|
|
|
* GetFileSizeEx
|
|
|
|
* GetFileTime
|
|
|
|
* GetFileType
|
|
|
|
* GetFinalPathNameByHandleA
|
|
|
|
* GetFinalPathNameByHandleW
|
|
|
|
* GetFullPathNameA
|
|
|
|
* GetFullPathNameW
|
|
|
|
* GetLogicalDrives
|
|
|
|
* GetLogicalDriveStringsW
|
|
|
|
* GetLongPathNameA
|
|
|
|
* GetLongPathNameW
|
|
|
|
* GetShortPathNameW
|
|
|
|
* GetTempFileNameW
|
|
|
|
* GetTempPathW
|
|
|
|
* GetVolumeInformationByHandleW
|
|
|
|
* GetVolumeInformationW
|
|
|
|
* GetVolumeNameForVolumeMountPointW
|
|
|
|
* GetVolumePathNamesForVolumeNameW
|
|
|
|
* GetVolumePathNameW
|
|
|
|
* QueryDosDeviceW
|
|
|
|
* SetFileAttributesA
|
|
|
|
* SetFileAttributesW
|
|
|
|
* SetFileTime
|
|
|
|
* SetFileValidData
|
2012-09-26 20:37:35 +04:00
|
|
|
* SetFileInformationByHandle
|
|
|
|
* ReadFile
|
|
|
|
* ReadFileEx
|
|
|
|
* ReadFileScatter
|
2012-09-24 23:58:33 +04:00
|
|
|
* WriteFile
|
|
|
|
* WriteFileEx
|
|
|
|
* WriteFileGather
|
2012-09-26 20:37:35 +04:00
|
|
|
* FlushFileBuffers
|
|
|
|
* SetEndOfFile
|
|
|
|
* SetFilePointer
|
|
|
|
* SetFilePointerEx
|
|
|
|
* LockFile
|
|
|
|
* LockFileEx
|
|
|
|
* UnlockFile
|
|
|
|
* UnlockFileEx
|
2012-09-24 23:58:33 +04:00
|
|
|
*/
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
/**
|
|
|
|
* File System Behavior in the Microsoft Windows Environment:
|
|
|
|
* http://download.microsoft.com/download/4/3/8/43889780-8d45-4b2e-9d3a-c696a890309f/File%20System%20Behavior%20Overview.pdf
|
|
|
|
*/
|
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
/**
|
|
|
|
* Asynchronous I/O - The GNU C Library:
|
|
|
|
* http://www.gnu.org/software/libc/manual/html_node/Asynchronous-I_002fO.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* aio.h - asynchronous input and output:
|
|
|
|
* http://pubs.opengroup.org/onlinepubs/009695399/basedefs/aio.h.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronous I/O User Guide:
|
|
|
|
* http://code.google.com/p/kernel/wiki/AIOUserGuide
|
|
|
|
*/
|
2017-04-27 09:31:53 +03:00
|
|
|
static wArrayList* _HandleCreators;
|
2014-04-18 21:25:25 +04:00
|
|
|
|
2014-06-19 14:03:36 +04:00
|
|
|
static pthread_once_t _HandleCreatorsInitialized = PTHREAD_ONCE_INIT;
|
2015-03-11 17:10:52 +03:00
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
extern HANDLE_CREATOR* GetNamedPipeClientHandleCreator(void);
|
2015-09-09 17:59:03 +03:00
|
|
|
|
|
|
|
#if defined __linux__ && !defined ANDROID
|
2021-07-28 16:18:03 +03:00
|
|
|
#include "../comm/comm.h"
|
2015-09-09 17:59:03 +03:00
|
|
|
#endif /* __linux__ && !defined ANDROID */
|
2015-03-11 19:57:01 +03:00
|
|
|
|
2023-03-06 11:06:19 +03:00
|
|
|
static void _HandleCreatorsInit(void)
|
2014-04-18 21:25:25 +04:00
|
|
|
{
|
2021-06-09 15:03:34 +03:00
|
|
|
WINPR_ASSERT(_HandleCreators == NULL);
|
2015-09-09 16:48:59 +03:00
|
|
|
_HandleCreators = ArrayList_New(TRUE);
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!_HandleCreators)
|
|
|
|
return;
|
|
|
|
|
2015-09-09 16:48:59 +03:00
|
|
|
/*
|
|
|
|
* Register all file handle creators.
|
|
|
|
*/
|
2021-05-17 13:30:18 +03:00
|
|
|
ArrayList_Append(_HandleCreators, GetNamedPipeClientHandleCreator());
|
2015-09-09 17:59:03 +03:00
|
|
|
#if defined __linux__ && !defined ANDROID
|
2021-05-17 13:30:18 +03:00
|
|
|
ArrayList_Append(_HandleCreators, GetCommHandleCreator());
|
2015-09-09 17:59:03 +03:00
|
|
|
#endif /* __linux__ && !defined ANDROID */
|
2021-05-17 13:30:18 +03:00
|
|
|
ArrayList_Append(_HandleCreators, GetFileHandleCreator());
|
2014-04-18 21:25:25 +04:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_AIO_H
|
2014-02-17 22:02:39 +04:00
|
|
|
|
|
|
|
static BOOL g_AioSignalHandlerInstalled = FALSE;
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void AioSignalHandler(int signum, siginfo_t* siginfo, void* arg)
|
2014-02-17 22:02:39 +04:00
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_INFO("%d", signum);
|
2014-02-17 22:02:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int InstallAioSignalHandler()
|
|
|
|
{
|
|
|
|
if (!g_AioSignalHandlerInstalled)
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
sigaddset(&action.sa_mask, SIGIO);
|
|
|
|
action.sa_flags = SA_SIGINFO;
|
2019-11-06 17:24:51 +03:00
|
|
|
action.sa_sigaction = (void*)&AioSignalHandler;
|
2014-02-17 22:02:39 +04:00
|
|
|
sigaction(SIGIO, &action, NULL);
|
|
|
|
g_AioSignalHandlerInstalled = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#endif /* WINPR_HAVE_AIO_H */
|
2014-02-17 22:02:39 +04:00
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
|
2019-11-06 17:24:51 +03:00
|
|
|
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
|
|
|
|
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
|
2012-09-27 00:25:23 +04:00
|
|
|
{
|
2020-11-10 10:14:56 +03:00
|
|
|
size_t i;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
|
|
|
if (!lpFileName)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
2014-06-19 14:03:36 +04:00
|
|
|
if (pthread_once(&_HandleCreatorsInitialized, _HandleCreatorsInit) != 0)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_DLL_INIT_FAILED);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2014-04-22 19:00:14 +04:00
|
|
|
if (_HandleCreators == NULL)
|
2014-06-19 14:03:36 +04:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_DLL_INIT_FAILED);
|
2014-04-22 19:00:14 +04:00
|
|
|
return INVALID_HANDLE_VALUE;
|
2014-06-19 14:03:36 +04:00
|
|
|
}
|
2014-04-22 19:00:14 +04:00
|
|
|
|
2015-09-09 16:48:59 +03:00
|
|
|
ArrayList_Lock(_HandleCreators);
|
2014-07-03 13:20:46 +04:00
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
for (i = 0; i <= ArrayList_Count(_HandleCreators); i++)
|
2014-04-18 21:25:25 +04:00
|
|
|
{
|
2015-09-09 16:48:59 +03:00
|
|
|
HANDLE_CREATOR* creator = ArrayList_GetItem(_HandleCreators, i);
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2014-04-22 19:00:14 +04:00
|
|
|
if (creator && creator->IsHandled(lpFileName))
|
2014-04-18 21:25:25 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
HANDLE newHandle =
|
|
|
|
creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
|
|
|
|
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
|
2015-09-09 16:48:59 +03:00
|
|
|
ArrayList_Unlock(_HandleCreators);
|
2014-07-03 13:20:46 +04:00
|
|
|
return newHandle;
|
2014-04-18 21:25:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 16:48:59 +03:00
|
|
|
ArrayList_Unlock(_HandleCreators);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
2012-09-27 00:25:23 +04:00
|
|
|
}
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
HANDLE CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
|
2019-11-06 17:24:51 +03:00
|
|
|
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
|
|
|
|
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
|
2012-09-27 00:25:23 +04:00
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
HANDLE hdl = NULL;
|
|
|
|
if (!lpFileName)
|
2015-12-09 20:29:16 +03:00
|
|
|
return NULL;
|
2022-10-28 09:09:27 +03:00
|
|
|
char* lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL);
|
|
|
|
|
|
|
|
if (!lpFileNameA)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-12-09 20:29:16 +03:00
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
hdl = CreateFileA(lpFileNameA, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
|
|
|
|
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
|
2022-10-28 09:09:27 +03:00
|
|
|
fail:
|
2017-04-27 09:31:53 +03:00
|
|
|
free(lpFileNameA);
|
2015-12-09 20:29:16 +03:00
|
|
|
return hdl;
|
2012-09-27 00:25:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DeleteFileA(LPCSTR lpFileName)
|
|
|
|
{
|
2013-09-26 07:16:33 +04:00
|
|
|
int status;
|
|
|
|
status = unlink(lpFileName);
|
|
|
|
return (status != -1) ? TRUE : FALSE;
|
2012-09-27 00:25:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DeleteFileW(LPCWSTR lpFileName)
|
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpFileName)
|
|
|
|
return FALSE;
|
|
|
|
LPSTR lpFileNameA = ConvertWCharToUtf8Alloc(lpFileName, NULL);
|
2015-12-09 20:29:16 +03:00
|
|
|
BOOL rc = FALSE;
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpFileNameA)
|
|
|
|
goto fail;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
rc = DeleteFileA(lpFileNameA);
|
2022-10-28 09:09:27 +03:00
|
|
|
fail:
|
2017-04-27 09:31:53 +03:00
|
|
|
free(lpFileNameA);
|
2015-12-09 20:29:16 +03:00
|
|
|
return rc;
|
2012-09-27 00:25:23 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 20:37:35 +04:00
|
|
|
BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
|
2017-04-27 09:31:53 +03:00
|
|
|
LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2012-10-08 04:25:50 +04:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2014-02-07 18:19:46 +04:00
|
|
|
|
2014-08-18 20:57:08 +04:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
2014-03-11 17:34:41 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
/*
|
|
|
|
* from http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
|
2014-02-07 18:19:46 +04:00
|
|
|
* lpNumberOfBytesRead can be NULL only when the lpOverlapped parameter is not NULL.
|
|
|
|
*/
|
2014-02-17 22:02:39 +04:00
|
|
|
|
2014-02-07 18:19:46 +04:00
|
|
|
if (!lpNumberOfBytesRead && !lpOverlapped)
|
|
|
|
return FALSE;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2015-07-03 10:36:58 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
2012-10-08 04:25:50 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-03-16 23:29:19 +03:00
|
|
|
if (handle->ops->ReadFile)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->ReadFile(handle, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead,
|
|
|
|
lpOverlapped);
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2015-03-16 23:29:19 +03:00
|
|
|
WLog_ERR(TAG, "ReadFile operation not implemented");
|
2012-10-08 04:25:50 +04:00
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ReadFileEx(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
|
2017-04-27 09:31:53 +03:00
|
|
|
LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->ReadFileEx)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->ReadFileEx(handle, lpBuffer, nNumberOfBytesToRead, lpOverlapped,
|
|
|
|
lpCompletionRoutine);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "ReadFileEx operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL ReadFileScatter(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToRead,
|
|
|
|
LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->ReadFileScatter)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->ReadFileScatter(handle, aSegmentArray, nNumberOfBytesToRead, lpReserved,
|
|
|
|
lpOverlapped);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "ReadFileScatter operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
|
2017-04-27 09:31:53 +03:00
|
|
|
LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2012-10-08 04:25:50 +04:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2014-08-18 20:57:08 +04:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
2014-03-11 17:34:41 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2015-07-03 10:36:58 +03:00
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
2012-10-08 04:25:50 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-03-16 23:29:19 +03:00
|
|
|
if (handle->ops->WriteFile)
|
2015-12-09 20:29:16 +03:00
|
|
|
return handle->ops->WriteFile(handle, lpBuffer, nNumberOfBytesToWrite,
|
2017-04-27 09:31:53 +03:00
|
|
|
lpNumberOfBytesWritten, lpOverlapped);
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
WLog_ERR(TAG, "WriteFile operation not implemented");
|
2012-10-08 04:25:50 +04:00
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WriteFileEx(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
|
2017-04-27 09:31:53 +03:00
|
|
|
LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->WriteFileEx)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->WriteFileEx(handle, lpBuffer, nNumberOfBytesToWrite, lpOverlapped,
|
|
|
|
lpCompletionRoutine);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "WriteFileEx operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[],
|
2017-04-27 09:31:53 +03:00
|
|
|
DWORD nNumberOfBytesToWrite, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->WriteFileGather)
|
|
|
|
return handle->ops->WriteFileGather(handle, aSegmentArray, nNumberOfBytesToWrite,
|
2017-04-27 09:31:53 +03:00
|
|
|
lpReserved, lpOverlapped);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "WriteFileGather operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FlushFileBuffers(HANDLE hFile)
|
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->FlushFileBuffers)
|
|
|
|
return handle->ops->FlushFileBuffers(handle);
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "FlushFileBuffers operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
BOOL WINAPI GetFileAttributesExA(LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
|
2017-04-06 01:35:03 +03:00
|
|
|
LPVOID lpFileInformation)
|
2016-12-01 00:47:06 +03:00
|
|
|
{
|
|
|
|
LPWIN32_FILE_ATTRIBUTE_DATA fd = lpFileInformation;
|
|
|
|
WIN32_FIND_DATAA findFileData;
|
|
|
|
HANDLE hFind;
|
|
|
|
|
2018-07-30 11:46:20 +03:00
|
|
|
if (!fd)
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-11 12:32:45 +03:00
|
|
|
if ((hFind = FindFirstFileA(lpFileName, &findFileData)) == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
FindClose(hFind);
|
2016-12-01 00:47:06 +03:00
|
|
|
fd->dwFileAttributes = findFileData.dwFileAttributes;
|
|
|
|
fd->ftCreationTime = findFileData.ftCreationTime;
|
|
|
|
fd->ftLastAccessTime = findFileData.ftLastAccessTime;
|
|
|
|
fd->ftLastWriteTime = findFileData.ftLastWriteTime;
|
|
|
|
fd->nFileSizeHigh = findFileData.nFileSizeHigh;
|
|
|
|
fd->nFileSizeLow = findFileData.nFileSizeLow;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
|
2017-04-06 01:35:03 +03:00
|
|
|
LPVOID lpFileInformation)
|
2016-12-01 00:47:06 +03:00
|
|
|
{
|
|
|
|
BOOL ret;
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpFileName)
|
|
|
|
return FALSE;
|
|
|
|
LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
|
2017-03-17 00:48:02 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpCFileName)
|
2017-03-17 00:48:02 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
ret = GetFileAttributesExA(lpCFileName, fInfoLevelId, lpFileInformation);
|
|
|
|
free(lpCFileName);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName)
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATAA findFileData;
|
|
|
|
HANDLE hFind;
|
|
|
|
|
|
|
|
if ((hFind = FindFirstFileA(lpFileName, &findFileData)) == INVALID_HANDLE_VALUE)
|
|
|
|
return INVALID_FILE_ATTRIBUTES;
|
|
|
|
|
|
|
|
FindClose(hFind);
|
|
|
|
return findFileData.dwFileAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpFileName)
|
|
|
|
return FALSE;
|
|
|
|
LPSTR lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
|
|
|
|
if (!lpCFileName)
|
2017-03-17 00:48:02 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
ret = GetFileAttributesA(lpCFileName);
|
|
|
|
free(lpCFileName);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:25:40 +03:00
|
|
|
BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation)
|
|
|
|
{
|
|
|
|
ULONG Type;
|
|
|
|
WINPR_HANDLE* handle;
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
|
|
|
if (handle->ops->GetFileInformationByHandle)
|
|
|
|
return handle->ops->GetFileInformationByHandle(handle, lpFileInformation);
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "GetFileInformationByHandle operation not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:07:57 +03:00
|
|
|
static char* append(char* buffer, size_t size, const char* append)
|
|
|
|
{
|
2022-06-03 11:25:30 +03:00
|
|
|
winpr_str_append(append, buffer, size, "|");
|
2021-09-06 10:07:57 +03:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* flagsToStr(char* buffer, size_t size, DWORD flags)
|
|
|
|
{
|
|
|
|
char strflags[32] = { 0 };
|
|
|
|
if (flags & FILE_ATTRIBUTE_READONLY)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_READONLY");
|
|
|
|
if (flags & FILE_ATTRIBUTE_HIDDEN)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_HIDDEN");
|
|
|
|
if (flags & FILE_ATTRIBUTE_SYSTEM)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_SYSTEM");
|
|
|
|
if (flags & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_DIRECTORY");
|
|
|
|
if (flags & FILE_ATTRIBUTE_ARCHIVE)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_ARCHIVE");
|
|
|
|
if (flags & FILE_ATTRIBUTE_DEVICE)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_DEVICE");
|
|
|
|
if (flags & FILE_ATTRIBUTE_NORMAL)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_NORMAL");
|
|
|
|
if (flags & FILE_ATTRIBUTE_TEMPORARY)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_TEMPORARY");
|
|
|
|
if (flags & FILE_ATTRIBUTE_SPARSE_FILE)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_SPARSE_FILE");
|
|
|
|
if (flags & FILE_ATTRIBUTE_REPARSE_POINT)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_REPARSE_POINT");
|
|
|
|
if (flags & FILE_ATTRIBUTE_COMPRESSED)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_COMPRESSED");
|
|
|
|
if (flags & FILE_ATTRIBUTE_OFFLINE)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_OFFLINE");
|
|
|
|
if (flags & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_NOT_CONTENT_INDEXED");
|
|
|
|
if (flags & FILE_ATTRIBUTE_ENCRYPTED)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_ENCRYPTED");
|
|
|
|
if (flags & FILE_ATTRIBUTE_VIRTUAL)
|
|
|
|
append(buffer, size, "FILE_ATTRIBUTE_VIRTUAL");
|
|
|
|
|
|
|
|
_snprintf(strflags, sizeof(strflags), " [0x%08" PRIx32 "]", flags);
|
2022-06-03 11:25:30 +03:00
|
|
|
winpr_str_append(strflags, buffer, size, NULL);
|
2021-09-06 10:07:57 +03:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
BOOL SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2021-09-06 10:07:57 +03:00
|
|
|
int fd;
|
|
|
|
BOOL rc = FALSE;
|
2016-12-01 00:47:06 +03:00
|
|
|
|
2021-09-06 10:07:57 +03:00
|
|
|
if (dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)
|
2016-12-01 00:47:06 +03:00
|
|
|
{
|
2021-09-06 10:07:57 +03:00
|
|
|
char buffer[8192] = { 0 };
|
|
|
|
const char* flags =
|
|
|
|
flagsToStr(buffer, sizeof(buffer), dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_WARN(TAG, "Unsupported flags %s, ignoring!", flags);
|
2016-12-01 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 10:07:57 +03:00
|
|
|
fd = open(lpFileName, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) != 0)
|
|
|
|
goto fail;
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
|
|
|
|
{
|
|
|
|
st.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st.st_mode |= S_IWUSR;
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:07:57 +03:00
|
|
|
if (fchmod(fd, st.st_mode) != 0)
|
|
|
|
goto fail;
|
2016-12-01 00:47:06 +03:00
|
|
|
|
2021-09-06 10:07:57 +03:00
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
|
|
|
close(fd);
|
|
|
|
return rc;
|
2016-12-01 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
LPSTR lpCFileName;
|
2017-03-17 00:48:02 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpFileName)
|
|
|
|
return FALSE;
|
|
|
|
|
2021-09-06 10:07:57 +03:00
|
|
|
if (dwFileAttributes & ~FILE_ATTRIBUTE_READONLY)
|
|
|
|
{
|
|
|
|
char buffer[8192] = { 0 };
|
|
|
|
const char* flags =
|
|
|
|
flagsToStr(buffer, sizeof(buffer), dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_WARN(TAG, "Unsupported flags %s, ignoring!", flags);
|
2021-09-06 10:07:57 +03:00
|
|
|
}
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
lpCFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
|
|
|
|
if (!lpCFileName)
|
2017-03-17 00:48:02 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
ret = SetFileAttributesA(lpCFileName, dwFileAttributes);
|
2016-12-01 00:47:06 +03:00
|
|
|
free(lpCFileName);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:37:35 +04:00
|
|
|
BOOL SetEndOfFile(HANDLE hFile)
|
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->SetEndOfFile)
|
|
|
|
return handle->ops->SetEndOfFile(handle);
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "SetEndOfFile operation not implemented");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
|
|
|
|
{
|
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->GetFileSize)
|
|
|
|
return handle->ops->GetFileSize(handle, lpFileSizeHigh);
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "GetFileSize operation not implemented");
|
|
|
|
return 0;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh,
|
|
|
|
DWORD dwMoveMethod)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->SetFilePointer)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->SetFilePointer(handle, lDistanceToMove, lpDistanceToMoveHigh,
|
|
|
|
dwMoveMethod);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "SetFilePointer operation not implemented");
|
|
|
|
return 0;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer,
|
|
|
|
DWORD dwMoveMethod)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->SetFilePointerEx)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->SetFilePointerEx(handle, liDistanceToMove, lpNewFilePointer,
|
|
|
|
dwMoveMethod);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "SetFilePointerEx operation not implemented");
|
|
|
|
return 0;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
|
2017-04-27 09:31:53 +03:00
|
|
|
DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->LockFile)
|
|
|
|
return handle->ops->LockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
|
2017-04-27 09:31:53 +03:00
|
|
|
nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "LockFile operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved, DWORD nNumberOfBytesToLockLow,
|
|
|
|
DWORD nNumberOfBytesToLockHigh, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->LockFileEx)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->LockFileEx(handle, dwFlags, dwReserved, nNumberOfBytesToLockLow,
|
|
|
|
nNumberOfBytesToLockHigh, lpOverlapped);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "LockFileEx operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
|
2017-04-27 09:31:53 +03:00
|
|
|
DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->UnlockFile)
|
|
|
|
return handle->ops->UnlockFile(handle, dwFileOffsetLow, dwFileOffsetHigh,
|
2017-04-27 09:31:53 +03:00
|
|
|
nNumberOfBytesToUnlockLow, nNumberOfBytesToUnlockHigh);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "UnLockFile operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
2012-09-24 23:58:33 +04:00
|
|
|
|
2012-09-26 20:37:35 +04:00
|
|
|
BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow,
|
2017-04-27 09:31:53 +03:00
|
|
|
DWORD nNumberOfBytesToUnlockHigh, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2015-12-09 20:29:16 +03:00
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2015-12-09 20:29:16 +03:00
|
|
|
if (handle->ops->UnlockFileEx)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->UnlockFileEx(handle, dwReserved, nNumberOfBytesToUnlockLow,
|
|
|
|
nNumberOfBytesToUnlockHigh, lpOverlapped);
|
2015-12-09 20:29:16 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "UnLockFileEx operation not implemented");
|
|
|
|
return FALSE;
|
2012-09-26 20:37:35 +04:00
|
|
|
}
|
2012-09-24 23:58:33 +04:00
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
BOOL WINAPI SetFileTime(HANDLE hFile, const FILETIME* lpCreationTime,
|
|
|
|
const FILETIME* lpLastAccessTime, const FILETIME* lpLastWriteTime)
|
2016-02-26 12:48:53 +03:00
|
|
|
{
|
|
|
|
ULONG Type;
|
2017-04-27 09:31:53 +03:00
|
|
|
WINPR_HANDLE* handle;
|
2016-02-26 12:48:53 +03:00
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &handle))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
handle = (WINPR_HANDLE*)hFile;
|
|
|
|
|
2016-02-26 12:48:53 +03:00
|
|
|
if (handle->ops->SetFileTime)
|
2019-11-06 17:24:51 +03:00
|
|
|
return handle->ops->SetFileTime(handle, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
|
2016-02-26 12:48:53 +03:00
|
|
|
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_ERR(TAG, "operation not implemented");
|
2016-02-26 12:48:53 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2012-11-04 01:24:52 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
DIR* pDir;
|
2012-11-04 01:24:52 +04:00
|
|
|
LPSTR lpPath;
|
|
|
|
LPSTR lpPattern;
|
2014-08-18 21:34:47 +04:00
|
|
|
struct dirent* pDirent;
|
2022-02-14 16:59:22 +03:00
|
|
|
} WIN32_FILE_SEARCH;
|
2012-11-04 01:24:52 +04:00
|
|
|
|
2018-07-11 11:53:26 +03:00
|
|
|
static BOOL FindDataFromStat(const char* path, const struct stat* fileStat,
|
|
|
|
LPWIN32_FIND_DATAA lpFindFileData)
|
|
|
|
{
|
|
|
|
UINT64 ft;
|
|
|
|
char* lastSep;
|
|
|
|
lpFindFileData->dwFileAttributes = 0;
|
|
|
|
|
|
|
|
if (S_ISDIR(fileStat->st_mode))
|
|
|
|
lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
|
|
|
|
if (lpFindFileData->dwFileAttributes == 0)
|
|
|
|
lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
|
|
|
|
|
|
|
|
lastSep = strrchr(path, '/');
|
|
|
|
|
|
|
|
if (lastSep)
|
|
|
|
{
|
|
|
|
const char* name = lastSep + 1;
|
|
|
|
const size_t namelen = strlen(name);
|
|
|
|
|
2018-07-11 17:27:14 +03:00
|
|
|
if ((namelen > 1) && (name[0] == '.') && (name[1] != '.'))
|
2018-07-11 11:53:26 +03:00
|
|
|
lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_HIDDEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(fileStat->st_mode & S_IWUSR))
|
|
|
|
lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
|
|
|
|
|
|
|
|
#ifdef _DARWIN_FEATURE_64_BIT_INODE
|
|
|
|
ft = STAT_TIME_TO_FILETIME(fileStat->st_birthtime);
|
|
|
|
#else
|
|
|
|
ft = STAT_TIME_TO_FILETIME(fileStat->st_ctime);
|
|
|
|
#endif
|
|
|
|
lpFindFileData->ftCreationTime.dwHighDateTime = ((UINT64)ft) >> 32ULL;
|
|
|
|
lpFindFileData->ftCreationTime.dwLowDateTime = ft & 0xFFFFFFFF;
|
|
|
|
ft = STAT_TIME_TO_FILETIME(fileStat->st_mtime);
|
|
|
|
lpFindFileData->ftLastWriteTime.dwHighDateTime = ((UINT64)ft) >> 32ULL;
|
|
|
|
lpFindFileData->ftLastWriteTime.dwLowDateTime = ft & 0xFFFFFFFF;
|
|
|
|
ft = STAT_TIME_TO_FILETIME(fileStat->st_atime);
|
|
|
|
lpFindFileData->ftLastAccessTime.dwHighDateTime = ((UINT64)ft) >> 32ULL;
|
|
|
|
lpFindFileData->ftLastAccessTime.dwLowDateTime = ft & 0xFFFFFFFF;
|
|
|
|
lpFindFileData->nFileSizeHigh = ((UINT64)fileStat->st_size) >> 32ULL;
|
|
|
|
lpFindFileData->nFileSizeLow = fileStat->st_size & 0xFFFFFFFF;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
|
|
|
|
{
|
2018-07-11 11:58:37 +03:00
|
|
|
BOOL isDir = FALSE;
|
2012-11-03 04:58:58 +04:00
|
|
|
struct stat fileStat;
|
2014-08-18 21:34:47 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
2018-07-30 11:46:20 +03:00
|
|
|
|
|
|
|
if (!lpFindFileData || !lpFileName)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_BAD_ARGUMENTS);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2012-11-23 00:32:21 +04:00
|
|
|
ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
|
2019-11-06 17:24:51 +03:00
|
|
|
pFileSearch = (WIN32_FILE_SEARCH*)calloc(1, sizeof(WIN32_FILE_SEARCH));
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!pFileSearch)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2018-07-11 11:58:37 +03:00
|
|
|
if (stat(lpFileName, &fileStat) >= 0)
|
2015-04-03 17:21:01 +03:00
|
|
|
{
|
2018-07-11 11:58:37 +03:00
|
|
|
isDir = (S_ISDIR(fileStat.st_mode) != 0);
|
2015-04-03 17:21:01 +03:00
|
|
|
}
|
2018-07-11 17:27:14 +03:00
|
|
|
else
|
|
|
|
errno = 0;
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2018-07-11 11:58:37 +03:00
|
|
|
if (isDir)
|
2015-04-03 17:21:01 +03:00
|
|
|
{
|
2018-07-11 11:58:37 +03:00
|
|
|
pFileSearch->lpPath = _strdup(lpFileName);
|
2018-08-13 14:17:56 +03:00
|
|
|
pFileSearch->lpPattern = _strdup(".");
|
2015-04-03 17:21:01 +03:00
|
|
|
}
|
2018-07-11 11:58:37 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LPSTR p;
|
|
|
|
size_t index;
|
|
|
|
size_t length;
|
|
|
|
/* Separate lpFileName into path and pattern components */
|
|
|
|
p = strrchr(lpFileName, '/');
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2018-07-11 11:58:37 +03:00
|
|
|
if (!p)
|
|
|
|
p = strrchr(lpFileName, '\\');
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2018-07-11 11:58:37 +03:00
|
|
|
index = (p - lpFileName);
|
|
|
|
length = (p - lpFileName) + 1;
|
2019-11-06 17:24:51 +03:00
|
|
|
pFileSearch->lpPath = (LPSTR)malloc(length + 1);
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2018-07-11 11:58:37 +03:00
|
|
|
if (!pFileSearch->lpPath)
|
|
|
|
{
|
|
|
|
free(pFileSearch);
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2018-07-11 11:58:37 +03:00
|
|
|
CopyMemory(pFileSearch->lpPath, lpFileName, length);
|
|
|
|
pFileSearch->lpPath[length] = '\0';
|
|
|
|
length = strlen(lpFileName) - index;
|
2019-11-06 17:24:51 +03:00
|
|
|
pFileSearch->lpPattern = (LPSTR)malloc(length + 1);
|
2018-07-11 11:58:37 +03:00
|
|
|
|
|
|
|
if (!pFileSearch->lpPattern)
|
|
|
|
{
|
|
|
|
free(pFileSearch->lpPath);
|
|
|
|
free(pFileSearch);
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyMemory(pFileSearch->lpPattern, &lpFileName[index + 1], length);
|
|
|
|
pFileSearch->lpPattern[length] = '\0';
|
|
|
|
|
|
|
|
/* Check if the path is a directory */
|
|
|
|
|
|
|
|
if (stat(pFileSearch->lpPath, &fileStat) < 0)
|
|
|
|
{
|
|
|
|
FindClose(pFileSearch);
|
2018-07-11 17:27:14 +03:00
|
|
|
SetLastError(map_posix_err(errno));
|
|
|
|
errno = 0;
|
2018-07-11 11:58:37 +03:00
|
|
|
return INVALID_HANDLE_VALUE; /* stat error */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(fileStat.st_mode) == 0)
|
|
|
|
{
|
|
|
|
FindClose(pFileSearch);
|
|
|
|
return INVALID_HANDLE_VALUE; /* not a directory */
|
|
|
|
}
|
2012-11-04 01:24:52 +04:00
|
|
|
}
|
2012-11-03 04:58:58 +04:00
|
|
|
|
|
|
|
/* Open directory for reading */
|
2012-11-04 01:24:52 +04:00
|
|
|
pFileSearch->pDir = opendir(pFileSearch->lpPath);
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2012-11-04 01:24:52 +04:00
|
|
|
if (!pFileSearch->pDir)
|
|
|
|
{
|
2013-09-02 18:25:34 +04:00
|
|
|
FindClose(pFileSearch);
|
2018-07-11 17:27:14 +03:00
|
|
|
SetLastError(map_posix_err(errno));
|
|
|
|
errno = 0;
|
2012-11-04 01:24:52 +04:00
|
|
|
return INVALID_HANDLE_VALUE; /* failed to open directory */
|
|
|
|
}
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (FindNextFileA((HANDLE)pFileSearch, lpFindFileData))
|
2018-08-23 11:04:26 +03:00
|
|
|
{
|
|
|
|
if (isDir)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
const char* name = strrchr(lpFileName, '/');
|
2018-08-23 11:04:26 +03:00
|
|
|
|
|
|
|
if (!name)
|
|
|
|
name = lpFileName;
|
|
|
|
else
|
|
|
|
name++;
|
|
|
|
|
|
|
|
pFileSearch->lpPattern[0] = '*';
|
2018-11-29 17:50:43 +03:00
|
|
|
sprintf_s(lpFindFileData->cFileName, ARRAYSIZE(lpFindFileData->cFileName), "%s", name);
|
2018-08-23 11:04:26 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return (HANDLE)pFileSearch;
|
2018-08-23 11:04:26 +03:00
|
|
|
}
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2013-09-02 18:25:34 +04:00
|
|
|
FindClose(pFileSearch);
|
2012-11-04 07:04:04 +04:00
|
|
|
return INVALID_HANDLE_VALUE;
|
2012-10-18 23:37:00 +04:00
|
|
|
}
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
static BOOL ConvertFindDataAToW(LPWIN32_FIND_DATAA lpFindFileDataA,
|
|
|
|
LPWIN32_FIND_DATAW lpFindFileDataW)
|
|
|
|
{
|
|
|
|
if (!lpFindFileDataA || !lpFindFileDataW)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lpFindFileDataW->dwFileAttributes = lpFindFileDataA->dwFileAttributes;
|
|
|
|
lpFindFileDataW->ftCreationTime = lpFindFileDataA->ftCreationTime;
|
|
|
|
lpFindFileDataW->ftLastAccessTime = lpFindFileDataA->ftLastAccessTime;
|
|
|
|
lpFindFileDataW->ftLastWriteTime = lpFindFileDataA->ftLastWriteTime;
|
|
|
|
lpFindFileDataW->nFileSizeHigh = lpFindFileDataA->nFileSizeHigh;
|
|
|
|
lpFindFileDataW->nFileSizeLow = lpFindFileDataA->nFileSizeLow;
|
|
|
|
lpFindFileDataW->dwReserved0 = lpFindFileDataA->dwReserved0;
|
|
|
|
lpFindFileDataW->dwReserved1 = lpFindFileDataA->dwReserved1;
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
if (ConvertUtf8NToWChar(lpFindFileDataA->cFileName, ARRAYSIZE(lpFindFileDataA->cFileName),
|
|
|
|
lpFindFileDataW->cFileName, ARRAYSIZE(lpFindFileDataW->cFileName)) < 0)
|
2017-04-27 09:31:53 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
return ConvertUtf8NToWChar(lpFindFileDataA->cAlternateFileName,
|
|
|
|
ARRAYSIZE(lpFindFileDataA->cAlternateFileName),
|
|
|
|
lpFindFileDataW->cAlternateFileName,
|
|
|
|
ARRAYSIZE(lpFindFileDataW->cAlternateFileName)) >= 0;
|
2017-04-27 09:31:53 +03:00
|
|
|
}
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
HANDLE FindFirstFileW(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData)
|
|
|
|
{
|
2017-04-27 09:58:01 +03:00
|
|
|
LPSTR utfFileName = NULL;
|
2017-03-27 21:11:54 +03:00
|
|
|
HANDLE h;
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpFileName)
|
|
|
|
return FALSE;
|
2017-04-27 09:31:53 +03:00
|
|
|
LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)calloc(1, sizeof(WIN32_FIND_DATAA));
|
2017-03-27 21:11:54 +03:00
|
|
|
|
2016-12-01 00:54:03 +03:00
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
utfFileName = ConvertWCharToUtf8Alloc(lpFileName, NULL);
|
|
|
|
if (!utfFileName)
|
2017-03-17 00:48:02 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2017-04-06 18:46:07 +03:00
|
|
|
free(fd);
|
2017-03-17 00:48:02 +03:00
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2016-12-01 00:54:03 +03:00
|
|
|
|
2017-03-27 21:11:54 +03:00
|
|
|
h = FindFirstFileA(utfFileName, fd);
|
2016-12-01 00:54:03 +03:00
|
|
|
free(utfFileName);
|
|
|
|
|
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2017-04-27 09:31:53 +03:00
|
|
|
if (!ConvertFindDataAToW(fd, lpFindFileData))
|
2016-12-01 00:54:03 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2018-08-20 10:47:33 +03:00
|
|
|
FindClose(h);
|
2017-06-22 05:21:20 +03:00
|
|
|
h = INVALID_HANDLE_VALUE;
|
|
|
|
goto out;
|
2016-12-01 00:54:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 05:21:20 +03:00
|
|
|
out:
|
2017-06-13 11:46:37 +03:00
|
|
|
free(fd);
|
2016-12-01 00:54:03 +03:00
|
|
|
return h;
|
2012-10-18 23:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE FindFirstFileExA(LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
|
2017-04-27 09:31:53 +03:00
|
|
|
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
|
2012-10-18 23:37:00 +04:00
|
|
|
{
|
2016-12-01 00:54:03 +03:00
|
|
|
return INVALID_HANDLE_VALUE;
|
2012-10-18 23:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE FindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
|
2017-04-27 09:31:53 +03:00
|
|
|
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
|
2012-10-18 23:37:00 +04:00
|
|
|
{
|
2016-12-01 00:54:03 +03:00
|
|
|
return INVALID_HANDLE_VALUE;
|
2012-10-18 23:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
2016-12-01 00:54:03 +03:00
|
|
|
struct stat fileStat;
|
|
|
|
char* fullpath;
|
2017-07-17 13:31:45 +03:00
|
|
|
size_t pathlen;
|
|
|
|
size_t namelen;
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2018-07-30 11:46:20 +03:00
|
|
|
if (!hFindFile || !lpFindFileData)
|
2012-11-04 07:04:04 +04:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (hFindFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
2018-07-30 11:46:20 +03:00
|
|
|
ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
|
2019-11-06 17:24:51 +03:00
|
|
|
pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
|
2012-11-04 02:31:01 +04:00
|
|
|
|
|
|
|
while ((pFileSearch->pDirent = readdir(pFileSearch->pDir)) != NULL)
|
|
|
|
{
|
|
|
|
if (FilePatternMatchA(pFileSearch->pDirent->d_name, pFileSearch->lpPattern))
|
|
|
|
{
|
2018-07-11 11:53:26 +03:00
|
|
|
BOOL success;
|
2017-08-16 11:25:23 +03:00
|
|
|
strncpy(lpFindFileData->cFileName, pFileSearch->pDirent->d_name, MAX_PATH);
|
|
|
|
namelen = strnlen(lpFindFileData->cFileName, MAX_PATH);
|
2016-12-01 00:54:03 +03:00
|
|
|
pathlen = strlen(pFileSearch->lpPath);
|
|
|
|
fullpath = (char*)malloc(pathlen + namelen + 2);
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2017-03-27 21:11:54 +03:00
|
|
|
if (fullpath == NULL)
|
|
|
|
{
|
2017-04-01 01:33:28 +03:00
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return FALSE;
|
2016-12-01 00:54:03 +03:00
|
|
|
}
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2016-12-01 00:54:03 +03:00
|
|
|
memcpy(fullpath, pFileSearch->lpPath, pathlen);
|
2019-01-30 17:43:46 +03:00
|
|
|
/* Ensure path is terminated with a separator, but prevent
|
|
|
|
* duplicate separators */
|
2019-11-06 17:24:51 +03:00
|
|
|
if (fullpath[pathlen - 1] != '/')
|
2019-01-30 17:43:46 +03:00
|
|
|
fullpath[pathlen++] = '/';
|
|
|
|
memcpy(fullpath + pathlen, pFileSearch->pDirent->d_name, namelen);
|
|
|
|
fullpath[pathlen + namelen] = 0;
|
2016-12-01 00:54:03 +03:00
|
|
|
|
2017-04-19 09:28:00 +03:00
|
|
|
if (stat(fullpath, &fileStat) != 0)
|
2017-03-27 21:11:54 +03:00
|
|
|
{
|
|
|
|
free(fullpath);
|
2017-04-01 01:33:28 +03:00
|
|
|
SetLastError(map_posix_err(errno));
|
2018-07-11 17:27:14 +03:00
|
|
|
errno = 0;
|
2017-08-16 11:25:23 +03:00
|
|
|
continue;
|
2017-03-27 21:11:54 +03:00
|
|
|
}
|
|
|
|
|
2017-08-04 10:01:13 +03:00
|
|
|
/* Skip FIFO entries. */
|
|
|
|
if (S_ISFIFO(fileStat.st_mode))
|
2018-07-11 11:53:26 +03:00
|
|
|
{
|
|
|
|
free(fullpath);
|
2017-08-04 10:01:13 +03:00
|
|
|
continue;
|
2018-07-11 11:53:26 +03:00
|
|
|
}
|
2016-12-01 00:54:03 +03:00
|
|
|
|
2018-07-11 11:53:26 +03:00
|
|
|
success = FindDataFromStat(fullpath, &fileStat, lpFindFileData);
|
|
|
|
free(fullpath);
|
|
|
|
return success;
|
2012-11-04 02:31:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 00:54:03 +03:00
|
|
|
SetLastError(ERROR_NO_MORE_FILES);
|
2012-10-18 23:37:00 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData)
|
|
|
|
{
|
2017-04-27 09:31:53 +03:00
|
|
|
LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)calloc(1, sizeof(WIN32_FIND_DATAA));
|
2017-03-27 21:11:54 +03:00
|
|
|
|
2016-12-01 00:54:03 +03:00
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2017-04-01 01:33:28 +03:00
|
|
|
return FALSE;
|
2016-12-01 00:54:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FindNextFileA(hFindFile, fd))
|
|
|
|
{
|
2017-04-27 09:31:53 +03:00
|
|
|
if (!ConvertFindDataAToW(fd, lpFindFileData))
|
2016-12-01 00:54:03 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2017-03-27 21:11:54 +03:00
|
|
|
free(fd);
|
2016-12-01 00:54:03 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-04-27 09:31:53 +03:00
|
|
|
free(fd);
|
2016-12-01 00:54:03 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(fd);
|
2012-10-18 23:37:00 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FindClose(HANDLE hFindFile)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch = (WIN32_FILE_SEARCH*)hFindFile;
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2019-01-29 19:16:46 +03:00
|
|
|
/* Since INVALID_HANDLE_VALUE != NULL the analyzer guesses that there
|
|
|
|
* is a initialized HANDLE that is not freed properly.
|
|
|
|
* Disable this return to stop confusing the analyzer. */
|
|
|
|
#ifndef __clang_analyzer__
|
2017-04-06 18:46:07 +03:00
|
|
|
if (!pFileSearch || (pFileSearch == INVALID_HANDLE_VALUE))
|
|
|
|
return FALSE;
|
2019-01-29 19:16:46 +03:00
|
|
|
#endif
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2017-04-06 18:46:07 +03:00
|
|
|
free(pFileSearch->lpPath);
|
|
|
|
free(pFileSearch->lpPattern);
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2017-04-06 18:46:07 +03:00
|
|
|
if (pFileSearch->pDir)
|
|
|
|
closedir(pFileSearch->pDir);
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2017-04-06 18:46:07 +03:00
|
|
|
free(pFileSearch);
|
|
|
|
return TRUE;
|
2012-10-18 23:37:00 +04:00
|
|
|
}
|
|
|
|
|
2013-03-22 23:52:43 +04:00
|
|
|
BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
|
|
|
{
|
|
|
|
if (!mkdir(lpPathName, S_IRUSR | S_IWUSR | S_IXUSR))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
|
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpPathName)
|
|
|
|
return FALSE;
|
|
|
|
char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL);
|
|
|
|
BOOL ret = FALSE;
|
2016-12-01 00:47:06 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!utfPathName)
|
2017-03-17 00:48:02 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2022-10-28 09:09:27 +03:00
|
|
|
goto fail;
|
2017-03-17 00:48:02 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
ret = CreateDirectoryA(utfPathName, lpSecurityAttributes);
|
2022-10-28 09:09:27 +03:00
|
|
|
fail:
|
2016-12-01 00:47:06 +03:00
|
|
|
free(utfPathName);
|
|
|
|
return ret;
|
2015-06-03 13:35:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL RemoveDirectoryA(LPCSTR lpPathName)
|
|
|
|
{
|
2016-12-01 00:47:06 +03:00
|
|
|
int ret = rmdir(lpPathName);
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
if (ret != 0)
|
|
|
|
SetLastError(map_posix_err(errno));
|
|
|
|
else
|
|
|
|
SetLastError(STATUS_SUCCESS);
|
2017-04-27 09:31:53 +03:00
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
return ret == 0;
|
2015-06-03 13:35:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL RemoveDirectoryW(LPCWSTR lpPathName)
|
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpPathName)
|
|
|
|
return FALSE;
|
|
|
|
char* utfPathName = ConvertWCharToUtf8Alloc(lpPathName, NULL);
|
|
|
|
BOOL ret = FALSE;
|
2016-12-01 00:47:06 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!utfPathName)
|
2017-03-17 00:48:02 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2022-10-28 09:09:27 +03:00
|
|
|
goto fail;
|
2017-03-17 00:48:02 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
ret = RemoveDirectoryA(utfPathName);
|
2022-10-28 09:09:27 +03:00
|
|
|
fail:
|
2016-12-01 00:47:06 +03:00
|
|
|
free(utfPathName);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL MoveFileExA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
ret = stat(lpNewFileName, &st);
|
|
|
|
|
|
|
|
if ((dwFlags & MOVEFILE_REPLACE_EXISTING) == 0)
|
|
|
|
{
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_ALREADY_EXISTS);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ret == 0 && (st.st_mode & S_IWUSR) == 0)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_ACCESS_DENIED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rename(lpExistingFileName, lpNewFileName);
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
SetLastError(map_posix_err(errno));
|
|
|
|
|
|
|
|
return ret == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL MoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags)
|
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!lpExistingFileName || !lpNewFileName)
|
2017-03-17 00:48:02 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
LPSTR lpCExistingFileName = ConvertWCharToUtf8Alloc(lpExistingFileName, NULL);
|
|
|
|
LPSTR lpCNewFileName = ConvertWCharToUtf8Alloc(lpNewFileName, NULL);
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (!lpCExistingFileName || !lpCNewFileName)
|
2017-03-17 00:48:02 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
2022-10-28 09:09:27 +03:00
|
|
|
goto fail;
|
2017-03-17 00:48:02 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 00:47:06 +03:00
|
|
|
ret = MoveFileExA(lpCExistingFileName, lpCNewFileName, dwFlags);
|
2022-10-28 09:09:27 +03:00
|
|
|
fail:
|
2016-12-01 00:47:06 +03:00
|
|
|
free(lpCNewFileName);
|
|
|
|
free(lpCExistingFileName);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName)
|
|
|
|
{
|
|
|
|
return MoveFileExA(lpExistingFileName, lpNewFileName, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL MoveFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
|
|
|
|
{
|
|
|
|
return MoveFileExW(lpExistingFileName, lpNewFileName, 0);
|
2013-03-22 23:52:43 +04:00
|
|
|
}
|
|
|
|
|
2012-09-24 23:58:33 +04:00
|
|
|
#endif
|
|
|
|
|
2013-07-30 18:57:54 +04:00
|
|
|
/* Extended API */
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int UnixChangeFileMode(const char* filename, int flags)
|
2013-07-30 18:57:54 +04:00
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!filename)
|
|
|
|
return -1;
|
2013-08-09 03:45:31 +04:00
|
|
|
#ifndef _WIN32
|
2013-07-30 18:57:54 +04:00
|
|
|
mode_t fl = 0;
|
|
|
|
fl |= (flags & 0x4000) ? S_ISUID : 0;
|
|
|
|
fl |= (flags & 0x2000) ? S_ISGID : 0;
|
|
|
|
fl |= (flags & 0x1000) ? S_ISVTX : 0;
|
|
|
|
fl |= (flags & 0x0400) ? S_IRUSR : 0;
|
|
|
|
fl |= (flags & 0x0200) ? S_IWUSR : 0;
|
|
|
|
fl |= (flags & 0x0100) ? S_IXUSR : 0;
|
|
|
|
fl |= (flags & 0x0040) ? S_IRGRP : 0;
|
|
|
|
fl |= (flags & 0x0020) ? S_IWGRP : 0;
|
|
|
|
fl |= (flags & 0x0010) ? S_IXGRP : 0;
|
|
|
|
fl |= (flags & 0x0004) ? S_IROTH : 0;
|
|
|
|
fl |= (flags & 0x0002) ? S_IWOTH : 0;
|
|
|
|
fl |= (flags & 0x0001) ? S_IXOTH : 0;
|
|
|
|
return chmod(filename, fl);
|
2013-08-09 03:45:31 +04:00
|
|
|
#else
|
2016-02-26 14:53:23 +03:00
|
|
|
int rc;
|
2022-10-28 09:09:27 +03:00
|
|
|
WCHAR* wfl = ConvertUtf8ToWCharAlloc(filename, NULL);
|
2016-02-26 14:53:23 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
if (!wfl)
|
2016-02-26 14:53:23 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Check for unsupported flags. */
|
2016-06-04 18:04:12 +03:00
|
|
|
if (flags & ~(_S_IREAD | _S_IWRITE))
|
2016-02-26 14:53:23 +03:00
|
|
|
WLog_WARN(TAG, "Unsupported file mode %d for _wchmod", flags);
|
|
|
|
|
|
|
|
rc = _wchmod(wfl, flags);
|
2017-04-27 09:31:53 +03:00
|
|
|
free(wfl);
|
2016-02-26 14:53:23 +03:00
|
|
|
return rc;
|
2013-08-09 03:45:31 +04:00
|
|
|
#endif
|
2013-07-30 18:57:54 +04:00
|
|
|
}
|