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: 263 556 47.3 %
Date: 2016-06-25 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             :         HINSTANCE kernel32;
     222             :         HANDLE hFile;
     223             :         DWORD dwRet;
     224             : 
     225             :         typedef BOOL (WINAPI *gfpnh_func)(HANDLE, LPTSTR, DWORD, DWORD);
     226             :         gfpnh_func pGetFinalPathNameByHandle;
     227             : 
     228             :         if (!target_len) {
     229             :                 return -1;
     230             :         }
     231             : 
     232             :         kernel32 = LoadLibrary("kernel32.dll");
     233             : 
     234             :         if (kernel32) {
     235             :                 pGetFinalPathNameByHandle = (gfpnh_func)GetProcAddress(kernel32, "GetFinalPathNameByHandleA");
     236             :                 if (pGetFinalPathNameByHandle == NULL) {
     237             :                         return -1;
     238             :                 }
     239             :         } else {
     240             :                 return -1;
     241             :         }
     242             : 
     243             :         hFile = CreateFile(link,            // file to open
     244             :                                  GENERIC_READ,          // open for reading
     245             :                                  FILE_SHARE_READ,       // share for reading
     246             :                                  NULL,                  // default security
     247             :                                  OPEN_EXISTING,         // existing file only
     248             :                                  FILE_FLAG_BACKUP_SEMANTICS, // normal file
     249             :                                  NULL);                 // no attr. template
     250             : 
     251             :         if( hFile == INVALID_HANDLE_VALUE) {
     252             :                         return -1;
     253             :         }
     254             : 
     255             :         /* Despite MSDN has documented it won't to, the length returned by
     256             :                 GetFinalPathNameByHandleA includes the length of the
     257             :                 null terminator. This behavior is at least reproducible
     258             :                 with VS2012 and earlier, and seems not to be fixed till
     259             :                 now. Thus, correcting target_len so it's suddenly don't
     260             :                 overflown. */
     261             :         dwRet = pGetFinalPathNameByHandle(hFile, target, target_len - 1, VOLUME_NAME_DOS);
     262             :         if(dwRet >= target_len || dwRet >= MAXPATHLEN || dwRet == 0) {
     263             :                 return -1;
     264             :         }
     265             : 
     266             :         CloseHandle(hFile);
     267             : 
     268             :         if(dwRet > 4) {
     269             :                 /* Skip first 4 characters if they are "\??\" */
     270             :                 if(target[0] == '\\' && target[1] == '\\' && target[2] == '?' && target[3] ==  '\\') {
     271             :                         char tmp[MAXPATHLEN];
     272             :                         unsigned int offset = 4;
     273             :                         dwRet -= 4;
     274             : 
     275             :                         /* \??\UNC\ */
     276             :                         if (dwRet > 7 && target[4] == 'U' && target[5] == 'N' && target[6] == 'C') {
     277             :                                 offset += 2;
     278             :                                 dwRet -= 2;
     279             :                                 target[offset] = '\\';
     280             :                         }
     281             : 
     282             :                         memcpy(tmp, target + offset, dwRet);
     283             :                         memcpy(target, tmp, dwRet);
     284             :                 }
     285             :         }
     286             : 
     287             :         target[dwRet] = '\0';
     288             :         return dwRet;
     289             : }
     290             : /* }}} */
     291             : 
     292             : CWD_API int php_sys_stat_ex(const char *path, zend_stat_t *buf, int lstat) /* {{{ */
     293             : {
     294             :         WIN32_FILE_ATTRIBUTE_DATA data;
     295             :         LARGE_INTEGER t;
     296             :         const size_t path_len = strlen(path);
     297             :         ALLOCA_FLAG(use_heap_large);
     298             : 
     299             :         if (!GetFileAttributesEx(path, GetFileExInfoStandard, &data)) {
     300             :                 return zend_stat(path, buf);
     301             :         }
     302             : 
     303             :         if (path_len >= 1 && path[1] == ':') {
     304             :                 if (path[0] >= 'A' && path[0] <= 'Z') {
     305             :                         buf->st_dev = buf->st_rdev = path[0] - 'A';
     306             :                 } else {
     307             :                         buf->st_dev = buf->st_rdev = path[0] - 'a';
     308             :                 }
     309             :         } else if (IS_UNC_PATH(path, path_len)) {
     310             :                 buf->st_dev = buf->st_rdev = 0;
     311             :         } else {
     312             :                 char  cur_path[MAXPATHLEN+1];
     313             :                 DWORD len = sizeof(cur_path);
     314             :                 char *tmp = cur_path;
     315             : 
     316             :                 while(1) {
     317             :                         DWORD r = GetCurrentDirectory(len, tmp);
     318             :                         if (r < len) {
     319             :                                 if (tmp[1] == ':') {
     320             :                                         if (path[0] >= 'A' && path[0] <= 'Z') {
     321             :                                                 buf->st_dev = buf->st_rdev = path[0] - 'A';
     322             :                                         } else {
     323             :                                                 buf->st_dev = buf->st_rdev = path[0] - 'a';
     324             :                                         }
     325             :                                 } else {
     326             :                                         buf->st_dev = buf->st_rdev = -1;
     327             :                                 }
     328             :                                 break;
     329             :                         } else if (!r) {
     330             :                                 buf->st_dev = buf->st_rdev = -1;
     331             :                                 break;
     332             :                         } else {
     333             :                                 len = r+1;
     334             :                                 tmp = (char*)malloc(len);
     335             :                         }
     336             :                 }
     337             :                 if (tmp != cur_path) {
     338             :                         free(tmp);
     339             :                 }
     340             :         }
     341             : 
     342             :         buf->st_uid = buf->st_gid = buf->st_ino = 0;
     343             : 
     344             :         if (lstat && data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
     345             :                 /* File is a reparse point. Get the target */
     346             :                 HANDLE hLink = NULL;
     347             :                 REPARSE_DATA_BUFFER * pbuffer;
     348             :                 DWORD retlength = 0;
     349             : 
     350             :                 hLink = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL);
     351             :                 if(hLink == INVALID_HANDLE_VALUE) {
     352             :                         return -1;
     353             :                 }
     354             : 
     355             :                 pbuffer = (REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
     356             :                 if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer,  MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
     357             :                         free_alloca(pbuffer, use_heap_large);
     358             :                         CloseHandle(hLink);
     359             :                         return -1;
     360             :                 }
     361             : 
     362             :                 CloseHandle(hLink);
     363             : 
     364             :                 if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
     365             :                         buf->st_mode = S_IFLNK;
     366             :                         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));
     367             :                 }
     368             : 
     369             : #if 0 /* Not used yet */
     370             :                 else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
     371             :                         buf->st_mode |=;
     372             :                 }
     373             : #endif
     374             :                 free_alloca(pbuffer, use_heap_large);
     375             :         } else {
     376             :                 buf->st_mode = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? (S_IFDIR|S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6)) : S_IFREG;
     377             :                 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));
     378             :         }
     379             : 
     380             :         if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
     381             :                 size_t len = strlen(path);
     382             : 
     383             :                 if (path[len-4] == '.') {
     384             :                         if (_memicmp(path+len-3, "exe", 3) == 0 ||
     385             :                                 _memicmp(path+len-3, "com", 3) == 0 ||
     386             :                                 _memicmp(path+len-3, "bat", 3) == 0 ||
     387             :                                 _memicmp(path+len-3, "cmd", 3) == 0) {
     388             :                                 buf->st_mode  |= (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
     389             :                         }
     390             :                 }
     391             :         }
     392             : 
     393             :         buf->st_nlink = 1;
     394             :         t.HighPart = data.nFileSizeHigh;
     395             :         t.LowPart = data.nFileSizeLow;
     396             :         /* It's an overflow on 32 bit, however it won't fix as long
     397             :         as zend_long is 32 bit. */
     398             :         buf->st_size = (zend_long)t.QuadPart;
     399             :         buf->st_atime = FileTimeToUnixTime(&data.ftLastAccessTime);
     400             :         buf->st_ctime = FileTimeToUnixTime(&data.ftCreationTime);
     401             :         buf->st_mtime = FileTimeToUnixTime(&data.ftLastWriteTime);
     402             :         return 0;
     403             : }
     404             : /* }}} */
     405             : #endif
     406             : 
     407           0 : static int php_is_dir_ok(const cwd_state *state)  /* {{{ */
     408             : {
     409             :         zend_stat_t buf;
     410             : 
     411           0 :         if (php_sys_stat(state->cwd, &buf) == 0 && S_ISDIR(buf.st_mode))
     412           0 :                 return (0);
     413             : 
     414           0 :         return (1);
     415             : }
     416             : /* }}} */
     417             : 
     418           0 : static int php_is_file_ok(const cwd_state *state)  /* {{{ */
     419             : {
     420             :         zend_stat_t buf;
     421             : 
     422           0 :         if (php_sys_stat(state->cwd, &buf) == 0 && S_ISREG(buf.st_mode))
     423           0 :                 return (0);
     424             : 
     425           0 :         return (1);
     426             : }
     427             : /* }}} */
     428             : 
     429       22587 : static void cwd_globals_ctor(virtual_cwd_globals *cwd_g) /* {{{ */
     430             : {
     431       22587 :         CWD_STATE_COPY(&cwd_g->cwd, &main_cwd_state);
     432       22587 :         cwd_g->realpath_cache_size = 0;
     433       22587 :         cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
     434       22587 :         cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL;
     435       22587 :         memset(cwd_g->realpath_cache, 0, sizeof(cwd_g->realpath_cache));
     436       22587 : }
     437             : /* }}} */
     438             : 
     439       22623 : static void cwd_globals_dtor(virtual_cwd_globals *cwd_g) /* {{{ */
     440             : {
     441       22623 :         realpath_cache_clean();
     442       22623 : }
     443             : /* }}} */
     444             : 
     445       22587 : CWD_API void virtual_cwd_startup(void) /* {{{ */
     446             : {
     447             :         char cwd[MAXPATHLEN];
     448             :         char *result;
     449             : 
     450             : #ifdef NETWARE
     451             :         result = getcwdpath(cwd, NULL, 1);
     452             :         if(result)
     453             :         {
     454             :                 char *c=cwd;
     455             :                 while(c = strchr(c, '\\'))
     456             :                 {
     457             :                         *c='/';
     458             :                         ++c;
     459             :                 }
     460             :         }
     461             : #else
     462             : #ifdef ZEND_WIN32
     463             :         ZeroMemory(&cwd, sizeof(cwd));
     464             : #endif
     465       22587 :         result = getcwd(cwd, sizeof(cwd));
     466             : #endif
     467       22587 :         if (!result) {
     468           0 :                 cwd[0] = '\0';
     469             :         }
     470             : 
     471       22587 :         main_cwd_state.cwd_length = (int)strlen(cwd);
     472             : #ifdef ZEND_WIN32
     473             :         if (main_cwd_state.cwd_length >= 2 && cwd[1] == ':') {
     474             :                 cwd[0] = toupper(cwd[0]);
     475             :         }
     476             : #endif
     477       22587 :         main_cwd_state.cwd = strdup(cwd);
     478             : 
     479             : #ifdef ZTS
     480             :         ts_allocate_id(&cwd_globals_id, sizeof(virtual_cwd_globals), (ts_allocate_ctor) cwd_globals_ctor, (ts_allocate_dtor) cwd_globals_dtor);
     481             : #else
     482       22587 :         cwd_globals_ctor(&cwd_globals);
     483             : #endif
     484             : 
     485             : #if (defined(ZEND_WIN32) || defined(NETWARE)) && defined(ZTS)
     486             :         cwd_mutex = tsrm_mutex_alloc();
     487             : #endif
     488       22587 : }
     489             : /* }}} */
     490             : 
     491       22623 : CWD_API void virtual_cwd_shutdown(void) /* {{{ */
     492             : {
     493             : #ifndef ZTS
     494       22623 :         cwd_globals_dtor(&cwd_globals);
     495             : #endif
     496             : #if (defined(ZEND_WIN32) || defined(NETWARE)) && defined(ZTS)
     497             :         tsrm_mutex_free(cwd_mutex);
     498             : #endif
     499             : 
     500       22623 :         free(main_cwd_state.cwd); /* Don't use CWD_STATE_FREE because the non global states will probably use emalloc()/efree() */
     501       22623 : }
     502             : /* }}} */
     503             : 
     504       22587 : CWD_API int virtual_cwd_activate(void) /* {{{ */
     505             : {
     506       22587 :         if (CWDG(cwd).cwd == NULL) {
     507       22587 :                 CWD_STATE_COPY(&CWDG(cwd), &main_cwd_state);
     508             :         }
     509       22587 :         return 0;
     510             : }
     511             : /* }}} */
     512             : 
     513       67789 : CWD_API int virtual_cwd_deactivate(void) /* {{{ */
     514             : {
     515       67789 :         if (CWDG(cwd).cwd != NULL) {
     516       45210 :                 CWD_STATE_FREE(&CWDG(cwd));
     517       45210 :                 CWDG(cwd).cwd = NULL;
     518             :         }
     519       67789 :         return 0;
     520             : }
     521             : /* }}} */
     522             : 
     523           0 : CWD_API char *virtual_getcwd_ex(size_t *length) /* {{{ */
     524             : {
     525             :         cwd_state *state;
     526             : 
     527           0 :         state = &CWDG(cwd);
     528             : 
     529           0 :         if (state->cwd_length == 0) {
     530             :                 char *retval;
     531             : 
     532           0 :                 *length = 1;
     533           0 :                 retval = (char *) emalloc(2);
     534           0 :                 if (retval == NULL) {
     535           0 :                         return NULL;
     536             :                 }
     537           0 :                 retval[0] = DEFAULT_SLASH;
     538           0 :                 retval[1] = '\0';
     539           0 :                 return retval;
     540             :         }
     541             : 
     542             : #ifdef ZEND_WIN32
     543             :         /* If we have something like C: */
     544             :         if (state->cwd_length == 2 && state->cwd[state->cwd_length-1] == ':') {
     545             :                 char *retval;
     546             : 
     547             :                 *length = state->cwd_length+1;
     548             :                 retval = (char *) emalloc(*length+1);
     549             :                 if (retval == NULL) {
     550             :                         return NULL;
     551             :                 }
     552             :                 memcpy(retval, state->cwd, *length);
     553             :                 retval[0] = toupper(retval[0]);
     554             :                 retval[*length-1] = DEFAULT_SLASH;
     555             :                 retval[*length] = '\0';
     556             :                 return retval;
     557             :         }
     558             : #endif
     559           0 :         if (!state->cwd) {
     560           0 :                 *length = 0;
     561           0 :                 return NULL;
     562             :         }
     563             : 
     564           0 :         *length = state->cwd_length;
     565           0 :         return estrdup(state->cwd);
     566             : }
     567             : /* }}} */
     568             : 
     569             : /* Same semantics as UNIX getcwd() */
     570           0 : CWD_API char *virtual_getcwd(char *buf, size_t size) /* {{{ */
     571             : {
     572             :         size_t length;
     573             :         char *cwd;
     574             : 
     575           0 :         cwd = virtual_getcwd_ex(&length);
     576             : 
     577           0 :         if (buf == NULL) {
     578           0 :                 return cwd;
     579             :         }
     580           0 :         if (length > size-1) {
     581           0 :                 efree(cwd);
     582           0 :                 errno = ERANGE; /* Is this OK? */
     583           0 :                 return NULL;
     584             :         }
     585           0 :         if (!cwd) {
     586           0 :                 return NULL;
     587             :         }
     588           0 :         memcpy(buf, cwd, length+1);
     589           0 :         efree(cwd);
     590           0 :         return buf;
     591             : }
     592             : /* }}} */
     593             : 
     594             : #ifdef ZEND_WIN32
     595             : static inline zend_ulong realpath_cache_key(const char *path, int path_len) /* {{{ */
     596             : {
     597             :         register zend_ulong h;
     598             :         char *bucket_key_start = tsrm_win32_get_path_sid_key(path);
     599             :         char *bucket_key = (char *)bucket_key_start;
     600             :         const char *e = bucket_key + strlen(bucket_key);
     601             : 
     602             :         if (!bucket_key) {
     603             :                 return 0;
     604             :         }
     605             : 
     606             :         for (h = Z_UL(2166136261); bucket_key < e;) {
     607             :                 h *= Z_UL(16777619);
     608             :                 h ^= *bucket_key++;
     609             :         }
     610             :         HeapFree(GetProcessHeap(), 0, (LPVOID)bucket_key_start);
     611             :         return h;
     612             : }
     613             : /* }}} */
     614             : #else
     615      929300 : static inline zend_ulong realpath_cache_key(const char *path, int path_len) /* {{{ */
     616             : {
     617             :         register zend_ulong h;
     618      929300 :         const char *e = path + path_len;
     619             : 
     620    33853998 :         for (h = Z_UL(2166136261); path < e;) {
     621    31995398 :                 h *= Z_UL(16777619);
     622    31995398 :                 h ^= *path++;
     623             :         }
     624             : 
     625      929300 :         return h;
     626             : }
     627             : /* }}} */
     628             : #endif /* defined(ZEND_WIN32) */
     629             : 
     630       52039 : CWD_API void realpath_cache_clean(void) /* {{{ */
     631             : {
     632             :         uint32_t i;
     633             : 
     634    53339975 :         for (i = 0; i < sizeof(CWDG(realpath_cache))/sizeof(CWDG(realpath_cache)[0]); i++) {
     635    53287936 :                 realpath_cache_bucket *p = CWDG(realpath_cache)[i];
     636   106914483 :                 while (p != NULL) {
     637      338611 :                         realpath_cache_bucket *r = p;
     638      338611 :                         p = p->next;
     639      338611 :                         free(r);
     640             :                 }
     641    53287936 :                 CWDG(realpath_cache)[i] = NULL;
     642             :         }
     643       52039 :         CWDG(realpath_cache_size) = 0;
     644       52039 : }
     645             : /* }}} */
     646             : 
     647           3 : CWD_API void realpath_cache_del(const char *path, int path_len) /* {{{ */
     648             : {
     649           3 :         zend_ulong key = realpath_cache_key(path, path_len);
     650           3 :         zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     651           3 :         realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
     652             : 
     653           6 :         while (*bucket != NULL) {
     654           4 :                 if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
     655           2 :                                         memcmp(path, (*bucket)->path, path_len) == 0) {
     656           2 :                         realpath_cache_bucket *r = *bucket;
     657           2 :                         *bucket = (*bucket)->next;
     658             : 
     659             :                         /* if the pointers match then only subtract the length of the path */
     660           2 :                         if(r->path == r->realpath) {
     661           0 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
     662             :                         } else {
     663           2 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
     664             :                         }
     665             : 
     666           2 :                         free(r);
     667           2 :                         return;
     668             :                 } else {
     669           0 :                         bucket = &(*bucket)->next;
     670             :                 }
     671             :         }
     672             : }
     673             : /* }}} */
     674             : 
     675      401220 : static inline void realpath_cache_add(const char *path, int path_len, const char *realpath, int realpath_len, int is_dir, time_t t) /* {{{ */
     676             : {
     677      401220 :         zend_long size = sizeof(realpath_cache_bucket) + path_len + 1;
     678      401220 :         int same = 1;
     679             : 
     680      799733 :         if (realpath_len != path_len ||
     681      398513 :                 memcmp(path, realpath, path_len) != 0) {
     682        2708 :                 size += realpath_len + 1;
     683        2708 :                 same = 0;
     684             :         }
     685             : 
     686      401220 :         if (CWDG(realpath_cache_size) + size <= CWDG(realpath_cache_size_limit)) {
     687      338319 :                 realpath_cache_bucket *bucket = malloc(size);
     688             :                 zend_ulong n;
     689             : 
     690      338319 :                 if (bucket == NULL) {
     691           0 :                         return;
     692             :                 }
     693             : 
     694      338319 :                 bucket->key = realpath_cache_key(path, path_len);
     695      338319 :                 bucket->path = (char*)bucket + sizeof(realpath_cache_bucket);
     696      338319 :                 memcpy(bucket->path, path, path_len+1);
     697      338319 :                 bucket->path_len = path_len;
     698      338319 :                 if (same) {
     699      335611 :                         bucket->realpath = bucket->path;
     700             :                 } else {
     701        2708 :                         bucket->realpath = bucket->path + (path_len + 1);
     702        2708 :                         memcpy(bucket->realpath, realpath, realpath_len+1);
     703             :                 }
     704      338319 :                 bucket->realpath_len = realpath_len;
     705      338319 :                 bucket->is_dir = is_dir;
     706             : #ifdef ZEND_WIN32
     707             :                 bucket->is_rvalid   = 0;
     708             :                 bucket->is_readable = 0;
     709             :                 bucket->is_wvalid   = 0;
     710             :                 bucket->is_writable = 0;
     711             : #endif
     712      338319 :                 bucket->expires = t + CWDG(realpath_cache_ttl);
     713      338319 :                 n = bucket->key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     714      338319 :                 bucket->next = CWDG(realpath_cache)[n];
     715      338319 :                 CWDG(realpath_cache)[n] = bucket;
     716      338319 :                 CWDG(realpath_cache_size) += size;
     717             :         }
     718             : }
     719             : /* }}} */
     720             : 
     721      590978 : static inline realpath_cache_bucket* realpath_cache_find(const char *path, int path_len, time_t t) /* {{{ */
     722             : {
     723      590978 :         zend_ulong key = realpath_cache_key(path, path_len);
     724      590978 :         zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     725      590978 :         realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
     726             : 
     727     1187779 :         while (*bucket != NULL) {
     728      144357 :                 if (CWDG(realpath_cache_ttl) && (*bucket)->expires < t) {
     729          14 :                         realpath_cache_bucket *r = *bucket;
     730          14 :                         *bucket = (*bucket)->next;
     731             : 
     732             :                         /* if the pointers match then only subtract the length of the path */
     733          14 :                         if(r->path == r->realpath) {
     734          14 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
     735             :                         } else {
     736           0 :                                 CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
     737             :                         }
     738          14 :                         free(r);
     739      282849 :                 } else if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
     740      138520 :                                         memcmp(path, (*bucket)->path, path_len) == 0) {
     741      138520 :                         return *bucket;
     742             :                 } else {
     743        5809 :                         bucket = &(*bucket)->next;
     744             :                 }
     745             :         }
     746      452458 :         return NULL;
     747             : }
     748             : /* }}} */
     749             : 
     750           0 : CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, int path_len, time_t t) /* {{{ */
     751             : {
     752           0 :         return realpath_cache_find(path, path_len, t);
     753             : }
     754             : /* }}} */
     755             : 
     756           1 : CWD_API zend_long realpath_cache_size(void)
     757             : {
     758           1 :         return CWDG(realpath_cache_size);
     759             : }
     760             : 
     761           1 : CWD_API zend_long realpath_cache_max_buckets(void)
     762             : {
     763           1 :         return (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     764             : }
     765             : 
     766           1 : CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void)
     767             : {
     768           1 :         return CWDG(realpath_cache);
     769             : }
     770             : 
     771             : 
     772             : #undef LINK_MAX
     773             : #define LINK_MAX 32
     774             : 
     775      840493 : 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) /* {{{ */
     776             : {
     777             :         int i, j, save;
     778      840493 :         int directory = 0;
     779             : #ifdef ZEND_WIN32
     780             :         WIN32_FIND_DATA data;
     781             :         HANDLE hFind;
     782             :         ALLOCA_FLAG(use_heap_large)
     783             : #else
     784             :         zend_stat_t st;
     785             : #endif
     786             :         realpath_cache_bucket *bucket;
     787             :         char *tmp;
     788             :         ALLOCA_FLAG(use_heap)
     789             : 
     790             :         while (1) {
     791      841899 :                 if (len <= start) {
     792          68 :                         if (link_is_dir) {
     793           0 :                                 *link_is_dir = 1;
     794             :                         }
     795          68 :                         return start;
     796             :                 }
     797             : 
     798      841831 :                 i = len;
     799     8693752 :                 while (i > start && !IS_SLASH(path[i-1])) {
     800     7010090 :                         i--;
     801             :                 }
     802             : 
     803     1683144 :                 if (i == len ||
     804      841313 :                         (i == len - 1 && path[i] == '.')) {
     805             :                         /* remove double slashes and '.' */
     806        1406 :                         len = i - 1;
     807        1406 :                         is_dir = 1;
     808        1406 :                         continue;
     809      840425 :                 } else if (i == len - 2 && path[i] == '.' && path[i+1] == '.') {
     810             :                         /* remove '..' and previous directory */
     811        2129 :                         is_dir = 1;
     812        2129 :                         if (link_is_dir) {
     813           0 :                                 *link_is_dir = 1;
     814             :                         }
     815        2129 :                         if (i - 1 <= start) {
     816           2 :                                 return start ? start : len;
     817             :                         }
     818        2127 :                         j = tsrm_realpath_r(path, start, i-1, ll, t, use_realpath, 1, NULL);
     819        2127 :                         if (j > start) {
     820        2127 :                                 j--;
     821       15014 :                                 while (j > start && !IS_SLASH(path[j])) {
     822       10760 :                                         j--;
     823             :                                 }
     824        2127 :                                 if (!start) {
     825             :                                         /* leading '..' must not be removed in case of relative path */
     826           0 :                                         if (j == 0 && path[0] == '.' && path[1] == '.' &&
     827           0 :                                                         IS_SLASH(path[2])) {
     828           0 :                                                 path[3] = '.';
     829           0 :                                                 path[4] = '.';
     830           0 :                                                 path[5] = DEFAULT_SLASH;
     831           0 :                                                 j = 5;
     832           0 :                                         } else if (j > 0 &&
     833           0 :                                                         path[j+1] == '.' && path[j+2] == '.' &&
     834           0 :                                                         IS_SLASH(path[j+3])) {
     835           0 :                                                 j += 4;
     836           0 :                                                 path[j++] = '.';
     837           0 :                                                 path[j++] = '.';
     838           0 :                                                 path[j] = DEFAULT_SLASH;
     839             :                                         }
     840             :                                 }
     841           0 :                         } else if (!start && !j) {
     842             :                                 /* leading '..' must not be removed in case of relative path */
     843           0 :                                 path[0] = '.';
     844           0 :                                 path[1] = '.';
     845           0 :                                 path[2] = DEFAULT_SLASH;
     846           0 :                                 j = 2;
     847             :                         }
     848        2127 :                         return j;
     849             :                 }
     850             : 
     851      838296 :                 path[len] = 0;
     852             : 
     853      838296 :                 save = (use_realpath != CWD_EXPAND);
     854             : 
     855      838296 :                 if (start && save && CWDG(realpath_cache_size_limit)) {
     856             :                         /* cache lookup for absolute path */
     857      590978 :                         if (!*t) {
     858      196444 :                                 *t = time(0);
     859             :                         }
     860      590978 :                         if ((bucket = realpath_cache_find(path, len, *t)) != NULL) {
     861      138520 :                                 if (is_dir && !bucket->is_dir) {
     862             :                                         /* not a directory */
     863           3 :                                         return -1;
     864             :                                 } else {
     865      138517 :                                         if (link_is_dir) {
     866          88 :                                                 *link_is_dir = bucket->is_dir;
     867             :                                         }
     868      138517 :                                         memcpy(path, bucket->realpath, bucket->realpath_len + 1);
     869      138517 :                                         return bucket->realpath_len;
     870             :                                 }
     871             :                         }
     872             :                 }
     873             : 
     874             : #ifdef ZEND_WIN32
     875             :                 if (save && (hFind = FindFirstFile(path, &data)) == INVALID_HANDLE_VALUE) {
     876             :                         if (use_realpath == CWD_REALPATH) {
     877             :                                 /* file not found */
     878             :                                 return -1;
     879             :                         }
     880             :                         /* continue resolution anyway but don't save result in the cache */
     881             :                         save = 0;
     882             :                 }
     883             : 
     884             :                 if (save) {
     885             :                         FindClose(hFind);
     886             :                 }
     887             : 
     888             :                 tmp = do_alloca(len+1, use_heap);
     889             :                 memcpy(tmp, path, len+1);
     890             : 
     891             :                 if(save &&
     892             :                                 !(IS_UNC_PATH(path, len) && len >= 3 && path[2] != '?') &&
     893             :                                 (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
     894             :                         /* File is a reparse point. Get the target */
     895             :                         HANDLE hLink = NULL;
     896             :                         REPARSE_DATA_BUFFER * pbuffer;
     897             :                         DWORD retlength = 0;
     898             :                         int bufindex = 0, isabsolute = 0;
     899             :                         wchar_t * reparsetarget;
     900             :                         BOOL isVolume = FALSE;
     901             :                         char printname[MAX_PATH];
     902             :                         char substitutename[MAX_PATH];
     903             :                         int printname_len, substitutename_len;
     904             :                         int substitutename_off = 0;
     905             : 
     906             :                         if(++(*ll) > LINK_MAX) {
     907             :                                 return -1;
     908             :                         }
     909             : 
     910             :                         hLink = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL);
     911             :                         if(hLink == INVALID_HANDLE_VALUE) {
     912             :                                 return -1;
     913             :                         }
     914             : 
     915             :                         pbuffer = (REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
     916             :                         if (pbuffer == NULL) {
     917             :                                 return -1;
     918             :                         }
     919             :                         if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer,  MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
     920             :                                 free_alloca(pbuffer, use_heap_large);
     921             :                                 CloseHandle(hLink);
     922             :                                 return -1;
     923             :                         }
     924             : 
     925             :                         CloseHandle(hLink);
     926             : 
     927             :                         if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
     928             :                                 reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
     929             :                                 printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
     930             :                                 isabsolute = (pbuffer->SymbolicLinkReparseBuffer.Flags == 0) ? 1 : 0;
     931             :                                 if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
     932             :                                         reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset  / sizeof(WCHAR),
     933             :                                         printname_len + 1,
     934             :                                         printname, MAX_PATH, NULL, NULL
     935             :                                 )) {
     936             :                                         free_alloca(pbuffer, use_heap_large);
     937             :                                         return -1;
     938             :                                 };
     939             :                                 printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
     940             :                                 printname[printname_len] = 0;
     941             : 
     942             :                                 substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
     943             :                                 if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
     944             :                                         reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR),
     945             :                                         substitutename_len + 1,
     946             :                                         substitutename, MAX_PATH, NULL, NULL
     947             :                                 )) {
     948             :                                         free_alloca(pbuffer, use_heap_large);
     949             :                                         return -1;
     950             :                                 };
     951             :                                 substitutename[substitutename_len] = 0;
     952             :                         }
     953             :                         else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
     954             :                                 isabsolute = 1;
     955             :                                 reparsetarget = pbuffer->MountPointReparseBuffer.ReparseTarget;
     956             :                                 printname_len = pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR);
     957             :                                 if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
     958             :                                         reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset  / sizeof(WCHAR),
     959             :                                         printname_len + 1,
     960             :                                         printname, MAX_PATH, NULL, NULL
     961             :                                 )) {
     962             :                                         free_alloca(pbuffer, use_heap_large);
     963             :                                         return -1;
     964             :                                 };
     965             :                                 printname[pbuffer->MountPointReparseBuffer.PrintNameLength / sizeof(WCHAR)] = 0;
     966             : 
     967             :                                 substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
     968             :                                 if (!WideCharToMultiByte(CP_THREAD_ACP, 0,
     969             :                                         reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR),
     970             :                                         substitutename_len + 1,
     971             :                                         substitutename, MAX_PATH, NULL, NULL
     972             :                                 )) {
     973             :                                         free_alloca(pbuffer, use_heap_large);
     974             :                                         return -1;
     975             :                                 };
     976             :                                 substitutename[substitutename_len] = 0;
     977             :                         }
     978             :                         else if (pbuffer->ReparseTag == IO_REPARSE_TAG_DEDUP) {
     979             :                                 isabsolute = 1;
     980             :                                 memcpy(substitutename, path, len + 1);
     981             :                                 substitutename_len = len;
     982             :                         } else {
     983             :                                 /* XXX this might be not the end, restart handling with REPARSE_GUID_DATA_BUFFER should be implemented. */
     984             :                                 free_alloca(pbuffer, use_heap_large);
     985             :                                 return -1;
     986             :                         }
     987             : 
     988             :                         if(isabsolute && substitutename_len > 4) {
     989             :                                 /* Do not resolve volumes (for now). A mounted point can
     990             :                                    target a volume without a drive, it is not certain that
     991             :                                    all IO functions we use in php and its deps support
     992             :                                    path with volume GUID instead of the DOS way, like:
     993             :                                    d:\test\mnt\foo
     994             :                                    \\?\Volume{62d1c3f8-83b9-11de-b108-806e6f6e6963}\foo
     995             :                                 */
     996             :                                 if (strncmp(substitutename, "\\??\\Volume{",11) == 0
     997             :                                         || strncmp(substitutename, "\\\\?\\Volume{",11) == 0
     998             :                                         || strncmp(substitutename, "\\??\\UNC\\", 8) == 0
     999             :                                         ) {
    1000             :                                         isVolume = TRUE;
    1001             :                                         substitutename_off = 0;
    1002             :                                 } else
    1003             :                                         /* do not use the \??\ and \\?\ prefix*/
    1004             :                                         if (strncmp(substitutename, "\\??\\", 4) == 0
    1005             :                                                 || strncmp(substitutename, "\\\\?\\", 4) == 0) {
    1006             :                                         substitutename_off = 4;
    1007             :                                 }
    1008             :                         }
    1009             : 
    1010             :                         if (!isVolume) {
    1011             :                                 char * tmp2 = substitutename + substitutename_off;
    1012             :                                 for(bufindex = 0; bufindex < (substitutename_len - substitutename_off); bufindex++) {
    1013             :                                         *(path + bufindex) = *(tmp2 + bufindex);
    1014             :                                 }
    1015             : 
    1016             :                                 *(path + bufindex) = 0;
    1017             :                                 j = bufindex;
    1018             :                         } else {
    1019             :                                 j = len;
    1020             :                         }
    1021             : 
    1022             : 
    1023             : #if VIRTUAL_CWD_DEBUG
    1024             :                         fprintf(stderr, "reparse: print: %s ", printname);
    1025             :                         fprintf(stderr, "sub: %s ", substitutename);
    1026             :                         fprintf(stderr, "resolved: %s ", path);
    1027             : #endif
    1028             :                         free_alloca(pbuffer, use_heap_large);
    1029             : 
    1030             :                         if(isabsolute == 1) {
    1031             :                                 if (!((j == 3) && (path[1] == ':') && (path[2] == '\\'))) {
    1032             :                                         /* use_realpath is 0 in the call below coz path is absolute*/
    1033             :                                         j = tsrm_realpath_r(path, 0, j, ll, t, 0, is_dir, &directory);
    1034             :                                         if(j < 0) {
    1035             :                                                 free_alloca(tmp, use_heap);
    1036             :                                                 return -1;
    1037             :                                         }
    1038             :                                 }
    1039             :                         }
    1040             :                         else {
    1041             :                                 if(i + j >= MAXPATHLEN - 1) {
    1042             :                                         free_alloca(tmp, use_heap);
    1043             :                                         return -1;
    1044             :                                 }
    1045             : 
    1046             :                                 memmove(path+i, path, j+1);
    1047             :                                 memcpy(path, tmp, i-1);
    1048             :                                 path[i-1] = DEFAULT_SLASH;
    1049             :                                 j  = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
    1050             :                                 if(j < 0) {
    1051             :                                         free_alloca(tmp, use_heap);
    1052             :                                         return -1;
    1053             :                                 }
    1054             :                         }
    1055             :                         directory = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
    1056             : 
    1057             :                         if(link_is_dir) {
    1058             :                                 *link_is_dir = directory;
    1059             :                         }
    1060             :                 }
    1061             :                 else {
    1062             :                         if (save) {
    1063             :                                 directory = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
    1064             :                                 if (is_dir && !directory) {
    1065             :                                         /* not a directory */
    1066             :                                         return -1;
    1067             :                                 }
    1068             :                         }
    1069             : 
    1070             : #elif defined(NETWARE)
    1071             :                 save = 0;
    1072             :                 tmp = do_alloca(len+1, use_heap);
    1073             :                 memcpy(tmp, path, len+1);
    1074             : #else
    1075      699776 :                 if (save && php_sys_lstat(path, &st) < 0) {
    1076       52087 :                         if (use_realpath == CWD_REALPATH) {
    1077             :                                 /* file not found */
    1078       13485 :                                 return -1;
    1079             :                         }
    1080             :                         /* continue resolution anyway but don't save result in the cache */
    1081       38602 :                         save = 0;
    1082             :                 }
    1083             : 
    1084      686291 :                 tmp = do_alloca(len+1, use_heap);
    1085      686291 :                 memcpy(tmp, path, len+1);
    1086             : 
    1087      686391 :                 if (save && S_ISLNK(st.st_mode)) {
    1088         101 :                         if (++(*ll) > LINK_MAX || (j = php_sys_readlink(tmp, path, MAXPATHLEN)) < 0) {
    1089             :                                 /* too many links or broken symlinks */
    1090           0 :                                 free_alloca(tmp, use_heap);
    1091           0 :                                 return -1;
    1092             :                         }
    1093         101 :                         path[j] = 0;
    1094         101 :                         if (IS_ABSOLUTE_PATH(path, j)) {
    1095          99 :                                 j = tsrm_realpath_r(path, 1, j, ll, t, use_realpath, is_dir, &directory);
    1096          99 :                                 if (j < 0) {
    1097           1 :                                         free_alloca(tmp, use_heap);
    1098           1 :                                         return -1;
    1099             :                                 }
    1100             :                         } else {
    1101           2 :                                 if (i + j >= MAXPATHLEN-1) {
    1102           0 :                                         free_alloca(tmp, use_heap);
    1103           0 :                                         return -1; /* buffer overflow */
    1104             :                                 }
    1105           2 :                                 memmove(path+i, path, j+1);
    1106           2 :                                 memcpy(path, tmp, i-1);
    1107           2 :                                 path[i-1] = DEFAULT_SLASH;
    1108           2 :                                 j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
    1109           2 :                                 if (j < 0) {
    1110           0 :                                         free_alloca(tmp, use_heap);
    1111           0 :                                         return -1;
    1112             :                                 }
    1113             :                         }
    1114         100 :                         if (link_is_dir) {
    1115           1 :                                 *link_is_dir = directory;
    1116             :                         }
    1117             :                 } else {
    1118      686190 :                         if (save) {
    1119      642859 :                                 directory = S_ISDIR(st.st_mode);
    1120      642859 :                                 if (link_is_dir) {
    1121          10 :                                         *link_is_dir = directory;
    1122             :                                 }
    1123      642859 :                                 if (is_dir && !directory) {
    1124             :                                         /* not a directory */
    1125           0 :                                         free_alloca(tmp, use_heap);
    1126           0 :                                         return -1;
    1127             :                                 }
    1128             :                         }
    1129             : #endif
    1130      686190 :                         if (i - 1 <= start) {
    1131       93895 :                                 j = start;
    1132             :                         } else {
    1133             :                                 /* some leading directories may be unaccessable */
    1134      592295 :                                 j = tsrm_realpath_r(path, start, i-1, ll, t, save ? CWD_FILEPATH : use_realpath, 1, NULL);
    1135      592295 :                                 if (j > start) {
    1136      592292 :                                         path[j++] = DEFAULT_SLASH;
    1137             :                                 }
    1138             :                         }
    1139             : #ifdef ZEND_WIN32
    1140             :                         if (j < 0 || j + len - i >= MAXPATHLEN-1) {
    1141             :                                 free_alloca(tmp, use_heap);
    1142             :                                 return -1;
    1143             :                         }
    1144             :                         if (save) {
    1145             :                                 i = (int)strlen(data.cFileName);
    1146             :                                 memcpy(path+j, data.cFileName, i+1);
    1147             :                                 j += i;
    1148             :                         } else {
    1149             :                                 /* use the original file or directory name as it wasn't found */
    1150             :                                 memcpy(path+j, tmp+i, len-i+1);
    1151             :                                 j += (len-i);
    1152             :                         }
    1153             :                 }
    1154             : #else
    1155      686190 :                         if (j < 0 || j + len - i >= MAXPATHLEN-1) {
    1156           1 :                                 free_alloca(tmp, use_heap);
    1157           1 :                                 return -1;
    1158             :                         }
    1159      686189 :                         memcpy(path+j, tmp+i, len-i+1);
    1160      686189 :                         j += (len-i);
    1161             :                 }
    1162             : #endif
    1163             : 
    1164      686289 :                 if (save && start && CWDG(realpath_cache_size_limit)) {
    1165             :                         /* save absolute path in the cache */
    1166      401220 :                         realpath_cache_add(tmp, len, path, j, directory, *t);
    1167             :                 }
    1168             : 
    1169      686289 :                 free_alloca(tmp, use_heap);
    1170      686289 :                 return j;
    1171        1406 :         }
    1172             : }
    1173             : /* }}} */
    1174             : 
    1175             : /* Resolve path relatively to state and put the real path into state */
    1176             : /* returns 0 for ok, 1 for error */
    1177      245974 : CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath) /* {{{ */
    1178             : {
    1179      245974 :         int path_length = (int)strlen(path);
    1180             :         char resolved_path[MAXPATHLEN];
    1181      245974 :         int start = 1;
    1182      245974 :         int ll = 0;
    1183             :         time_t t;
    1184             :         int ret;
    1185             :         int add_slash;
    1186             :         void *tmp;
    1187             : 
    1188      245974 :         if (path_length == 0 || path_length >= MAXPATHLEN-1) {
    1189             : #ifdef ZEND_WIN32
    1190             :                 _set_errno(EINVAL);
    1191             : #else
    1192           4 :                 errno = EINVAL;
    1193             : #endif
    1194           4 :                 return 1;
    1195             :         }
    1196             : 
    1197             : #if VIRTUAL_CWD_DEBUG
    1198             :         fprintf(stderr,"cwd = %s path = %s\n", state->cwd, path);
    1199             : #endif
    1200             : 
    1201             :         /* cwd_length can be 0 when getcwd() fails.
    1202             :          * This can happen under solaris when a dir does not have read permissions
    1203             :          * but *does* have execute permissions */
    1204      245970 :         if (!IS_ABSOLUTE_PATH(path, path_length)) {
    1205        8206 :                 if (state->cwd_length == 0) {
    1206             :                         /* resolve relative path */
    1207          12 :                         start = 0;
    1208          12 :                         memcpy(resolved_path , path, path_length + 1);
    1209             :                 } else {
    1210        8194 :                         int state_cwd_length = state->cwd_length;
    1211             : 
    1212             : #ifdef ZEND_WIN32
    1213             :                         if (IS_SLASH(path[0])) {
    1214             :                                 if (state->cwd[1] == ':') {
    1215             :                                         /* Copy only the drive name */
    1216             :                                         state_cwd_length = 2;
    1217             :                                 } else if (IS_UNC_PATH(state->cwd, state->cwd_length)) {
    1218             :                                         /* Copy only the share name */
    1219             :                                         state_cwd_length = 2;
    1220             :                                         while (IS_SLASH(state->cwd[state_cwd_length])) {
    1221             :                                                 state_cwd_length++;
    1222             :                                         }
    1223             :                                         while (state->cwd[state_cwd_length] &&
    1224             :                                                         !IS_SLASH(state->cwd[state_cwd_length])) {
    1225             :                                                 state_cwd_length++;
    1226             :                                         }
    1227             :                                         while (IS_SLASH(state->cwd[state_cwd_length])) {
    1228             :                                                 state_cwd_length++;
    1229             :                                         }
    1230             :                                         while (state->cwd[state_cwd_length] &&
    1231             :                                                         !IS_SLASH(state->cwd[state_cwd_length])) {
    1232             :                                                 state_cwd_length++;
    1233             :                                         }
    1234             :                                 }
    1235             :                         }
    1236             : #endif
    1237        8194 :                         if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
    1238           0 :                                 return 1;
    1239             :                         }
    1240        8194 :                         memcpy(resolved_path, state->cwd, state_cwd_length);
    1241        8194 :                         if (resolved_path[state_cwd_length-1] == DEFAULT_SLASH) {
    1242          17 :                                 memcpy(resolved_path + state_cwd_length, path, path_length + 1);
    1243          17 :                                 path_length += state_cwd_length;
    1244             :                         } else {
    1245        8177 :                                 resolved_path[state_cwd_length] = DEFAULT_SLASH;
    1246        8177 :                                 memcpy(resolved_path + state_cwd_length + 1, path, path_length + 1);
    1247        8177 :                                 path_length += state_cwd_length + 1;
    1248             :                         }
    1249             :                 }
    1250             :         } else {
    1251             : #ifdef ZEND_WIN32
    1252             :                 if (path_length > 2 && path[1] == ':' && !IS_SLASH(path[2])) {
    1253             :                         resolved_path[0] = path[0];
    1254             :                         resolved_path[1] = ':';
    1255             :                         resolved_path[2] = DEFAULT_SLASH;
    1256             :                         memcpy(resolved_path + 3, path + 2, path_length - 1);
    1257             :                         path_length++;
    1258             :                 } else
    1259             : #endif
    1260      237764 :                 memcpy(resolved_path, path, path_length + 1);
    1261             :         }
    1262             : 
    1263             : #ifdef ZEND_WIN32
    1264             :         if (memchr(resolved_path, '*', path_length) ||
    1265             :                 memchr(resolved_path, '?', path_length)) {
    1266             :                 return 1;
    1267             :         }
    1268             : #endif
    1269             : 
    1270             : #ifdef ZEND_WIN32
    1271             :         if (IS_UNC_PATH(resolved_path, path_length)) {
    1272             :                 /* skip UNC name */
    1273             :                 resolved_path[0] = DEFAULT_SLASH;
    1274             :                 resolved_path[1] = DEFAULT_SLASH;
    1275             :                 start = 2;
    1276             :                 while (!IS_SLASH(resolved_path[start])) {
    1277             :                         if (resolved_path[start] == 0) {
    1278             :                                 goto verify;
    1279             :                         }
    1280             :                         resolved_path[start] = toupper(resolved_path[start]);
    1281             :                         start++;
    1282             :                 }
    1283             :                 resolved_path[start++] = DEFAULT_SLASH;
    1284             :                 while (!IS_SLASH(resolved_path[start])) {
    1285             :                         if (resolved_path[start] == 0) {
    1286             :                                 goto verify;
    1287             :                         }
    1288             :                         resolved_path[start] = toupper(resolved_path[start]);
    1289             :                         start++;
    1290             :                 }
    1291             :                 resolved_path[start++] = DEFAULT_SLASH;
    1292             :         } else if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
    1293             :                 /* skip DRIVE name */
    1294             :                 resolved_path[0] = toupper(resolved_path[0]);
    1295             :                 resolved_path[2] = DEFAULT_SLASH;
    1296             :                 start = 3;
    1297             :         }
    1298             : #elif defined(NETWARE)
    1299             :         if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
    1300             :                 /* skip VOLUME name */
    1301             :                 start = 0;
    1302             :                 while (start != ':') {
    1303             :                         if (resolved_path[start] == 0) return -1;
    1304             :                         start++;
    1305             :                 }
    1306             :                 start++;
    1307             :                 if (!IS_SLASH(resolved_path[start])) return -1;
    1308             :                 resolved_path[start++] = DEFAULT_SLASH;
    1309             :         }
    1310             : #endif
    1311             : 
    1312      245970 :         add_slash = (use_realpath != CWD_REALPATH) && path_length > 0 && IS_SLASH(resolved_path[path_length-1]);
    1313      245970 :         t = CWDG(realpath_cache_ttl) ? 0 : -1;
    1314      245970 :         path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
    1315             : 
    1316      245970 :         if (path_length < 0) {
    1317       13488 :                 errno = ENOENT;
    1318       13488 :                 return 1;
    1319             :         }
    1320             : 
    1321      232482 :         if (!start && !path_length) {
    1322           0 :                 resolved_path[path_length++] = '.';
    1323             :         }
    1324      232482 :         if (add_slash && path_length && !IS_SLASH(resolved_path[path_length-1])) {
    1325         683 :                 if (path_length >= MAXPATHLEN-1) {
    1326           0 :                         return -1;
    1327             :                 }
    1328         683 :                 resolved_path[path_length++] = DEFAULT_SLASH;
    1329             :         }
    1330      232482 :         resolved_path[path_length] = 0;
    1331             : 
    1332             : #ifdef ZEND_WIN32
    1333             : verify:
    1334             : #endif
    1335      232482 :         if (verify_path) {
    1336             :                 cwd_state old_state;
    1337             : 
    1338           0 :                 CWD_STATE_COPY(&old_state, state);
    1339           0 :                 state->cwd_length = path_length;
    1340             : 
    1341           0 :                 tmp = erealloc(state->cwd, state->cwd_length+1);
    1342           0 :                 if (tmp == NULL) {
    1343             : #if VIRTUAL_CWD_DEBUG
    1344             :                         fprintf (stderr, "Out of memory\n");
    1345             : #endif
    1346           0 :                         return 1;
    1347             :                 }
    1348           0 :                 state->cwd = (char *) tmp;
    1349             : 
    1350           0 :                 memcpy(state->cwd, resolved_path, state->cwd_length+1);
    1351           0 :                 if (verify_path(state)) {
    1352           0 :                         CWD_STATE_FREE(state);
    1353           0 :                         *state = old_state;
    1354           0 :                         ret = 1;
    1355             :                 } else {
    1356           0 :                         CWD_STATE_FREE(&old_state);
    1357           0 :                         ret = 0;
    1358             :                 }
    1359             :         } else {
    1360      232482 :                 state->cwd_length = path_length;
    1361      232482 :                 tmp = erealloc(state->cwd, state->cwd_length+1);
    1362      232482 :                 if (tmp == NULL) {
    1363             : #if VIRTUAL_CWD_DEBUG
    1364             :                         fprintf (stderr, "Out of memory\n");
    1365             : #endif
    1366           0 :                         return 1;
    1367             :                 }
    1368      232482 :                 state->cwd = (char *) tmp;
    1369             : 
    1370      232482 :                 memcpy(state->cwd, resolved_path, state->cwd_length+1);
    1371      232482 :                 ret = 0;
    1372             :         }
    1373             : 
    1374             : #if VIRTUAL_CWD_DEBUG
    1375             :         fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
    1376             : #endif
    1377      232482 :         return (ret);
    1378             : }
    1379             : /* }}} */
    1380             : 
    1381           0 : CWD_API int virtual_chdir(const char *path) /* {{{ */
    1382             : {
    1383           0 :         return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH)?-1:0;
    1384             : }
    1385             : /* }}} */
    1386             : 
    1387         243 : CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path)) /* {{{ */
    1388             : {
    1389         243 :         int length = (int)strlen(path);
    1390             :         char *temp;
    1391             :         int retval;
    1392             :         ALLOCA_FLAG(use_heap)
    1393             : 
    1394         243 :         if (length == 0) {
    1395           0 :                 return 1; /* Can't cd to empty string */
    1396             :         }
    1397        4756 :         while(--length >= 0 && !IS_SLASH(path[length])) {
    1398             :         }
    1399             : 
    1400         243 :         if (length == -1) {
    1401             :                 /* No directory only file name */
    1402           0 :                 errno = ENOENT;
    1403           0 :                 return -1;
    1404             :         }
    1405             : 
    1406         243 :         if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
    1407           0 :                 length++;
    1408             :         }
    1409         243 :         temp = (char *) do_alloca(length+1, use_heap);
    1410         243 :         memcpy(temp, path, length);
    1411         243 :         temp[length] = 0;
    1412             : #if VIRTUAL_CWD_DEBUG
    1413             :         fprintf (stderr, "Changing directory to %s\n", temp);
    1414             : #endif
    1415         243 :         retval = p_chdir(temp);
    1416         243 :         free_alloca(temp, use_heap);
    1417         243 :         return retval;
    1418             : }
    1419             : /* }}} */
    1420             : 
    1421           0 : CWD_API char *virtual_realpath(const char *path, char *real_path) /* {{{ */
    1422             : {
    1423             :         cwd_state new_state;
    1424             :         char *retval;
    1425             :         char cwd[MAXPATHLEN];
    1426             : 
    1427             :         /* realpath("") returns CWD */
    1428           0 :         if (!*path) {
    1429           0 :                 new_state.cwd = (char*)emalloc(1);
    1430           0 :                 if (new_state.cwd == NULL) {
    1431           0 :                         retval = NULL;
    1432           0 :                         goto end;
    1433             :                 }
    1434           0 :                 new_state.cwd[0] = '\0';
    1435           0 :                 new_state.cwd_length = 0;
    1436           0 :                 if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
    1437           0 :                         path = cwd;
    1438             :                 }
    1439           0 :         } else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
    1440           0 :                 CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1441             :         } else {
    1442           0 :                 new_state.cwd = (char*)emalloc(1);
    1443           0 :                 if (new_state.cwd == NULL) {
    1444           0 :                         retval = NULL;
    1445           0 :                         goto end;
    1446             :                 }
    1447           0 :                 new_state.cwd[0] = '\0';
    1448           0 :                 new_state.cwd_length = 0;
    1449             :         }
    1450             : 
    1451           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)==0) {
    1452           0 :                 int len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
    1453             : 
    1454           0 :                 memcpy(real_path, new_state.cwd, len);
    1455           0 :                 real_path[len] = '\0';
    1456           0 :                 retval = real_path;
    1457             :         } else {
    1458           0 :                 retval = NULL;
    1459             :         }
    1460             : 
    1461           0 :         CWD_STATE_FREE(&new_state);
    1462             : end:
    1463           0 :         return retval;
    1464             : }
    1465             : /* }}} */
    1466             : 
    1467           0 : CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path) /* {{{ */
    1468             : {
    1469             :         cwd_state new_state;
    1470             :         int retval;
    1471             : 
    1472           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1473           0 :         retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH);
    1474             : 
    1475           0 :         *filepath = new_state.cwd;
    1476             : 
    1477           0 :         return retval;
    1478             : 
    1479             : }
    1480             : /* }}} */
    1481             : 
    1482           0 : CWD_API int virtual_filepath(const char *path, char **filepath) /* {{{ */
    1483             : {
    1484           0 :         return virtual_filepath_ex(path, filepath, php_is_file_ok);
    1485             : }
    1486             : /* }}} */
    1487             : 
    1488           0 : CWD_API FILE *virtual_fopen(const char *path, const char *mode) /* {{{ */
    1489             : {
    1490             :         cwd_state new_state;
    1491             :         FILE *f;
    1492             : 
    1493           0 :         if (path[0] == '\0') { /* Fail to open empty path */
    1494           0 :                 return NULL;
    1495             :         }
    1496             : 
    1497           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1498           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
    1499           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1500           0 :                 return NULL;
    1501             :         }
    1502             : 
    1503           0 :         f = fopen(new_state.cwd, mode);
    1504             : 
    1505           0 :         CWD_STATE_FREE_ERR(&new_state);
    1506             : 
    1507           0 :         return f;
    1508             : }
    1509             : /* }}} */
    1510             : 
    1511           0 : CWD_API int virtual_access(const char *pathname, int mode) /* {{{ */
    1512             : {
    1513             :         cwd_state new_state;
    1514             :         int ret;
    1515             : 
    1516           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1517           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
    1518           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1519           0 :                 return -1;
    1520             :         }
    1521             : 
    1522             : #if defined(ZEND_WIN32)
    1523             :         ret = tsrm_win32_access(new_state.cwd, mode);
    1524             : #else
    1525           0 :         ret = access(new_state.cwd, mode);
    1526             : #endif
    1527             : 
    1528           0 :         CWD_STATE_FREE_ERR(&new_state);
    1529             : 
    1530           0 :         return ret;
    1531             : }
    1532             : /* }}} */
    1533             : 
    1534             : #if HAVE_UTIME
    1535           0 : CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */
    1536             : {
    1537             :         cwd_state new_state;
    1538             :         int ret;
    1539             : 
    1540           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1541           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
    1542           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1543           0 :                 return -1;
    1544             :         }
    1545             : 
    1546             : #ifdef ZEND_WIN32
    1547             :         ret = win32_utime(new_state.cwd, buf);
    1548             : #else
    1549           0 :         ret = utime(new_state.cwd, buf);
    1550             : #endif
    1551             : 
    1552           0 :         CWD_STATE_FREE_ERR(&new_state);
    1553           0 :         return ret;
    1554             : }
    1555             : /* }}} */
    1556             : #endif
    1557             : 
    1558           0 : CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
    1559             : {
    1560             :         cwd_state new_state;
    1561             :         int ret;
    1562             : 
    1563           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1564           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
    1565           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1566           0 :                 return -1;
    1567             :         }
    1568             : 
    1569             : #ifdef ZEND_WIN32
    1570             :         {
    1571             :                 mode_t _tmp = mode;
    1572             : 
    1573             :                 mode = 0;
    1574             : 
    1575             :                 if (_tmp & _S_IREAD) {
    1576             :                         mode |= _S_IREAD;
    1577             :                 }
    1578             :                 if (_tmp & _S_IWRITE) {
    1579             :                         mode |= _S_IWRITE;
    1580             :                 }
    1581             :         }
    1582             : #endif
    1583           0 :         ret = chmod(new_state.cwd, mode);
    1584             : 
    1585           0 :         CWD_STATE_FREE_ERR(&new_state);
    1586           0 :         return ret;
    1587             : }
    1588             : /* }}} */
    1589             : 
    1590             : #if !defined(ZEND_WIN32) && !defined(NETWARE)
    1591           0 : CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link) /* {{{ */
    1592             : {
    1593             :         cwd_state new_state;
    1594             :         int ret;
    1595             : 
    1596           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1597           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
    1598           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1599           0 :                 return -1;
    1600             :         }
    1601             : 
    1602           0 :         if (link) {
    1603             : #if HAVE_LCHOWN
    1604           0 :                 ret = lchown(new_state.cwd, owner, group);
    1605             : #else
    1606             :                 ret = -1;
    1607             : #endif
    1608             :         } else {
    1609           0 :                 ret = chown(new_state.cwd, owner, group);
    1610             :         }
    1611             : 
    1612           0 :         CWD_STATE_FREE_ERR(&new_state);
    1613           0 :         return ret;
    1614             : }
    1615             : /* }}} */
    1616             : #endif
    1617             : 
    1618           0 : CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */
    1619             : {
    1620             :         cwd_state new_state;
    1621             :         int f;
    1622             : 
    1623           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1624           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
    1625           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1626           0 :                 return -1;
    1627             :         }
    1628             : 
    1629           0 :         if (flags & O_CREAT) {
    1630             :                 mode_t mode;
    1631             :                 va_list arg;
    1632             : 
    1633           0 :                 va_start(arg, flags);
    1634           0 :                 mode = (mode_t) va_arg(arg, int);
    1635           0 :                 va_end(arg);
    1636             : 
    1637           0 :                 f = open(new_state.cwd, flags, mode);
    1638             :         } else {
    1639           0 :                 f = open(new_state.cwd, flags);
    1640             :         }
    1641           0 :         CWD_STATE_FREE_ERR(&new_state);
    1642           0 :         return f;
    1643             : }
    1644             : /* }}} */
    1645             : 
    1646           0 : CWD_API int virtual_creat(const char *path, mode_t mode) /* {{{ */
    1647             : {
    1648             :         cwd_state new_state;
    1649             :         int f;
    1650             : 
    1651           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1652           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
    1653           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1654           0 :                 return -1;
    1655             :         }
    1656             : 
    1657           0 :         f = creat(new_state.cwd,  mode);
    1658             : 
    1659           0 :         CWD_STATE_FREE_ERR(&new_state);
    1660           0 :         return f;
    1661             : }
    1662             : /* }}} */
    1663             : 
    1664           0 : CWD_API int virtual_rename(const char *oldname, const char *newname) /* {{{ */
    1665             : {
    1666             :         cwd_state old_state;
    1667             :         cwd_state new_state;
    1668             :         int retval;
    1669             : 
    1670           0 :         CWD_STATE_COPY(&old_state, &CWDG(cwd));
    1671           0 :         if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND)) {
    1672           0 :                 CWD_STATE_FREE_ERR(&old_state);
    1673           0 :                 return -1;
    1674             :         }
    1675           0 :         oldname = old_state.cwd;
    1676             : 
    1677           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1678           0 :         if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND)) {
    1679           0 :                 CWD_STATE_FREE_ERR(&old_state);
    1680           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1681           0 :                 return -1;
    1682             :         }
    1683           0 :         newname = new_state.cwd;
    1684             : 
    1685             :         /* rename on windows will fail if newname already exists.
    1686             :            MoveFileEx has to be used */
    1687             : #ifdef ZEND_WIN32
    1688             :         /* MoveFileEx returns 0 on failure, other way 'round for this function */
    1689             :         retval = (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED) == 0) ? -1 : 0;
    1690             : #else
    1691           0 :         retval = rename(oldname, newname);
    1692             : #endif
    1693             : 
    1694           0 :         CWD_STATE_FREE_ERR(&old_state);
    1695           0 :         CWD_STATE_FREE_ERR(&new_state);
    1696             : 
    1697           0 :         return retval;
    1698             : }
    1699             : /* }}} */
    1700             : 
    1701           0 : CWD_API int virtual_stat(const char *path, zend_stat_t *buf) /* {{{ */
    1702             : {
    1703             :         cwd_state new_state;
    1704             :         int retval;
    1705             : 
    1706           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1707           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
    1708           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1709           0 :                 return -1;
    1710             :         }
    1711             : 
    1712           0 :         retval = php_sys_stat(new_state.cwd, buf);
    1713             : 
    1714           0 :         CWD_STATE_FREE_ERR(&new_state);
    1715           0 :         return retval;
    1716             : }
    1717             : /* }}} */
    1718             : 
    1719           0 : CWD_API int virtual_lstat(const char *path, zend_stat_t *buf) /* {{{ */
    1720             : {
    1721             :         cwd_state new_state;
    1722             :         int retval;
    1723             : 
    1724           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1725           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
    1726           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1727           0 :                 return -1;
    1728             :         }
    1729             : 
    1730           0 :         retval = php_sys_lstat(new_state.cwd, buf);
    1731             : 
    1732           0 :         CWD_STATE_FREE_ERR(&new_state);
    1733           0 :         return retval;
    1734             : }
    1735             : /* }}} */
    1736             : 
    1737           0 : CWD_API int virtual_unlink(const char *path) /* {{{ */
    1738             : {
    1739             :         cwd_state new_state;
    1740             :         int retval;
    1741             : 
    1742           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1743           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
    1744           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1745           0 :                 return -1;
    1746             :         }
    1747             : 
    1748           0 :         retval = unlink(new_state.cwd);
    1749             : 
    1750           0 :         CWD_STATE_FREE_ERR(&new_state);
    1751           0 :         return retval;
    1752             : }
    1753             : /* }}} */
    1754             : 
    1755           0 : CWD_API int virtual_mkdir(const char *pathname, mode_t mode) /* {{{ */
    1756             : {
    1757             :         cwd_state new_state;
    1758             :         int retval;
    1759             : 
    1760           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1761           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH)) {
    1762           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1763           0 :                 return -1;
    1764             :         }
    1765             : 
    1766             : #ifdef ZEND_WIN32
    1767             :         retval = mkdir(new_state.cwd);
    1768             : #else
    1769           0 :         retval = mkdir(new_state.cwd, mode);
    1770             : #endif
    1771           0 :         CWD_STATE_FREE_ERR(&new_state);
    1772           0 :         return retval;
    1773             : }
    1774             : /* }}} */
    1775             : 
    1776           0 : CWD_API int virtual_rmdir(const char *pathname) /* {{{ */
    1777             : {
    1778             :         cwd_state new_state;
    1779             :         int retval;
    1780             : 
    1781           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1782           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND)) {
    1783           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1784           0 :                 return -1;
    1785             :         }
    1786             : 
    1787           0 :         retval = rmdir(new_state.cwd);
    1788             : 
    1789           0 :         CWD_STATE_FREE_ERR(&new_state);
    1790           0 :         return retval;
    1791             : }
    1792             : /* }}} */
    1793             : 
    1794             : #ifdef ZEND_WIN32
    1795             : DIR *opendir(const char *name);
    1796             : #endif
    1797             : 
    1798           0 : CWD_API DIR *virtual_opendir(const char *pathname) /* {{{ */
    1799             : {
    1800             :         cwd_state new_state;
    1801             :         DIR *retval;
    1802             : 
    1803           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1804           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
    1805           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1806           0 :                 return NULL;
    1807             :         }
    1808             : 
    1809           0 :         retval = opendir(new_state.cwd);
    1810             : 
    1811           0 :         CWD_STATE_FREE_ERR(&new_state);
    1812           0 :         return retval;
    1813             : }
    1814             : /* }}} */
    1815             : 
    1816             : #ifdef ZEND_WIN32
    1817             : CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
    1818             : {
    1819             :         return popen_ex(command, type, CWDG(cwd).cwd, NULL);
    1820             : }
    1821             : /* }}} */
    1822             : #elif defined(NETWARE)
    1823             : /* On NetWare, the trick of prepending "cd cwd; " doesn't work so we need to perform
    1824             :    a VCWD_CHDIR() and mutex it
    1825             :  */
    1826             : CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
    1827             : {
    1828             :         char prev_cwd[MAXPATHLEN];
    1829             :         char *getcwd_result;
    1830             :         FILE *retval;
    1831             : 
    1832             :         getcwd_result = VCWD_GETCWD(prev_cwd, MAXPATHLEN);
    1833             :         if (!getcwd_result) {
    1834             :                 return NULL;
    1835             :         }
    1836             : 
    1837             : #ifdef ZTS
    1838             :         tsrm_mutex_lock(cwd_mutex);
    1839             : #endif
    1840             : 
    1841             :         VCWD_CHDIR(CWDG(cwd).cwd);
    1842             :         retval = popen(command, type);
    1843             :         VCWD_CHDIR(prev_cwd);
    1844             : 
    1845             : #ifdef ZTS
    1846             :         tsrm_mutex_unlock(cwd_mutex);
    1847             : #endif
    1848             : 
    1849             :         return retval;
    1850             : }
    1851             : /* }}} */
    1852             : #else /* Unix */
    1853           0 : CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
    1854             : {
    1855             :         int command_length;
    1856           0 :         int dir_length, extra = 0;
    1857             :         char *command_line;
    1858             :         char *ptr, *dir;
    1859             :         FILE *retval;
    1860             : 
    1861           0 :         command_length = strlen(command);
    1862             : 
    1863           0 :         dir_length = CWDG(cwd).cwd_length;
    1864           0 :         dir = CWDG(cwd).cwd;
    1865           0 :         while (dir_length > 0) {
    1866           0 :                 if (*dir == '\'') extra+=3;
    1867           0 :                 dir++;
    1868           0 :                 dir_length--;
    1869             :         }
    1870           0 :         dir_length = CWDG(cwd).cwd_length;
    1871           0 :         dir = CWDG(cwd).cwd;
    1872             : 
    1873           0 :         ptr = command_line = (char *) emalloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
    1874           0 :         if (!command_line) {
    1875           0 :                 return NULL;
    1876             :         }
    1877           0 :         memcpy(ptr, "cd ", sizeof("cd ")-1);
    1878           0 :         ptr += sizeof("cd ")-1;
    1879             : 
    1880           0 :         if (CWDG(cwd).cwd_length == 0) {
    1881           0 :                 *ptr++ = DEFAULT_SLASH;
    1882             :         } else {
    1883           0 :                 *ptr++ = '\'';
    1884           0 :                 while (dir_length > 0) {
    1885           0 :                         switch (*dir) {
    1886             :                         case '\'':
    1887           0 :                                 *ptr++ = '\'';
    1888           0 :                                 *ptr++ = '\\';
    1889           0 :                                 *ptr++ = '\'';
    1890             :                                 /* fall-through */
    1891             :                         default:
    1892           0 :                                 *ptr++ = *dir;
    1893             :                         }
    1894           0 :                         dir++;
    1895           0 :                         dir_length--;
    1896             :                 }
    1897           0 :                 *ptr++ = '\'';
    1898             :         }
    1899             : 
    1900           0 :         *ptr++ = ' ';
    1901           0 :         *ptr++ = ';';
    1902           0 :         *ptr++ = ' ';
    1903             : 
    1904           0 :         memcpy(ptr, command, command_length+1);
    1905           0 :         retval = popen(command_line, type);
    1906             : 
    1907           0 :         efree(command_line);
    1908           0 :         return retval;
    1909             : }
    1910             : /* }}} */
    1911             : #endif
    1912             : 
    1913      107571 : CWD_API char *tsrm_realpath(const char *path, char *real_path) /* {{{ */
    1914             : {
    1915             :         cwd_state new_state;
    1916             :         char cwd[MAXPATHLEN];
    1917             : 
    1918             :         /* realpath("") returns CWD */
    1919      107571 :         if (!*path) {
    1920           5 :                 new_state.cwd = (char*)emalloc(1);
    1921           5 :                 if (new_state.cwd == NULL) {
    1922           0 :                         return NULL;
    1923             :                 }
    1924           5 :                 new_state.cwd[0] = '\0';
    1925           5 :                 new_state.cwd_length = 0;
    1926           5 :                 if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
    1927           5 :                         path = cwd;
    1928             :                 }
    1929      121164 :         } else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
    1930        6799 :                                         VCWD_GETCWD(cwd, MAXPATHLEN)) {
    1931        6799 :                 new_state.cwd = estrdup(cwd);
    1932        6799 :                 new_state.cwd_length = (int)strlen(cwd);
    1933             :         } else {
    1934      100767 :                 new_state.cwd = (char*)emalloc(1);
    1935      100767 :                 if (new_state.cwd == NULL) {
    1936           0 :                         return NULL;
    1937             :                 }
    1938      100767 :                 new_state.cwd[0] = '\0';
    1939      100767 :                 new_state.cwd_length = 0;
    1940             :         }
    1941             : 
    1942      107571 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
    1943       13477 :                 efree(new_state.cwd);
    1944       13477 :                 return NULL;
    1945             :         }
    1946             : 
    1947       94094 :         if (real_path) {
    1948       93876 :                 int copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
    1949       93876 :                 memcpy(real_path, new_state.cwd, copy_len);
    1950       93876 :                 real_path[copy_len] = '\0';
    1951       93876 :                 efree(new_state.cwd);
    1952       93876 :                 return real_path;
    1953             :         } else {
    1954         218 :                 return new_state.cwd;
    1955             :         }
    1956             : }
    1957             : /* }}} */
    1958             : 
    1959             : /*
    1960             :  * Local variables:
    1961             :  * tab-width: 4
    1962             :  * c-basic-offset: 4
    1963             :  * End:
    1964             :  */

Generated by: LCOV version 1.10

Generated at Sat, 25 Jun 2016 07:08:53 +0000 (5 days ago)

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