PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_virtual_cwd.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 264 556 47.5 %
Date: 2016-07-26 Functions: 18 42 42.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Sascha Schumann <sascha@schumann.cx>                        |
      17             :    |          Pierre Joye <pierre@php.net>                                |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include <sys/types.h>
      24             : #include <sys/stat.h>
      25             : #include <string.h>
      26             : #include <stdio.h>
      27             : #include <limits.h>
      28             : #include <errno.h>
      29             : #include <stdlib.h>
      30             : #include <fcntl.h>
      31             : #include <time.h>
      32             : 
      33             : #include "zend.h"
      34             : #include "zend_virtual_cwd.h"
      35             : #include "tsrm_strtok_r.h"
      36             : 
      37             : #ifdef ZEND_WIN32
      38             : #include <io.h>
      39             : #include "tsrm_win32.h"
      40             : # ifndef IO_REPARSE_TAG_SYMLINK
      41             : #  define IO_REPARSE_TAG_SYMLINK 0xA000000C
      42             : # endif
      43             : 
      44             : # ifndef IO_REPARSE_TAG_DEDUP
      45             : #  define IO_REPARSE_TAG_DEDUP   0x80000013
      46             : # endif
      47             : 
      48             : # ifndef VOLUME_NAME_NT
      49             : #  define VOLUME_NAME_NT 0x2
      50             : # endif
      51             : 
      52             : # ifndef VOLUME_NAME_DOS
      53             : #  define VOLUME_NAME_DOS 0x0
      54             : # endif
      55             : #endif
      56             : 
      57             : #ifdef NETWARE
      58             : #include <fsio.h>
      59             : #endif
      60             : 
      61             : #ifndef HAVE_REALPATH
      62             : #define realpath(x,y) strcpy(y,x)
      63             : #endif
      64             : 
      65             : #define VIRTUAL_CWD_DEBUG 0
      66             : 
      67             : #include "TSRM.h"
      68             : 
      69             : /* Only need mutex for popen() in Windows and NetWare because it doesn't chdir() on UNIX */
      70             : #if (defined(ZEND_WIN32) || defined(NETWARE)) && defined(ZTS)
      71             : MUTEX_T cwd_mutex;
      72             : #endif
      73             : 
      74             : #ifdef ZTS
      75             : ts_rsrc_id cwd_globals_id;
      76             : #else
      77             : virtual_cwd_globals cwd_globals;
      78             : #endif
      79             : 
      80             : cwd_state main_cwd_state; /* True global */
      81             : 
      82             : #ifndef ZEND_WIN32
      83             : #include <unistd.h>
      84             : #else
      85             : #include <direct.h>
      86             : #endif
      87             : 
      88             : #ifdef ZEND_WIN32
      89             : #include <tchar.h>
      90             : #define tsrm_strtok_r(a,b,c) _tcstok((a),(b))
      91             : #define TOKENIZER_STRING "/\\"
      92             : 
      93             : static int php_check_dots(const char *element, int n)
      94             : {
      95             :         while (n-- > 0) if (element[n] != '.') break;
      96             : 
      97             :         return (n != -1);
      98             : }
      99             : 
     100             : #define IS_DIRECTORY_UP(element, len) \
     101             :         (len >= 2 && !php_check_dots(element, len))
     102             : 
     103             : #define IS_DIRECTORY_CURRENT(element, len) \
     104             :         (len == 1 && element[0] == '.')
     105             : 
     106             : #elif defined(NETWARE)
     107             : /* NetWare has strtok() (in LibC) and allows both slashes in paths, like Windows --
     108             :    but rest of the stuff is like Unix */
     109             : /* strtok() call in LibC is abending when used in a different address space -- hence using
     110             :    PHP's version itself for now */
     111             : /*#define tsrm_strtok_r(a,b,c) strtok((a),(b))*/
     112             : #define TOKENIZER_STRING "/\\"
     113             : 
     114             : #else
     115             : #define TOKENIZER_STRING "/"
     116             : #endif
     117             : 
     118             : /* default macros */
     119             : 
     120             : #ifndef IS_DIRECTORY_UP
     121             : #define IS_DIRECTORY_UP(element, len) \
     122             :         (len == 2 && element[0] == '.' && element[1] == '.')
     123             : #endif
     124             : 
     125             : #ifndef IS_DIRECTORY_CURRENT
     126             : #define IS_DIRECTORY_CURRENT(element, len) \
     127             :         (len == 1 && element[0] == '.')
     128             : #endif
     129             : 
     130             : /* define this to check semantics */
     131             : #define IS_DIR_OK(s) (1)
     132             : 
     133             : #ifndef IS_DIR_OK
     134             : #define IS_DIR_OK(state) (php_is_dir_ok(state) == 0)
     135             : #endif
     136             : 
     137             : 
     138             : #define CWD_STATE_COPY(d, s)                            \
     139             :         (d)->cwd_length = (s)->cwd_length;                \
     140             :         (d)->cwd = (char *) emalloc((s)->cwd_length+1);   \
     141             :         memcpy((d)->cwd, (s)->cwd, (s)->cwd_length+1);
     142             : 
     143             : #define CWD_STATE_FREE(s)                       \
     144             :         efree((s)->cwd);
     145             : 
     146             : #ifdef ZEND_WIN32
     147             : # define CWD_STATE_FREE_ERR(state) do { \
     148             :                 DWORD last_error = GetLastError(); \
     149             :                 CWD_STATE_FREE(state); \
     150             :                 SetLastError(last_error); \
     151             :         } while (0)
     152             : #else
     153             : # define CWD_STATE_FREE_ERR(state) CWD_STATE_FREE(state)
     154             : #endif
     155             : 
     156             : #ifdef ZEND_WIN32
     157             : 
     158             : #ifdef CTL_CODE
     159             : #undef CTL_CODE
     160             : #endif
     161             : #define CTL_CODE(DeviceType,Function,Method,Access) (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
     162             : #define FILE_DEVICE_FILE_SYSTEM 0x00000009
     163             : #define METHOD_BUFFERED         0
     164             : #define FILE_ANY_ACCESS         0
     165             : #define FSCTL_GET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS)
     166             : #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE  ( 16 * 1024 )
     167             : 
     168             : typedef struct {
     169             :         unsigned long  ReparseTag;
     170             :         unsigned short ReparseDataLength;
     171             :         unsigned short Reserved;
     172             :         union {
     173             :                 struct {
     174             :                         unsigned short SubstituteNameOffset;
     175             :                         unsigned short SubstituteNameLength;
     176             :                         unsigned short PrintNameOffset;
     177             :                         unsigned short PrintNameLength;
     178             :                         unsigned long  Flags;
     179             :                         wchar_t        ReparseTarget[1];
     180             :                 } SymbolicLinkReparseBuffer;
     181             :                 struct {
     182             :                         unsigned short SubstituteNameOffset;
     183             :                         unsigned short SubstituteNameLength;
     184             :                         unsigned short PrintNameOffset;
     185             :                         unsigned short PrintNameLength;
     186             :                         wchar_t        ReparseTarget[1];
     187             :                 } MountPointReparseBuffer;
     188             :                 struct {
     189             :                         unsigned char  ReparseTarget[1];
     190             :                 } GenericReparseBuffer;
     191             :         };
     192             : } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
     193             : 
     194             : #define SECS_BETWEEN_EPOCHS (__int64)11644473600
     195             : #define SECS_TO_100NS (__int64)10000000
     196             : static inline time_t FileTimeToUnixTime(const FILETIME *FileTime)
     197             : {
     198             :         __int64 UnixTime;
     199             :         long *nsec = NULL;
     200             :         SYSTEMTIME SystemTime;
     201             :         FileTimeToSystemTime(FileTime, &SystemTime);
     202             : 
     203             :         UnixTime = ((__int64)FileTime->dwHighDateTime << 32) +
     204             :         FileTime->dwLowDateTime;
     205             : 
     206             :         UnixTime -= (SECS_BETWEEN_EPOCHS * SECS_TO_100NS);
     207             : 
     208             :         if (nsec) {
     209             :                 *nsec = (UnixTime % SECS_TO_100NS) * (__int64)100;
     210             :         }
     211             : 
     212             :         UnixTime /= SECS_TO_100NS; /* now convert to seconds */
     213             : 
     214             :         if ((time_t)UnixTime != UnixTime) {
     215             :                 UnixTime = 0;
     216             :         }
     217             :         return (time_t)UnixTime;
     218             : }
     219             : 
     220             : CWD_API int php_sys_readlink(const char *link, char *target, size_t target_len){ /* {{{ */
     221             :         HANDLE hFile;
     222             :         DWORD dwRet;
     223             :         wchar_t *linkw = php_win32_ioutil_any_to_w(link), targetw[MAXPATHLEN];
     224             :         size_t _tmp_len;
     225             :         char *_tmp;
     226             : 
     227             :         if (!linkw) {
     228             :                 return -1;
     229             :         }
     230             : 
     231             :         if (!target_len) {
     232             :                 free(linkw);
     233             :                 return -1;
     234             :         }
     235             : 
     236             :         hFile = CreateFileW(linkw,            // file to open
     237             :                                  GENERIC_READ,          // open for reading
     238             :                                  FILE_SHARE_READ,       // share for reading
     239             :                                  NULL,                  // default security
     240             :                                  OPEN_EXISTING,         // existing file only
     241             :                                  FILE_FLAG_BACKUP_SEMANTICS, // normal file
     242             :                                  NULL);                 // no attr. template
     243             :         if( hFile == INVALID_HANDLE_VALUE) {
     244             :                 free(linkw);
     245             :                 return -1;
     246             :         }
     247             : 
     248             :         /* Despite MSDN has documented it won't to, the length returned by
     249             :                 GetFinalPathNameByHandleA includes the length of the
     250             :                 null terminator. This behavior is at least reproducible
     251             :                 with VS2012 and earlier, and seems not to be fixed till
     252             :                 now. Thus, correcting target_len so it's suddenly don't
     253             :                 overflown. */
     254             :         dwRet = GetFinalPathNameByHandleW(hFile, targetw, MAXPATHLEN, VOLUME_NAME_DOS);
     255             :         if(dwRet >= target_len || dwRet >= MAXPATHLEN || dwRet == 0) {
     256             :                 free(linkw);
     257             :                 CloseHandle(hFile);
     258             :                 return -1;
     259             :         }
     260             : 
     261             :         _tmp = php_win32_ioutil_conv_w_to_any(targetw, dwRet, &_tmp_len);
     262             :         if (!_tmp || _tmp_len >= MAXPATHLEN) {
     263             :                 CloseHandle(hFile);
     264             :                 free(linkw);
     265             :                 return -1;
     266             :         }
     267             :         memcpy(target, _tmp, _tmp_len);
     268             :         free(_tmp);
     269             : 
     270             :         CloseHandle(hFile);
     271             :         free(linkw);
     272             : 
     273             :         if(dwRet > 4) {
     274             :                 /* Skip first 4 characters if they are "\??\" */
     275             :                 if(target[0] == '\\' && target[1] == '\\' && target[2] == '?' && target[3] ==  '\\') {
     276             :                         char tmp[MAXPATHLEN];
     277             :                         unsigned int offset = 4;
     278             :                         dwRet -= 4;
     279             : 
     280             :                         /* \??\UNC\ */
     281             :                         if (dwRet > 7 && target[4] == 'U' && target[5] == 'N' && target[6] == 'C') {
     282             :                                 offset += 2;
     283             :                                 dwRet -= 2;
     284             :                                 target[offset] = '\\';
     285             :                         }
     286             : 
     287             :                         memcpy(tmp, target + offset, dwRet);
     288             :                         memcpy(target, tmp, dwRet);
     289             :                 }
     290             :         }
     291             : 
     292             :         target[dwRet] = '\0';
     293             :         return dwRet;
     294             : }
     295             : /* }}} */
     296             : 
     297             : CWD_API int php_sys_stat_ex(const char *path, zend_stat_t *buf, int lstat) /* {{{ */
     298             : {
     299             :         WIN32_FILE_ATTRIBUTE_DATA data;
     300             :         LARGE_INTEGER t;
     301             :         const size_t path_len = strlen(path);
     302             :         ALLOCA_FLAG(use_heap_large);
     303             :         wchar_t *pathw = php_win32_ioutil_any_to_w(path);
     304             : 
     305             :         if (!pathw) {
     306             :                 return -1;
     307             :         }
     308             : 
     309             :         if (!GetFileAttributesExW(pathw, GetFileExInfoStandard, &data)) {
     310             :                 int ret;
     311             : #if ZEND_ENABLE_ZVAL_LONG64
     312             :                 ret = _wstat64(pathw, buf);
     313             : #else
     314             :                 ret = _wstat(pathw, (struct _stat32 *)buf);
     315             : #endif
     316             :                 free(pathw);
     317             : 
     318             :                 return ret;
     319             :         }
     320             : 
     321             :         if (path_len >= 1 && path[1] == ':') {
     322             :                 if (path[0] >= 'A' && path[0] <= 'Z') {
     323             :                         buf->st_dev = buf->st_rdev = path[0] - 'A';
     324             :                 } else {
     325             :                         buf->st_dev = buf->st_rdev = path[0] - 'a';
     326             :                 }
     327             :         } else if (IS_UNC_PATH(path, path_len)) {
     328             :                 buf->st_dev = buf->st_rdev = 0;
     329             :         } else {
     330             :                 wchar_t cur_path[MAXPATHLEN+1];
     331             :                 DWORD len = sizeof(cur_path);
     332             :                 wchar_t *tmp = cur_path;
     333             : 
     334             :                 while(1) {
     335             :                         DWORD r = GetCurrentDirectoryW(len, tmp);
     336             :                         if (r < len) {
     337             :                                 if (tmp[1] == L':') {
     338             :                                         if (pathw[0] >= L'A' && pathw[0] <= L'Z') {
     339             :                                                 buf->st_dev = buf->st_rdev = pathw[0] - L'A';
     340             :                                         } else {
     341             :                                                 buf->st_dev = buf->st_rdev = pathw[0] - L'a';
     342             :                                         }
     343             :                                 } else {
     344             :                                         buf->st_dev = buf->st_rdev = -1;
     345             :                                 }
     346             :                                 break;
     347             :                         } else if (!r) {
     348             :                                 buf->st_dev = buf->st_rdev = -1;
     349             :                                 break;
     350             :                         } else {
     351             :                                 len = r+1;
     352             :                                 tmp = (wchar_t*)malloc(len*sizeof(wchar_t));
     353             :                         }
     354             :                 }
     355             :                 if (tmp != cur_path) {
     356             :                         free(tmp);
     357             :                 }
     358             :         }
     359             : 
     360             :         buf->st_uid = buf->st_gid = buf->st_ino = 0;
     361             : 
     362             :         if (lstat && data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
     363             :                 /* File is a reparse point. Get the target */
     364             :                 HANDLE hLink = NULL;
     365             :                 REPARSE_DATA_BUFFER * pbuffer;
     366             :                 DWORD retlength = 0;
     367             : 
     368             :                 hLink = CreateFileW(pathw, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL);
     369             :                 if(hLink == INVALID_HANDLE_VALUE) {
     370             :                         free(pathw);
     371             :                         return -1;
     372             :                 }
     373             : 
     374             :                 pbuffer = (REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
     375             :                 if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer,  MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
     376             :                         free_alloca(pbuffer, use_heap_large);
     377             :                         CloseHandle(hLink);
     378             :                         free(pathw);
     379             :                         return -1;
     380             :                 }
     381             : 
     382             :                 CloseHandle(hLink);
     383             : 
     384             :                 if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
     385             :                         buf->st_mode = S_IFLNK;
     386             :                         buf->st_mode |= (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)) : (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)|S_IWRITE|(S_IWRITE>>3)|(S_IWRITE>>6));
     387             :                 }
     388             : 
     389             : #if 0 /* Not used yet */
     390             :                 else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
     391             :                         buf->st_mode |=;
     392             :                 }
     393             : #endif
     394             :                 free_alloca(pbuffer, use_heap_large);
     395             :         } else {
     396             :                 buf->st_mode = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? (S_IFDIR|S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6)) : S_IFREG;
     397             :                 buf->st_mode |= (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)) : (S_IREAD|(S_IREAD>>3)|(S_IREAD>>6)|S_IWRITE|(S_IWRITE>>3)|(S_IWRITE>>6));
     398             :         }
     399             : 
     400             :         if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
     401             :                 size_t len = strlen(path);
     402             : 
     403             :                 if (path[len-4] == '.') {
     404             :                         if (_memicmp(path+len-3, "exe", 3) == 0 ||
     405             :                                 _memicmp(path+len-3, "com", 3) == 0 ||
     406             :                                 _memicmp(path+len-3, "bat", 3) == 0 ||
     407             :                                 _memicmp(path+len-3, "cmd", 3) == 0) {
     408             :                                 buf->st_mode  |= (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
     409             :                         }
     410             :                 }
     411             :         }
     412             : 
     413             :         buf->st_nlink = 1;
     414             :         t.HighPart = data.nFileSizeHigh;
     415             :         t.LowPart = data.nFileSizeLow;
     416             :         /* It's an overflow on 32 bit, however it won't fix as long
     417             :         as zend_long is 32 bit. */
     418             :         buf->st_size = (zend_long)t.QuadPart;
     419             :         buf->st_atime = FileTimeToUnixTime(&data.ftLastAccessTime);
     420             :         buf->st_ctime = FileTimeToUnixTime(&data.ftCreationTime);
     421             :         buf->st_mtime = FileTimeToUnixTime(&data.ftLastWriteTime);
     422             : 
     423             :         free(pathw);
     424             : 
     425             :         return 0;
     426             : }
     427             : /* }}} */
     428             : #endif
     429             : 
     430           0 : static int php_is_dir_ok(const cwd_state *state)  /* {{{ */
     431             : {
     432             :         zend_stat_t buf;
     433             : 
     434           0 :         if (php_sys_stat(state->cwd, &buf) == 0 && S_ISDIR(buf.st_mode))
     435           0 :                 return (0);
     436             : 
     437           0 :         return (1);
     438             : }
     439             : /* }}} */
     440             : 
     441           0 : static int php_is_file_ok(const cwd_state *state)  /* {{{ */
     442             : {
     443             :         zend_stat_t buf;
     444             : 
     445           0 :         if (php_sys_stat(state->cwd, &buf) == 0 && S_ISREG(buf.st_mode))
     446           0 :                 return (0);
     447             : 
     448           0 :         return (1);
     449             : }
     450             : /* }}} */
     451             : 
     452       23453 : static void cwd_globals_ctor(virtual_cwd_globals *cwd_g) /* {{{ */
     453             : {
     454       23453 :         CWD_STATE_COPY(&cwd_g->cwd, &main_cwd_state);
     455       23453 :         cwd_g->realpath_cache_size = 0;
     456       23453 :         cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
     457       23453 :         cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL;
     458       23453 :         memset(cwd_g->realpath_cache, 0, sizeof(cwd_g->realpath_cache));
     459       23453 : }
     460             : /* }}} */
     461             : 
     462       23489 : static void cwd_globals_dtor(virtual_cwd_globals *cwd_g) /* {{{ */
     463             : {
     464       23489 :         realpath_cache_clean();
     465       23489 : }
     466             : /* }}} */
     467             : 
     468       23453 : CWD_API void virtual_cwd_startup(void) /* {{{ */
     469             : {
     470             :         char cwd[MAXPATHLEN];
     471             :         char *result;
     472             : 
     473             : #ifdef NETWARE
     474             :         result = getcwdpath(cwd, NULL, 1);
     475             :         if(result)
     476             :         {
     477             :                 char *c=cwd;
     478             :                 while(c = strchr(c, '\\'))
     479             :                 {
     480             :                         *c='/';
     481             :                         ++c;
     482             :                 }
     483             :         }
     484             : #else
     485             : #ifdef ZEND_WIN32
     486             :         ZeroMemory(&cwd, sizeof(cwd));
     487             : #endif
     488       23453 :         result = getcwd(cwd, sizeof(cwd));
     489             : #endif
     490       23453 :         if (!result) {
     491           0 :                 cwd[0] = '\0';
     492             :         }
     493             : 
     494       23453 :         main_cwd_state.cwd_length = (int)strlen(cwd);
     495             : #ifdef ZEND_WIN32
     496             :         if (main_cwd_state.cwd_length >= 2 && cwd[1] == ':') {
     497             :                 cwd[0] = toupper(cwd[0]);
     498             :         }
     499             : #endif
     500       23453 :         main_cwd_state.cwd = strdup(cwd);
     501             : 
     502             : #ifdef ZTS
     503             :         ts_allocate_id(&cwd_globals_id, sizeof(virtual_cwd_globals), (ts_allocate_ctor) cwd_globals_ctor, (ts_allocate_dtor) cwd_globals_dtor);
     504             : #else
     505       23453 :         cwd_globals_ctor(&cwd_globals);
     506             : #endif
     507             : 
     508             : #if (defined(ZEND_WIN32) || defined(NETWARE)) && defined(ZTS)
     509             :         cwd_mutex = tsrm_mutex_alloc();
     510             : #endif
     511       23453 : }
     512             : /* }}} */
     513             : 
     514       23489 : CWD_API void virtual_cwd_shutdown(void) /* {{{ */
     515             : {
     516             : #ifndef ZTS
     517       23489 :         cwd_globals_dtor(&cwd_globals);
     518             : #endif
     519             : #if (defined(ZEND_WIN32) || defined(NETWARE)) && defined(ZTS)
     520             :         tsrm_mutex_free(cwd_mutex);
     521             : #endif
     522             : 
     523       23489 :         free(main_cwd_state.cwd); /* Don't use CWD_STATE_FREE because the non global states will probably use emalloc()/efree() */
     524       23489 : }
     525             : /* }}} */
     526             : 
     527       23453 : CWD_API int virtual_cwd_activate(void) /* {{{ */
     528             : {
     529       23453 :         if (CWDG(cwd).cwd == NULL) {
     530       23453 :                 CWD_STATE_COPY(&CWDG(cwd), &main_cwd_state);
     531             :         }
     532       23453 :         return 0;
     533             : }
     534             : /* }}} */
     535             : 
     536       70387 : CWD_API int virtual_cwd_deactivate(void) /* {{{ */
     537             : {
     538       70387 :         if (CWDG(cwd).cwd != NULL) {
     539       46942 :                 CWD_STATE_FREE(&CWDG(cwd));
     540       46942 :                 CWDG(cwd).cwd = NULL;
     541             :         }
     542       70387 :         return 0;
     543             : }
     544             : /* }}} */
     545             : 
     546           0 : CWD_API char *virtual_getcwd_ex(size_t *length) /* {{{ */
     547             : {
     548             :         cwd_state *state;
     549             : 
     550           0 :         state = &CWDG(cwd);
     551             : 
     552           0 :         if (state->cwd_length == 0) {
     553             :                 char *retval;
     554             : 
     555           0 :                 *length = 1;
     556           0 :                 retval = (char *) emalloc(2);
     557           0 :                 if (retval == NULL) {
     558           0 :                         return NULL;
     559             :                 }
     560           0 :                 retval[0] = DEFAULT_SLASH;
     561           0 :                 retval[1] = '\0';
     562           0 :                 return retval;
     563             :         }
     564             : 
     565             : #ifdef ZEND_WIN32
     566             :         /* If we have something like C: */
     567             :         if (state->cwd_length == 2 && state->cwd[state->cwd_length-1] == ':') {
     568             :                 char *retval;
     569             : 
     570             :                 *length = state->cwd_length+1;
     571             :                 retval = (char *) emalloc(*length+1);
     572             :                 if (retval == NULL) {
     573             :                         return NULL;
     574             :                 }
     575             :                 memcpy(retval, state->cwd, *length);
     576             :                 retval[0] = toupper(retval[0]);
     577             :                 retval[*length-1] = DEFAULT_SLASH;
     578             :                 retval[*length] = '\0';
     579             :                 return retval;
     580             :         }
     581             : #endif
     582           0 :         if (!state->cwd) {
     583           0 :                 *length = 0;
     584           0 :                 return NULL;
     585             :         }
     586             : 
     587           0 :         *length = state->cwd_length;
     588           0 :         return estrdup(state->cwd);
     589             : }
     590             : /* }}} */
     591             : 
     592             : /* Same semantics as UNIX getcwd() */
     593           0 : CWD_API char *virtual_getcwd(char *buf, size_t size) /* {{{ */
     594             : {
     595             :         size_t length;
     596             :         char *cwd;
     597             : 
     598           0 :         cwd = virtual_getcwd_ex(&length);
     599             : 
     600           0 :         if (buf == NULL) {
     601           0 :                 return cwd;
     602             :         }
     603           0 :         if (length > size-1) {
     604           0 :                 efree(cwd);
     605           0 :                 errno = ERANGE; /* Is this OK? */
     606           0 :                 return NULL;
     607             :         }
     608           0 :         if (!cwd) {
     609           0 :                 return NULL;
     610             :         }
     611           0 :         memcpy(buf, cwd, length+1);
     612           0 :         efree(cwd);
     613           0 :         return buf;
     614             : }
     615             : /* }}} */
     616             : 
     617             : #ifdef ZEND_WIN32
     618             : static inline zend_ulong realpath_cache_key(const char *path, int path_len) /* {{{ */
     619             : {
     620             :         register zend_ulong h;
     621             :         char *bucket_key_start = tsrm_win32_get_path_sid_key(path);
     622             :         char *bucket_key = (char *)bucket_key_start;
     623             :         const char *e;
     624             : 
     625             :         if (!bucket_key) {
     626             :                 return 0;
     627             :         }
     628             : 
     629             :         e = bucket_key + strlen(bucket_key);
     630             :         for (h = Z_UL(2166136261); bucket_key < e;) {
     631             :                 h *= Z_UL(16777619);
     632             :                 h ^= *bucket_key++;
     633             :         }
     634             :         HeapFree(GetProcessHeap(), 0, (LPVOID)bucket_key_start);
     635             :         return h;
     636             : }
     637             : /* }}} */
     638             : #else
     639      956374 : static inline zend_ulong realpath_cache_key(const char *path, int path_len) /* {{{ */
     640             : {
     641             :         register zend_ulong h;
     642      956374 :         const char *e = path + path_len;
     643             : 
     644    34904503 :         for (h = Z_UL(2166136261); path < e;) {
     645    32991755 :                 h *= Z_UL(16777619);
     646    32991755 :                 h ^= *path++;
     647             :         }
     648             : 
     649      956374 :         return h;
     650             : }
     651             : /* }}} */
     652             : #endif /* defined(ZEND_WIN32) */
     653             : 
     654       53449 : CWD_API void realpath_cache_clean(void) /* {{{ */
     655             : {
     656             :         uint32_t i;
     657             : 
     658    54785225 :         for (i = 0; i < sizeof(CWDG(realpath_cache))/sizeof(CWDG(realpath_cache)[0]); i++) {
     659    54731776 :                 realpath_cache_bucket *p = CWDG(realpath_cache)[i];
     660   109811859 :                 while (p != NULL) {
     661      348307 :                         realpath_cache_bucket *r = p;
     662      348307 :                         p = p->next;
     663      348307 :                         free(r);
     664             :                 }
     665    54731776 :                 CWDG(realpath_cache)[i] = NULL;
     666             :         }
     667       53449 :         CWDG(realpath_cache_size) = 0;
     668       53449 : }
     669             : /* }}} */
     670             : 
     671           3 : CWD_API void realpath_cache_del(const char *path, int path_len) /* {{{ */
     672             : {
     673           3 :         zend_ulong key = realpath_cache_key(path, path_len);
     674           3 :         zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     675           3 :         realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
     676             : 
     677           6 :         while (*bucket != NULL) {
     678           4 :                 if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
     679           2 :                                         memcmp(path, (*bucket)->path, path_len) == 0) {
     680           2 :                         realpath_cache_bucket *r = *bucket;
     681           2 :                         *bucket = (*bucket)->next;
     682             : 
     683             :                         /* if the pointers match then only subtract the length of the path */
     684           2 :                         if(r->path == r->realpath) {
     685           0 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
     686             :                         } else {
     687           2 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
     688             :                         }
     689             : 
     690           2 :                         free(r);
     691           2 :                         return;
     692             :                 } else {
     693           0 :                         bucket = &(*bucket)->next;
     694             :                 }
     695             :         }
     696             : }
     697             : /* }}} */
     698             : 
     699      413621 : static inline void realpath_cache_add(const char *path, int path_len, const char *realpath, int realpath_len, int is_dir, time_t t) /* {{{ */
     700             : {
     701      413621 :         zend_long size = sizeof(realpath_cache_bucket) + path_len + 1;
     702      413621 :         int same = 1;
     703             : 
     704      824490 :         if (realpath_len != path_len ||
     705      410869 :                 memcmp(path, realpath, path_len) != 0) {
     706        2753 :                 size += realpath_len + 1;
     707        2753 :                 same = 0;
     708             :         }
     709             : 
     710      413621 :         if (CWDG(realpath_cache_size) + size <= CWDG(realpath_cache_size_limit)) {
     711      348024 :                 realpath_cache_bucket *bucket = malloc(size);
     712             :                 zend_ulong n;
     713             : 
     714      348024 :                 if (bucket == NULL) {
     715           0 :                         return;
     716             :                 }
     717             : 
     718      348024 :                 bucket->key = realpath_cache_key(path, path_len);
     719      348024 :                 bucket->path = (char*)bucket + sizeof(realpath_cache_bucket);
     720      348024 :                 memcpy(bucket->path, path, path_len+1);
     721      348024 :                 bucket->path_len = path_len;
     722      348024 :                 if (same) {
     723      345271 :                         bucket->realpath = bucket->path;
     724             :                 } else {
     725        2753 :                         bucket->realpath = bucket->path + (path_len + 1);
     726        2753 :                         memcpy(bucket->realpath, realpath, realpath_len+1);
     727             :                 }
     728      348024 :                 bucket->realpath_len = realpath_len;
     729      348024 :                 bucket->is_dir = is_dir;
     730             : #ifdef ZEND_WIN32
     731             :                 bucket->is_rvalid   = 0;
     732             :                 bucket->is_readable = 0;
     733             :                 bucket->is_wvalid   = 0;
     734             :                 bucket->is_writable = 0;
     735             : #endif
     736      348024 :                 bucket->expires = t + CWDG(realpath_cache_ttl);
     737      348024 :                 n = bucket->key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     738      348024 :                 bucket->next = CWDG(realpath_cache)[n];
     739      348024 :                 CWDG(realpath_cache)[n] = bucket;
     740      348024 :                 CWDG(realpath_cache_size) += size;
     741             :         }
     742             : }
     743             : /* }}} */
     744             : 
     745      608347 : static inline realpath_cache_bucket* realpath_cache_find(const char *path, int path_len, time_t t) /* {{{ */
     746             : {
     747      608347 :         zend_ulong key = realpath_cache_key(path, path_len);
     748      608347 :         zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     749      608347 :         realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
     750             : 
     751     1222679 :         while (*bucket != NULL) {
     752      148344 :                 if (CWDG(realpath_cache_ttl) && (*bucket)->expires < t) {
     753          23 :                         realpath_cache_bucket *r = *bucket;
     754          23 :                         *bucket = (*bucket)->next;
     755             : 
     756             :                         /* if the pointers match then only subtract the length of the path */
     757          23 :                         if(r->path == r->realpath) {
     758          21 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
     759             :                         } else {
     760           2 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
     761             :                         }
     762          23 :                         free(r);
     763      290634 :                 } else if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
     764      142336 :                                         memcmp(path, (*bucket)->path, path_len) == 0) {
     765      142336 :                         return *bucket;
     766             :                 } else {
     767        5962 :                         bucket = &(*bucket)->next;
     768             :                 }
     769             :         }
     770      466011 :         return NULL;
     771             : }
     772             : /* }}} */
     773             : 
     774           0 : CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, int path_len, time_t t) /* {{{ */
     775             : {
     776           0 :         return realpath_cache_find(path, path_len, t);
     777             : }
     778             : /* }}} */
     779             : 
     780           1 : CWD_API zend_long realpath_cache_size(void)
     781             : {
     782           1 :         return CWDG(realpath_cache_size);
     783             : }
     784             : 
     785           1 : CWD_API zend_long realpath_cache_max_buckets(void)
     786             : {
     787           1 :         return (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     788             : }
     789             : 
     790           1 : CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void)
     791             : {
     792           1 :         return CWDG(realpath_cache);
     793             : }
     794             : 
     795             : 
     796             : #undef LINK_MAX
     797             : #define LINK_MAX 32
     798             : 
     799      866673 : static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, int use_realpath, int is_dir, int *link_is_dir) /* {{{ */
     800             : {
     801             :         int i, j, save;
     802      866673 :         int directory = 0;
     803             : #ifdef ZEND_WIN32
     804             :         WIN32_FIND_DATAW dataw;
     805             :         HANDLE hFind = INVALID_HANDLE_VALUE;
     806             :         ALLOCA_FLAG(use_heap_large)
     807             :         wchar_t *pathw = NULL;
     808             : #define FREE_PATHW() \
     809             :         do { free(pathw); } while(0);
     810             : 
     811             : #else
     812             :         zend_stat_t st;
     813             : #endif
     814             :         realpath_cache_bucket *bucket;
     815             :         char *tmp;
     816             :         ALLOCA_FLAG(use_heap)
     817             : 
     818             :         while (1) {
     819      868100 :                 if (len <= start) {
     820          68 :                         if (link_is_dir) {
     821           0 :                                 *link_is_dir = 1;
     822             :                         }
     823          68 :                         return start;
     824             :                 }
     825             : 
     826      868032 :                 i = len;
     827     8975961 :                 while (i > start && !IS_SLASH(path[i-1])) {
     828     7239897 :                         i--;
     829             :                 }
     830             : 
     831     1735534 :                 if (i == len ||
     832      867502 :                         (i == len - 1 && path[i] == '.')) {
     833             :                         /* remove double slashes and '.' */
     834        1427 :                         len = i - 1;
     835        1427 :                         is_dir = 1;
     836        1427 :                         continue;
     837      866605 :                 } else if (i == len - 2 && path[i] == '.' && path[i+1] == '.') {
     838             :                         /* remove '..' and previous directory */
     839        2157 :                         is_dir = 1;
     840        2157 :                         if (link_is_dir) {
     841           0 :                                 *link_is_dir = 1;
     842             :                         }
     843        2157 :                         if (i - 1 <= start) {
     844           2 :                                 return start ? start : len;
     845             :                         }
     846        2155 :                         j = tsrm_realpath_r(path, start, i-1, ll, t, use_realpath, 1, NULL);
     847        2155 :                         if (j > start) {
     848        2155 :                                 j--;
     849       15244 :                                 while (j > start && !IS_SLASH(path[j])) {
     850       10934 :                                         j--;
     851             :                                 }
     852        2155 :                                 if (!start) {
     853             :                                         /* leading '..' must not be removed in case of relative path */
     854           0 :                                         if (j == 0 && path[0] == '.' && path[1] == '.' &&
     855           0 :                                                         IS_SLASH(path[2])) {
     856           0 :                                                 path[3] = '.';
     857           0 :                                                 path[4] = '.';
     858           0 :                                                 path[5] = DEFAULT_SLASH;
     859           0 :                                                 j = 5;
     860           0 :                                         } else if (j > 0 &&
     861           0 :                                                         path[j+1] == '.' && path[j+2] == '.' &&
     862           0 :                                                         IS_SLASH(path[j+3])) {
     863           0 :                                                 j += 4;
     864           0 :                                                 path[j++] = '.';
     865           0 :                                                 path[j++] = '.';
     866           0 :                                                 path[j] = DEFAULT_SLASH;
     867             :                                         }
     868             :                                 }
     869           0 :                         } else if (!start && !j) {
     870             :                                 /* leading '..' must not be removed in case of relative path */
     871           0 :                                 path[0] = '.';
     872           0 :                                 path[1] = '.';
     873           0 :                                 path[2] = DEFAULT_SLASH;
     874           0 :                                 j = 2;
     875             :                         }
     876        2155 :                         return j;
     877             :                 }
     878             : 
     879      864448 :                 path[len] = 0;
     880             : 
     881      864448 :                 save = (use_realpath != CWD_EXPAND);
     882             : 
     883      864448 :                 if (start && save && CWDG(realpath_cache_size_limit)) {
     884             :                         /* cache lookup for absolute path */
     885      608347 :                         if (!*t) {
     886      201598 :                                 *t = time(0);
     887             :                         }
     888      608347 :                         if ((bucket = realpath_cache_find(path, len, *t)) != NULL) {
     889      142336 :                                 if (is_dir && !bucket->is_dir) {
     890             :                                         /* not a directory */
     891           3 :                                         return -1;
     892             :                                 } else {
     893      142333 :                                         if (link_is_dir) {
     894          88 :                                                 *link_is_dir = bucket->is_dir;
     895             :                                         }
     896      142333 :                                         memcpy(path, bucket->realpath, bucket->realpath_len + 1);
     897      142333 :                                         return bucket->realpath_len;
     898             :                                 }
     899             :                         }
     900             :                 }
     901             : 
     902             : #ifdef ZEND_WIN32
     903             :                 if (save) {
     904             :                         pathw = php_win32_ioutil_any_to_w(path);
     905             :                         if (!pathw) {
     906             :                                 return -1;
     907             :                         }
     908             :                         hFind = FindFirstFileW(pathw, &dataw);
     909             :                         if (INVALID_HANDLE_VALUE == hFind) {
     910             :                                 if (use_realpath == CWD_REALPATH) {
     911             :                                         /* file not found */
     912             :                                         FREE_PATHW()
     913             :                                         return -1;
     914             :                                 }
     915             :                                 /* continue resolution anyway but don't save result in the cache */
     916             :                                 save = 0;
     917             :                         } else {
     918             :                                 FindClose(hFind);
     919             :                         }
     920             :                 }
     921             : 
     922             :                 tmp = do_alloca(len+1, use_heap);
     923             :                 memcpy(tmp, path, len+1);
     924             : 
     925             :                 if(save &&
     926             :                                 !(IS_UNC_PATH(path, len) && len >= 3 && path[2] != '?') &&
     927             :                                (dataw.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
     928             :                                 ) {
     929             :                         /* File is a reparse point. Get the target */
     930             :                         HANDLE hLink = NULL;
     931             :                         REPARSE_DATA_BUFFER * pbuffer;
     932             :                         DWORD retlength = 0;
     933             :                         int bufindex = 0, isabsolute = 0;
     934             :                         wchar_t * reparsetarget;
     935             :                         BOOL isVolume = FALSE;
     936             :                         char *printname = NULL, *substitutename = NULL;
     937             :                         int printname_len, substitutename_len;
     938             :                         int substitutename_off = 0;
     939             : 
     940             :                         if(++(*ll) > LINK_MAX) {
     941             :                                 free_alloca(tmp, use_heap);
     942             :                                 FREE_PATHW()
     943             :                                 return -1;
     944             :                         }
     945             : 
     946             :                         hLink = CreateFileW(pathw, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL);
     947             :                         if(hLink == INVALID_HANDLE_VALUE) {
     948             :                                 free_alloca(tmp, use_heap);
     949             :                                 FREE_PATHW()
     950             :                                 return -1;
     951             :                         }
     952             : 
     953             :                         pbuffer = (REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
     954             :                         if (pbuffer == NULL) {
     955             :                                 CloseHandle(hLink);
     956             :                                 free_alloca(tmp, use_heap);
     957             :                                 FREE_PATHW()
     958             :                                 return -1;
     959             :                         }
     960             :                         if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer,  MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
     961             :                                 free_alloca(pbuffer, use_heap_large);
     962             :                                 free_alloca(tmp, use_heap);
     963             :                                 CloseHandle(hLink);
     964             :                                 FREE_PATHW()
     965             :                                 return -1;
     966             :                         }
     967             : 
     968             :                         CloseHandle(hLink);
     969             : 
     970             :                         if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
     971             :                                 reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
     972             :                                 printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
     973             :                                 isabsolute = (pbuffer->SymbolicLinkReparseBuffer.Flags == 0) ? 1 : 0;
     974             :                                 printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset  / sizeof(WCHAR));
     975             :                                 if (!printname) {
     976             :                                         free_alloca(pbuffer, use_heap_large);
     977             :                                         free_alloca(tmp, use_heap);
     978             :                                         FREE_PATHW()
     979             :                                         return -1;
     980             :                                 }
     981             : 
     982             :                                 substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
     983             :                                 substitutename = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR));
     984             :                                 if (!substitutename) {
     985             :                                         free_alloca(pbuffer, use_heap_large);
     986             :                                         free_alloca(tmp, use_heap);
     987             :                                         free(printname);
     988             :                                         FREE_PATHW()
     989             :                                         return -1;
     990             :                                 }
     991             :                         }
     992             :                         else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
     993             :                                 isabsolute = 1;
     994             :                                 reparsetarget = pbuffer->MountPointReparseBuffer.ReparseTarget;
     995             :                                 printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
     996             :                                 printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset  / sizeof(WCHAR));
     997             :                                 if (!printname) {
     998             :                                         free_alloca(pbuffer, use_heap_large);
     999             :                                         free_alloca(tmp, use_heap);
    1000             :                                         FREE_PATHW()
    1001             :                                         return -1;
    1002             :                                 }
    1003             : 
    1004             : 
    1005             :                                 substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
    1006             :                                 substitutename = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR));
    1007             :                                 if (!substitutename) {
    1008             :                                         free_alloca(pbuffer, use_heap_large);
    1009             :                                         free_alloca(tmp, use_heap);
    1010             :                                         free(printname);
    1011             :                                         FREE_PATHW()
    1012             :                                         return -1;
    1013             :                                 }
    1014             :                         }
    1015             :                         else if (pbuffer->ReparseTag == IO_REPARSE_TAG_DEDUP) {
    1016             :                                 isabsolute = 1;
    1017             :                                 substitutename = malloc((len + 1) * sizeof(char));
    1018             :                                 if (!substitutename) {
    1019             :                                         free_alloca(pbuffer, use_heap_large);
    1020             :                                         free_alloca(tmp, use_heap);
    1021             :                                         FREE_PATHW()
    1022             :                                         return -1;
    1023             :                                 }
    1024             :                                 memcpy(substitutename, path, len + 1);
    1025             :                                 substitutename_len = len;
    1026             :                         } else {
    1027             :                                 /* XXX this might be not the end, restart handling with REPARSE_GUID_DATA_BUFFER should be implemented. */
    1028             :                                 free_alloca(pbuffer, use_heap_large);
    1029             :                                 free_alloca(tmp, use_heap);
    1030             :                                 FREE_PATHW()
    1031             :                                 return -1;
    1032             :                         }
    1033             : 
    1034             :                         if(isabsolute && substitutename_len > 4) {
    1035             :                                 /* Do not resolve volumes (for now). A mounted point can
    1036             :                                    target a volume without a drive, it is not certain that
    1037             :                                    all IO functions we use in php and its deps support
    1038             :                                    path with volume GUID instead of the DOS way, like:
    1039             :                                    d:\test\mnt\foo
    1040             :                                    \\?\Volume{62d1c3f8-83b9-11de-b108-806e6f6e6963}\foo
    1041             :                                 */
    1042             :                                 if (strncmp(substitutename, "\\??\\Volume{",11) == 0
    1043             :                                         || strncmp(substitutename, "\\\\?\\Volume{",11) == 0
    1044             :                                         || strncmp(substitutename, "\\??\\UNC\\", 8) == 0
    1045             :                                         ) {
    1046             :                                         isVolume = TRUE;
    1047             :                                         substitutename_off = 0;
    1048             :                                 } else
    1049             :                                         /* do not use the \??\ and \\?\ prefix*/
    1050             :                                         if (strncmp(substitutename, "\\??\\", 4) == 0
    1051             :                                                 || strncmp(substitutename, "\\\\?\\", 4) == 0) {
    1052             :                                         substitutename_off = 4;
    1053             :                                 }
    1054             :                         }
    1055             : 
    1056             :                         if (!isVolume) {
    1057             :                                 char * tmp2 = substitutename + substitutename_off;
    1058             :                                 for(bufindex = 0; bufindex < (substitutename_len - substitutename_off); bufindex++) {
    1059             :                                         *(path + bufindex) = *(tmp2 + bufindex);
    1060             :                                 }
    1061             : 
    1062             :                                 *(path + bufindex) = 0;
    1063             :                                 j = bufindex;
    1064             :                         } else {
    1065             :                                 j = len;
    1066             :                         }
    1067             : 
    1068             : 
    1069             : #if VIRTUAL_CWD_DEBUG
    1070             :                         fprintf(stderr, "reparse: print: %s ", printname);
    1071             :                         fprintf(stderr, "sub: %s ", substitutename);
    1072             :                         fprintf(stderr, "resolved: %s ", path);
    1073             : #endif
    1074             :                         free_alloca(pbuffer, use_heap_large);
    1075             :                         free(printname);
    1076             :                         free(substitutename);
    1077             : 
    1078             :                         if(isabsolute == 1) {
    1079             :                                 if (!((j == 3) && (path[1] == ':') && (path[2] == '\\'))) {
    1080             :                                         /* use_realpath is 0 in the call below coz path is absolute*/
    1081             :                                         j = tsrm_realpath_r(path, 0, j, ll, t, 0, is_dir, &directory);
    1082             :                                         if(j < 0) {
    1083             :                                                 free_alloca(tmp, use_heap);
    1084             :                                                 FREE_PATHW()
    1085             :                                                 return -1;
    1086             :                                         }
    1087             :                                 }
    1088             :                         }
    1089             :                         else {
    1090             :                                 if(i + j >= MAXPATHLEN - 1) {
    1091             :                                         free_alloca(tmp, use_heap);
    1092             :                                         FREE_PATHW()
    1093             :                                         return -1;
    1094             :                                 }
    1095             : 
    1096             :                                 memmove(path+i, path, j+1);
    1097             :                                 memcpy(path, tmp, i-1);
    1098             :                                 path[i-1] = DEFAULT_SLASH;
    1099             :                                 j  = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
    1100             :                                 if(j < 0) {
    1101             :                                         free_alloca(tmp, use_heap);
    1102             :                                         FREE_PATHW()
    1103             :                                         return -1;
    1104             :                                 }
    1105             :                         }
    1106             :                         directory = (dataw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
    1107             : 
    1108             :                         if(link_is_dir) {
    1109             :                                 *link_is_dir = directory;
    1110             :                         }
    1111             :                 }
    1112             :                 else {
    1113             :                         if (save) {
    1114             :                                 directory = (dataw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
    1115             :                                 if (is_dir && !directory) {
    1116             :                                         /* not a directory */
    1117             :                                         free_alloca(tmp, use_heap);
    1118             :                                         FREE_PATHW()
    1119             :                                         return -1;
    1120             :                                 }
    1121             :                         }
    1122             : 
    1123             : #elif defined(NETWARE)
    1124             :                 save = 0;
    1125             :                 tmp = do_alloca(len+1, use_heap);
    1126             :                 memcpy(tmp, path, len+1);
    1127             : #else
    1128      722112 :                 if (save && php_sys_lstat(path, &st) < 0) {
    1129       53239 :                         if (use_realpath == CWD_REALPATH) {
    1130             :                                 /* file not found */
    1131       13673 :                                 return -1;
    1132             :                         }
    1133             :                         /* continue resolution anyway but don't save result in the cache */
    1134       39566 :                         save = 0;
    1135             :                 }
    1136             : 
    1137      708439 :                 tmp = do_alloca(len+1, use_heap);
    1138      708439 :                 memcpy(tmp, path, len+1);
    1139             : 
    1140      708539 :                 if (save && S_ISLNK(st.st_mode)) {
    1141         101 :                         if (++(*ll) > LINK_MAX || (j = php_sys_readlink(tmp, path, MAXPATHLEN)) < 0) {
    1142             :                                 /* too many links or broken symlinks */
    1143           0 :                                 free_alloca(tmp, use_heap);
    1144           0 :                                 return -1;
    1145             :                         }
    1146         101 :                         path[j] = 0;
    1147         101 :                         if (IS_ABSOLUTE_PATH(path, j)) {
    1148          99 :                                 j = tsrm_realpath_r(path, 1, j, ll, t, use_realpath, is_dir, &directory);
    1149          99 :                                 if (j < 0) {
    1150           1 :                                         free_alloca(tmp, use_heap);
    1151           1 :                                         return -1;
    1152             :                                 }
    1153             :                         } else {
    1154           2 :                                 if (i + j >= MAXPATHLEN-1) {
    1155           0 :                                         free_alloca(tmp, use_heap);
    1156           0 :                                         return -1; /* buffer overflow */
    1157             :                                 }
    1158           2 :                                 memmove(path+i, path, j+1);
    1159           2 :                                 memcpy(path, tmp, i-1);
    1160           2 :                                 path[i-1] = DEFAULT_SLASH;
    1161           2 :                                 j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
    1162           2 :                                 if (j < 0) {
    1163           0 :                                         free_alloca(tmp, use_heap);
    1164           0 :                                         return -1;
    1165             :                                 }
    1166             :                         }
    1167         100 :                         if (link_is_dir) {
    1168           1 :                                 *link_is_dir = directory;
    1169             :                         }
    1170             :                 } else {
    1171      708338 :                         if (save) {
    1172      663908 :                                 directory = S_ISDIR(st.st_mode);
    1173      663908 :                                 if (link_is_dir) {
    1174          10 :                                         *link_is_dir = directory;
    1175             :                                 }
    1176      663908 :                                 if (is_dir && !directory) {
    1177             :                                         /* not a directory */
    1178           0 :                                         free_alloca(tmp, use_heap);
    1179           0 :                                         return -1;
    1180             :                                 }
    1181             :                         }
    1182             : #endif
    1183      708338 :                         if (i - 1 <= start) {
    1184       96800 :                                 j = start;
    1185             :                         } else {
    1186             :                                 /* some leading directories may be unaccessable */
    1187      611538 :                                 j = tsrm_realpath_r(path, start, i-1, ll, t, save ? CWD_FILEPATH : use_realpath, 1, NULL);
    1188      611538 :                                 if (j > start) {
    1189      611535 :                                         path[j++] = DEFAULT_SLASH;
    1190             :                                 }
    1191             :                         }
    1192             : #ifdef ZEND_WIN32
    1193             :                         if (j < 0 || j + len - i >= MAXPATHLEN-1) {
    1194             :                                 free_alloca(tmp, use_heap);
    1195             :                                 FREE_PATHW()
    1196             :                                 return -1;
    1197             :                         }
    1198             :                         if (save) {
    1199             :                                 size_t sz;
    1200             :                                 char *tmp_path = php_win32_ioutil_conv_w_to_any(dataw.cFileName, PHP_WIN32_CP_IGNORE_LEN, &sz);
    1201             :                                 if (!tmp_path) {
    1202             :                                         free_alloca(tmp, use_heap);
    1203             :                                         FREE_PATHW()
    1204             :                                         return -1;
    1205             :                                 }
    1206             :                                 i = (int)sz;
    1207             :                                 memcpy(path+j, tmp_path, i+1);
    1208             :                                 free(tmp_path);
    1209             :                                 j += i;
    1210             :                         } else {
    1211             :                                 /* use the original file or directory name as it wasn't found */
    1212             :                                 memcpy(path+j, tmp+i, len-i+1);
    1213             :                                 j += (len-i);
    1214             :                         }
    1215             :                 }
    1216             : #else
    1217      708338 :                         if (j < 0 || j + len - i >= MAXPATHLEN-1) {
    1218           1 :                                 free_alloca(tmp, use_heap);
    1219           1 :                                 return -1;
    1220             :                         }
    1221      708337 :                         memcpy(path+j, tmp+i, len-i+1);
    1222      708337 :                         j += (len-i);
    1223             :                 }
    1224             : #endif
    1225             : 
    1226      708437 :                 if (save && start && CWDG(realpath_cache_size_limit)) {
    1227             :                         /* save absolute path in the cache */
    1228      413621 :                         realpath_cache_add(tmp, len, path, j, directory, *t);
    1229             :                 }
    1230             : 
    1231      708437 :                 free_alloca(tmp, use_heap);
    1232             : #ifdef ZEND_WIN32
    1233             :                 FREE_PATHW()
    1234             : #undef FREE_PATHW
    1235             : #endif
    1236      708437 :                 return j;
    1237        1427 :         }
    1238             : }
    1239             : /* }}} */
    1240             : 
    1241             : /* Resolve path relatively to state and put the real path into state */
    1242             : /* returns 0 for ok, 1 for error */
    1243      252883 : CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath) /* {{{ */
    1244             : {
    1245      252883 :         int path_length = (int)strlen(path);
    1246             :         char resolved_path[MAXPATHLEN];
    1247      252883 :         int start = 1;
    1248      252883 :         int ll = 0;
    1249             :         time_t t;
    1250             :         int ret;
    1251             :         int add_slash;
    1252             :         void *tmp;
    1253             : 
    1254      252883 :         if (path_length <= 0 || path_length >= MAXPATHLEN-1) {
    1255             : #ifdef ZEND_WIN32
    1256             :                 _set_errno(EINVAL);
    1257             : #else
    1258           4 :                 errno = EINVAL;
    1259             : #endif
    1260           4 :                 return 1;
    1261             :         }
    1262             : 
    1263             : #if VIRTUAL_CWD_DEBUG
    1264             :         fprintf(stderr,"cwd = %s path = %s\n", state->cwd, path);
    1265             : #endif
    1266             : 
    1267             :         /* cwd_length can be 0 when getcwd() fails.
    1268             :          * This can happen under solaris when a dir does not have read permissions
    1269             :          * but *does* have execute permissions */
    1270      252879 :         if (!IS_ABSOLUTE_PATH(path, path_length)) {
    1271        8334 :                 if (state->cwd_length == 0) {
    1272             :                         /* resolve relative path */
    1273          14 :                         start = 0;
    1274          14 :                         memcpy(resolved_path , path, path_length + 1);
    1275             :                 } else {
    1276        8320 :                         int state_cwd_length = state->cwd_length;
    1277             : 
    1278             : #ifdef ZEND_WIN32
    1279             :                         if (IS_SLASH(path[0])) {
    1280             :                                 if (state->cwd[1] == ':') {
    1281             :                                         /* Copy only the drive name */
    1282             :                                         state_cwd_length = 2;
    1283             :                                 } else if (IS_UNC_PATH(state->cwd, state->cwd_length)) {
    1284             :                                         /* Copy only the share name */
    1285             :                                         state_cwd_length = 2;
    1286             :                                         while (IS_SLASH(state->cwd[state_cwd_length])) {
    1287             :                                                 state_cwd_length++;
    1288             :                                         }
    1289             :                                         while (state->cwd[state_cwd_length] &&
    1290             :                                                         !IS_SLASH(state->cwd[state_cwd_length])) {
    1291             :                                                 state_cwd_length++;
    1292             :                                         }
    1293             :                                         while (IS_SLASH(state->cwd[state_cwd_length])) {
    1294             :                                                 state_cwd_length++;
    1295             :                                         }
    1296             :                                         while (state->cwd[state_cwd_length] &&
    1297             :                                                         !IS_SLASH(state->cwd[state_cwd_length])) {
    1298             :                                                 state_cwd_length++;
    1299             :                                         }
    1300             :                                 }
    1301             :                         }
    1302             : #endif
    1303        8320 :                         if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
    1304           0 :                                 return 1;
    1305             :                         }
    1306        8320 :                         memcpy(resolved_path, state->cwd, state_cwd_length);
    1307        8320 :                         if (resolved_path[state_cwd_length-1] == DEFAULT_SLASH) {
    1308          24 :                                 memcpy(resolved_path + state_cwd_length, path, path_length + 1);
    1309          24 :                                 path_length += state_cwd_length;
    1310             :                         } else {
    1311        8296 :                                 resolved_path[state_cwd_length] = DEFAULT_SLASH;
    1312        8296 :                                 memcpy(resolved_path + state_cwd_length + 1, path, path_length + 1);
    1313        8296 :                                 path_length += state_cwd_length + 1;
    1314             :                         }
    1315             :                 }
    1316             :         } else {
    1317             : #ifdef ZEND_WIN32
    1318             :                 if (path_length > 2 && path[1] == ':' && !IS_SLASH(path[2])) {
    1319             :                         resolved_path[0] = path[0];
    1320             :                         resolved_path[1] = ':';
    1321             :                         resolved_path[2] = DEFAULT_SLASH;
    1322             :                         memcpy(resolved_path + 3, path + 2, path_length - 1);
    1323             :                         path_length++;
    1324             :                 } else
    1325             : #endif
    1326      244545 :                 memcpy(resolved_path, path, path_length + 1);
    1327             :         }
    1328             : 
    1329             : #ifdef ZEND_WIN32
    1330             :         if (memchr(resolved_path, '*', path_length) ||
    1331             :                 memchr(resolved_path, '?', path_length)) {
    1332             :                 return 1;
    1333             :         }
    1334             : #endif
    1335             : 
    1336             : #ifdef ZEND_WIN32
    1337             :         if (IS_UNC_PATH(resolved_path, path_length)) {
    1338             :                 /* skip UNC name */
    1339             :                 resolved_path[0] = DEFAULT_SLASH;
    1340             :                 resolved_path[1] = DEFAULT_SLASH;
    1341             :                 start = 2;
    1342             :                 while (!IS_SLASH(resolved_path[start])) {
    1343             :                         if (resolved_path[start] == 0) {
    1344             :                                 goto verify;
    1345             :                         }
    1346             :                         resolved_path[start] = toupper(resolved_path[start]);
    1347             :                         start++;
    1348             :                 }
    1349             :                 resolved_path[start++] = DEFAULT_SLASH;
    1350             :                 while (!IS_SLASH(resolved_path[start])) {
    1351             :                         if (resolved_path[start] == 0) {
    1352             :                                 goto verify;
    1353             :                         }
    1354             :                         resolved_path[start] = toupper(resolved_path[start]);
    1355             :                         start++;
    1356             :                 }
    1357             :                 resolved_path[start++] = DEFAULT_SLASH;
    1358             :         } else if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
    1359             :                 /* skip DRIVE name */
    1360             :                 resolved_path[0] = toupper(resolved_path[0]);
    1361             :                 resolved_path[2] = DEFAULT_SLASH;
    1362             :                 start = 3;
    1363             :         }
    1364             : #elif defined(NETWARE)
    1365             :         if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
    1366             :                 /* skip VOLUME name */
    1367             :                 start = 0;
    1368             :                 while (start != ':') {
    1369             :                         if (resolved_path[start] == 0) return -1;
    1370             :                         start++;
    1371             :                 }
    1372             :                 start++;
    1373             :                 if (!IS_SLASH(resolved_path[start])) return -1;
    1374             :                 resolved_path[start++] = DEFAULT_SLASH;
    1375             :         }
    1376             : #endif
    1377             : 
    1378      252879 :         add_slash = (use_realpath != CWD_REALPATH) && path_length > 0 && IS_SLASH(resolved_path[path_length-1]);
    1379      252879 :         t = CWDG(realpath_cache_ttl) ? 0 : -1;
    1380      252879 :         path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
    1381             : 
    1382      252879 :         if (path_length < 0) {
    1383       13676 :                 errno = ENOENT;
    1384       13676 :                 return 1;
    1385             :         }
    1386             : 
    1387      239203 :         if (!start && !path_length) {
    1388           0 :                 resolved_path[path_length++] = '.';
    1389             :         }
    1390      239203 :         if (add_slash && path_length && !IS_SLASH(resolved_path[path_length-1])) {
    1391         687 :                 if (path_length >= MAXPATHLEN-1) {
    1392           0 :                         return -1;
    1393             :                 }
    1394         687 :                 resolved_path[path_length++] = DEFAULT_SLASH;
    1395             :         }
    1396      239203 :         resolved_path[path_length] = 0;
    1397             : 
    1398             : #ifdef ZEND_WIN32
    1399             : verify:
    1400             : #endif
    1401      239203 :         if (verify_path) {
    1402             :                 cwd_state old_state;
    1403             : 
    1404           0 :                 CWD_STATE_COPY(&old_state, state);
    1405           0 :                 state->cwd_length = path_length;
    1406             : 
    1407           0 :                 tmp = erealloc(state->cwd, state->cwd_length+1);
    1408           0 :                 if (tmp == NULL) {
    1409             : #if VIRTUAL_CWD_DEBUG
    1410             :                         fprintf (stderr, "Out of memory\n");
    1411             : #endif
    1412           0 :                         return 1;
    1413             :                 }
    1414           0 :                 state->cwd = (char *) tmp;
    1415             : 
    1416           0 :                 memcpy(state->cwd, resolved_path, state->cwd_length+1);
    1417           0 :                 if (verify_path(state)) {
    1418           0 :                         CWD_STATE_FREE(state);
    1419           0 :                         *state = old_state;
    1420           0 :                         ret = 1;
    1421             :                 } else {
    1422           0 :                         CWD_STATE_FREE(&old_state);
    1423           0 :                         ret = 0;
    1424             :                 }
    1425             :         } else {
    1426      239203 :                 state->cwd_length = path_length;
    1427      239203 :                 tmp = erealloc(state->cwd, state->cwd_length+1);
    1428      239203 :                 if (tmp == NULL) {
    1429             : #if VIRTUAL_CWD_DEBUG
    1430             :                         fprintf (stderr, "Out of memory\n");
    1431             : #endif
    1432           0 :                         return 1;
    1433             :                 }
    1434      239203 :                 state->cwd = (char *) tmp;
    1435             : 
    1436      239203 :                 memcpy(state->cwd, resolved_path, state->cwd_length+1);
    1437      239203 :                 ret = 0;
    1438             :         }
    1439             : 
    1440             : #if VIRTUAL_CWD_DEBUG
    1441             :         fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
    1442             : #endif
    1443      239203 :         return (ret);
    1444             : }
    1445             : /* }}} */
    1446             : 
    1447           0 : CWD_API int virtual_chdir(const char *path) /* {{{ */
    1448             : {
    1449           0 :         return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH)?-1:0;
    1450             : }
    1451             : /* }}} */
    1452             : 
    1453         244 : CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path)) /* {{{ */
    1454             : {
    1455         244 :         int length = (int)strlen(path);
    1456             :         char *temp;
    1457             :         int retval;
    1458             :         ALLOCA_FLAG(use_heap)
    1459             : 
    1460         244 :         if (length == 0) {
    1461           0 :                 return 1; /* Can't cd to empty string */
    1462             :         }
    1463        4769 :         while(--length >= 0 && !IS_SLASH(path[length])) {
    1464             :         }
    1465             : 
    1466         244 :         if (length == -1) {
    1467             :                 /* No directory only file name */
    1468           0 :                 errno = ENOENT;
    1469           0 :                 return -1;
    1470             :         }
    1471             : 
    1472         244 :         if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
    1473           0 :                 length++;
    1474             :         }
    1475         244 :         temp = (char *) do_alloca(length+1, use_heap);
    1476         244 :         memcpy(temp, path, length);
    1477         244 :         temp[length] = 0;
    1478             : #if VIRTUAL_CWD_DEBUG
    1479             :         fprintf (stderr, "Changing directory to %s\n", temp);
    1480             : #endif
    1481         244 :         retval = p_chdir(temp);
    1482         244 :         free_alloca(temp, use_heap);
    1483         244 :         return retval;
    1484             : }
    1485             : /* }}} */
    1486             : 
    1487           0 : CWD_API char *virtual_realpath(const char *path, char *real_path) /* {{{ */
    1488             : {
    1489             :         cwd_state new_state;
    1490             :         char *retval;
    1491             :         char cwd[MAXPATHLEN];
    1492             : 
    1493             :         /* realpath("") returns CWD */
    1494           0 :         if (!*path) {
    1495           0 :                 new_state.cwd = (char*)emalloc(1);
    1496           0 :                 if (new_state.cwd == NULL) {
    1497           0 :                         retval = NULL;
    1498           0 :                         goto end;
    1499             :                 }
    1500           0 :                 new_state.cwd[0] = '\0';
    1501           0 :                 new_state.cwd_length = 0;
    1502           0 :                 if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
    1503           0 :                         path = cwd;
    1504             :                 }
    1505           0 :         } else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
    1506           0 :                 CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1507             :         } else {
    1508           0 :                 new_state.cwd = (char*)emalloc(1);
    1509           0 :                 if (new_state.cwd == NULL) {
    1510           0 :                         retval = NULL;
    1511           0 :                         goto end;
    1512             :                 }
    1513           0 :                 new_state.cwd[0] = '\0';
    1514           0 :                 new_state.cwd_length = 0;
    1515             :         }
    1516             : 
    1517           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)==0) {
    1518           0 :                 int len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
    1519             : 
    1520           0 :                 memcpy(real_path, new_state.cwd, len);
    1521           0 :                 real_path[len] = '\0';
    1522           0 :                 retval = real_path;
    1523             :         } else {
    1524           0 :                 retval = NULL;
    1525             :         }
    1526             : 
    1527           0 :         CWD_STATE_FREE(&new_state);
    1528             : end:
    1529           0 :         return retval;
    1530             : }
    1531             : /* }}} */
    1532             : 
    1533           0 : CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path) /* {{{ */
    1534             : {
    1535             :         cwd_state new_state;
    1536             :         int retval;
    1537             : 
    1538           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1539           0 :         retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH);
    1540             : 
    1541           0 :         *filepath = new_state.cwd;
    1542             : 
    1543           0 :         return retval;
    1544             : 
    1545             : }
    1546             : /* }}} */
    1547             : 
    1548           0 : CWD_API int virtual_filepath(const char *path, char **filepath) /* {{{ */
    1549             : {
    1550           0 :         return virtual_filepath_ex(path, filepath, php_is_file_ok);
    1551             : }
    1552             : /* }}} */
    1553             : 
    1554           0 : CWD_API FILE *virtual_fopen(const char *path, const char *mode) /* {{{ */
    1555             : {
    1556             :         cwd_state new_state;
    1557             :         FILE *f;
    1558             : 
    1559           0 :         if (path[0] == '\0') { /* Fail to open empty path */
    1560           0 :                 return NULL;
    1561             :         }
    1562             : 
    1563           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1564           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
    1565           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1566           0 :                 return NULL;
    1567             :         }
    1568             : 
    1569             : #ifdef ZEND_WIN32
    1570             :         f = php_win32_ioutil_fopen(new_state.cwd, mode);
    1571             : #else
    1572           0 :         f = fopen(new_state.cwd, mode);
    1573             : #endif
    1574             : 
    1575           0 :         CWD_STATE_FREE_ERR(&new_state);
    1576             : 
    1577           0 :         return f;
    1578             : }
    1579             : /* }}} */
    1580             : 
    1581           0 : CWD_API int virtual_access(const char *pathname, int mode) /* {{{ */
    1582             : {
    1583             :         cwd_state new_state;
    1584             :         int ret;
    1585             : 
    1586           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1587           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
    1588           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1589           0 :                 return -1;
    1590             :         }
    1591             : 
    1592             : #if defined(ZEND_WIN32)
    1593             :         ret = tsrm_win32_access(new_state.cwd, mode);
    1594             : #else
    1595           0 :         ret = access(new_state.cwd, mode);
    1596             : #endif
    1597             : 
    1598           0 :         CWD_STATE_FREE_ERR(&new_state);
    1599             : 
    1600           0 :         return ret;
    1601             : }
    1602             : /* }}} */
    1603             : 
    1604             : #if HAVE_UTIME
    1605           0 : CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */
    1606             : {
    1607             :         cwd_state new_state;
    1608             :         int ret;
    1609             : 
    1610           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1611           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
    1612           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1613           0 :                 return -1;
    1614             :         }
    1615             : 
    1616             : #ifdef ZEND_WIN32
    1617             :         ret = win32_utime(new_state.cwd, buf);
    1618             : #else
    1619           0 :         ret = utime(new_state.cwd, buf);
    1620             : #endif
    1621             : 
    1622           0 :         CWD_STATE_FREE_ERR(&new_state);
    1623           0 :         return ret;
    1624             : }
    1625             : /* }}} */
    1626             : #endif
    1627             : 
    1628           0 : CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
    1629             : {
    1630             :         cwd_state new_state;
    1631             :         int ret;
    1632             : 
    1633           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1634           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
    1635           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1636           0 :                 return -1;
    1637             :         }
    1638             : 
    1639             : #ifdef ZEND_WIN32
    1640             :         {
    1641             :                 mode_t _tmp = mode;
    1642             : 
    1643             :                 mode = 0;
    1644             : 
    1645             :                 if (_tmp & _S_IREAD) {
    1646             :                         mode |= _S_IREAD;
    1647             :                 }
    1648             :                 if (_tmp & _S_IWRITE) {
    1649             :                         mode |= _S_IWRITE;
    1650             :                 }
    1651             :                 ret = php_win32_ioutil_chmod(new_state.cwd, mode);
    1652             :         }
    1653             : #else
    1654           0 :         ret = chmod(new_state.cwd, mode);
    1655             : #endif
    1656             : 
    1657           0 :         CWD_STATE_FREE_ERR(&new_state);
    1658           0 :         return ret;
    1659             : }
    1660             : /* }}} */
    1661             : 
    1662             : #if !defined(ZEND_WIN32) && !defined(NETWARE)
    1663           0 : CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link) /* {{{ */
    1664             : {
    1665             :         cwd_state new_state;
    1666             :         int ret;
    1667             : 
    1668           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1669           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
    1670           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1671           0 :                 return -1;
    1672             :         }
    1673             : 
    1674           0 :         if (link) {
    1675             : #if HAVE_LCHOWN
    1676           0 :                 ret = lchown(new_state.cwd, owner, group);
    1677             : #else
    1678             :                 ret = -1;
    1679             : #endif
    1680             :         } else {
    1681           0 :                 ret = chown(new_state.cwd, owner, group);
    1682             :         }
    1683             : 
    1684           0 :         CWD_STATE_FREE_ERR(&new_state);
    1685           0 :         return ret;
    1686             : }
    1687             : /* }}} */
    1688             : #endif
    1689             : 
    1690           0 : CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */
    1691             : {
    1692             :         cwd_state new_state;
    1693             :         int f;
    1694             : 
    1695           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1696           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
    1697           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1698           0 :                 return -1;
    1699             :         }
    1700             : 
    1701           0 :         if (flags & O_CREAT) {
    1702             :                 mode_t mode;
    1703             :                 va_list arg;
    1704             : 
    1705           0 :                 va_start(arg, flags);
    1706           0 :                 mode = (mode_t) va_arg(arg, int);
    1707           0 :                 va_end(arg);
    1708             : 
    1709             : #ifdef ZEND_WIN32
    1710             :                 f = php_win32_ioutil_open(new_state.cwd, flags, mode);
    1711             : #else
    1712           0 :                 f = open(new_state.cwd, flags, mode);
    1713             : #endif
    1714             :         } else {
    1715             : #ifdef ZEND_WIN32
    1716             :                 f = php_win32_ioutil_open(new_state.cwd, flags);
    1717             : #else
    1718           0 :                 f = open(new_state.cwd, flags);
    1719             : #endif
    1720             :         }
    1721           0 :         CWD_STATE_FREE_ERR(&new_state);
    1722           0 :         return f;
    1723             : }
    1724             : /* }}} */
    1725             : 
    1726           0 : CWD_API int virtual_creat(const char *path, mode_t mode) /* {{{ */
    1727             : {
    1728             :         cwd_state new_state;
    1729             :         int f;
    1730             : 
    1731           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1732           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
    1733           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1734           0 :                 return -1;
    1735             :         }
    1736             : 
    1737           0 :         f = creat(new_state.cwd,  mode);
    1738             : 
    1739           0 :         CWD_STATE_FREE_ERR(&new_state);
    1740           0 :         return f;
    1741             : }
    1742             : /* }}} */
    1743             : 
    1744           0 : CWD_API int virtual_rename(const char *oldname, const char *newname) /* {{{ */
    1745             : {
    1746             :         cwd_state old_state;
    1747             :         cwd_state new_state;
    1748             :         int retval;
    1749             : 
    1750           0 :         CWD_STATE_COPY(&old_state, &CWDG(cwd));
    1751           0 :         if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND)) {
    1752           0 :                 CWD_STATE_FREE_ERR(&old_state);
    1753           0 :                 return -1;
    1754             :         }
    1755           0 :         oldname = old_state.cwd;
    1756             : 
    1757           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1758           0 :         if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND)) {
    1759           0 :                 CWD_STATE_FREE_ERR(&old_state);
    1760           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1761           0 :                 return -1;
    1762             :         }
    1763           0 :         newname = new_state.cwd;
    1764             : 
    1765             :         /* rename on windows will fail if newname already exists.
    1766             :            MoveFileEx has to be used */
    1767             : #ifdef ZEND_WIN32
    1768             :         /* MoveFileEx returns 0 on failure, other way 'round for this function */
    1769             :         retval = php_win32_ioutil_rename(oldname, newname);
    1770             : #else
    1771           0 :         retval = rename(oldname, newname);
    1772             : #endif
    1773             : 
    1774           0 :         CWD_STATE_FREE_ERR(&old_state);
    1775           0 :         CWD_STATE_FREE_ERR(&new_state);
    1776             : 
    1777           0 :         return retval;
    1778             : }
    1779             : /* }}} */
    1780             : 
    1781           0 : CWD_API int virtual_stat(const char *path, zend_stat_t *buf) /* {{{ */
    1782             : {
    1783             :         cwd_state new_state;
    1784             :         int retval;
    1785             : 
    1786           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1787           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
    1788           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1789           0 :                 return -1;
    1790             :         }
    1791             : 
    1792           0 :         retval = php_sys_stat(new_state.cwd, buf);
    1793             : 
    1794           0 :         CWD_STATE_FREE_ERR(&new_state);
    1795           0 :         return retval;
    1796             : }
    1797             : /* }}} */
    1798             : 
    1799           0 : CWD_API int virtual_lstat(const char *path, zend_stat_t *buf) /* {{{ */
    1800             : {
    1801             :         cwd_state new_state;
    1802             :         int retval;
    1803             : 
    1804           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1805           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
    1806           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1807           0 :                 return -1;
    1808             :         }
    1809             : 
    1810           0 :         retval = php_sys_lstat(new_state.cwd, buf);
    1811             : 
    1812           0 :         CWD_STATE_FREE_ERR(&new_state);
    1813           0 :         return retval;
    1814             : }
    1815             : /* }}} */
    1816             : 
    1817           0 : CWD_API int virtual_unlink(const char *path) /* {{{ */
    1818             : {
    1819             :         cwd_state new_state;
    1820             :         int retval;
    1821             : 
    1822           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1823           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
    1824           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1825           0 :                 return -1;
    1826             :         }
    1827             : 
    1828             : #ifdef ZEND_WIN32
    1829             :         retval = php_win32_ioutil_unlink(new_state.cwd);
    1830             : #else
    1831           0 :         retval = unlink(new_state.cwd);
    1832             : #endif
    1833             : 
    1834           0 :         CWD_STATE_FREE_ERR(&new_state);
    1835           0 :         return retval;
    1836             : }
    1837             : /* }}} */
    1838             : 
    1839           0 : CWD_API int virtual_mkdir(const char *pathname, mode_t mode) /* {{{ */
    1840             : {
    1841             :         cwd_state new_state;
    1842             :         int retval;
    1843             : 
    1844           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1845           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH)) {
    1846           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1847           0 :                 return -1;
    1848             :         }
    1849             : 
    1850             : #ifdef ZEND_WIN32
    1851             :         retval = php_win32_ioutil_mkdir(new_state.cwd, mode);
    1852             : #else
    1853           0 :         retval = mkdir(new_state.cwd, mode);
    1854             : #endif
    1855           0 :         CWD_STATE_FREE_ERR(&new_state);
    1856           0 :         return retval;
    1857             : }
    1858             : /* }}} */
    1859             : 
    1860           0 : CWD_API int virtual_rmdir(const char *pathname) /* {{{ */
    1861             : {
    1862             :         cwd_state new_state;
    1863             :         int retval;
    1864             : 
    1865           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1866           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND)) {
    1867           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1868           0 :                 return -1;
    1869             :         }
    1870             : 
    1871             : #ifdef ZEND_WIN32
    1872             :         retval = php_win32_ioutil_rmdir(new_state.cwd);
    1873             : #else
    1874           0 :         retval = rmdir(new_state.cwd);
    1875             : #endif
    1876           0 :         CWD_STATE_FREE_ERR(&new_state);
    1877           0 :         return retval;
    1878             : }
    1879             : /* }}} */
    1880             : 
    1881             : #ifdef ZEND_WIN32
    1882             : DIR *opendir(const char *name);
    1883             : #endif
    1884             : 
    1885           0 : CWD_API DIR *virtual_opendir(const char *pathname) /* {{{ */
    1886             : {
    1887             :         cwd_state new_state;
    1888             :         DIR *retval;
    1889             : 
    1890           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1891           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
    1892           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1893           0 :                 return NULL;
    1894             :         }
    1895             : 
    1896           0 :         retval = opendir(new_state.cwd);
    1897             : 
    1898           0 :         CWD_STATE_FREE_ERR(&new_state);
    1899           0 :         return retval;
    1900             : }
    1901             : /* }}} */
    1902             : 
    1903             : #ifdef ZEND_WIN32
    1904             : CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
    1905             : {
    1906             :         return popen_ex(command, type, CWDG(cwd).cwd, NULL);
    1907             : }
    1908             : /* }}} */
    1909             : #elif defined(NETWARE)
    1910             : /* On NetWare, the trick of prepending "cd cwd; " doesn't work so we need to perform
    1911             :    a VCWD_CHDIR() and mutex it
    1912             :  */
    1913             : CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
    1914             : {
    1915             :         char prev_cwd[MAXPATHLEN];
    1916             :         char *getcwd_result;
    1917             :         FILE *retval;
    1918             : 
    1919             :         getcwd_result = VCWD_GETCWD(prev_cwd, MAXPATHLEN);
    1920             :         if (!getcwd_result) {
    1921             :                 return NULL;
    1922             :         }
    1923             : 
    1924             : #ifdef ZTS
    1925             :         tsrm_mutex_lock(cwd_mutex);
    1926             : #endif
    1927             : 
    1928             :         VCWD_CHDIR(CWDG(cwd).cwd);
    1929             :         retval = popen(command, type);
    1930             :         VCWD_CHDIR(prev_cwd);
    1931             : 
    1932             : #ifdef ZTS
    1933             :         tsrm_mutex_unlock(cwd_mutex);
    1934             : #endif
    1935             : 
    1936             :         return retval;
    1937             : }
    1938             : /* }}} */
    1939             : #else /* Unix */
    1940           0 : CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
    1941             : {
    1942             :         int command_length;
    1943           0 :         int dir_length, extra = 0;
    1944             :         char *command_line;
    1945             :         char *ptr, *dir;
    1946             :         FILE *retval;
    1947             : 
    1948           0 :         command_length = strlen(command);
    1949             : 
    1950           0 :         dir_length = CWDG(cwd).cwd_length;
    1951           0 :         dir = CWDG(cwd).cwd;
    1952           0 :         while (dir_length > 0) {
    1953           0 :                 if (*dir == '\'') extra+=3;
    1954           0 :                 dir++;
    1955           0 :                 dir_length--;
    1956             :         }
    1957           0 :         dir_length = CWDG(cwd).cwd_length;
    1958           0 :         dir = CWDG(cwd).cwd;
    1959             : 
    1960           0 :         ptr = command_line = (char *) emalloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
    1961           0 :         if (!command_line) {
    1962           0 :                 return NULL;
    1963             :         }
    1964           0 :         memcpy(ptr, "cd ", sizeof("cd ")-1);
    1965           0 :         ptr += sizeof("cd ")-1;
    1966             : 
    1967           0 :         if (CWDG(cwd).cwd_length == 0) {
    1968           0 :                 *ptr++ = DEFAULT_SLASH;
    1969             :         } else {
    1970           0 :                 *ptr++ = '\'';
    1971           0 :                 while (dir_length > 0) {
    1972           0 :                         switch (*dir) {
    1973             :                         case '\'':
    1974           0 :                                 *ptr++ = '\'';
    1975           0 :                                 *ptr++ = '\\';
    1976           0 :                                 *ptr++ = '\'';
    1977             :                                 /* fall-through */
    1978             :                         default:
    1979           0 :                                 *ptr++ = *dir;
    1980             :                         }
    1981           0 :                         dir++;
    1982           0 :                         dir_length--;
    1983             :                 }
    1984           0 :                 *ptr++ = '\'';
    1985             :         }
    1986             : 
    1987           0 :         *ptr++ = ' ';
    1988           0 :         *ptr++ = ';';
    1989           0 :         *ptr++ = ' ';
    1990             : 
    1991           0 :         memcpy(ptr, command, command_length+1);
    1992           0 :         retval = popen(command_line, type);
    1993             : 
    1994           0 :         efree(command_line);
    1995           0 :         return retval;
    1996             : }
    1997             : /* }}} */
    1998             : #endif
    1999             : 
    2000      110966 : CWD_API char *tsrm_realpath(const char *path, char *real_path) /* {{{ */
    2001             : {
    2002             :         cwd_state new_state;
    2003             :         char cwd[MAXPATHLEN];
    2004             : 
    2005             :         /* realpath("") returns CWD */
    2006      110966 :         if (!*path) {
    2007           5 :                 new_state.cwd = (char*)emalloc(1);
    2008           5 :                 if (new_state.cwd == NULL) {
    2009           0 :                         return NULL;
    2010             :                 }
    2011           5 :                 new_state.cwd[0] = '\0';
    2012           5 :                 new_state.cwd_length = 0;
    2013           5 :                 if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
    2014           5 :                         path = cwd;
    2015             :                 }
    2016      124759 :         } else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
    2017        6899 :                                         VCWD_GETCWD(cwd, MAXPATHLEN)) {
    2018        6899 :                 new_state.cwd = estrdup(cwd);
    2019        6899 :                 new_state.cwd_length = (int)strlen(cwd);
    2020             :         } else {
    2021      104062 :                 new_state.cwd = (char*)emalloc(1);
    2022      104062 :                 if (new_state.cwd == NULL) {
    2023           0 :                         return NULL;
    2024             :                 }
    2025      104062 :                 new_state.cwd[0] = '\0';
    2026      104062 :                 new_state.cwd_length = 0;
    2027             :         }
    2028             : 
    2029      110966 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
    2030       13665 :                 efree(new_state.cwd);
    2031       13665 :                 return NULL;
    2032             :         }
    2033             : 
    2034       97301 :         if (real_path) {
    2035       97082 :                 int copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
    2036       97082 :                 memcpy(real_path, new_state.cwd, copy_len);
    2037       97082 :                 real_path[copy_len] = '\0';
    2038       97082 :                 efree(new_state.cwd);
    2039       97082 :                 return real_path;
    2040             :         } else {
    2041         219 :                 return new_state.cwd;
    2042             :         }
    2043             : }
    2044             : /* }}} */
    2045             : 
    2046             : /*
    2047             :  * Local variables:
    2048             :  * tab-width: 4
    2049             :  * c-basic-offset: 4
    2050             :  * End:
    2051             :  */

Generated by: LCOV version 1.10

Generated at Tue, 26 Jul 2016 17:07:32 +0000 (4 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.