Merge pull request #3864 from DavBfr/rewrite-disk-redirection

Rewrite disk redirection using WinPR
This commit is contained in:
David Fort 2017-04-06 17:32:21 +02:00 committed by GitHub
commit 9fd3974817
19 changed files with 1084 additions and 1094 deletions

View File

@ -22,13 +22,6 @@ set(${MODULE_PREFIX}_SRCS
drive_file.h
drive_main.c)
if(WIN32)
set(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_SRCS}
statvfs.c
statvfs.h
dirent.h)
endif()
add_channel_client_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} TRUE "DeviceServiceEntry")

View File

@ -1,374 +0,0 @@
/*****************************************************************************
* dirent.h - dirent API for Microsoft Visual Studio
*
* Copyright (C) 2006 Toni Ronkko
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* ``Software''), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Mar 15, 2011, Toni Ronkko
* Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
*
* Aug 11, 2010, Toni Ronkko
* Added d_type and d_namlen fields to dirent structure. The former is
* especially useful for determining whether directory entry represents a
* file or a directory. For more information, see
* http://www.delorie.com/gnu/docs/glibc/libc_270.html
*
* Aug 11, 2010, Toni Ronkko
* Improved conformance to the standards. For example, errno is now set
* properly on failure and assert() is never used. Thanks to Peter Brockam
* for suggestions.
*
* Aug 11, 2010, Toni Ronkko
* Fixed a bug in rewinddir(): when using relative directory names, change
* of working directory no longer causes rewinddir() to fail.
*
* Dec 15, 2009, John Cunningham
* Added rewinddir member function
*
* Jan 18, 2008, Toni Ronkko
* Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
* between multi-byte and unicode representations. This makes the
* code simpler and also allows the code to be compiled under MingW. Thanks
* to Azriel Fasten for the suggestion.
*
* Mar 4, 2007, Toni Ronkko
* Bug fix: due to the strncpy_s() function this file only compiled in
* Visual Studio 2005. Using the new string functions only when the
* compiler version allows.
*
* Nov 2, 2006, Toni Ronkko
* Major update: removed support for Watcom C, MS-DOS and Turbo C to
* simplify the file, updated the code to compile cleanly on Visual
* Studio 2005 with both unicode and multi-byte character strings,
* removed rewinddir() as it had a bug.
*
* Aug 20, 2006, Toni Ronkko
* Removed all remarks about MSVC 1.0, which is antiqued now. Simplified
* comments by removing SGML tags.
*
* May 14 2002, Toni Ronkko
* Embedded the function definitions directly to the header so that no
* source modules need to be included in the Visual Studio project. Removed
* all the dependencies to other projects so that this very header can be
* used independently.
*
* May 28 1998, Toni Ronkko
* First version.
*****************************************************************************/
#ifndef DIRENT_H
#define DIRENT_H
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
/* Entries missing from MSVC 6.0 */
#if !defined(FILE_ATTRIBUTE_DEVICE)
# define FILE_ATTRIBUTE_DEVICE 0x40
#endif
/* File type and permission flags for stat() */
#if defined(_MSC_VER) && !defined(S_IREAD)
# define S_IFMT _S_IFMT /* file type mask */
# define S_IFDIR _S_IFDIR /* directory */
# define S_IFCHR _S_IFCHR /* character device */
# define S_IFFIFO _S_IFFIFO /* pipe */
# define S_IFREG _S_IFREG /* regular file */
# define S_IREAD _S_IREAD /* read permission */
# define S_IWRITE _S_IWRITE /* write permission */
# define S_IEXEC _S_IEXEC /* execute permission */
#endif
#define S_IFBLK 0 /* block device */
#define S_IFLNK 0 /* link */
#define S_IFSOCK 0 /* socket */
#if defined(_MSC_VER)
# define S_IRUSR S_IREAD /* read, user */
# define S_IWUSR S_IWRITE /* write, user */
# define S_IXUSR 0 /* execute, user */
# define S_IRGRP 0 /* read, group */
# define S_IWGRP 0 /* write, group */
# define S_IXGRP 0 /* execute, group */
# define S_IROTH 0 /* read, others */
# define S_IWOTH 0 /* write, others */
# define S_IXOTH 0 /* execute, others */
#endif
/* Indicates that d_type field is available in dirent structure */
#define _DIRENT_HAVE_D_TYPE
/* File type flags for d_type */
#define DT_UNKNOWN 0
#define DT_REG S_IFREG
#define DT_DIR S_IFDIR
#define DT_FIFO S_IFFIFO
#define DT_SOCK S_IFSOCK
#define DT_CHR S_IFCHR
#define DT_BLK S_IFBLK
/* Macros for converting between st_mode and d_type */
#define IFTODT(mode) ((mode) & S_IFMT)
#define DTTOIF(type) (type)
/*
* File type macros. Note that block devices, sockets and links cannot be
* distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
* only defined for compatibility. These macros should always return FALSE
* on Windows.
*/
#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO)
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct dirent
{
char d_name[MAX_PATH + 1]; /* File name */
size_t d_namlen; /* Length of name without \0 */
int d_type; /* File type */
} dirent;
typedef struct DIR
{
dirent curentry; /* Current directory entry */
WIN32_FIND_DATAA find_data; /* Private file data */
int cached; /* True if data is valid */
HANDLE search_handle; /* Win32 search handle */
char patt[MAX_PATH + 3]; /* Initial directory name */
} DIR;
/* Forward declarations */
static DIR *opendir(const char *dirname);
static struct dirent *readdir(DIR *dirp);
static int closedir(DIR *dirp);
static void rewinddir(DIR* dirp);
/* Use the new safe string functions introduced in Visual Studio 2005 */
#if defined(_MSC_VER) && _MSC_VER >= 1400
# define DIRENT_STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
#else
# define DIRENT_STRNCPY(dest,src,size) strncpy((dest),(src),(size))
#endif
/* Set errno variable */
#if defined(_MSC_VER)
#define DIRENT_SET_ERRNO(x) _set_errno (x)
#else
#define DIRENT_SET_ERRNO(x) (errno = (x))
#endif
/*****************************************************************************
* Open directory stream DIRNAME for read and return a pointer to the
* internal working area that is used to retrieve individual directory
* entries.
*/
static DIR *opendir(const char *dirname)
{
DIR *dirp;
/* ensure that the resulting search pattern will be a valid file name */
if (dirname == NULL) {
DIRENT_SET_ERRNO (ENOENT);
return NULL;
}
if (strlen (dirname) + 3 >= MAX_PATH) {
DIRENT_SET_ERRNO (ENAMETOOLONG);
return NULL;
}
/* construct new DIR structure */
dirp = (DIR*) malloc (sizeof (struct DIR));
if (dirp != NULL) {
int error;
/*
* Convert relative directory name to an absolute one. This
* allows rewinddir() to function correctly when the current working
* directory is changed between opendir() and rewinddir().
*/
if (GetFullPathNameA(dirname, MAX_PATH, dirp->patt, NULL)) {
char *p;
/* append the search pattern "\\*\0" to the directory name */
p = strchr (dirp->patt, '\0');
if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
*p++ = '\\';
}
*p++ = '*';
*p = '\0';
/* open directory stream and retrieve the first entry */
dirp->search_handle = FindFirstFileA(dirp->patt, &dirp->find_data);
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
/* a directory entry is now waiting in memory */
dirp->cached = 1;
error = 0;
} else {
/* search pattern is not a directory name? */
DIRENT_SET_ERRNO (ENOENT);
error = 1;
}
} else {
/* buffer too small */
DIRENT_SET_ERRNO (ENOMEM);
error = 1;
}
if (error) {
free (dirp);
dirp = NULL;
}
}
return dirp;
}
/*****************************************************************************
* Read a directory entry, and return a pointer to a dirent structure
* containing the name of the entry in d_name field. Individual directory
* entries returned by this very function include regular files,
* sub-directories, pseudo-directories "." and "..", but also volume labels,
* hidden files and system files may be returned.
*/
static struct dirent *readdir(DIR *dirp)
{
DWORD attr;
if (dirp == NULL) {
/* directory stream did not open */
DIRENT_SET_ERRNO (EBADF);
return NULL;
}
/* get next directory entry */
if (dirp->cached != 0) {
/* a valid directory entry already in memory */
dirp->cached = 0;
} else {
/* get the next directory entry from stream */
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
return NULL;
}
if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) {
/* the very last entry has been processed or an error occurred */
FindClose (dirp->search_handle);
dirp->search_handle = INVALID_HANDLE_VALUE;
return NULL;
}
}
/* copy as a multibyte character string */
DIRENT_STRNCPY ( dirp->curentry.d_name,
dirp->find_data.cFileName,
sizeof(dirp->curentry.d_name) );
dirp->curentry.d_name[MAX_PATH] = '\0';
/* compute the length of name */
dirp->curentry.d_namlen = strlen (dirp->curentry.d_name);
/* determine file type */
attr = dirp->find_data.dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
dirp->curentry.d_type = DT_CHR;
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
dirp->curentry.d_type = DT_DIR;
} else {
dirp->curentry.d_type = DT_REG;
}
return &dirp->curentry;
}
/*****************************************************************************
* Close directory stream opened by opendir() function. Close of the
* directory stream invalidates the DIR structure as well as any previously
* read directory entry.
*/
static int closedir(DIR *dirp)
{
if (dirp == NULL) {
/* invalid directory stream */
DIRENT_SET_ERRNO (EBADF);
return -1;
}
/* release search handle */
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
FindClose (dirp->search_handle);
dirp->search_handle = INVALID_HANDLE_VALUE;
}
/* release directory structure */
free (dirp);
return 0;
}
/*****************************************************************************
* Resets the position of the directory stream to which dirp refers to the
* beginning of the directory. It also causes the directory stream to refer
* to the current state of the corresponding directory, as a call to opendir()
* would have done. If dirp does not refer to a directory stream, the effect
* is undefined.
*/
static void rewinddir(DIR* dirp)
{
if (dirp != NULL) {
/* release search handle */
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
FindClose (dirp->search_handle);
}
/* open new search handle and retrieve the first entry */
dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
/* a directory entry is now waiting in memory */
dirp->cached = 1;
} else {
/* failed to re-open directory: no directory entry in memory */
dirp->cached = 0;
}
}
}
#ifdef __cplusplus
}
#endif
#endif /*DIRENT_H*/

