Use wide character abstraction functions for file operations.

Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Klaas Freitag 2012-10-12 17:08:32 +02:00 committed by Andreas Schneider
parent 922254ba94
commit 9120098811
5 changed files with 142 additions and 53 deletions

View file

@ -1127,8 +1127,10 @@ static ssize_t owncloud_read(csync_vio_method_handle_t *fhandle, void *buf, size
#ifdef _WIN32 #ifdef _WIN32
_fmode = _O_BINARY; _fmode = _O_BINARY;
#endif #endif
if (( writeCtx->fd = open( writeCtx->tmpFileName, O_RDONLY )) < 0) { tmpFileName = c_multibyte(writeCtx->tmpFileName);
DEBUG_WEBDAV(("Could not open local file %s\n", writeCtx->tmpFileName )); if (( writeCtx->fd = _topen( tmpFileName, O_RDONLY )) < 0) {
c_free_multibyte(tmpFileName);
DEBUG_WEBDAV(("Could not open local file %s", writeCtx->tmpFileName ));
errno = EIO; errno = EIO;
return -1; return -1;
} else { } else {

View file

@ -30,18 +30,20 @@
#include "c_macro.h" #include "c_macro.h"
#include "c_alloc.h" #include "c_alloc.h"
#include "c_dir.h" #include "c_dir.h"
#include "c_private.h" #include "c_string.h"
int c_mkdirs(const char *path, mode_t mode) { int c_mkdirs(const char *path, mode_t mode) {
int tmp; int tmp;
csync_stat_t sb; csync_stat_t sb;
const _TCHAR *wpath = c_multibyte(path);
const _TCHAR *swpath = NULL;
if (path == NULL) { if (path == NULL) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
if (lstat(path, &sb) == 0) { if (_tstat(wpath, &sb) == 0) {
if (! S_ISDIR(sb.st_mode)) { if (! S_ISDIR(sb.st_mode)) {
errno = ENOTDIR; errno = ENOTDIR;
return -1; return -1;
@ -57,41 +59,46 @@ int c_mkdirs(const char *path, mode_t mode) {
char subpath[tmp + 1]; char subpath[tmp + 1];
memcpy(subpath, path, tmp); memcpy(subpath, path, tmp);
subpath[tmp] = '\0'; subpath[tmp] = '\0';
swpath = c_multibyte(subpath);
if (lstat(subpath, &sb) == 0) { if (_tstat(swpath, &sb) == 0) {
if (! S_ISDIR(sb.st_mode)) { if (! S_ISDIR(sb.st_mode)) {
errno = ENOTDIR; errno = ENOTDIR;
return -1; return -1;
} }
} else if (errno != ENOENT) { } else if (errno != ENOENT) {
c_free_multibyte(swpath);
return -1; return -1;
} else if (c_mkdirs(subpath, mode) < 0) { } else if (c_mkdirs(subpath, mode) < 0) {
c_free_multibyte(swpath);
return -1; return -1;
} }
} }
#ifdef _WIN32
#ifndef _WIN32 tmp = _tmkdir(wpath);
tmp = mkdir(path, mode);
#else #else
tmp = mkdir(path); tmp = _tmkdir(wpath, mode);
#endif #endif
c_free_multibyte(swpath);
c_free_multibyte(wpath);
if ((tmp < 0) && (errno == EEXIST)) { if ((tmp < 0) && (errno == EEXIST)) {
return 0; return 0;
} }
return tmp; return tmp;
} }
int c_rmdirs(const char *path) { int c_rmdirs(const char *path) {
DIR *d; _TDIR *d;
struct dirent *dp; struct _tdirent *dp;
csync_stat_t sb; csync_stat_t sb;
char *fname; char *fname = NULL;
const _TCHAR *wfname = NULL;
const _TCHAR *wpath = c_multibyte(path);
if ((d = opendir(path)) != NULL) { if ((d = _topendir(wpath)) != NULL) {
while(stat(path, &sb) == 0) { while( _tstat(wpath, &sb) == 0) {
/* if we can remove the directory we're done */ /* if we can remove the directory we're done */
if (rmdir(path) == 0) { if (_trmdir(path) == 0) {
break; break;
} }
switch (errno) { switch (errno) {
@ -100,11 +107,11 @@ int c_rmdirs(const char *path) {
case EBADF: case EBADF:
break; /* continue */ break; /* continue */
default: default:
closedir(d); _tclosedir(d);
return 0; return 0;
} }
while ((dp = readdir(d)) != NULL) { while ((dp = _treaddir(d)) != NULL) {
size_t len; size_t len;
/* skip '.' and '..' */ /* skip '.' and '..' */
if (dp->d_name[0] == '.' && if (dp->d_name[0] == '.' &&
@ -113,50 +120,54 @@ int c_rmdirs(const char *path) {
continue; continue;
} }
len = strlen(path) + strlen(dp->d_name) + 2; len = strlen(path) + _tcslen(dp->d_name) + 2;
fname = c_malloc(len); fname = c_malloc(len);
if (fname == NULL) { if (fname == NULL) {
closedir(d); closedir(d);
return -1; return -1;
} }
snprintf(fname, len, "%s/%s", path, dp->d_name); snprintf(fname, len, "%s/%s", path, dp->d_name);
wfname = c_multibyte(fname);
/* stat the file */ /* stat the file */
if (lstat(fname, &sb) != -1) { if (_tstat(wfname, &sb) != -1) {
#ifdef __unix__ #ifdef __unix__
if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode)) { if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode)) {
#else #else
if (S_ISDIR(sb.st_mode)) { if (S_ISDIR(sb.st_mode)) {
#endif #endif
if (rmdir(fname) < 0) { /* can't be deleted */ if (_trmdir(wfname) < 0) { /* can't be deleted */
if (errno == EACCES) { if (errno == EACCES) {
closedir(d); _tclosedir(d);
SAFE_FREE(fname); SAFE_FREE(fname);
c_free_multibyte(wfname);
return -1; return -1;
} }
c_rmdirs(fname); c_rmdirs(fname);
} }
} else { } else {
unlink(fname); _tunlink(wfname);
} }
} /* lstat */ } /* lstat */
SAFE_FREE(fname); SAFE_FREE(fname);
c_free_multibyte(wfname);
} /* readdir */ } /* readdir */
rewinddir(d); _trewinddir(d);
} }
} else { } else {
return -1; return -1;
} }
closedir(d); _tclosedir(d);
return 0; return 0;
} }
int c_isdir(const char *path) { int c_isdir(const char *path) {
csync_stat_t sb; csync_stat_t sb;
const _TCHAR *wpath = c_multibyte(path);
if (lstat (path, &sb) == 0 && S_ISDIR(sb.st_mode)) { if (_tstat (wpath, &sb) == 0 && S_ISDIR(sb.st_mode)) {
return 1; return 1;
} }

View file

@ -39,8 +39,11 @@
/* check if path is a file */ /* check if path is a file */
int c_isfile(const char *path) { int c_isfile(const char *path) {
csync_stat_t sb; csync_stat_t sb;
const _TCHAR *wpath = c_multibyte(path);
int re = _tstat(wpath, &sb);
c_free_multibyte(wpath);
if (lstat (path, &sb) < 0) { if (re< 0) {
return 0; return 0;
} }
@ -73,8 +76,9 @@ int c_copy(const char* src, const char *dst, mode_t mode) {
return -1; return -1;
} }
#endif #else
/* Win32 does not come here. */
if (c_streq(src, dst)) { if (c_streq(src, dst)) {
return -1; return -1;
} }
@ -150,5 +154,6 @@ out:
unlink(dst); unlink(dst);
} }
return rc; return rc;
#endif
} }

View file

@ -49,25 +49,49 @@ int csync_vio_init(CSYNC *ctx, const char *module, const char *args) {
char *err = NULL; char *err = NULL;
csync_vio_method_t *m = NULL; csync_vio_method_t *m = NULL;
csync_vio_method_init_fn init_fn; csync_vio_method_init_fn init_fn;
const _TCHAR *mpath = NULL;
if (asprintf(&path, "%s/csync_%s.%s", PLUGINDIR, module, MODULE_EXTENSION) < 0) { if (asprintf(&path, "%s/csync_%s.%s", PLUGINDIR, module, MODULE_EXTENSION) < 0) {
return -1; return -1;
} }
if (lstat(path, &sb) < 0) { mpath = c_multibyte(path);
if (_tstat(mpath, &sb) < 0) {
SAFE_FREE(path); SAFE_FREE(path);
if (asprintf(&path, "%s/modules/csync_%s.%s", BINARYDIR, module, MODULE_EXTENSION) < 0) { if (asprintf(&path, "%s/modules/csync_%s.%s", BINARYDIR, module, MODULE_EXTENSION) < 0) {
return -1; return -1;
} }
} }
c_free_multibyte(mpath);
#ifdef _WIN32 #ifdef _WIN32
if (lstat(path, &sb) < 0) { mpath = c_multibyte(path);
if (_tstat(mpath, &sb) < 0) {
SAFE_FREE(path); SAFE_FREE(path);
if (asprintf(&path, "modules/csync_%s.%s", module, MODULE_EXTENSION) < 0) { if (asprintf(&path, "modules/csync_%s.%s", module, MODULE_EXTENSION) < 0) {
return -1; return -1;
} }
} }
c_free_multibyte(mpath);
#endif
#ifdef __APPLE__
if (lstat(path, &sb) < 0) {
SAFE_FREE(path);
char path_tmp[1024];
uint32_t size = sizeof(path_tmp);
if (_NSGetExecutablePath(path_tmp, &size) == 0)
printf("executable path is %s\n", path_tmp);
char* path2 = NULL;
path2 = c_dirname(path_tmp);
if (asprintf(&path, "%s/../Plugins/csync_%s.%s", path2, module, MODULE_EXTENSION) < 0) {
return -1;
}
}
#endif #endif
ctx->module.handle = dlopen(path, RTLD_LAZY); ctx->module.handle = dlopen(path, RTLD_LAZY);

View file

@ -27,19 +27,29 @@
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
#ifdef _WIN32
#include "windows.h"
#define _UNICODE
#endif
#include "c_private.h" #include "c_private.h"
#include "c_lib.h" #include "c_lib.h"
#include "c_string.h"
#include "vio/csync_vio_local.h" #include "vio/csync_vio_local.h"
typedef struct fhandle_s { typedef struct fhandle_s {
int fd; int fd;
} fhandle_t; } fhandle_t;
/* the url comes in as utf-8 and in windows, it needs to be multibyte. */
csync_vio_method_handle_t *csync_vio_local_open(const char *durl, int flags, mode_t mode) { csync_vio_method_handle_t *csync_vio_local_open(const char *durl, int flags, mode_t mode) {
fhandle_t *handle = NULL; fhandle_t *handle = NULL;
int fd = -1; int fd = -1;
const _TCHAR *url = c_multibyte(durl);
if ((fd = open(durl, flags, mode)) < 0) { if ((fd = _topen(url, flags, mode)) < 0) {
return NULL; return NULL;
} }
@ -50,15 +60,19 @@ csync_vio_method_handle_t *csync_vio_local_open(const char *durl, int flags, mod
} }
handle->fd = fd; handle->fd = fd;
c_free_multibyte(url);
return (csync_vio_method_handle_t *) handle; return (csync_vio_method_handle_t *) handle;
} }
csync_vio_method_handle_t *csync_vio_local_creat(const char *durl, mode_t mode) { csync_vio_method_handle_t *csync_vio_local_creat(const char *durl, mode_t mode) {
fhandle_t *handle = NULL; fhandle_t *handle = NULL;
int fd = -1; int fd = -1;
const _TCHAR *url = c_multibyte(durl);
if ((fd = creat(durl, mode)) < 0) { if(( fd = _tcreat( url, mode)) < 0) {
return NULL; return NULL;
} }
handle = c_malloc(sizeof(fhandle_t)); handle = c_malloc(sizeof(fhandle_t));
@ -68,6 +82,7 @@ csync_vio_method_handle_t *csync_vio_local_creat(const char *durl, mode_t mode)
} }
handle->fd = fd; handle->fd = fd;
c_free_multibyte(url);
return (csync_vio_method_handle_t *) handle; return (csync_vio_method_handle_t *) handle;
} }
@ -138,24 +153,28 @@ off_t csync_vio_local_lseek(csync_vio_method_handle_t *fhandle, off_t offset, in
*/ */
typedef struct dhandle_s { typedef struct dhandle_s {
DIR *dh; _TDIR *dh;
char *path; char *path;
} dhandle_t; } dhandle_t;
csync_vio_method_handle_t *csync_vio_local_opendir(const char *name) { csync_vio_method_handle_t *csync_vio_local_opendir(const char *name) {
dhandle_t *handle = NULL; dhandle_t *handle = NULL;
const _TCHAR *dirname = c_multibyte(name);
handle = c_malloc(sizeof(dhandle_t)); handle = c_malloc(sizeof(dhandle_t));
if (handle == NULL) { if (handle == NULL) {
return NULL; return NULL;
} }
handle->dh = opendir(name); handle->dh = _topendir( dirname );
if (handle->dh == NULL) { if (handle->dh == NULL) {
SAFE_FREE(handle); SAFE_FREE(handle);
return NULL; return NULL;
} }
handle->path = c_strdup(name); #ifdef _WIN32
handle->path = c_utf8(dirname);
#else
handle->path = c_strdup(dirname);
#endif
return (csync_vio_method_handle_t *) handle; return (csync_vio_method_handle_t *) handle;
} }
@ -170,8 +189,7 @@ int csync_vio_local_closedir(csync_vio_method_handle_t *dhandle) {
} }
handle = (dhandle_t *) dhandle; handle = (dhandle_t *) dhandle;
rc = _tclosedir(handle->dh);
rc = closedir(handle->dh);
SAFE_FREE(handle->path); SAFE_FREE(handle->path);
SAFE_FREE(handle); SAFE_FREE(handle);
@ -180,14 +198,15 @@ int csync_vio_local_closedir(csync_vio_method_handle_t *dhandle) {
} }
csync_vio_file_stat_t *csync_vio_local_readdir(csync_vio_method_handle_t *dhandle) { csync_vio_file_stat_t *csync_vio_local_readdir(csync_vio_method_handle_t *dhandle) {
struct dirent *dirent = NULL; struct _tdirent *dirent = NULL;
dhandle_t *handle = NULL; dhandle_t *handle = NULL;
csync_vio_file_stat_t *file_stat = NULL; csync_vio_file_stat_t *file_stat = NULL;
handle = (dhandle_t *) dhandle; handle = (dhandle_t *) dhandle;
errno = 0; errno = 0;
dirent = readdir(handle->dh); dirent = _treaddir(handle->dh);
if (dirent == NULL) { if (dirent == NULL) {
if (errno) { if (errno) {
goto err; goto err;
@ -201,7 +220,11 @@ csync_vio_file_stat_t *csync_vio_local_readdir(csync_vio_method_handle_t *dhandl
goto err; goto err;
} }
#ifdef _WIN32
file_stat->name = c_utf8(dirent->d_name);
#else
file_stat->name = c_strdup(dirent->d_name); file_stat->name = c_strdup(dirent->d_name);
#endif
file_stat->fields = CSYNC_VIO_FILE_STAT_FIELDS_NONE; file_stat->fields = CSYNC_VIO_FILE_STAT_FIELDS_NONE;
#ifndef _WIN32 #ifndef _WIN32
@ -241,19 +264,28 @@ int csync_vio_local_mkdir(const char *uri, mode_t mode) {
} }
int csync_vio_local_rmdir(const char *uri) { int csync_vio_local_rmdir(const char *uri) {
return rmdir(uri); const _TCHAR *dirname = c_multibyte(uri);
int re = -1;
re = _trmdir(dirname);
c_free_multibyte(dirname);
return re;
} }
int csync_vio_local_stat(const char *uri, csync_vio_file_stat_t *buf) { int csync_vio_local_stat(const char *uri, csync_vio_file_stat_t *buf) {
csync_stat_t sb; csync_stat_t sb;
const _TCHAR *wuri = c_multibyte( uri );
if (lstat(uri, &sb) < 0) { if( _tstat(wuri, &sb) < 0) {
c_free_multibyte(wuri);
return -1; return -1;
} }
buf->name = c_basename(uri); buf->name = c_basename(uri);
if (buf->name == NULL) { if (buf->name == NULL) {
csync_vio_file_stat_destroy(buf); csync_vio_file_stat_destroy(buf);
c_free_multibyte(wuri);
return -1; return -1;
} }
buf->fields = CSYNC_VIO_FILE_STAT_FIELDS_NONE; buf->fields = CSYNC_VIO_FILE_STAT_FIELDS_NONE;
@ -336,38 +368,53 @@ int csync_vio_local_stat(const char *uri, csync_vio_file_stat_t *buf) {
buf->ctime = sb.st_ctime; buf->ctime = sb.st_ctime;
buf->fields |= CSYNC_VIO_FILE_STAT_FIELDS_CTIME; buf->fields |= CSYNC_VIO_FILE_STAT_FIELDS_CTIME;
c_free_multibyte(wuri);
return 0; return 0;
} }
int csync_vio_local_rename(const char *olduri, const char *newuri) { int csync_vio_local_rename(const char *olduri, const char *newuri) {
#ifdef _WIN32 #ifdef _WIN32
if(olduri && newuri) { const _TCHAR *nuri = c_multibyte(newuri);
if (MoveFileEx(olduri, newuri, MOVEFILE_COPY_ALLOWED + MOVEFILE_REPLACE_EXISTING + MOVEFILE_WRITE_THROUGH )) { const _TCHAR *ouri = c_multibyte(olduri);
if(ouri && nuri) {
if (MoveFileExW(ouri, nuri, MOVEFILE_COPY_ALLOWED + MOVEFILE_REPLACE_EXISTING + MOVEFILE_WRITE_THROUGH )) {
return 0; return 0;
} }
errno = GetLastError(); errno = GetLastError();
} else { } else {
errno = ENOENT; errno = ENOENT;
} }
c_free_multibyte(nuri);
c_free_multibyte(ouri);
return -1; return -1;
#else
return rename(olduri, newuri);
#endif #endif
return rename(olduri, newuri);
} }
int csync_vio_local_unlink(const char *uri) { int csync_vio_local_unlink(const char *uri) {
return unlink(uri); const _TCHAR *nuri = c_multibyte(uri);
int re = _tunlink( nuri );
c_free_multibyte(nuri);
return re;
} }
int csync_vio_local_chmod(const char *uri, mode_t mode) { int csync_vio_local_chmod(const char *uri, mode_t mode) {
return chmod(uri, mode); const _TCHAR *nuri = c_multibyte(uri);
int re = -1;
re = _tchmod(nuri, mode);
c_free_multibyte(nuri);
return re;
} }
int csync_vio_local_chown(const char *uri, uid_t owner, gid_t group) { int csync_vio_local_chown(const char *uri, uid_t owner, gid_t group) {
#ifndef _WIN32 #ifdef _WIN32
return chown(uri, owner, group); return 0;
#else #else
return 0; return chown(uri, owner, group);
#endif #endif
} }