Remove strange _tcslen define and fixed some potential leaks.

This commit is contained in:
Klaas Freitag 2013-02-07 14:45:12 +02:00
parent b7a740d0b3
commit a79c380707
3 changed files with 24 additions and 13 deletions

View file

@ -26,6 +26,7 @@ int c_mkdirs(const char *path, mode_t mode) {
if (_tstat(wpath, &sb) == 0) {
if (! S_ISDIR(sb.st_mode)) {
errno = ENOTDIR;
c_free_multibyte(wpath);
return -1;
}
}
@ -43,13 +44,17 @@ int c_mkdirs(const char *path, mode_t mode) {
if (_tstat(swpath, &sb) == 0) {
if (! S_ISDIR(sb.st_mode)) {
errno = ENOTDIR;
c_free_multibyte(swpath);
c_free_multibyte(wpath);
return -1;
}
} else if (errno != ENOENT) {
c_free_multibyte(swpath);
c_free_multibyte(wpath);
return -1;
} else if (c_mkdirs(subpath, mode) < 0) {
c_free_multibyte(swpath);
c_free_multibyte(wpath);
return -1;
}
}
@ -74,7 +79,8 @@ int c_rmdirs(const char *path) {
char *fname = NULL;
_TCHAR *wfname = NULL;
_TCHAR *wpath = c_multibyte(path);
char *rd_name = NULL;
if ((d = _topendir(wpath)) != NULL) {
while( _tstat(wpath, &sb) == 0) {
/* if we can remove the directory we're done */
@ -87,27 +93,30 @@ int c_rmdirs(const char *path) {
case EBADF:
break; /* continue */
default:
c_free_multibyte(wpath);
_tclosedir(d);
return 0;
}
while ((dp = _treaddir(d)) != NULL) {
size_t len;
rd_name = c_utf8(dp->d_name);
/* skip '.' and '..' */
if (dp->d_name[0] == '.' &&
(dp->d_name[1] == '\0' ||
(dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) {
continue;
if( c_streq( rd_name, "." ) || c_streq( rd_name, ".." ) ) {
c_free_utf8(rd_name);
continue;
}
len = strlen(path) + _tcslen(dp->d_name) + 2;
len = strlen(path) + strlen(rd_name) + 2;
fname = c_malloc(len);
if (fname == NULL) {
c_free_multibyte(wpath);
c_free_utf8(rd_name);
_tclosedir(d);
return -1;
}
snprintf(fname, len, "%s/%s", path, dp->d_name);
wfname = c_multibyte(fname);
snprintf(fname, len, "%s/%s", path, rd_name);
wfname = c_multibyte(fname);
/* stat the file */
if (_tstat(wfname, &sb) != -1) {
@ -120,7 +129,9 @@ int c_rmdirs(const char *path) {
if (errno == EACCES) {
_tclosedir(d);
SAFE_FREE(fname);
c_free_multibyte(wfname);
c_free_multibyte(wpath);
c_free_multibyte(wfname);
c_free_utf8(rd_name);
return -1;
}
c_rmdirs(fname);
@ -130,7 +141,8 @@ int c_rmdirs(const char *path) {
}
} /* lstat */
SAFE_FREE(fname);
c_free_multibyte(wfname);
c_free_multibyte(wfname);
c_free_utf8(rd_name);
} /* readdir */
_trewinddir(d);
@ -138,6 +150,7 @@ int c_rmdirs(const char *path) {
} else {
return -1;
}
c_free_multibyte(wpath);
_tclosedir(d);
return 0;

View file

@ -77,7 +77,6 @@ typedef struct stat csync_stat_t;
#if defined _WIN32 && defined _UNICODE
typedef wchar_t _TCHAR;
#define _tcslen wcslen
#define _topen _wopen
#define _tdirent _wdirent
#define _TDIR _WDIR
@ -97,7 +96,6 @@ typedef wchar_t _TCHAR;
#else
typedef char _TCHAR;
#define _tdirent dirent
#define _tcslen strlen
#define _topen open
#define _TDIR DIR
#define _topendir opendir

View file

@ -281,7 +281,7 @@ char* c_utf8(const _TCHAR *wstr)
char *dst = NULL;
#ifdef _WIN32
if(!wstr) return NULL;
size_t len = _tcslen( wstr );
size_t len = wcslen( wstr );
/* Call once to get the required size. */
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL);
if( size_needed > 0 ) {