Skip to content

Commit

Permalink
Modernize realpath and integrate quick variant into virtual_file_ex
Browse files Browse the repository at this point in the history
The slower I/O as a traditional bottleneck on Windows which is
the target of this patch. The recursive path resolution, while being
an allround solution, is expensive when it comes to the common case.
Files with proper ACLs set can be resolved in one go by usage of specific
API. Those are available since Vista, so actually can be called old. Those
simpler api is used for the cases where no CWD_EXPAND is requested. For
the cases where ACLs are improper, the existing solution based on
FindFirstFile still does good job also partially providing quirks. Cases
involing reparse tags and other non local filesystems are also partially
server by new APIs.

The approach uses both APIs - the quick one for the common case still
integrating realpath cache, and the existing one as a fallback. The tests
show the I/O load drop on the realpath resolution part due to less
system calls for the sub part resolution of paths. In most case it is
justified, as the sub parts were otherwise cached or unused as well. The
realpath() implementation in ioutil is also closer to the POSIX.
  • Loading branch information
weltling committed Dec 15, 2017
1 parent 66a6041 commit f3f6cd2
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 16 deletions.
1 change: 0 additions & 1 deletion TSRM/tsrm_config.w32.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#define HAVE_UTIME 1
#define HAVE_ALLOCA 1
#define HAVE_REALPATH 1

#include <malloc.h>
#include <stdlib.h>
Expand Down
9 changes: 0 additions & 9 deletions TSRM/tsrm_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,15 +766,6 @@ TSRM_API int shmctl(int key, int cmd, struct shmid_ds *buf)
}
}/*}}}*/

TSRM_API char *realpath(char *orig_path, char *buffer)
{/*{{{*/
int ret = GetFullPathName(orig_path, _MAX_PATH, buffer, NULL);
if(!ret || ret > _MAX_PATH) {
return NULL;
}
return buffer;
}/*}}}*/

