Improve ioutil access impl and refactor tsrm_win32_access

This commit is contained in:
Anatol Belski 2017-12-08 16:35:45 +01:00
parent 2fbdaec03c
commit a9a49b8250
3 changed files with 167 additions and 161 deletions

View file

@ -209,41 +209,27 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
realpath_cache_bucket * bucket = NULL;
char * real_path = NULL;
PHP_WIN32_IOUTIL_INIT_W(pathname)
if (!pathw) {
return -1;
}
if (mode == 1 /*X_OK*/) {
DWORD type;
int ret;
ret = GetBinaryTypeW(pathw, &type) ? 0 : -1;
PHP_WIN32_IOUTIL_CLEANUP_W()
return ret;
} else {
if(!IS_ABSOLUTE_PATH(pathname, strlen(pathname)+1)) {
real_path = (char *)malloc(MAXPATHLEN);
if(tsrm_realpath(pathname, real_path) == NULL) {
goto Finished;
SET_ERRNO_FROM_WIN32_CODE(ERROR_FILE_NOT_FOUND);
return -1;
}
pathname = real_path;
PHP_WIN32_IOUTIL_REINIT_W(pathname);
}
if(php_win32_ioutil_access(pathname, mode)) {
PHP_WIN32_IOUTIL_CLEANUP_W()
PHP_WIN32_IOUTIL_INIT_W(pathname)
if (!pathw) {
free(real_path);
return errno;
return -1;
}
/* If only existence check is made, return now */
if (mode == 0) {
/* Either access call failed, or the mode was asking for a specific check.*/
int ret = php_win32_ioutil_access_w(pathw, mode);
if (0 > ret || X_OK == mode || F_OK == mode) {
PHP_WIN32_IOUTIL_CLEANUP_W()
free(real_path);
return 0;
return ret;
}
/* Only in NTS when impersonate==1 (aka FastCGI) */
@ -393,7 +379,6 @@ Finished:
} else {
return 0;
}
}
}/*}}}*/

View file

@ -655,13 +655,33 @@ BOOL php_win32_ioutil_init(void)
return TRUE;
}/*}}}*/
/* an extended version could be implemented, for now direct functions can be used. */
#if 0
PW32IO int php_win32_ioutil_access_w(const wchar_t *path, mode_t mode)
{
return _waccess(path, mode);
}
#endif
{/*{{{*/
DWORD attr, err;
if ((mode & X_OK) == X_OK) {
DWORD type;
return GetBinaryTypeW(path, &type) ? 0 : -1;
}
attr = GetFileAttributesW(path);
if (attr == INVALID_FILE_ATTRIBUTES) {
err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return -1;
}
if (F_OK == mode) {
return 0;
}
if ((mode &W_OK) == W_OK && (attr & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_ACCESS_DENIED);
return -1;
}
return 0;
}/*}}}*/
#if 0
PW32IO HANDLE php_win32_ioutil_findfirstfile_w(char *path, WIN32_FIND_DATA *data)

View file

@ -242,6 +242,7 @@ PW32IO int php_win32_ioutil_chdir_w(const wchar_t *path);
PW32IO int php_win32_ioutil_rename_w(const wchar_t *oldname, const wchar_t *newname);
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);
#if 0
PW32IO int php_win32_ioutil_mkdir_w(const wchar_t *path, mode_t mode);
@ -264,14 +265,14 @@ __forceinline static int php_win32_ioutil_access(const char *path, mode_t mode)
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, -1, 1)
ret = _waccess(pathw, mode);
ret = php_win32_ioutil_access_w(pathw, mode);
if (0 > ret) {
_get_errno(&err);
err = GetLastError();
}
PHP_WIN32_IOUTIL_CLEANUP_W()
if (0 > ret) {
_set_errno(err);
SET_ERRNO_FROM_WIN32_CODE(err);
}
return ret;