File diff suppressed because it is too large Load Diff

View File

@ -30,69 +30,6 @@
#include <sys/stat.h>
#include <freerdp/channels/log.h>
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#include "dirent.h"
#include "statvfs.h"
#else
#include <dirent.h>
#ifdef ANDROID
#include <sys/vfs.h>
#else
#include <sys/statvfs.h>
#endif
#endif
#ifdef _WIN32
#define STAT __stat64
#define OPEN _open
#define close _close
#define read _read
#define write _write
#define LSEEK _lseeki64
#define FSTAT _fstat64
#define STATVFS statvfs
#define mkdir(a,b) _mkdir(a)
#define rmdir _rmdir
#define unlink(a) _unlink(a)
#define ftruncate(a,b) _chsize(a,b)
typedef UINT32 ssize_t;
typedef UINT32 mode_t;
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__)
#define STAT stat
#define OPEN open
#define LSEEK lseek
#define FSTAT fstat
#define STATVFS statvfs
#define O_LARGEFILE 0
#elif defined(ANDROID)
#define STAT stat
#define OPEN open
#define LSEEK lseek
#define FSTAT fstat
#define STATVFS statfs
#else
#define STAT stat64
#define OPEN open64
#define LSEEK lseek64
#define FSTAT fstat64
#define STATVFS statvfs64
#endif
#define EPOCH_DIFF 11644473600LL
#define FILE_TIME_SYSTEM_TO_RDP(_t) \
(((UINT64)(_t) + EPOCH_DIFF) * 10000000LL)
#define FILE_ATTR_SYSTEM_TO_RDP(_f, _st) ( \
(S_ISDIR(_st.st_mode) ? FILE_ATTRIBUTE_DIRECTORY : 0) | \
(_f->filename[0] == '.' ? FILE_ATTRIBUTE_HIDDEN : 0) | \
(_f->delete_pending ? FILE_ATTRIBUTE_TEMPORARY : 0) | \
(st.st_mode & S_IWUSR ? 0 : FILE_ATTRIBUTE_READONLY))
#define TAG CHANNELS_TAG("drive.client")
typedef struct _DRIVE_FILE DRIVE_FILE;
@ -101,28 +38,34 @@ struct _DRIVE_FILE
{
UINT32 id;
BOOL is_dir;
int fd;
int err;
DIR* dir;
char* basepath;
char* fullpath;
char* filename;
char* pattern;
HANDLE file_handle;
HANDLE find_handle;
WIN32_FIND_DATAW find_data;
WCHAR* basepath;
WCHAR* fullpath;
WCHAR* filename;
BOOL delete_pending;
UINT32 FileAttributes;
UINT32 SharedAccess;
UINT32 DesiredAccess;
UINT32 CreateDisposition;
UINT32 CreateOptions;
};
DRIVE_FILE* drive_file_new(const char* base_path, const char* path, UINT32 id,
UINT32 DesiredAccess, UINT32 CreateDisposition, UINT32 CreateOptions);
void drive_file_free(DRIVE_FILE* file);
DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 PathLength, UINT32 id,
UINT32 DesiredAccess, UINT32 CreateDisposition,
UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess);
BOOL drive_file_free(DRIVE_FILE* file);
BOOL drive_file_open(DRIVE_FILE* file);
BOOL drive_file_seek(DRIVE_FILE* file, UINT64 Offset);
BOOL drive_file_read(DRIVE_FILE* file, BYTE* buffer, UINT32* Length);
BOOL drive_file_write(DRIVE_FILE* file, BYTE* buffer, UINT32 Length);
BOOL drive_file_query_information(DRIVE_FILE* file, UINT32 FsInformationClass, wStream* output);
BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length, wStream* input);
BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
wStream* input);
BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
const char* path, wStream* output);
int dir_empty(const char *path);
const WCHAR* path, UINT32 PathLength, wStream* output);
extern UINT sys_code_page;

View File

