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.
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-10-19 00:58:27 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-07-30 18:57:54 +04:00
|
|
|
#include <winpr/path.h>
|
2013-09-17 01:10:27 +04:00
|
|
|
#include <winpr/synch.h>
|
2013-10-01 20:03:08 +04:00
|
|
|
#include <winpr/error.h>
|
2012-10-08 04:25:50 +04:00
|
|
|
#include <winpr/handle.h>
|
2013-08-23 06:36:37 +04:00
|
|
|
#include <winpr/platform.h>
|
2014-04-18 21:25:25 +04:00
|
|
|
#include <winpr/collections.h>
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2012-09-24 23:58:33 +04:00
|
|
|
#include <winpr/file.h>
|
|
|
|
|
2012-10-08 04:25:50 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-03 04:58:58 +04:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#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")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2012-09-24 23:58:33 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-19 14:03:36 +04:00
|
|
|
#include <assert.h>
|
2012-11-03 04:58:58 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2014-06-19 14:03:36 +04:00
|
|
|
#include <pthread.h>
|
2012-11-03 04:58:58 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
2013-07-23 06:53:44 +04:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/un.h>
|
2012-11-03 04:58:58 +04:00
|
|
|
#include <sys/stat.h>
|
2013-07-23 06:53:44 +04:00
|
|
|
#include <sys/socket.h>
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
#ifdef HAVE_AIO_H
|
|
|
|
#undef HAVE_AIO_H /* disable for now, incomplete */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_AIO_H
|
|
|
|
#include <aio.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-03 04:58:58 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#else
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
#include "../handle/handle.h"
|
|
|
|
|
|
|
|
#include "../pipe/pipe.h"
|
|
|
|
|
2014-04-18 21:25:25 +04:00
|
|
|
/* TODO: FIXME: use of a wArrayList and split winpr-utils with
|
2014-04-22 19:00:14 +04:00
|
|
|
* winpr-collections to avoid a circular dependency
|
2014-04-18 21:25:25 +04:00
|
|
|
* _HandleCreators = ArrayList_New(TRUE);
|
|
|
|
*/
|
2014-06-19 14:03:36 +04:00
|
|
|
/* _HandleCreators is a NULL-terminated array with a maximun of HANDLE_CREATOR_MAX HANDLE_CREATOR */
|
2014-04-18 21:25:25 +04:00
|
|
|
#define HANDLE_CREATOR_MAX 128
|
2014-08-18 21:34:47 +04:00
|
|
|
static HANDLE_CREATOR** _HandleCreators = NULL;
|
2014-07-03 13:20:46 +04:00
|
|
|
static CRITICAL_SECTION _HandleCreatorsLock;
|
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
|
|
|
|
|
|
|
static HANDLE_CLOSE_CB _FileHandleCloseCb;
|
|
|
|
|
|
|
|
static BOOL FileCloseHandle(HANDLE handle);
|
|
|
|
|
|
|
|
static BOOL FileIsHandled(HANDLE handle)
|
|
|
|
{
|
|
|
|
WINPR_NAMED_PIPE* pFile = (WINPR_NAMED_PIPE*) handle;
|
|
|
|
|
|
|
|
if (!pFile || pFile->Type != HANDLE_TYPE_NAMED_PIPE)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FileCloseHandle(HANDLE handle) {
|
|
|
|
WINPR_NAMED_PIPE* pNamedPipe = (WINPR_NAMED_PIPE*) handle;
|
|
|
|
|
|
|
|
|
|
|
|
if (!FileIsHandled(handle))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (pNamedPipe->clientfd != -1)
|
|
|
|
{
|
|
|
|
//WLOG_DBG(TAG, "closing clientfd %d", pNamedPipe->clientfd);
|
|
|
|
close(pNamedPipe->clientfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pNamedPipe->serverfd != -1)
|
|
|
|
{
|
|
|
|
//WLOG_DBG(TAG, "closing serverfd %d", pNamedPipe->serverfd);
|
|
|
|
close(pNamedPipe->serverfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pNamedPipe->pfnUnrefNamedPipe)
|
|
|
|
pNamedPipe->pfnUnrefNamedPipe(pNamedPipe);
|
|
|
|
|
|
|
|
if (pNamedPipe->lpFileName)
|
|
|
|
free((void*)pNamedPipe->lpFileName);
|
|
|
|
|
|
|
|
if (pNamedPipe->lpFilePath)
|
|
|
|
free((void*)pNamedPipe->lpFilePath);
|
|
|
|
|
|
|
|
if (pNamedPipe->name)
|
|
|
|
free((void*)pNamedPipe->name);
|
|
|
|
|
|
|
|
free(pNamedPipe);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-04-18 21:25:25 +04:00
|
|
|
static void _HandleCreatorsInit()
|
|
|
|
{
|
2014-06-19 14:03:36 +04:00
|
|
|
/* NB: error management to be done outside of this function */
|
|
|
|
assert(_HandleCreators == NULL);
|
2014-08-18 21:34:47 +04:00
|
|
|
_HandleCreators = (HANDLE_CREATOR**)calloc(HANDLE_CREATOR_MAX+1, sizeof(HANDLE_CREATOR*));
|
2014-07-03 13:20:46 +04:00
|
|
|
InitializeCriticalSection(&_HandleCreatorsLock);
|
2014-06-19 14:03:36 +04:00
|
|
|
assert(_HandleCreators != NULL);
|
2015-03-11 17:10:52 +03:00
|
|
|
|
|
|
|
_FileHandleCloseCb.IsHandled = FileIsHandled;
|
|
|
|
_FileHandleCloseCb.CloseHandle = FileCloseHandle;
|
|
|
|
RegisterHandleCloseCb(&_FileHandleCloseCb);
|
2014-04-18 21:25:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE on success, FALSE otherwise.
|
|
|
|
*
|
|
|
|
* ERRORS:
|
2014-06-19 14:03:36 +04:00
|
|
|
* ERROR_DLL_INIT_FAILED
|
2014-04-18 21:25:25 +04:00
|
|
|
* ERROR_INSUFFICIENT_BUFFER _HandleCreators full
|
|
|
|
*/
|
|
|
|
BOOL RegisterHandleCreator(PHANDLE_CREATOR pHandleCreator)
|
|
|
|
{
|
|
|
|
int i;
|
2014-04-22 19:00:14 +04:00
|
|
|
|
2014-06-19 14:03:36 +04:00
|
|
|
if (pthread_once(&_HandleCreatorsInitialized, _HandleCreatorsInit) != 0)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_DLL_INIT_FAILED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_HandleCreators == NULL)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_DLL_INIT_FAILED);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:20:46 +04:00
|
|
|
EnterCriticalSection(&_HandleCreatorsLock);
|
|
|
|
|
2014-04-18 21:25:25 +04:00
|
|
|
for (i=0; i<HANDLE_CREATOR_MAX; i++)
|
|
|
|
{
|
|
|
|
if (_HandleCreators[i] == NULL)
|
|
|
|
{
|
|
|
|
_HandleCreators[i] = pHandleCreator;
|
2014-07-03 13:20:46 +04:00
|
|
|
LeaveCriticalSection(&_HandleCreatorsLock);
|
2014-04-18 21:25:25 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
2014-07-03 13:20:46 +04:00
|
|
|
LeaveCriticalSection(&_HandleCreatorsLock);
|
2014-04-18 21:25:25 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
#ifdef HAVE_AIO_H
|
|
|
|
|
|
|
|
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;
|
2014-08-18 21:34:47 +04:00
|
|
|
action.sa_sigaction = (void*) &AioSignalHandler;
|
2014-02-17 22:02:39 +04:00
|
|
|
sigaction(SIGIO, &action, NULL);
|
|
|
|
g_AioSignalHandlerInstalled = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-18 21:25:25 +04:00
|
|
|
#endif /* HAVE_AIO_H */
|
2014-02-17 22:02:39 +04:00
|
|
|
|
2012-09-27 00:25:23 +04:00
|
|
|
HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
|
2012-09-27 00:25:23 +04:00
|
|
|
{
|
2014-04-22 19:00:14 +04:00
|
|
|
int i;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* name;
|
2013-07-23 06:53:44 +04:00
|
|
|
int status;
|
2013-07-23 02:20:34 +04:00
|
|
|
HANDLE hNamedPipe;
|
2013-07-23 06:53:44 +04:00
|
|
|
struct sockaddr_un s;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
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
|
|
|
|
2014-07-03 13:20:46 +04:00
|
|
|
EnterCriticalSection(&_HandleCreatorsLock);
|
|
|
|
|
2014-04-22 19:00:14 +04:00
|
|
|
for (i=0; _HandleCreators[i] != NULL; i++)
|
2014-04-18 21:25:25 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
HANDLE_CREATOR* creator = (HANDLE_CREATOR*)_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
|
|
|
{
|
2014-07-03 13:20:46 +04:00
|
|
|
HANDLE newHandle = creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
|
2014-08-18 20:57:08 +04:00
|
|
|
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
|
2014-07-03 13:20:46 +04:00
|
|
|
LeaveCriticalSection(&_HandleCreatorsLock);
|
|
|
|
return newHandle;
|
2014-04-18 21:25:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:20:46 +04:00
|
|
|
LeaveCriticalSection(&_HandleCreatorsLock);
|
|
|
|
|
2014-06-19 14:03:36 +04:00
|
|
|
/* TODO: use of a HANDLE_CREATOR for named pipes as well */
|
2014-04-02 23:51:28 +04:00
|
|
|
|
2014-04-18 21:25:25 +04:00
|
|
|
if (!IsNamedPipeFileNameA(lpFileName))
|
|
|
|
return INVALID_HANDLE_VALUE;
|
2014-04-02 23:51:28 +04:00
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
name = GetNamedPipeNameWithoutPrefixA(lpFileName);
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
free(name);
|
2014-08-18 21:34:47 +04:00
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) calloc(1, sizeof(WINPR_NAMED_PIPE));
|
2013-07-23 02:20:34 +04:00
|
|
|
hNamedPipe = (HANDLE) pNamedPipe;
|
2013-07-23 06:53:44 +04:00
|
|
|
WINPR_HANDLE_SET_TYPE(pNamedPipe, HANDLE_TYPE_NAMED_PIPE);
|
2013-07-23 02:20:34 +04:00
|
|
|
pNamedPipe->name = _strdup(lpFileName);
|
|
|
|
pNamedPipe->dwOpenMode = 0;
|
|
|
|
pNamedPipe->dwPipeMode = 0;
|
|
|
|
pNamedPipe->nMaxInstances = 0;
|
|
|
|
pNamedPipe->nOutBufferSize = 0;
|
|
|
|
pNamedPipe->nInBufferSize = 0;
|
|
|
|
pNamedPipe->nDefaultTimeOut = 0;
|
2013-08-23 06:36:37 +04:00
|
|
|
pNamedPipe->dwFlagsAndAttributes = dwFlagsAndAttributes;
|
2013-07-23 06:53:44 +04:00
|
|
|
pNamedPipe->lpFileName = GetNamedPipeNameWithoutPrefixA(lpFileName);
|
|
|
|
pNamedPipe->lpFilePath = GetNamedPipeUnixDomainSocketFilePathA(lpFileName);
|
|
|
|
pNamedPipe->clientfd = socket(PF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
pNamedPipe->serverfd = -1;
|
2013-09-26 03:13:39 +04:00
|
|
|
pNamedPipe->ServerMode = FALSE;
|
2013-07-23 06:53:44 +04:00
|
|
|
ZeroMemory(&s, sizeof(struct sockaddr_un));
|
|
|
|
s.sun_family = AF_UNIX;
|
|
|
|
strcpy(s.sun_path, pNamedPipe->lpFilePath);
|
2014-08-18 21:34:47 +04:00
|
|
|
status = connect(pNamedPipe->clientfd, (struct sockaddr*) &s, sizeof(struct sockaddr_un));
|
2013-07-23 06:53:44 +04:00
|
|
|
|
|
|
|
if (status != 0)
|
2013-08-29 17:30:22 +04:00
|
|
|
{
|
|
|
|
close(pNamedPipe->clientfd);
|
2014-08-18 21:34:47 +04:00
|
|
|
free((char*) pNamedPipe->name);
|
|
|
|
free((char*) pNamedPipe->lpFileName);
|
|
|
|
free((char*) pNamedPipe->lpFilePath);
|
2013-08-29 17:30:22 +04:00
|
|
|
free(pNamedPipe);
|
2013-07-23 06:53:44 +04:00
|
|
|
return INVALID_HANDLE_VALUE;
|
2013-08-29 17:30:22 +04:00
|
|
|
}
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
int flags = fcntl(pNamedPipe->clientfd, F_GETFL);
|
|
|
|
|
|
|
|
if (flags != -1)
|
|
|
|
fcntl(pNamedPipe->clientfd, F_SETFL, flags | O_NONBLOCK);
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
return hNamedPipe;
|
2012-09-27 00:25:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
|
2012-09-27 00:25:23 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:37:35 +04:00
|
|
|
BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
|
2014-08-18 20:57:08 +04:00
|
|
|
LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2012-10-08 04:25:50 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
2014-02-17 22:02:39 +04:00
|
|
|
BOOL status = TRUE;
|
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
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type == HANDLE_TYPE_ANONYMOUS_PIPE)
|
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
int io_status;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_PIPE* pipe;
|
|
|
|
pipe = (WINPR_PIPE*) Object;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2014-06-19 00:09:07 +04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
io_status = read(pipe->fd, lpBuffer, nNumberOfBytesToRead);
|
|
|
|
}
|
|
|
|
while ((io_status < 0) && (errno == EINTR));
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
if (io_status < 0)
|
2014-02-07 18:19:46 +04:00
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
status = FALSE;
|
|
|
|
|
2014-02-07 18:19:46 +04:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
SetLastError(ERROR_NO_DATA);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpNumberOfBytesRead)
|
2014-02-17 22:02:39 +04:00
|
|
|
*lpNumberOfBytesRead = io_status;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
return status;
|
2012-10-08 04:25:50 +04:00
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
else if (Type == HANDLE_TYPE_NAMED_PIPE)
|
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
int io_status;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pipe;
|
|
|
|
pipe = (WINPR_NAMED_PIPE*) Object;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
if (!(pipe->dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED))
|
|
|
|
{
|
|
|
|
if (pipe->clientfd == -1)
|
|
|
|
return FALSE;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-06-19 00:09:07 +04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
io_status = read(pipe->clientfd, lpBuffer, nNumberOfBytesToRead);
|
|
|
|
}
|
|
|
|
while ((io_status < 0) && (errno == EINTR));
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
if (io_status == 0)
|
2013-10-01 20:03:08 +04:00
|
|
|
{
|
2014-06-13 01:09:49 +04:00
|
|
|
SetLastError(ERROR_BROKEN_PIPE);
|
2014-05-28 15:54:08 +04:00
|
|
|
status = FALSE;
|
2013-10-01 20:03:08 +04:00
|
|
|
}
|
2014-02-17 22:02:39 +04:00
|
|
|
else if (io_status < 0)
|
2013-08-23 06:36:37 +04:00
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
status = FALSE;
|
|
|
|
|
2014-02-07 18:19:46 +04:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
SetLastError(ERROR_NO_DATA);
|
|
|
|
break;
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2014-06-13 01:09:49 +04:00
|
|
|
default:
|
|
|
|
SetLastError(ERROR_BROKEN_PIPE);
|
|
|
|
break;
|
2014-02-07 18:19:46 +04:00
|
|
|
}
|
2013-08-23 06:36:37 +04:00
|
|
|
}
|
|
|
|
|
2014-11-16 18:47:38 +03:00
|
|
|
if (lpNumberOfBytesRead)
|
|
|
|
*lpNumberOfBytesRead = io_status;
|
2013-07-23 06:53:44 +04:00
|
|
|
}
|
2013-08-23 06:36:37 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Overlapped I/O */
|
|
|
|
if (!lpOverlapped)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (pipe->clientfd == -1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
pipe->lpOverlapped = lpOverlapped;
|
|
|
|
#ifdef HAVE_AIO_H
|
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
int aio_status;
|
2013-08-23 06:36:37 +04:00
|
|
|
struct aiocb cb;
|
|
|
|
ZeroMemory(&cb, sizeof(struct aiocb));
|
|
|
|
cb.aio_fildes = pipe->clientfd;
|
|
|
|
cb.aio_buf = lpBuffer;
|
2014-02-17 22:02:39 +04:00
|
|
|
cb.aio_nbytes = nNumberOfBytesToRead;
|
|
|
|
cb.aio_offset = lpOverlapped->Offset;
|
|
|
|
cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
cb.aio_sigevent.sigev_signo = SIGIO;
|
2014-08-18 21:34:47 +04:00
|
|
|
cb.aio_sigevent.sigev_value.sival_ptr = (void*) lpOverlapped;
|
2014-02-17 22:02:39 +04:00
|
|
|
InstallAioSignalHandler();
|
|
|
|
aio_status = aio_read(&cb);
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_DBG(TAG, "aio_read status: %d", aio_status);
|
2014-02-17 22:02:39 +04:00
|
|
|
|
|
|
|
if (aio_status < 0)
|
|
|
|
status = FALSE;
|
|
|
|
|
|
|
|
return status;
|
2013-08-23 06:36:37 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* synchronous behavior */
|
|
|
|
lpOverlapped->Internal = 0;
|
|
|
|
lpOverlapped->InternalHigh = (ULONG_PTR) nNumberOfBytesToRead;
|
|
|
|
lpOverlapped->Pointer = (PVOID) lpBuffer;
|
|
|
|
SetEvent(lpOverlapped->hEvent);
|
|
|
|
#endif
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
return status;
|
2013-07-23 02:20:34 +04:00
|
|
|
}
|
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,
|
2014-08-18 20:57:08 +04:00
|
|
|
LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ReadFileScatter(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[],
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD nNumberOfBytesToRead, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
|
2014-08-18 20:57:08 +04:00
|
|
|
LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
2012-10-08 04:25:50 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
2014-02-17 22:02:39 +04:00
|
|
|
BOOL status = TRUE;
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-08 04:25:50 +04:00
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type == HANDLE_TYPE_ANONYMOUS_PIPE)
|
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
int io_status;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_PIPE* pipe;
|
|
|
|
pipe = (WINPR_PIPE*) Object;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2014-06-19 00:09:07 +04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
io_status = write(pipe->fd, lpBuffer, nNumberOfBytesToWrite);
|
|
|
|
}
|
|
|
|
while ((io_status < 0) && (errno == EINTR));
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
if ((io_status < 0) && (errno == EWOULDBLOCK))
|
|
|
|
io_status = 0;
|
2014-02-07 18:19:46 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
*lpNumberOfBytesWritten = io_status;
|
2012-10-08 04:25:50 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
else if (Type == HANDLE_TYPE_NAMED_PIPE)
|
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
int io_status;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pipe;
|
|
|
|
pipe = (WINPR_NAMED_PIPE*) Object;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
if (!(pipe->dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED))
|
|
|
|
{
|
2014-02-17 22:02:39 +04:00
|
|
|
io_status = nNumberOfBytesToWrite;
|
2013-08-23 06:36:37 +04:00
|
|
|
|
|
|
|
if (pipe->clientfd == -1)
|
|
|
|
return FALSE;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2014-06-19 00:09:07 +04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
io_status = write(pipe->clientfd, lpBuffer, nNumberOfBytesToWrite);
|
|
|
|
}
|
|
|
|
while ((io_status < 0) && (errno == EINTR));
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
if (io_status < 0)
|
2013-08-23 06:36:37 +04:00
|
|
|
{
|
|
|
|
*lpNumberOfBytesWritten = 0;
|
2014-02-07 18:19:46 +04:00
|
|
|
|
2014-08-18 20:57:08 +04:00
|
|
|
switch (errno)
|
2014-02-07 18:19:46 +04:00
|
|
|
{
|
|
|
|
case EWOULDBLOCK:
|
2014-02-17 22:02:39 +04:00
|
|
|
io_status = 0;
|
|
|
|
status = TRUE;
|
2014-02-07 18:19:46 +04:00
|
|
|
break;
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2014-02-07 18:19:46 +04:00
|
|
|
default:
|
2014-02-17 22:02:39 +04:00
|
|
|
status = FALSE;
|
2014-02-07 18:19:46 +04:00
|
|
|
}
|
2013-08-23 06:36:37 +04:00
|
|
|
}
|
|
|
|
|
2014-02-17 22:02:39 +04:00
|
|
|
*lpNumberOfBytesWritten = io_status;
|
|
|
|
return status;
|
2013-07-23 06:53:44 +04:00
|
|
|
}
|
2013-08-23 06:36:37 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Overlapped I/O */
|
|
|
|
if (!lpOverlapped)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (pipe->clientfd == -1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
pipe->lpOverlapped = lpOverlapped;
|
|
|
|
#ifdef HAVE_AIO_H
|
|
|
|
{
|
|
|
|
struct aiocb cb;
|
|
|
|
ZeroMemory(&cb, sizeof(struct aiocb));
|
|
|
|
cb.aio_fildes = pipe->clientfd;
|
2014-08-18 21:34:47 +04:00
|
|
|
cb.aio_buf = (void*) lpBuffer;
|
2014-02-17 22:02:39 +04:00
|
|
|
cb.aio_nbytes = nNumberOfBytesToWrite;
|
2013-08-23 06:36:37 +04:00
|
|
|
cb.aio_offset = lpOverlapped->Offset;
|
2014-02-17 22:02:39 +04:00
|
|
|
cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
cb.aio_sigevent.sigev_signo = SIGIO;
|
2014-08-18 21:34:47 +04:00
|
|
|
cb.aio_sigevent.sigev_value.sival_ptr = (void*) lpOverlapped;
|
2014-02-17 22:02:39 +04:00
|
|
|
InstallAioSignalHandler();
|
|
|
|
io_status = aio_write(&cb);
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_DBG("aio_write status: %d", io_status);
|
2014-02-17 22:02:39 +04:00
|
|
|
|
|
|
|
if (io_status < 0)
|
|
|
|
status = FALSE;
|
|
|
|
|
|
|
|
return status;
|
2013-08-23 06:36:37 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* synchronous behavior */
|
|
|
|
lpOverlapped->Internal = 1;
|
|
|
|
lpOverlapped->InternalHigh = (ULONG_PTR) nNumberOfBytesToWrite;
|
|
|
|
lpOverlapped->Pointer = (PVOID) lpBuffer;
|
|
|
|
SetEvent(lpOverlapped->hEvent);
|
|
|
|
#endif
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
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,
|
2014-08-18 20:57:08 +04:00
|
|
|
LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[],
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD nNumberOfBytesToWrite, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FlushFileBuffers(HANDLE hFile)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetEndOfFile(HANDLE hFile)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove,
|
2014-08-18 20:57:08 +04:00
|
|
|
PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove,
|
2014-08-18 20:57:08 +04:00
|
|
|
PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved,
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-24 23:58:33 +04:00
|
|
|
|
2012-09-26 20:37:35 +04:00
|
|
|
BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow,
|
2014-08-18 20:57:08 +04:00
|
|
|
DWORD nNumberOfBytesToUnlockHigh, LPOVERLAPPED lpOverlapped)
|
2012-09-26 20:37:35 +04:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-24 23:58:33 +04:00
|
|
|
|
2012-11-04 01:24:52 +04:00
|
|
|
struct _WIN32_FILE_SEARCH
|
|
|
|
{
|
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;
|
2012-11-04 01:24:52 +04:00
|
|
|
};
|
|
|
|
typedef struct _WIN32_FILE_SEARCH WIN32_FILE_SEARCH;
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* p;
|
2012-11-03 04:58:58 +04:00
|
|
|
int index;
|
|
|
|
int length;
|
|
|
|
struct stat fileStat;
|
2014-08-18 21:34:47 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
2012-11-23 00:32:21 +04:00
|
|
|
ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
|
2014-08-18 21:34:47 +04:00
|
|
|
pFileSearch = (WIN32_FILE_SEARCH*) malloc(sizeof(WIN32_FILE_SEARCH));
|
2012-11-04 01:24:52 +04:00
|
|
|
ZeroMemory(pFileSearch, sizeof(WIN32_FILE_SEARCH));
|
2012-11-03 04:58:58 +04:00
|
|
|
/* Separate lpFileName into path and pattern components */
|
2012-11-04 01:24:52 +04:00
|
|
|
p = strrchr(lpFileName, '/');
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
p = strrchr(lpFileName, '\\');
|
2012-11-03 04:58:58 +04:00
|
|
|
|
|
|
|
index = (p - lpFileName);
|
|
|
|
length = (p - lpFileName);
|
2012-11-04 01:24:52 +04:00
|
|
|
pFileSearch->lpPath = (LPSTR) malloc(length + 1);
|
|
|
|
CopyMemory(pFileSearch->lpPath, lpFileName, length);
|
|
|
|
pFileSearch->lpPath[length] = '\0';
|
2012-11-03 04:58:58 +04:00
|
|
|
length = strlen(lpFileName) - index;
|
2012-11-04 01:24:52 +04:00
|
|
|
pFileSearch->lpPattern = (LPSTR) malloc(length + 1);
|
|
|
|
CopyMemory(pFileSearch->lpPattern, &lpFileName[index + 1], length);
|
|
|
|
pFileSearch->lpPattern[length] = '\0';
|
2012-11-03 04:58:58 +04:00
|
|
|
|
|
|
|
/* Check if the path is a directory */
|
|
|
|
|
2012-11-04 01:24:52 +04:00
|
|
|
if (lstat(pFileSearch->lpPath, &fileStat) < 0)
|
|
|
|
{
|
2013-09-02 18:25:34 +04:00
|
|
|
FindClose(pFileSearch);
|
2012-11-03 04:58:58 +04:00
|
|
|
return INVALID_HANDLE_VALUE; /* stat error */
|
2012-11-04 01:24:52 +04:00
|
|
|
}
|
2012-11-03 04:58:58 +04:00
|
|
|
|
|
|
|
if (S_ISDIR(fileStat.st_mode) == 0)
|
2012-11-04 01:24:52 +04:00
|
|
|
{
|
2013-09-02 18:25:34 +04:00
|
|
|
FindClose(pFileSearch);
|
2012-11-03 04:58:58 +04:00
|
|
|
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);
|
2012-11-04 01:24:52 +04:00
|
|
|
return INVALID_HANDLE_VALUE; /* failed to open directory */
|
|
|
|
}
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2012-11-04 01:24:52 +04:00
|
|
|
while ((pFileSearch->pDirent = readdir(pFileSearch->pDir)) != NULL)
|
2012-11-03 04:58:58 +04:00
|
|
|
{
|
2012-11-04 01:24:52 +04:00
|
|
|
if ((strcmp(pFileSearch->pDirent->d_name, ".") == 0) || (strcmp(pFileSearch->pDirent->d_name, "..") == 0))
|
2012-11-03 04:58:58 +04:00
|
|
|
{
|
|
|
|
/* skip "." and ".." */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-11-04 01:24:52 +04:00
|
|
|
if (FilePatternMatchA(pFileSearch->pDirent->d_name, pFileSearch->lpPattern))
|
2012-11-03 04:58:58 +04:00
|
|
|
{
|
2012-11-04 01:24:52 +04:00
|
|
|
strcpy(lpFindFileData->cFileName, pFileSearch->pDirent->d_name);
|
|
|
|
return (HANDLE) pFileSearch;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE FindFirstFileW(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE FindFirstFileExA(LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
|
2014-08-18 20:57:08 +04:00
|
|
|
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
|
2012-10-18 23:37:00 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE FindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
|
2014-08-18 20:57:08 +04:00
|
|
|
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
|
2012-10-18 23:37:00 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2012-11-04 07:04:04 +04:00
|
|
|
if (!hFindFile)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (hFindFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-08-18 21:34:47 +04: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))
|
|
|
|
{
|
|
|
|
strcpy(lpFindFileData->cFileName, pFileSearch->pDirent->d_name);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FindClose(HANDLE hFindFile)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
|
|
|
pFileSearch = (WIN32_FILE_SEARCH*) hFindFile;
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2013-09-02 18:25:34 +04:00
|
|
|
if (pFileSearch)
|
|
|
|
{
|
|
|
|
if (pFileSearch->lpPath)
|
|
|
|
free(pFileSearch->lpPath);
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2013-09-02 18:25:34 +04:00
|
|
|
if (pFileSearch->lpPattern)
|
|
|
|
free(pFileSearch->lpPattern);
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2013-09-02 18:25:34 +04:00
|
|
|
if (pFileSearch->pDir)
|
|
|
|
closedir(pFileSearch->pDir);
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2014-08-18 20:57:08 +04:00
|
|
|
free(pFileSearch);
|
2013-09-02 18:25:34 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-11-04 02:31:01 +04:00
|
|
|
|
2013-09-02 18:25:34 +04:00
|
|
|
return FALSE;
|
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)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-24 23:58:33 +04:00
|
|
|
#endif
|
|
|
|
|
2013-07-30 18:57:54 +04:00
|
|
|
/* Extended API */
|
|
|
|
|
|
|
|
#define NAMED_PIPE_PREFIX_PATH "\\\\.\\pipe\\"
|
|
|
|
|
2014-04-02 23:51:28 +04:00
|
|
|
BOOL IsNamedPipeFileNameA(LPCSTR lpName)
|
|
|
|
{
|
|
|
|
if (strncmp(lpName, NAMED_PIPE_PREFIX_PATH, sizeof(NAMED_PIPE_PREFIX_PATH) - 1) != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* GetNamedPipeNameWithoutPrefixA(LPCSTR lpName)
|
2013-07-30 18:57:54 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* lpFileName;
|
2013-07-30 18:57:54 +04:00
|
|
|
|
|
|
|
if (!lpName)
|
|
|
|
return NULL;
|
|
|
|
|
2014-04-02 23:51:28 +04:00
|
|
|
if (!IsNamedPipeFileNameA(lpName))
|
2013-07-30 18:57:54 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
lpFileName = _strdup(&lpName[strlen(NAMED_PIPE_PREFIX_PATH)]);
|
|
|
|
return lpFileName;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* GetNamedPipeUnixDomainSocketBaseFilePathA()
|
2013-07-30 18:57:54 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* lpTempPath;
|
|
|
|
char* lpPipePath;
|
2013-07-30 18:57:54 +04:00
|
|
|
lpTempPath = GetKnownPath(KNOWN_PATH_TEMP);
|
|
|
|
lpPipePath = GetCombinedPath(lpTempPath, ".pipe");
|
|
|
|
free(lpTempPath);
|
|
|
|
return lpPipePath;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* GetNamedPipeUnixDomainSocketFilePathA(LPCSTR lpName)
|
2013-07-30 18:57:54 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* lpPipePath;
|
|
|
|
char* lpFileName;
|
|
|
|
char* lpFilePath;
|
2013-07-30 18:57:54 +04:00
|
|
|
lpPipePath = GetNamedPipeUnixDomainSocketBaseFilePathA();
|
|
|
|
lpFileName = GetNamedPipeNameWithoutPrefixA(lpName);
|
2014-08-18 21:34:47 +04:00
|
|
|
lpFilePath = GetCombinedPath(lpPipePath, (char*) lpFileName);
|
2013-07-30 18:57:54 +04:00
|
|
|
free(lpPipePath);
|
|
|
|
free(lpFileName);
|
|
|
|
return lpFilePath;
|
|
|
|
}
|
|
|
|
|
2013-09-26 03:13:39 +04:00
|
|
|
int GetNamePipeFileDescriptor(HANDLE hNamedPipe)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int fd;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) hNamedPipe;
|
2013-09-26 03:13:39 +04:00
|
|
|
|
2014-05-07 23:23:54 +04:00
|
|
|
if (!pNamedPipe || pNamedPipe->Type != HANDLE_TYPE_NAMED_PIPE)
|
2013-09-26 03:13:39 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
fd = (pNamedPipe->ServerMode) ? pNamedPipe->serverfd : pNamedPipe->clientfd;
|
|
|
|
return fd;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
int UnixChangeFileMode(const char* filename, int flags)
|
2013-07-30 18:57:54 +04:00
|
|
|
{
|
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
|
|
|
|
return 0;
|
|
|
|
#endif
|
2013-07-30 18:57:54 +04:00
|
|
|
}
|