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

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:12 +0000 (33 hours ago)

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