#if HAVE_UTIME
static zend_always_inline void UnixTimeToFileTime(time_t t, LPFILETIME pft) /* {{{ */
{
Expand Down
2 changes: 0 additions & 2 deletions TSRM/tsrm_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ TSRM_API int shmget(key_t key, size_t size, int flags);
TSRM_API void *shmat(int key, const void *shmaddr, int flags);
TSRM_API int shmdt(const void *shmaddr);
TSRM_API int shmctl(int key, int cmd, struct shmid_ds *buf);

TSRM_API char *realpath(char *orig_path, char *buffer);
#endif

/*
Expand Down
70 changes: 69 additions & 1 deletion Zend/zend_virtual_cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,12 +1209,59 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
}
/* }}} */

#ifdef ZEND_WIN32
static size_t tsrm_win32_realpath_quick(char *path, size_t len, time_t *t) /* {{{ */
{
char tmp_resolved_path[MAXPATHLEN];
int tmp_resolved_path_len;
BY_HANDLE_FILE_INFORMATION info;
realpath_cache_bucket *bucket;

if (!*t) {
*t = time(0);
}

if (CWDG(realpath_cache_size_limit) && (bucket = realpath_cache_find(path, len, *t)) != NULL) {
memcpy(path, bucket->realpath, bucket->realpath_len + 1);
return bucket->realpath_len;
}

#if 0
if (!php_win32_ioutil_realpath_ex0(resolved_path, tmp_resolved_path, &info)) {
if (CWD_REALPATH == use_realpath) {
DWORD err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return 1;
} else {
/* Fallback to expand only to retain BC. */
path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
}
} else {
#endif

if (!php_win32_ioutil_realpath_ex0(path, tmp_resolved_path, &info)) {
DWORD err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return (size_t)-1;
}

tmp_resolved_path_len = strlen(tmp_resolved_path);
if (CWDG(realpath_cache_size_limit)) {
realpath_cache_add(path, len, tmp_resolved_path, tmp_resolved_path_len, info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY, *t);
}
memmove(path, tmp_resolved_path, tmp_resolved_path_len + 1);

return tmp_resolved_path_len;
}
/* }}} */
#endif

/* Resolve path relatively to state and put the real path into state */
/* returns 0 for ok, 1 for error */
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath) /* {{{ */
{
size_t path_length = strlen(path);
char resolved_path[MAXPATHLEN];
char resolved_path[MAXPATHLEN] = {0};
size_t start = 1;
int ll = 0;
time_t t;
Expand Down Expand Up @@ -1336,7 +1383,27 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func

add_slash = (use_realpath != CWD_REALPATH) && path_length > 0 && IS_SLASH(resolved_path[path_length-1]);
t = CWDG(realpath_cache_ttl) ? 0 : -1;
#ifdef ZEND_WIN32
if (CWD_EXPAND != use_realpath) {
size_t tmp_len = tsrm_win32_realpath_quick(resolved_path, path_length, &t);
if ((size_t)-1 != tmp_len) {
path_length = tmp_len;
} else {
DWORD err = GetLastError();
/* The access denied error can mean something completely else,
fallback to complicated way. */
if (CWD_REALPATH == use_realpath && ERROR_ACCESS_DENIED != err) {
SET_ERRNO_FROM_WIN32_CODE(err);
return 1;
}
path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
}
} else {
path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
}
#else
path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
#endif

if (path_length == (size_t)-1) {
errno = ENOENT;
Expand All @@ -1346,6 +1413,7 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
if (!start && !path_length) {
resolved_path[path_length++] = '.';
}

if (add_slash && path_length && !IS_SLASH(resolved_path[path_length-1])) {
if (path_length >= MAXPATHLEN-1) {
return -1;
Expand Down
3 changes: 3 additions & 0 deletions ext/opcache/Optimizer/zend_func_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "zend_call_graph.h"
#include "zend_func_info.h"
#include "zend_inference.h"
#ifdef _WIN32
#include "win32/ioutil.h"
#endif

typedef uint32_t (*info_func_t)(const zend_call_info *call_info, const zend_ssa *ssa);

Expand Down
1 change: 1 addition & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#ifdef PHP_WIN32
#include "win32/php_win32_globals.h"
#include "win32/time.h"
#include "win32/ioutil.h"
#endif

typedef struct yy_buffer_state *YY_BUFFER_STATE;
Expand Down
1 change: 1 addition & 0 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
# include "win32/param.h"
# include "win32/winutil.h"
# include "win32/fnmatch.h"
# include "win32/ioutil.h"
#else
# if HAVE_SYS_PARAM_H
# include <sys/param.h>
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/tests/file/realpath_cache_win32.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (substr(PHP_OS, 0, 3) != 'WIN') {

var_dump(realpath_cache_size());
$data = realpath_cache_get();
var_dump($data[__DIR__]);
var_dump($data[__FILE__]);

echo "Done\n";
?>
Expand All @@ -21,9 +21,9 @@ array(8) {
["key"]=>
%s(%d%s)
["is_dir"]=>
bool(true)
bool(false)
["realpath"]=>
string(%d) "%sfile"
string(%d) "%sphp"
["expires"]=>
int(%d)
["is_rvalid"]=>
Expand Down
89 changes: 89 additions & 0 deletions win32/ioutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,95 @@ PW32IO FILE *php_win32_ioutil_fopen_w(const wchar_t *path, const wchar_t *mode)
return ret;
}/*}}}*/

static size_t php_win32_ioutil_realpath_h(HANDLE *h, wchar_t **resolved)
{/*{{{*/
wchar_t ret[PHP_WIN32_IOUTIL_MAXPATHLEN], *ret_real;
DWORD ret_len, ret_real_len;

ret_len = GetFinalPathNameByHandleW(h, ret, PHP_WIN32_IOUTIL_MAXPATHLEN-1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (0 == ret_len) {
DWORD err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return (size_t)-1;
} else if (ret_len > PHP_WIN32_IOUTIL_MAXPATHLEN) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return (size_t)-1;
}

if (NULL == *resolved) {
/* ret is expected to be either NULL or a buffer of capable size. */
*resolved = (wchar_t *) malloc((ret_len + 1)*sizeof(wchar_t));
if (!*resolved) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
return (size_t)-1;
}
}

ret_real = ret;
ret_real_len = ret_len;
if (0 == wcsncmp(ret, PHP_WIN32_IOUTIL_UNC_PATH_PREFIXW, PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW)) {
ret_real += (PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW - 2);
ret_real[0] = L'\\';
ret_real_len -= (PHP_WIN32_IOUTIL_UNC_PATH_PREFIX_LENW - 2);
} else if (PHP_WIN32_IOUTIL_IS_LONG_PATHW(ret, ret_len)) {
ret_real += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
ret_real_len -= PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
}
memmove(*resolved, ret_real, (ret_real_len+1)*sizeof(wchar_t));

return ret_real_len;
}/*}}}*/

PW32IO wchar_t *php_win32_ioutil_realpath_w(const wchar_t *path, wchar_t *resolved)
{/*{{{*/
return php_win32_ioutil_realpath_w_ex0(path, resolved, NULL);
}/*}}}*/

PW32IO wchar_t *php_win32_ioutil_realpath_w_ex0(const wchar_t *path, wchar_t *resolved, PBY_HANDLE_FILE_INFORMATION info)
{/*{{{*/
HANDLE h;
size_t ret_len;

PHP_WIN32_IOUTIL_CHECK_PATH_W(path, NULL, 0)

h = CreateFileW(path,
0,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (INVALID_HANDLE_VALUE == h) {
DWORD err = GetLastError();
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}

ret_len = php_win32_ioutil_realpath_h(h, &resolved);
if ((size_t)-1 == ret_len) {
DWORD err = GetLastError();
CloseHandle(h);
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}

if (NULL != info && !GetFileInformationByHandle(h, info)) {
DWORD err = GetLastError();
CloseHandle(h);
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}

CloseHandle(h);

return resolved;
}/*}}}*/

PW32IO char *realpath(const char *path, char *resolved)
{/*{{{*/
return php_win32_ioutil_realpath(path, resolved);
}/*}}}*/

/*
* Local variables:
* tab-width: 4
Expand Down
55 changes: 55 additions & 0 deletions win32/ioutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ 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);
PW32IO FILE *php_win32_ioutil_fopen_w(const wchar_t *path, const wchar_t *mode);
PW32IO wchar_t *php_win32_ioutil_realpath_w(const wchar_t *path, wchar_t *resolved);
PW32IO wchar_t *php_win32_ioutil_realpath_w_ex0(const wchar_t *path, wchar_t *resolved, PBY_HANDLE_FILE_INFORMATION info);

__forceinline static int php_win32_ioutil_access(const char *path, mode_t mode)
{/*{{{*/
Expand Down Expand Up @@ -582,6 +584,59 @@ __forceinline static int php_win32_ioutil_mkdir(const char *path, mode_t mode)
return ret;
}/*}}}*/

#define HAVE_REALPATH 1
PW32IO char *realpath(const char *path, char *resolved);

__forceinline static char *php_win32_ioutil_realpath_ex0(const char *path, char *resolved, PBY_HANDLE_FILE_INFORMATION info)
{/*{{{*/
wchar_t retw[PHP_WIN32_IOUTIL_MAXPATHLEN];
char *reta;
size_t reta_len;

PHP_WIN32_IOUTIL_INIT_W(path)
if (!pathw) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
return NULL;
}

if (NULL == php_win32_ioutil_realpath_w_ex0(pathw, retw, info)) {
DWORD err = GetLastError();
PHP_WIN32_IOUTIL_CLEANUP_W()
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}

reta = php_win32_cp_conv_w_to_any(retw, PHP_WIN32_CP_IGNORE_LEN, &reta_len);
if (!reta || reta_len > PHP_WIN32_IOUTIL_MAXPATHLEN) {
DWORD err = GetLastError();
PHP_WIN32_IOUTIL_CLEANUP_W()
SET_ERRNO_FROM_WIN32_CODE(err);
return NULL;
}

if (NULL == resolved) {
/* ret is expected to be either NULL or a buffer of capable size. */
resolved = (char *) malloc(reta_len + 1);
if (!resolved) {
free(reta);
PHP_WIN32_IOUTIL_CLEANUP_W()
SET_ERRNO_FROM_WIN32_CODE(ERROR_NOT_ENOUGH_MEMORY);
return NULL;
}
}
memmove(resolved, reta, reta_len+1);

PHP_WIN32_IOUTIL_CLEANUP_W()
free(reta);

return resolved;
}/*}}}*/

__forceinline static char *php_win32_ioutil_realpath(const char *path, char *resolved)
{/*{{{*/
return php_win32_ioutil_realpath_ex0(path, resolved, NULL);
}/*}}}*/

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit f3f6cd2

Please sign in to comment.