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

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

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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:03 +0000 (8 days ago)

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