2012-09-24 23:58:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* File Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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>
|
2012-10-08 04:25:50 +04:00
|
|
|
#include <winpr/handle.h>
|
2013-08-23 06:36:37 +04:00
|
|
|
#include <winpr/platform.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
|
|
|
|
|
2012-09-24 23:58:33 +04:00
|
|
|
/**
|
|
|
|
* api-ms-win-core-file-l1-2-0.dll:
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2012-11-03 04:58:58 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#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
|
|
|
|
|
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"
|
|
|
|
|
2012-09-27 00:25:23 +04:00
|
|
|
HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
|
|
|
|
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
|
|
|
|
{
|
2013-07-23 06:53:44 +04:00
|
|
|
char* name;
|
|
|
|
int status;
|
2013-07-23 02:20:34 +04:00
|
|
|
HANDLE hNamedPipe;
|
2013-07-23 06:53:44 +04:00
|
|
|
unsigned long flags;
|
|
|
|
struct sockaddr_un s;
|
2013-07-23 02:20:34 +04:00
|
|
|
WINPR_NAMED_PIPE* pNamedPipe;
|
|
|
|
|
|
|
|
if (!lpFileName)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
name = GetNamedPipeNameWithoutPrefixA(lpFileName);
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
|
2013-07-23 02:20:34 +04:00
|
|
|
pNamedPipe = (WINPR_NAMED_PIPE*) malloc(sizeof(WINPR_NAMED_PIPE));
|
|
|
|
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 02:20:34 +04:00
|
|
|
|
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-07-23 02:20:34 +04:00
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
if (0)
|
|
|
|
{
|
|
|
|
flags = fcntl(pNamedPipe->clientfd, F_GETFL);
|
|
|
|
flags = flags | O_NONBLOCK;
|
|
|
|
fcntl(pNamedPipe->clientfd, F_SETFL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZeroMemory(&s, sizeof(struct sockaddr_un));
|
|
|
|
s.sun_family = AF_UNIX;
|
|
|
|
strcpy(s.sun_path, pNamedPipe->lpFilePath);
|
|
|
|
|
|
|
|
status = connect(pNamedPipe->clientfd, (struct sockaddr*) &s, sizeof(struct sockaddr_un));
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
return hNamedPipe;
|
2012-09-27 00:25:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
|
|
|
|
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DeleteFileA(LPCSTR lpFileName)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DeleteFileW(LPCWSTR lpFileName)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:37:35 +04:00
|
|
|
BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
|
|
|
|
LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
2012-10-08 04:25:50 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type == HANDLE_TYPE_ANONYMOUS_PIPE)
|
|
|
|
{
|
|
|
|
int status;
|
2013-07-23 02:20:34 +04:00
|
|
|
WINPR_PIPE* pipe;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
pipe = (WINPR_PIPE*) Object;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
status = read(pipe->fd, lpBuffer, nNumberOfBytesToRead);
|
2012-10-08 04:25:50 +04:00
|
|
|
|
|
|
|
*lpNumberOfBytesRead = status;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
else if (Type == HANDLE_TYPE_NAMED_PIPE)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
WINPR_NAMED_PIPE* pipe;
|
|
|
|
|
|
|
|
pipe = (WINPR_NAMED_PIPE*) Object;
|
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
if (!(pipe->dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED))
|
|
|
|
{
|
|
|
|
status = nNumberOfBytesToRead;
|
|
|
|
|
|
|
|
if (pipe->clientfd == -1)
|
|
|
|
return FALSE;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
status = read(pipe->clientfd, lpBuffer, nNumberOfBytesToRead);
|
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
*lpNumberOfBytesRead = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*lpNumberOfBytesRead = status;
|
2013-07-23 06:53:44 +04:00
|
|
|
}
|
2013-08-23 06:36:37 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Overlapped I/O */
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
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_nbytes = nNumberOfBytesToRead;
|
|
|
|
cb.aio_fildes = pipe->clientfd;
|
|
|
|
cb.aio_offset = lpOverlapped->Offset;
|
|
|
|
cb.aio_buf = lpBuffer;
|
|
|
|
|
|
|
|
status = aio_read(&cb);
|
|
|
|
|
|
|
|
printf("aio_read status: %d\n", status);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
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,
|
|
|
|
LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ReadFileScatter(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[],
|
|
|
|
DWORD nNumberOfBytesToRead, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
|
|
|
|
LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
2012-10-08 04:25:50 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hFile, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type == HANDLE_TYPE_ANONYMOUS_PIPE)
|
|
|
|
{
|
|
|
|
int status;
|
2013-07-23 02:20:34 +04:00
|
|
|
WINPR_PIPE* pipe;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
pipe = (WINPR_PIPE*) Object;
|
2012-10-08 04:25:50 +04:00
|
|
|
|
2013-05-17 01:32:58 +04:00
|
|
|
status = write(pipe->fd, lpBuffer, nNumberOfBytesToWrite);
|
2012-10-08 04:25:50 +04:00
|
|
|
|
|
|
|
*lpNumberOfBytesWritten = status;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-07-23 02:20:34 +04:00
|
|
|
else if (Type == HANDLE_TYPE_NAMED_PIPE)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
WINPR_NAMED_PIPE* pipe;
|
|
|
|
|
|
|
|
pipe = (WINPR_NAMED_PIPE*) Object;
|
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
if (!(pipe->dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED))
|
|
|
|
{
|
|
|
|
status = nNumberOfBytesToWrite;
|
|
|
|
|
|
|
|
if (pipe->clientfd == -1)
|
|
|
|
return FALSE;
|
2013-07-23 02:20:34 +04:00
|
|
|
|
2013-07-23 06:53:44 +04:00
|
|
|
status = write(pipe->clientfd, lpBuffer, nNumberOfBytesToWrite);
|
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
*lpNumberOfBytesWritten = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*lpNumberOfBytesWritten = status;
|
2013-07-23 06:53:44 +04:00
|
|
|
}
|
2013-08-23 06:36:37 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Overlapped I/O */
|
2013-07-23 06:53:44 +04:00
|
|
|
|
2013-08-23 06:36:37 +04:00
|
|
|
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_nbytes = nNumberOfBytesToWrite;
|
|
|
|
cb.aio_fildes = pipe->clientfd;
|
|
|
|
cb.aio_offset = lpOverlapped->Offset;
|
|
|
|
cb.aio_buf = lpBuffer;
|
|
|
|
|
|
|
|
status = aio_write(&cb);
|
|
|
|
|
|
|
|
printf("aio_write status: %d\n", status);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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,
|
|
|
|
LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[],
|
|
|
|
DWORD nNumberOfBytesToWrite, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FlushFileBuffers(HANDLE hFile)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetEndOfFile(HANDLE hFile)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD SetFilePointer(HANDLE hFile, LONG lDistanceToMove,
|
|
|
|
PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove,
|
|
|
|
PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
|
|
|
|
DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved,
|
|
|
|
DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
|
|
|
|
DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
DWORD nNumberOfBytesToUnlockHigh, LPOVERLAPPED lpOverlapped)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-24 23:58:33 +04:00
|
|
|
|
2012-11-04 01:24:52 +04:00
|
|
|
struct _WIN32_FILE_SEARCH
|
|
|
|
{
|
|
|
|
DIR* pDir;
|
|
|
|
LPSTR lpPath;
|
|
|
|
LPSTR lpPattern;
|
|
|
|
struct dirent* pDirent;
|
|
|
|
};
|
|
|
|
typedef struct _WIN32_FILE_SEARCH WIN32_FILE_SEARCH;
|
|
|
|
|
2012-10-18 23:37:00 +04:00
|
|
|
HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
|
|
|
|
{
|
2012-11-03 04:58:58 +04:00
|
|
|
char* p;
|
|
|
|
int index;
|
|
|
|
int length;
|
|
|
|
struct stat fileStat;
|
2012-11-04 01:24:52 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
2012-11-03 04:58:58 +04:00
|
|
|
|
2012-11-23 00:32:21 +04:00
|
|
|
ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
|
2012-10-19 00:58:27 +04:00
|
|
|
|
2012-11-04 01:24:52 +04:00
|
|
|
pFileSearch = (WIN32_FILE_SEARCH*) malloc(sizeof(WIN32_FILE_SEARCH));
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
free(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
|
|
|
{
|
|
|
|
free(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)
|
|
|
|
{
|
|
|
|
free(pFileSearch);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE FindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
|
|
|
|
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
|
|
|
|
{
|
2012-11-04 02:31:01 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
|
|
|
|
2012-11-04 07:04:04 +04:00
|
|
|
if (!hFindFile)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (hFindFile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
2012-11-04 02:31:01 +04:00
|
|
|
pFileSearch = (WIN32_FILE_SEARCH*) hFindFile;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2012-11-04 02:31:01 +04:00
|
|
|
WIN32_FILE_SEARCH* pFileSearch;
|
|
|
|
|
|
|
|
pFileSearch = (WIN32_FILE_SEARCH*) hFindFile;
|
|
|
|
|
|
|
|
free(pFileSearch->lpPath);
|
|
|
|
free(pFileSearch->lpPattern);
|
|
|
|
closedir(pFileSearch->pDir);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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\\"
|
|
|
|
|
|
|
|
char* GetNamedPipeNameWithoutPrefixA(LPCSTR lpName)
|
|
|
|
{
|
|
|
|
char* lpFileName;
|
|
|
|
|
|
|
|
if (!lpName)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (strncmp(lpName, NAMED_PIPE_PREFIX_PATH, sizeof(NAMED_PIPE_PREFIX_PATH) - 1) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
lpFileName = _strdup(&lpName[strlen(NAMED_PIPE_PREFIX_PATH)]);
|
|
|
|
|
|
|
|
return lpFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* GetNamedPipeUnixDomainSocketBaseFilePathA()
|
|
|
|
{
|
|
|
|
char* lpTempPath;
|
|
|
|
char* lpPipePath;
|
|
|
|
|
|
|
|
lpTempPath = GetKnownPath(KNOWN_PATH_TEMP);
|
|
|
|
lpPipePath = GetCombinedPath(lpTempPath, ".pipe");
|
|
|
|
|
|
|
|
free(lpTempPath);
|
|
|
|
|
|
|
|
return lpPipePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* GetNamedPipeUnixDomainSocketFilePathA(LPCSTR lpName)
|
|
|
|
{
|
|
|
|
char* lpPipePath;
|
|
|
|
char* lpFileName;
|
|
|
|
char* lpFilePath;
|
|
|
|
|
|
|
|
lpPipePath = GetNamedPipeUnixDomainSocketBaseFilePathA();
|
|
|
|
|
|
|
|
lpFileName = GetNamedPipeNameWithoutPrefixA(lpName);
|
|
|
|
lpFilePath = GetCombinedPath(lpPipePath, (char*) lpFileName);
|
|
|
|
|
|
|
|
free(lpPipePath);
|
|
|
|
free(lpFileName);
|
|
|
|
|
|
|
|
return lpFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
int UnixChangeFileMode(const char* filename, int flags)
|
|
|
|
{
|
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
|
|
|
}
|