1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Rework mkdir impl and expose wide char variant

This commit is contained in:
Anatol Belski
2017-12-09 00:11:33 +01:00
parent 6bd18c0f0b
commit 94f16f1961
2 changed files with 51 additions and 44 deletions

View File

@@ -282,33 +282,20 @@ PW32IO int php_win32_ioutil_close(int fd)
return result;
}/*}}}*/
#if 0
PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode)
{/*{{{*/
int ret = 0;
DWORD err = 0;
size_t path_len;
wchar_t *my_path;
if (!path) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(path, -1, 0)
/* TODO extend with mode usage */
if (!CreateDirectoryW(path, NULL)) {
err = GetLastError();
ret = -1;
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
#endif
PW32IO int php_win32_ioutil_mkdir(const char *path, mode_t mode)
{/*{{{*/
size_t pathw_len = 0;
wchar_t *pathw = php_win32_ioutil_conv_any_to_w(path, 0, &pathw_len);
int ret = 0;
DWORD err = 0;
if (pathw_len < _MAX_PATH && pathw_len > _MAX_PATH - 12) {
path_len = wcslen(path);
if (path_len < _MAX_PATH && path_len > _MAX_PATH - 12) {
/* Special case here. From the doc:
"When using an API to create a directory, the specified path cannot be
@@ -318,41 +305,36 @@ PW32IO int php_win32_ioutil_mkdir(const char *path, mode_t mode)
already needs to be a long path. The given path is already normalized
and prepared, need only to prefix it.
*/
wchar_t *tmp = (wchar_t *) malloc((pathw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW + 1) * sizeof(wchar_t));
wchar_t *tmp = (wchar_t *) malloc((path_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW + 1) * sizeof(wchar_t));
if (!tmp) {
free(pathw);
SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
return -1;
}
memmove(tmp, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW, PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW * sizeof(wchar_t));
memmove(tmp+PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, pathw, pathw_len * sizeof(wchar_t));
pathw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
tmp[pathw_len] = L'\0';
memmove(tmp+PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, path, path_len * sizeof(wchar_t));
path_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
tmp[path_len] = L'\0';
free(pathw);
pathw = tmp;
my_path = tmp;
} else {
my_path = path;
}
/* TODO extend with mode usage */
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
if (!CreateDirectoryW(my_path, NULL)) {
DWORD err = GetLastError();
if (my_path != path) {
free((void *)my_path);
}
SET_ERRNO_FROM_WIN32_CODE(err);
return -1;
}
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, -1, 1)
if (!CreateDirectoryW(pathw, NULL)) {
err = GetLastError();
ret = -1;
}
free(pathw);
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
if (my_path != path) {
free((void *)my_path);
}
return ret;
return 0;
}/*}}}*/
PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path)

View File

@@ -248,7 +248,6 @@ __forceinline static wchar_t *php_win32_ioutil_conv_any_to_w(const char* in, siz
PW32IO int php_win32_ioutil_close(int fd);
PW32IO BOOL php_win32_ioutil_posix_to_open_opts(int flags, mode_t mode, php_ioutil_open_opts *opts);
PW32IO int php_win32_ioutil_mkdir(const char *path, mode_t mode);
PW32IO size_t php_win32_ioutil_dirname(char *buf, size_t len);
PW32IO int php_win32_ioutil_open_w(const wchar_t *path, int flags, ...);
@@ -257,6 +256,7 @@ PW32IO int php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newn
PW32IO wchar_t *php_win32_ioutil_getcwd_w(wchar_t *buf, size_t len);
PW32IO int php_win32_ioutil_unlink_w(const wchar_t *path);
PW32IO int php_win32_ioutil_access_w(const wchar_t *path, mode_t mode);
PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode);
#if 0
PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode);
@@ -554,6 +554,31 @@ __forceinline static int php_win32_ioutil_chmod(const char *patha, int mode)
return ret;
}/*}}}*/
__forceinline static int php_win32_ioutil_mkdir(const char *path, mode_t mode)
{/*{{{*/
int ret;
wchar_t *pathw = php_win32_ioutil_any_to_w(path);
DWORD err = 0;
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return -1;
}
ret = php_win32_ioutil_mkdir_w(pathw, mode);
if (0 > ret) {
err = GetLastError();
}
free(pathw);
if (0 > ret) {
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;
}/*}}}*/
#ifdef __cplusplus
}
#endif