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

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:12 +0000 (3 days ago)

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