@ -6,6 +6,7 @@
* Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,6 +40,7 @@
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/file.h>
#include <winpr/string.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
@ -46,6 +48,7 @@
#include <winpr/environment.h>
#include <winpr/interlocked.h>
#include <winpr/collections.h>
#include <winpr/shell.h>
#include <freerdp/channels/rdpdr.h>
@ -57,7 +60,8 @@ struct _DRIVE_DEVICE
{
DEVICE device;
char* path;
WCHAR* path;
UINT32 PathLength;
wListDictionary* files;
HANDLE thread;
@ -68,37 +72,59 @@ struct _DRIVE_DEVICE
rdpContext* rdpcontext;
};
static UINT32 drive_map_posix_err(int fs_errno)
static DWORD drive_map_windows_err(DWORD fs_errno)
{
UINT32 rc;
DWORD rc;
/* try to return NTSTATUS version of error code */
switch (fs_errno)
{
case EPERM:
case EACCES:
case STATUS_SUCCESS:
rc = STATUS_SUCCESS;
break;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
rc = STATUS_ACCESS_DENIED;
break;
case ENOENT:
case ERROR_FILE_NOT_FOUND:
rc = STATUS_NO_SUCH_FILE;
break;
case EBUSY:
case ERROR_BUSY_DRIVE:
rc = STATUS_DEVICE_BUSY;
break;
case EEXIST:
case ERROR_FILE_EXISTS:
case ERROR_ALREADY_EXISTS:
rc = STATUS_OBJECT_NAME_COLLISION;
break;
case EISDIR:
rc = STATUS_FILE_IS_A_DIRECTORY;
case ERROR_INVALID_NAME:
rc = STATUS_NO_SUCH_FILE;
break;
case ERROR_INVALID_HANDLE:
rc = STATUS_INVALID_HANDLE;
break;
case ERROR_NO_MORE_FILES:
rc = STATUS_NO_MORE_FILES;
break;
case ERROR_DIRECTORY:
rc = STATUS_NOT_A_DIRECTORY;
break;
case ERROR_PATH_NOT_FOUND:
rc = STATUS_OBJECT_PATH_NOT_FOUND;
break;
default:
rc = STATUS_UNSUCCESSFUL;
WLog_ERR(TAG, "Error code not found: %"PRId32"", fs_errno);
break;
}
@ -120,54 +146,35 @@ static DRIVE_FILE* drive_get_file_by_id(DRIVE_DEVICE* drive, UINT32 id)
*/
static UINT drive_process_irp_create(DRIVE_DEVICE* drive, IRP* irp)
{
int status;
void* key;
UINT32 FileId;
DRIVE_FILE* file;
BYTE Information;
UINT32 FileAttributes;
UINT32 SharedAccess;
UINT32 DesiredAccess;
UINT32 CreateDisposition;
UINT32 CreateOptions;
UINT32 PathLength;
char* path = NULL;
const WCHAR* path;
Stream_Read_UINT32(irp->input, DesiredAccess);
Stream_Seek(irp->input,
16); /* AllocationSize(8), FileAttributes(4), SharedAccess(4) */
Stream_Seek(irp->input, 8); /* AllocationSize(8) */
Stream_Read_UINT32(irp->input, FileAttributes);
Stream_Read_UINT32(irp->input, SharedAccess);
Stream_Read_UINT32(irp->input, CreateDisposition);
Stream_Read_UINT32(irp->input, CreateOptions);
Stream_Read_UINT32(irp->input, PathLength);
status = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) Stream_Pointer(irp->input),
PathLength / 2, &path, 0, NULL, NULL);
if (status < 1)
{
path = (char*) calloc(1, 1);
if (!path)
{
WLog_ERR(TAG, "calloc failed!");
return CHANNEL_RC_NO_MEMORY;
}
}
path = (WCHAR*) Stream_Pointer(irp->input);
FileId = irp->devman->id_sequence++;
file = drive_file_new(drive->path, path, FileId,
DesiredAccess, CreateDisposition, CreateOptions);
file = drive_file_new(drive->path, path, PathLength, FileId, DesiredAccess, CreateDisposition,
CreateOptions, FileAttributes, SharedAccess);
if (!file)
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
irp->IoStatus = drive_map_windows_err(GetLastError());
FileId = 0;
Information = 0;
}
else if (file->err)
{
FileId = 0;
Information = 0;
/* map errno to windows result */
irp->IoStatus = drive_map_posix_err(file->err);
drive_file_free(file);
}
else
{
key = (void*)(size_t) file->id;
@ -175,7 +182,6 @@ static UINT drive_process_irp_create(DRIVE_DEVICE* drive, IRP* irp)
if (!ListDictionary_Add(drive->files, key, file))
{
WLog_ERR(TAG, "ListDictionary_Add failed!");
free(path);
return ERROR_INTERNAL_ERROR;
}
@ -204,7 +210,6 @@ static UINT drive_process_irp_create(DRIVE_DEVICE* drive, IRP* irp)
Stream_Write_UINT32(irp->output, FileId);
Stream_Write_UINT8(irp->output, Information);
free(path);
return irp->Complete(irp);
}
@ -227,7 +232,11 @@ static UINT drive_process_irp_close(DRIVE_DEVICE* drive, IRP* irp)
else
{
ListDictionary_Remove(drive->files, key);
drive_file_free(file);
if (drive_file_free(file))
irp->IoStatus = STATUS_SUCCESS;
else
irp->IoStatus = drive_map_windows_err(GetLastError());
}
Stream_Zero(irp->output, 5); /* Padding(5) */
@ -256,7 +265,7 @@ static UINT drive_process_irp_read(DRIVE_DEVICE* drive, IRP* irp)
}
else if (!drive_file_seek(file, Offset))
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
irp->IoStatus = drive_map_windows_err(GetLastError());
Length = 0;
}
else
@ -271,7 +280,7 @@ static UINT drive_process_irp_read(DRIVE_DEVICE* drive, IRP* irp)
if (!drive_file_read(file, buffer, &Length))
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
irp->IoStatus = drive_map_windows_err(GetLastError());
free(buffer);
buffer = NULL;
Length = 0;
@ -317,12 +326,12 @@ static UINT drive_process_irp_write(DRIVE_DEVICE* drive, IRP* irp)
}
else if (!drive_file_seek(file, Offset))
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
irp->IoStatus = drive_map_windows_err(GetLastError());
Length = 0;
}
else if (!drive_file_write(file, Stream_Pointer(irp->input), Length))
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
irp->IoStatus = drive_map_windows_err(GetLastError());
Length = 0;
}
@ -349,7 +358,7 @@ static UINT drive_process_irp_query_information(DRIVE_DEVICE* drive, IRP* irp)
}
else if (!drive_file_query_information(file, FsInformationClass, irp->output))
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
irp->IoStatus = drive_map_windows_err(GetLastError());
}
return irp->Complete(irp);
@ -377,16 +386,17 @@ static UINT drive_process_irp_set_information(DRIVE_DEVICE* drive, IRP* irp)
else if (!drive_file_set_information(file, FsInformationClass, Length,
irp->input))
{
irp->IoStatus = STATUS_UNSUCCESSFUL;
irp->IoStatus = drive_map_windows_err(GetLastError());
}
if (file && file->is_dir && !dir_empty(file->fullpath))
if (file && file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
irp->IoStatus = STATUS_DIRECTORY_NOT_EMPTY;
Stream_Write_UINT32(irp->output, Length);
return irp->Complete(irp);
}
/**
* Function description
*
@ -397,21 +407,29 @@ static UINT drive_process_irp_query_volume_information(DRIVE_DEVICE* drive,
{
UINT32 FsInformationClass;
wStream* output = irp->output;
struct STATVFS svfst;
struct STAT st;
char* volumeLabel = {"FREERDP"};
char* diskType = {"FAT32"};
WCHAR* outStr = NULL;
int length;
DWORD lpSectorsPerCluster;
DWORD lpBytesPerSector;
DWORD lpNumberOfFreeClusters;
DWORD lpTotalNumberOfClusters;
WIN32_FILE_ATTRIBUTE_DATA wfad;
Stream_Read_UINT32(irp->input, FsInformationClass);
STATVFS(drive->path, &svfst);
STAT(drive->path, &st);
GetDiskFreeSpaceW(drive->path, &lpSectorsPerCluster, &lpBytesPerSector, &lpNumberOfFreeClusters,
&lpTotalNumberOfClusters);
switch (FsInformationClass)
{
case FileFsVolumeInformation:
/* http://msdn.microsoft.com/en-us/library/cc232108.aspx */
length = ConvertToUnicode(sys_code_page, 0, volumeLabel, -1, &outStr, 0) * 2;
if ((length = ConvertToUnicode(sys_code_page, 0, volumeLabel, -1, &outStr, 0) * 2) <= 0)
{
WLog_ERR(TAG, "ConvertToUnicode failed!");
return CHANNEL_RC_NO_MEMORY;
}
Stream_Write_UINT32(output, 17 + length); /* Length */
if (!Stream_EnsureRemainingCapacity(output, 17 + length))
@ -421,13 +439,10 @@ static UINT drive_process_irp_query_volume_information(DRIVE_DEVICE* drive,
return CHANNEL_RC_NO_MEMORY;
}
Stream_Write_UINT64(output,
FILE_TIME_SYSTEM_TO_RDP(st.st_ctime)); /* VolumeCreationTime */
#ifdef ANDROID
Stream_Write_UINT32(output, svfst.f_fsid.__val[0]); /* VolumeSerialNumber */
#else
Stream_Write_UINT32(output, svfst.f_fsid); /* VolumeSerialNumber */
#endif
GetFileAttributesExW(drive->path, GetFileExInfoStandard, &wfad);
Stream_Write_UINT32(output, wfad.ftCreationTime.dwLowDateTime); /* VolumeCreationTime */
Stream_Write_UINT32(output, wfad.ftCreationTime.dwHighDateTime); /* VolumeCreationTime */
Stream_Write_UINT32(output, lpNumberOfFreeClusters & 0xffff); /* VolumeSerialNumber */
Stream_Write_UINT32(output, length); /* VolumeLabelLength */
Stream_Write_UINT8(output, 0); /* SupportsObjects */
/* Reserved(1), MUST NOT be added! */
@ -445,15 +460,20 @@ static UINT drive_process_irp_query_volume_information(DRIVE_DEVICE* drive,
return CHANNEL_RC_NO_MEMORY;
}
Stream_Write_UINT64(output, svfst.f_blocks); /* TotalAllocationUnits */
Stream_Write_UINT64(output, svfst.f_bavail); /* AvailableAllocationUnits */
Stream_Write_UINT32(output, 1); /* SectorsPerAllocationUnit */
Stream_Write_UINT32(output, svfst.f_bsize); /* BytesPerSector */
Stream_Write_UINT64(output, lpTotalNumberOfClusters); /* TotalAllocationUnits */
Stream_Write_UINT64(output, lpNumberOfFreeClusters); /* AvailableAllocationUnits */
Stream_Write_UINT32(output, lpSectorsPerCluster); /* SectorsPerAllocationUnit */
Stream_Write_UINT32(output, lpBytesPerSector); /* BytesPerSector */
break;
case FileFsAttributeInformation:
/* http://msdn.microsoft.com/en-us/library/cc232101.aspx */
length = ConvertToUnicode(sys_code_page, 0, diskType, -1, &outStr, 0) * 2;
if ((length = ConvertToUnicode(sys_code_page, 0, diskType, -1, &outStr, 0) * 2) <= 0)
{
WLog_ERR(TAG, "ConvertToUnicode failed!");
return CHANNEL_RC_NO_MEMORY;
}
Stream_Write_UINT32(output, 12 + length); /* Length */
if (!Stream_EnsureRemainingCapacity(output, 12 + length))
@ -466,12 +486,7 @@ static UINT drive_process_irp_query_volume_information(DRIVE_DEVICE* drive,
FILE_CASE_SENSITIVE_SEARCH |
FILE_CASE_PRESERVED_NAMES |
FILE_UNICODE_ON_DISK); /* FileSystemAttributes */
#ifdef ANDROID
Stream_Write_UINT32(output, 255); /* MaximumComponentNameLength */
#else
Stream_Write_UINT32(output,
svfst.f_namemax/*510*/); /* MaximumComponentNameLength */
#endif
Stream_Write_UINT32(output, MAX_PATH); /* MaximumComponentNameLength */
Stream_Write_UINT32(output, length); /* FileSystemNameLength */
Stream_Write(output, outStr, length); /* FileSystemName (Unicode) */
free(outStr);
@ -487,12 +502,11 @@ static UINT drive_process_irp_query_volume_information(DRIVE_DEVICE* drive,
return CHANNEL_RC_NO_MEMORY;
}
Stream_Write_UINT64(output, svfst.f_blocks); /* TotalAllocationUnits */
Stream_Write_UINT64(output,
svfst.f_bavail); /* CallerAvailableAllocationUnits */
Stream_Write_UINT64(output, svfst.f_bfree); /* AvailableAllocationUnits */
Stream_Write_UINT32(output, 1); /* SectorsPerAllocationUnit */
Stream_Write_UINT32(output, svfst.f_bsize); /* BytesPerSector */
Stream_Write_UINT64(output, lpTotalNumberOfClusters); /* TotalAllocationUnits */
Stream_Write_UINT64(output, lpNumberOfFreeClusters); /* CallerAvailableAllocationUnits */
Stream_Write_UINT64(output, lpNumberOfFreeClusters); /* AvailableAllocationUnits */
Stream_Write_UINT32(output, lpSectorsPerCluster); /* SectorsPerAllocationUnit */
Stream_Write_UINT32(output, lpBytesPerSector); /* BytesPerSector */
break;
case FileFsDeviceInformation:
@ -541,8 +555,7 @@ static UINT drive_process_irp_silent_ignore(DRIVE_DEVICE* drive, IRP* irp)
*/
static UINT drive_process_irp_query_directory(DRIVE_DEVICE* drive, IRP* irp)
{
char* path = NULL;
int status;
const WCHAR* path;
DRIVE_FILE* file;
BYTE InitialQuery;
UINT32 PathLength;
@ -551,16 +564,8 @@ static UINT drive_process_irp_query_directory(DRIVE_DEVICE* drive, IRP* irp)
Stream_Read_UINT8(irp->input, InitialQuery);
Stream_Read_UINT32(irp->input, PathLength);
Stream_Seek(irp->input, 23); /* Padding */
status = ConvertFromUnicode(CP_UTF8, 0, (WCHAR*) Stream_Pointer(irp->input),
PathLength / 2, &path, 0, NULL, NULL);
if (status < 1)
if (!(path = (char*) calloc(1, 1)))
{
WLog_ERR(TAG, "calloc failed!");
return CHANNEL_RC_NO_MEMORY;
}
path = (WCHAR*) Stream_Pointer(irp->input);
file = drive_get_file_by_id(drive, irp->FileId);
if (file == NULL)
@ -568,13 +573,12 @@ static UINT drive_process_irp_query_directory(DRIVE_DEVICE* drive, IRP* irp)
irp->IoStatus = STATUS_UNSUCCESSFUL;
Stream_Write_UINT32(irp->output, 0); /* Length */
}
else if (!drive_file_query_directory(file, FsInformationClass, InitialQuery,
path, irp->output))
else if (!drive_file_query_directory(file, FsInformationClass, InitialQuery, path, PathLength,
irp->output))
{
irp->IoStatus = STATUS_NO_MORE_FILES;
irp->IoStatus = drive_map_windows_err(GetLastError());
}
free(path);
return irp->Complete(irp);
}
@ -714,8 +718,7 @@ static void* drive_thread_func(void* arg)
}
if (error && drive->rdpcontext)
setChannelError(drive->rdpcontext, error,
"drive_thread_func reported an error");
setChannelError(drive->rdpcontext, error, "drive_thread_func reported an error");
ExitThread((DWORD)error);
return NULL;
@ -820,7 +823,13 @@ UINT drive_register_drive_path(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints,
for (i = 0; i <= length; i++)
Stream_Write_UINT8(drive->device.data, name[i] < 0 ? '_' : name[i]);
drive->path = path;
if (ConvertToUnicode(sys_code_page, 0, path, -1, &drive->path, 0) <= 0)
{
WLog_ERR(TAG, "ConvertToUnicode failed!");
error = CHANNEL_RC_NO_MEMORY;
goto out_error;
}
drive->files = ListDictionary_New(TRUE);
if (!drive->files)
@ -830,8 +839,7 @@ UINT drive_register_drive_path(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints,
goto out_error;
}
ListDictionary_ValueObject(drive->files)->fnObjectFree =
(OBJECT_FREE_FN) drive_file_free;
ListDictionary_ValueObject(drive->files)->fnObjectFree = (OBJECT_FREE_FN) drive_file_free;
drive->IrpQueue = MessageQueue_New(NULL);
if (!drive->IrpQueue)
@ -848,8 +856,8 @@ UINT drive_register_drive_path(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints,
goto out_error;
}
if (!(drive->thread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) drive_thread_func, drive, CREATE_SUSPENDED, NULL)))
if (!(drive->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) drive_thread_func, drive,
CREATE_SUSPENDED, NULL)))
{
WLog_ERR(TAG, "CreateThread failed!");
goto out_error;

View File

@ -1,60 +0,0 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* statvfs emulation for Windows
*
* Copyright 2012 Gerald Richter
* Copyright 2016 Inuvika Inc.
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.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.
*/
#include <string.h>
#include <malloc.h>
#include <winpr/crt.h>
#include <winpr/windows.h>
#include "statvfs.h"
int statvfs(const char *path, struct statvfs *buf)
{
BOOL res;
int len;
LPWSTR unicodestr = NULL;
DWORD lpSectorsPerCluster;
DWORD lpBytesPerSector;
DWORD lpNumberOfFreeClusters;
DWORD lpTotalNumberOfClusters;
len = ConvertToUnicode(CP_ACP, 0, path, -1, &unicodestr, 0);
if (len <= 0)
return -1;
res = GetDiskFreeSpaceW(unicodestr, &lpSectorsPerCluster, &lpBytesPerSector, &lpNumberOfFreeClusters, &lpTotalNumberOfClusters);
free(unicodestr);
buf->f_bsize = lpBytesPerSector; /* file system block size */
buf->f_frsize = 0; /* fragment size */
buf->f_blocks = lpTotalNumberOfClusters; /* size of fs in f_frsize units */
buf->f_bfree = lpNumberOfFreeClusters; /* # free blocks */
buf->f_bavail = lpNumberOfFreeClusters; /* # free blocks for unprivileged users */
buf->f_files = 0; /* # inodes */
buf->f_ffree = 0; /* # free inodes */
buf->f_favail = 0; /* # free inodes for unprivileged users */
buf->f_fsid = lpNumberOfFreeClusters & 0xffff; /* file system ID */
buf->f_flag = 0; /* mount flags */
buf->f_namemax = 250; /* maximum filename length */
return res;
}

View File

@ -1,50 +0,0 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* statvfs emulation for windows
*
* Copyright 2012 Gerald Richter
*
* 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.
*/
#ifndef RDPDR_DISK_STATVFS_H
#define RDPDR_DISK_STATVFS_H
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned long long fsblkcnt_t;
typedef unsigned long long fsfilcnt_t;
struct statvfs {
unsigned long f_bsize; /* file system block size */
unsigned long f_frsize; /* fragment size */
fsblkcnt_t f_blocks; /* size of fs in f_frsize units */
fsblkcnt_t f_bfree; /* # free blocks */
fsblkcnt_t f_bavail; /* # free blocks for unprivileged users */
fsfilcnt_t f_files; /* # inodes */
fsfilcnt_t f_ffree; /* # free inodes */
fsfilcnt_t f_favail; /* # free inodes for unprivileged users */
unsigned long f_fsid; /* file system ID */
unsigned long f_flag; /* mount flags */
unsigned long f_namemax; /* maximum filename length */
};
int statvfs(const char *path, struct statvfs *buf);
#ifdef __cplusplus
}
#endif
#endif /* RDPDR_DISK_STATVFS_H */

View File

@ -30,6 +30,7 @@ set (WITH_DEBUG_RAIL OFF CACHE BOOL "enable debug")
set (WITH_DEBUG_RDP OFF CACHE BOOL "enable debug")
set (WITH_DEBUG_RDPEI OFF CACHE BOOL "enable debug")
set (WITH_DEBUG_REDIR OFF CACHE BOOL "enable debug")
set (WITH_DEBUG_RDPDR OFF CACHE BOOL "enable debug")
set (WITH_DEBUG_RFX OFF CACHE BOOL "enable debug")
set (WITH_DEBUG_SCARD OFF CACHE BOOL "enable debug")
set (WITH_DEBUG_SND OFF CACHE BOOL "enable debug")

View File

@ -112,6 +112,7 @@ option(WITH_DEBUG_RAIL "Print RemoteApp debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_RDP "Print RDP debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_RDPEI "Print input virtual channel debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_REDIR "Redirection debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_RDPDR "Rdpdr debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_RFX "Print RemoteFX debug messages." ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_SCARD "Print smartcard debug messages" ${DEFAULT_DEBUG_OPTION})
option(WITH_DEBUG_SND "Print rdpsnd debug messages" ${DEFAULT_DEBUG_OPTION})

View File

@ -71,6 +71,7 @@
#cmakedefine WITH_DEBUG_RAIL
#cmakedefine WITH_DEBUG_RDP
#cmakedefine WITH_DEBUG_REDIR
#cmakedefine WITH_DEBUG_RDPDR
#cmakedefine WITH_DEBUG_RFX
#cmakedefine WITH_DEBUG_SCARD
#cmakedefine WITH_DEBUG_SND

View File

@ -3,6 +3,7 @@
* File Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -172,6 +173,13 @@
#define LOCKFILE_FAIL_IMMEDIATELY 1
#define LOCKFILE_EXCLUSIVE_LOCK 2
#define MOVEFILE_REPLACE_EXISTING 0x1
#define MOVEFILE_COPY_ALLOWED 0x2
#define MOVEFILE_DELAY_UNTIL_REBOOT 0x4
#define MOVEFILE_WRITE_THROUGH 0x8
#define MOVEFILE_CREATE_HARDLINK 0x10
#define MOVEFILE_FAIL_IF_NOT_TRACKABLE 0x20
typedef union _FILE_SEGMENT_ELEMENT
{
PVOID64 Buffer;
@ -266,6 +274,34 @@ WINPR_API BOOL WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[
WINPR_API BOOL FlushFileBuffers(HANDLE hFile);
typedef struct _WIN32_FILE_ATTRIBUTE_DATA
{
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
} WIN32_FILE_ATTRIBUTE_DATA, *LPWIN32_FILE_ATTRIBUTE_DATA;
typedef enum _GET_FILEEX_INFO_LEVELS
{
GetFileExInfoStandard,
GetFileExMaxInfoLevel
} GET_FILEEX_INFO_LEVELS;
WINPR_API BOOL GetFileAttributesExA(LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation);
WINPR_API DWORD GetFileAttributesA(LPCSTR lpFileName);
WINPR_API BOOL GetFileAttributesExW(LPCWSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation);
WINPR_API DWORD GetFileAttributesW(LPCWSTR lpFileName);
WINPR_API BOOL SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes);
WINPR_API BOOL SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes);
WINPR_API BOOL SetEndOfFile(HANDLE hFile);
WINPR_API DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh);
@ -314,6 +350,20 @@ WINPR_API HANDLE GetStdHandle(DWORD nStdHandle);
WINPR_API BOOL SetStdHandle(DWORD nStdHandle, HANDLE hHandle);
WINPR_API BOOL SetStdHandleEx(DWORD dwStdHandle, HANDLE hNewHandle, HANDLE* phOldHandle);
WINPR_API BOOL GetDiskFreeSpaceA(LPCSTR lpRootPathName, LPDWORD lpSectorsPerCluster,
LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters);
WINPR_API BOOL GetDiskFreeSpaceW(LPCWSTR lpRootPathName, LPDWORD lpSectorsPerCluster,
LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters);
WINPR_API BOOL MoveFileExA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName, DWORD dwFlags);
WINPR_API BOOL MoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags);
WINPR_API BOOL MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName);
WINPR_API BOOL MoveFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName);
#ifdef __cplusplus
}
#endif
@ -326,6 +376,12 @@ WINPR_API BOOL SetStdHandleEx(DWORD dwStdHandle, HANDLE hNewHandle, HANDLE* phOl
#define FindNextFile FindNextFileW
#define CreateDirectory CreateDirectoryW
#define RemoveDirectory RemoveDirectoryW
#define GetFileAttributesEx GetFileAttributesExW
#define GetFileAttributes GetFileAttributesW
#define SetFileAttributes SetFileAttributesW
#define GetDiskFreeSpace GetDiskFreeSpaceW
#define MoveFileEx MoveFileExW
#define MoveFile MoveFileW
#else
#define CreateFile CreateFileA
#define DeleteFile DeleteFileA
@ -334,6 +390,12 @@ WINPR_API BOOL SetStdHandleEx(DWORD dwStdHandle, HANDLE hNewHandle, HANDLE* phOl
#define FindNextFile FindNextFileA
#define CreateDirectory CreateDirectoryA
#define RemoveDirectory RemoveDirectoryA
#define GetFileAttributesEx GetFileAttributesExA
#define GetFileAttributes GetFileAttributesA
#define SetFileAttributes SetFileAttributesA
#define GetDiskFreeSpace GetDiskFreeSpaceA
#define MoveFileEx MoveFileExA
#define MoveFile MoveFileA
#endif
/* Extra Functions */

View File

@ -115,7 +115,6 @@ WINPR_API HRESULT PathCchStripPrefixW(PWSTR pszPath, size_t cchPath);
WINPR_API HRESULT PathCchRemoveFileSpecA(PSTR pszPath, size_t cchPath);
WINPR_API HRESULT PathCchRemoveFileSpecW(PWSTR pszPath, size_t cchPath);
#ifdef UNICODE
#define PathCchAddBackslash PathCchAddBackslashW
#define PathCchRemoveBackslash PathCchRemoveBackslashW
@ -292,10 +291,15 @@ WINPR_API BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes);
WINPR_API BOOL PathFileExistsA(LPCSTR pszPath);
WINPR_API BOOL PathFileExistsW(LPCWSTR pszPath);
WINPR_API BOOL PathIsDirectoryEmptyA(LPCSTR pszPath);
WINPR_API BOOL PathIsDirectoryEmptyW(LPCWSTR pszPath);
#ifdef UNICODE
#define PathFileExists PathFileExistsW
#define PathIsDirectoryEmpty PathIsDirectoryEmptyW
#else
#define PathFileExists PathFileExistsA
#define PathIsDirectoryEmpty PathIsDirectoryEmptyA
#endif
#endif

View File

@ -3,6 +3,7 @@
* Shell Functions
*
* Copyright 2015 Dell Software <Mike.McDonald@software.dell.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -49,4 +50,3 @@ WINPR_API BOOL GetUserProfileDirectoryW(HANDLE hToken, LPWSTR lpProfileDir, LPDW
#endif
#endif /* WINPR_SHELL_H */

View File

@ -3,6 +3,7 @@
* String Manipulation (CRT)
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -58,6 +59,7 @@ WINPR_API int _wcscmp(const WCHAR* string1, const WCHAR* string2);
WINPR_API size_t _wcslen(const WCHAR* str);
WINPR_API WCHAR* _wcschr(const WCHAR* str, WCHAR c);
WINPR_API WCHAR* _wcsrchr(const WCHAR* str, WCHAR c);
WINPR_API char* strtok_s(char* strToken, const char* strDelimit,
char** context);
@ -69,6 +71,7 @@ WINPR_API WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit,
#define _wcscmp wcscmp
#define _wcslen wcslen
#define _wcschr wcschr
#define _wcsrchr wcsrchr
#endif

View File

@ -129,6 +129,23 @@ WCHAR* _wcschr(const WCHAR* str, WCHAR c)
return ((*p == value) ? p : NULL);
}
/* _wcsrchr -> wcsrchr */
WCHAR* _wcsrchr(const WCHAR* str, WCHAR c)
{
WCHAR *p;
WCHAR ch;
if (!str)
return NULL;
for (p = (WCHAR *) 0; (ch = *str); str++)
if (ch == c)
p = (WCHAR *) str;
return p;
}
char* strtok_s(char* strToken, const char* strDelimit, char** context)
{
return strtok_r(strToken, strDelimit, context);

View File

@ -4,6 +4,7 @@
*
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 Bernhard Miklautz <bernhard.miklautz@thincast.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,6 +45,13 @@
#include <sys/stat.h>
#include <sys/time.h>
#ifdef ANDROID
#include <sys/vfs.h>
#else
#include <sys/statvfs.h>
#endif
static BOOL FileIsHandled(HANDLE handle)
{
WINPR_FILE* pFile = (WINPR_FILE*) handle;
@ -97,10 +105,12 @@ static BOOL FileSetEndOfFile(HANDLE hFile)
return FALSE;
size = ftell(pFile->fp);
if (ftruncate(fileno(pFile->fp), size) < 0)
{
WLog_ERR(TAG, "ftruncate %s failed with %s [0x%08X]",
pFile->lpFileName, strerror(errno), errno);
SetLastError(map_posix_err(errno));
return FALSE;
}
@ -161,9 +171,10 @@ static BOOL FileRead(PVOID Object, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
return FALSE;
file = (WINPR_FILE *)Object;
io_status = fread(lpBuffer, nNumberOfBytesToRead, 1, file->fp);
clearerr(file->fp);
io_status = fread(lpBuffer, 1, nNumberOfBytesToRead, file->fp);
if (io_status != 1)
if (io_status == 0 && ferror(file->fp))
{
status = FALSE;
@ -172,11 +183,13 @@ static BOOL FileRead(PVOID Object, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
case EWOULDBLOCK:
SetLastError(ERROR_NO_DATA);
break;
default:
SetLastError(map_posix_err(errno));
}
}
if (lpNumberOfBytesRead)
*lpNumberOfBytesRead = nNumberOfBytesToRead;
*lpNumberOfBytesRead = io_status;
return status;
}
@ -199,11 +212,15 @@ static BOOL FileWrite(PVOID Object, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrit
file = (WINPR_FILE *)Object;
io_status = fwrite(lpBuffer, nNumberOfBytesToWrite, 1, file->fp);
if (io_status != 1)
clearerr(file->fp);
io_status = fwrite(lpBuffer, 1, nNumberOfBytesToWrite, file->fp);
if (io_status == 0 && ferror(file->fp))
{
SetLastError(map_posix_err(errno));
return FALSE;
}
*lpNumberOfBytesWritten = nNumberOfBytesToWrite;
*lpNumberOfBytesWritten = io_status;
return TRUE;
}
@ -507,7 +524,7 @@ static HANDLE_OPS shmOps = {
static const char* FileGetMode(DWORD dwDesiredAccess, DWORD dwCreationDisposition, BOOL* create)
{
BOOL writeable = dwDesiredAccess & GENERIC_WRITE;
BOOL writeable = (dwDesiredAccess & (GENERIC_WRITE | STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0;
switch(dwCreationDisposition)
{
@ -522,7 +539,7 @@ static const char* FileGetMode(DWORD dwDesiredAccess, DWORD dwCreationDispositio
return "rb+";
case OPEN_EXISTING:
*create = FALSE;
return "rb+";
return (writeable) ? "rb+" : "rb";
case TRUNCATE_EXISTING:
*create = FALSE;
return "wb+";
@ -532,6 +549,51 @@ static const char* FileGetMode(DWORD dwDesiredAccess, DWORD dwCreationDispositio
}
}
UINT32 map_posix_err(int fs_errno)
{
UINT32 rc;
/* try to return NTSTATUS version of error code */
switch (fs_errno)
{
case 0:
rc = STATUS_SUCCESS;
break;
case EPERM:
case EACCES:
rc = ERROR_ACCESS_DENIED;
break;
case ENOENT:
rc = ERROR_FILE_NOT_FOUND;
break;
case EBUSY:
rc = ERROR_BUSY_DRIVE;
break;
case EEXIST:
rc = ERROR_FILE_EXISTS;
break;
case EISDIR:
rc = STATUS_FILE_IS_A_DIRECTORY;
break;
case ENOTEMPTY:
rc = STATUS_DIRECTORY_NOT_EMPTY;
break;
default:
rc = STATUS_UNSUCCESSFUL;
break;
}
return rc;
}
static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
{
@ -540,6 +602,7 @@ static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dw
const char* mode = FileGetMode(dwDesiredAccess, dwCreationDisposition, &create);
int lock = 0;
FILE* fp = NULL;
struct stat st;
if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED)
{
@ -575,9 +638,21 @@ static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dw
if (create)
{
if (dwCreationDisposition == CREATE_NEW)
{
if (stat(pFile->lpFileName, &st) == 0)
{
SetLastError(ERROR_FILE_EXISTS);
free(pFile->lpFileName);
free(pFile);
return INVALID_HANDLE_VALUE;
}
}
fp = fopen(pFile->lpFileName, "ab");
if (!fp)
{
SetLastError(map_posix_err(errno));
free(pFile->lpFileName);
free(pFile);
return INVALID_HANDLE_VALUE;
@ -594,6 +669,7 @@ static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dw
{
/* This case can occur when trying to open a
* not existing file without create flag. */
SetLastError(map_posix_err(errno));
free(pFile->lpFileName);
free(pFile);
return INVALID_HANDLE_VALUE;
@ -612,6 +688,7 @@ static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dw
{
WLog_ERR(TAG, "flock failed with %s [0x%08X]",
strerror(errno), errno);
SetLastError(map_posix_err(errno));
FileCloseHandle(pFile);
return INVALID_HANDLE_VALUE;
}
@ -619,6 +696,13 @@ static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dw
pFile->bLocked = TRUE;
}
if (fstat(fileno(pFile->fp), &st)==0 && dwFlagsAndAttributes & FILE_ATTRIBUTE_READONLY)
{
st.st_mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
fchmod(fileno(pFile->fp), st.st_mode);
}
SetLastError(STATUS_SUCCESS);
return pFile;
}
@ -695,6 +779,41 @@ BOOL SetStdHandleEx(DWORD dwStdHandle, HANDLE hNewHandle, HANDLE* phOldHandle)
return FALSE;
}
BOOL GetDiskFreeSpaceA(LPCSTR lpRootPathName, LPDWORD lpSectorsPerCluster,
LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters)
{
#if defined(ANDROID)
#define STATVFS statfs
#else
#define STATVFS statvfs
#endif
struct STATVFS svfst;
STATVFS(lpRootPathName, &svfst);
*lpSectorsPerCluster = svfst.f_frsize;
*lpBytesPerSector = 1;
*lpNumberOfFreeClusters = svfst.f_bavail;
*lpTotalNumberOfClusters = svfst.f_blocks;
return TRUE;
}
BOOL GetDiskFreeSpaceW(LPCWSTR lpwRootPathName, LPDWORD lpSectorsPerCluster,
LPDWORD lpBytesPerSector, LPDWORD lpNumberOfFreeClusters, LPDWORD lpTotalNumberOfClusters)
{
LPSTR lpRootPathName;
BOOL ret;
if (ConvertFromUnicode(CP_UTF8, 0, lpwRootPathName, -1, &lpRootPathName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
ret = GetDiskFreeSpaceA(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector,
lpNumberOfFreeClusters, lpTotalNumberOfClusters);
free(lpRootPathName);
return ret;
}
#endif /* _WIN32 */
#ifdef _UWP

View File

@ -4,6 +4,7 @@
*
* Copyright 2015 Armin Novak <armin.novak@thincast.com>
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -55,6 +56,8 @@ typedef struct winpr_file WINPR_FILE;
HANDLE_CREATOR *GetFileHandleCreator(void);
UINT32 map_posix_err(int fs_errno);
#endif /* _WIN32 */
#endif /* WINPR_FILE_PRIV_H */

View File

@ -4,6 +4,7 @@
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2014 Hewlett-Packard Development Company, L.P.
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,6 +45,8 @@
#include <assert.h>
#include <pthread.h>
#include <dirent.h>
#include <libgen.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/stat.h>
@ -173,6 +176,10 @@
* http://code.google.com/p/kernel/wiki/AIOUserGuide
*/
#define EPOCH_DIFF 11644473600LL
#define STAT_TIME_TO_FILETIME(_t) (((UINT64)(_t) + EPOCH_DIFF) * 10000000LL)
static wArrayList *_HandleCreators;
static pthread_once_t _HandleCreatorsInitialized = PTHREAD_ONCE_INIT;
@ -461,6 +468,112 @@ BOOL FlushFileBuffers(HANDLE hFile)
return FALSE;
}
BOOL WINAPI GetFileAttributesExA(LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId,
LPVOID lpFileInformation)
{
LPWIN32_FILE_ATTRIBUTE_DATA fd = lpFileInformation;
WIN32_FIND_DATAA findFileData;
HANDLE hFind;
if ((hFind = FindFirstFileA(lpFileName, &findFileData) != INVALID_HANDLE_VALUE))
FindClose(hFind);
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,
LPVOID lpFileInformation)
{
BOOL ret;
LPSTR lpCFileName;
if (ConvertFromUnicode(CP_UTF8, 0, lpFileName, -1, &lpCFileName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
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;
LPSTR lpCFileName;
if (ConvertFromUnicode(CP_UTF8, 0, lpFileName, -1, &lpCFileName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
ret = GetFileAttributesA(lpCFileName);
free(lpCFileName);
return ret;
}
BOOL SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes)
{
struct stat st;
if (stat(lpFileName, &st) != 0)
{
return FALSE;
}
if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
{
st.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
}
else
{
st.st_mode |= S_IWUSR;
}
if (chmod(lpFileName, st.st_mode) != 0)
{
return FALSE;
}
return TRUE;
}
BOOL SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
{
BOOL ret;
LPSTR lpCFileName;
if (ConvertFromUnicode(CP_UTF8, 0, lpFileName, -1, &lpCFileName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
ret = SetFileAttributesA(lpCFileName,dwFileAttributes);
free(lpCFileName);
return ret;
}
BOOL SetEndOfFile(HANDLE hFile)
{
ULONG Type;
@ -721,20 +834,8 @@ HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
return INVALID_HANDLE_VALUE; /* failed to open directory */
}
while ((pFileSearch->pDirent = readdir(pFileSearch->pDir)) != NULL)
{
if ((strcmp(pFileSearch->pDirent->d_name, ".") == 0) || (strcmp(pFileSearch->pDirent->d_name, "..") == 0))
{
/* skip "." and ".." */
continue;
}
if (FilePatternMatchA(pFileSearch->pDirent->d_name, pFileSearch->lpPattern))
{
strcpy(lpFindFileData->cFileName, pFileSearch->pDirent->d_name);
if (FindNextFileA((HANDLE) pFileSearch, lpFindFileData))
return (HANDLE) pFileSearch;
}
}
FindClose(pFileSearch);
return INVALID_HANDLE_VALUE;
@ -742,24 +843,68 @@ HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
HANDLE FindFirstFileW(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData)
{
return NULL;
char* utfFileName = NULL;
HANDLE h;
WCHAR* unicodeFileName;
int length;
LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)malloc(sizeof(WIN32_FIND_DATAA));
if (!fd)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return INVALID_HANDLE_VALUE;
}
if (ConvertFromUnicode(CP_UTF8, 0, lpFileName, -1, &utfFileName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return INVALID_HANDLE_VALUE;
}
h = FindFirstFileA(utfFileName, fd);
free(utfFileName);
if (h != INVALID_HANDLE_VALUE)
{
CopyMemory(lpFindFileData, fd, 352);
unicodeFileName = NULL;
length = ConvertToUnicode(CP_UTF8, 0, fd->cFileName, -1, &unicodeFileName, 0) * 2;
if (length == 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
free(fd);
return INVALID_HANDLE_VALUE;
}
CopyMemory(&lpFindFileData->cFileName, unicodeFileName, length);
free(unicodeFileName);
}
return h;
}
HANDLE FindFirstFileExA(LPCSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
{
return NULL;
return INVALID_HANDLE_VALUE;
}
HANDLE FindFirstFileExW(LPCWSTR lpFileName, FINDEX_INFO_LEVELS fInfoLevelId, LPVOID lpFindFileData,
FINDEX_SEARCH_OPS fSearchOp, LPVOID lpSearchFilter, DWORD dwAdditionalFlags)
{
return NULL;
return INVALID_HANDLE_VALUE;
}
BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
{
WIN32_FILE_SEARCH* pFileSearch;
struct stat fileStat;
char* fullpath;
int pathlen;
int namelen;
UINT64 ft;
ZeroMemory(lpFindFileData, sizeof(WIN32_FIND_DATAA));
if (!hFindFile)
return FALSE;
@ -774,15 +919,103 @@ BOOL FindNextFileA(HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData)
if (FilePatternMatchA(pFileSearch->pDirent->d_name, pFileSearch->lpPattern))
{
strcpy(lpFindFileData->cFileName, pFileSearch->pDirent->d_name);
namelen = strlen(lpFindFileData->cFileName);
pathlen = strlen(pFileSearch->lpPath);
fullpath = (char*)malloc(pathlen + namelen + 2);
if (fullpath == NULL)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
memcpy(fullpath, pFileSearch->lpPath, pathlen);
fullpath[pathlen] = '/';
memcpy(fullpath + pathlen + 1, pFileSearch->pDirent->d_name, namelen);
fullpath[pathlen+namelen+1] = 0;
if (lstat(fullpath, &fileStat) != 0)
{
free(fullpath);
SetLastError(map_posix_err(errno));
return FALSE;
}
free(fullpath);
lpFindFileData->dwFileAttributes = 0;
if (S_ISDIR(fileStat.st_mode))
lpFindFileData->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
if (lpFindFileData->dwFileAttributes == 0)
lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
if (pFileSearch->pDirent->d_name[0] == '.' && namelen != 1 &&
(pFileSearch->pDirent->d_name[1] != '.' && namelen != 2))
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 = ft >> 32;
lpFindFileData->ftCreationTime.dwLowDateTime = ft & 0xFFFFFFFF;
ft = STAT_TIME_TO_FILETIME(fileStat.st_mtime);
lpFindFileData->ftLastWriteTime.dwHighDateTime = ft >> 32;
lpFindFileData->ftCreationTime.dwLowDateTime = ft & 0xFFFFFFFF;
ft = STAT_TIME_TO_FILETIME(fileStat.st_atime);
lpFindFileData->ftLastAccessTime.dwHighDateTime = ft >> 32;
lpFindFileData->ftLastAccessTime.dwLowDateTime = ft & 0xFFFFFFFF;
lpFindFileData->nFileSizeHigh = fileStat.st_size >> 32;
lpFindFileData->nFileSizeLow = fileStat.st_size & 0xFFFFFFFF;
return TRUE;
}
}
SetLastError(ERROR_NO_MORE_FILES);
return FALSE;
}
BOOL FindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData)
{
WCHAR* unicodeFileName;
int length;
LPWIN32_FIND_DATAA fd = (LPWIN32_FIND_DATAA)malloc(sizeof(WIN32_FIND_DATAA));
if (!fd)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
if (FindNextFileA(hFindFile, fd))
{
CopyMemory(lpFindFileData, fd, 352);
unicodeFileName = NULL;
length = ConvertToUnicode(CP_UTF8, 0, fd->cFileName, -1, &unicodeFileName, 0) * 2;
if (length == 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
free(fd);
return FALSE;
}
CopyMemory(&lpFindFileData->cFileName, unicodeFileName, length);
free(unicodeFileName);
free(fd);
return TRUE;
}
free(fd);
return FALSE;
}
@ -816,19 +1049,111 @@ BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttribu
BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
char* utfPathName = NULL;
BOOL ret;
if (ConvertFromUnicode(CP_UTF8, 0, lpPathName, -1, &utfPathName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
ret = CreateDirectoryA(utfPathName, lpSecurityAttributes);
free(utfPathName);
return ret;
}
BOOL RemoveDirectoryA(LPCSTR lpPathName)
{
return (rmdir(lpPathName) == 0);
int ret = rmdir(lpPathName);
if (ret != 0)
SetLastError(map_posix_err(errno));
else
SetLastError(STATUS_SUCCESS);
return ret == 0;
}
BOOL RemoveDirectoryW(LPCWSTR lpPathName)
{
char* utfPathName = NULL;
BOOL ret;
if (ConvertFromUnicode(CP_UTF8, 0, lpPathName, -1, &utfPathName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
ret = RemoveDirectoryA(utfPathName);
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)
{
LPSTR lpCExistingFileName;
LPSTR lpCNewFileName;
BOOL ret;
if (ConvertFromUnicode(CP_UTF8, 0, lpExistingFileName, -1, &lpCExistingFileName, 0, NULL, NULL) <= 0)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
if (ConvertFromUnicode(CP_UTF8, 0, lpNewFileName, -1, &lpCNewFileName, 0, NULL, NULL) <= 0)
{
free(lpCExistingFileName);
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return FALSE;
}
ret = MoveFileExA(lpCExistingFileName, lpCNewFileName, dwFlags);
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);
}
#endif
/* Extended API */

View File

@ -3,6 +3,7 @@
* Path Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,6 +44,7 @@
#include <Shlobj.h>
#else
#include <errno.h>
#include <dirent.h>
#endif
static char* GetPath_XDG_CONFIG_HOME(void);
@ -505,9 +507,54 @@ BOOL PathFileExistsA(LPCSTR pszPath)
BOOL PathFileExistsW(LPCWSTR pszPath)
{
LPSTR lpFileNameA = NULL;
BOOL ret;
if (ConvertFromUnicode(CP_UTF8, 0, pszPath, -1, &lpFileNameA, 0, NULL, NULL) < 1)
return FALSE;
ret = PathFileExistsA(lpFileNameA);
free (lpFileNameA);
return ret;
}
BOOL PathIsDirectoryEmptyA(LPCSTR pszPath)
{
struct dirent *dp;
int empty = 1;
DIR *dir = opendir(pszPath);
if (dir == NULL) /* Not a directory or doesn't exist */
return 1;
while ((dp = readdir(dir)) != NULL) {
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
continue; /* Skip . and .. */
empty = 0;
break;
}
closedir(dir);
return empty;
}
BOOL PathIsDirectoryEmptyW(LPCWSTR pszPath)
{
LPSTR lpFileNameA = NULL;
BOOL ret;
if (ConvertFromUnicode(CP_UTF8, 0, pszPath, -1, &lpFileNameA, 0, NULL, NULL) < 1)
return FALSE;
ret = PathIsDirectoryEmptyA(lpFileNameA);
free (lpFileNameA);
return ret;
}
#else
#ifdef _WIN32