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-10-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             :                 unsigned int 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             :                 int 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       20423 : static void cwd_globals_ctor(virtual_cwd_globals *cwd_g TSRMLS_DC) /* {{{ */
     432             : {
     433       20423 :         CWD_STATE_COPY(&cwd_g->cwd, &main_cwd_state);
     434       20423 :         cwd_g->realpath_cache_size = 0;
     435       20423 :         cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
     436       20423 :         cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL;
     437       20423 :         memset(cwd_g->realpath_cache, 0, sizeof(cwd_g->realpath_cache));
     438       20423 : }
     439             : /* }}} */
     440             : 
     441       20457 : static void cwd_globals_dtor(virtual_cwd_globals *cwd_g TSRMLS_DC) /* {{{ */
     442             : {
     443       20457 :         realpath_cache_clean(TSRMLS_C);
     444       20457 : }
     445             : /* }}} */
     446             : 
     447       20423 : 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       20423 :         result = getcwd(cwd, sizeof(cwd));
     465             : #endif
     466       20423 :         if (!result) {
     467           0 :                 cwd[0] = '\0';
     468             :         }
     469             : 
     470       20423 :         main_cwd_state.cwd_length = 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       20423 :         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       20423 :         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       20423 : }
     488             : /* }}} */
     489             : 
     490       20457 : CWD_API void virtual_cwd_shutdown(void) /* {{{ */
     491             : {
     492             : #ifndef ZTS
     493       20457 :         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       20457 :         free(main_cwd_state.cwd); /* Don't use CWD_STATE_FREE because the non global states will probably use emalloc()/efree() */
     500       20457 : }
     501             : /* }}} */
     502             : 
     503       20423 : CWD_API int virtual_cwd_activate(TSRMLS_D) /* {{{ */
     504             : {
     505       20423 :         if (CWDG(cwd).cwd == NULL) {
     506       20423 :                 CWD_STATE_COPY(&CWDG(cwd), &main_cwd_state);
     507             :         }
     508       20423 :         return 0;
     509             : }
     510             : /* }}} */
     511             : 
     512       61296 : CWD_API int virtual_cwd_deactivate(TSRMLS_D) /* {{{ */
     513             : {
     514       61296 :         if (CWDG(cwd).cwd != NULL) {
     515       40880 :                 CWD_STATE_FREE(&CWDG(cwd));
     516       40880 :                 CWDG(cwd).cwd = NULL;
     517             :         }
     518       61296 :         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      859226 : static inline zend_ulong realpath_cache_key(const char *path, int path_len) /* {{{ */
     607             : {
     608             :         register zend_ulong h;
     609      859226 :         const char *e = path + path_len;
     610             : 
     611    31396008 :         for (h = Z_UL(2166136261); path < e;) {
     612    29677556 :                 h *= Z_UL(16777619);
     613    29677556 :                 h ^= *path++;
     614             :         }
     615             : 
     616      859226 :         return h;
     617             : }
     618             : /* }}} */
     619             : #endif /* defined(PHP_WIN32) */
     620             : 
     621       48503 : CWD_API void realpath_cache_clean(TSRMLS_D) /* {{{ */
     622             : {
     623             :         int i;
     624             : 
     625    49715575 :         for (i = 0; i < sizeof(CWDG(realpath_cache))/sizeof(CWDG(realpath_cache)[0]); i++) {
     626    49667072 :                 realpath_cache_bucket *p = CWDG(realpath_cache)[i];
     627    99645969 :                 while (p != NULL) {
     628      311825 :                         realpath_cache_bucket *r = p;
     629      311825 :                         p = p->next;
     630      311825 :                         free(r);
     631             :                 }
     632    49667072 :                 CWDG(realpath_cache)[i] = NULL;
     633             :         }
     634       48503 :         CWDG(realpath_cache_size) = 0;
     635       48503 : }
     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      369392 : 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      369392 :         zend_long size = sizeof(realpath_cache_bucket) + path_len + 1;
     673      369392 :         int same = 1;
     674             : 
     675      736461 :         if (realpath_len != path_len ||
     676      367069 :                 memcmp(path, realpath, path_len) != 0) {
     677        2324 :                 size += realpath_len + 1;
     678        2324 :                 same = 0;
     679             :         }
     680             : 
     681      369392 :         if (CWDG(realpath_cache_size) + size <= CWDG(realpath_cache_size_limit)) {
     682      311560 :                 realpath_cache_bucket *bucket = malloc(size);
     683             :                 zend_ulong n;
     684             : 
     685      311560 :                 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      311560 :                 bucket->key = realpath_cache_key(path, path_len);
     693             : #endif
     694      311560 :                 bucket->path = (char*)bucket + sizeof(realpath_cache_bucket);
     695      311560 :                 memcpy(bucket->path, path, path_len+1);
     696      311560 :                 bucket->path_len = path_len;
     697      311560 :                 if (same) {
     698      309236 :                         bucket->realpath = bucket->path;
     699             :                 } else {
     700        2324 :                         bucket->realpath = bucket->path + (path_len + 1);
     701        2324 :                         memcpy(bucket->realpath, realpath, realpath_len+1);
     702             :                 }
     703      311560 :                 bucket->realpath_len = realpath_len;
     704      311560 :                 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      311560 :                 bucket->expires = t + CWDG(realpath_cache_ttl);
     712      311560 :                 n = bucket->key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     713      311560 :                 bucket->next = CWDG(realpath_cache)[n];
     714      311560 :                 CWDG(realpath_cache)[n] = bucket;
     715      311560 :                 CWDG(realpath_cache_size) += size;
     716             :         }
     717             : }
     718             : /* }}} */
     719             : 
     720      547663 : 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      547663 :         zend_ulong key = realpath_cache_key(path, path_len);
     726             : #endif
     727             : 
     728      547663 :         zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
     729      547663 :         realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
     730             : 
     731     1100939 :         while (*bucket != NULL) {
     732      135138 :                 if (CWDG(realpath_cache_ttl) && (*bucket)->expires < t) {
     733          27 :                         realpath_cache_bucket *r = *bucket;
     734          27 :                         *bucket = (*bucket)->next;
     735             : 
     736             :                         /* if the pointers match then only subtract the length of the path */              
     737          27 :                         if(r->path == r->realpath) {
     738          27 :                                 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          27 :                         free(r);
     743      264582 :                 } else if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
     744      129498 :                                         memcmp(path, (*bucket)->path, path_len) == 0) {
     745      129498 :                         return *bucket;
     746             :                 } else {
     747        5586 :                         bucket = &(*bucket)->next;
     748             :                 }
     749             :         }
     750      418165 :         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      775282 : 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      775282 :         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      776679 :                 if (len <= start) {
     796          68 :                         if (link_is_dir) {
     797           0 :                                 *link_is_dir = 1;
     798             :                         }
     799          68 :                         return start;
     800             :                 }
     801             : 
     802      776611 :                 i = len;
     803     8053904 :                 while (i > start && !IS_SLASH(path[i-1])) {
     804     6500682 :                         i--;
     805             :                 }
     806             : 
     807     1552705 :                 if (i == len ||
     808      776094 :                         (i == len - 1 && path[i] == '.')) {
     809             :                         /* remove double slashes and '.' */
     810        1397 :                         len = i - 1;
     811        1397 :                         is_dir = 1;
     812        1397 :                         continue;
     813      775214 :                 } else if (i == len - 2 && path[i] == '.' && path[i+1] == '.') {
     814             :                         /* remove '..' and previous directory */
     815        1884 :                         is_dir = 1;
     816        1884 :                         if (link_is_dir) {
     817           0 :                                 *link_is_dir = 1;
     818             :                         }
     819        1884 :                         if (i - 1 <= start) {
     820           0 :                                 return start ? start : len;
     821             :                         }
     822        1884 :                         j = tsrm_realpath_r(path, start, i-1, ll, t, use_realpath, 1, NULL TSRMLS_CC);
     823        1884 :                         if (j > start) {
     824        1884 :                                 j--;
     825       13278 :                                 while (j > start && !IS_SLASH(path[j])) {
     826        9510 :                                         j--;
     827             :                                 }
     828        1884 :                                 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        1884 :                         return j;
     853             :                 }
     854             : 
     855      773330 :                 path[len] = 0;
     856             : 
     857      773330 :                 save = (use_realpath != CWD_EXPAND);
     858             : 
     859      773330 :                 if (start && save && CWDG(realpath_cache_size_limit)) {
     860             :                         /* cache lookup for absolute path */
     861      547663 :                         if (!*t) {
     862      183685 :                                 *t = time(0);
     863             :                         }
     864      547663 :                         if ((bucket = realpath_cache_find(path, len, *t TSRMLS_CC)) != NULL) {
     865      129498 :                                 if (is_dir && !bucket->is_dir) {
     866             :                                         /* not a directory */
     867           3 :                                         return -1;
     868             :                                 } else {
     869      129495 :                                         if (link_is_dir) {
     870          88 :                                                 *link_is_dir = bucket->is_dir;
     871             :                                         }
     872      129495 :                                         memcpy(path, bucket->realpath, bucket->realpath_len + 1);
     873      129495 :                                         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             :                         unsigned int 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      643832 :                 if (save && php_sys_lstat(path, &st) < 0) {
    1080       49622 :                         if (use_realpath == CWD_REALPATH) {
    1081             :                                 /* file not found */
    1082       13222 :                                 return -1;
    1083             :                         }
    1084             :                         /* continue resolution anyway but don't save result in the cache */
    1085       36400 :                         save = 0;
    1086             :                 }
    1087             : 
    1088      630610 :                 tmp = do_alloca(len+1, use_heap);
    1089      630610 :                 memcpy(tmp, path, len+1);
    1090             : 
    1091      630710 :                 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      630509 :                         if (save) {
    1123      589419 :                                 directory = S_ISDIR(st.st_mode);
    1124      589419 :                                 if (link_is_dir) {
    1125          10 :                                         *link_is_dir = directory;
    1126             :                                 }
    1127      589419 :                                 if (is_dir && !directory) {
    1128             :                                         /* not a directory */
    1129           0 :                                         free_alloca(tmp, use_heap);
    1130           0 :                                         return -1;
    1131             :                                 }
    1132             :                         }
    1133             : #endif
    1134      630509 :                         if (i - 1 <= start) {
    1135       86085 :                                 j = start;
    1136             :                         } else {
    1137             :                                 /* some leading directories may be unaccessable */
    1138      544424 :                                 j = tsrm_realpath_r(path, start, i-1, ll, t, save ? CWD_FILEPATH : use_realpath, 1, NULL TSRMLS_CC);
    1139      544424 :                                 if (j > start) {
    1140      544422 :                                         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 = 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      630509 :                         if (j < 0 || j + len - i >= MAXPATHLEN-1) {
    1160           1 :                                 free_alloca(tmp, use_heap);
    1161           1 :                                 return -1;
    1162             :                         }
    1163      630508 :                         memcpy(path+j, tmp+i, len-i+1);
    1164      630508 :                         j += (len-i);
    1165             :                 }
    1166             : #endif
    1167             : 
    1168      630608 :                 if (save && start && CWDG(realpath_cache_size_limit)) {
    1169             :                         /* save absolute path in the cache */
    1170      369392 :                         realpath_cache_add(tmp, len, path, j, directory, *t TSRMLS_CC);
    1171             :                 }
    1172             : 
    1173      630608 :                 free_alloca(tmp, use_heap);
    1174      630608 :                 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      228876 : CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath TSRMLS_DC) /* {{{ */
    1182             : {
    1183      228876 :         int path_length = strlen(path);
    1184             :         char resolved_path[MAXPATHLEN];
    1185      228876 :         int start = 1;
    1186      228876 :         int ll = 0;
    1187             :         time_t t;
    1188             :         int ret;
    1189             :         int add_slash;
    1190             :         void *tmp;
    1191             : 
    1192      228876 :         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      228873 :         if (!IS_ABSOLUTE_PATH(path, path_length)) {
    1213        8085 :                 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        8074 :                         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        8074 :                         if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
    1246           0 :                                 return 1;
    1247             :                         }
    1248        8074 :                         memcpy(resolved_path, state->cwd, state_cwd_length);
    1249        8074 :                         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        8074 :                                 resolved_path[state_cwd_length] = DEFAULT_SLASH;
    1254        8074 :                                 memcpy(resolved_path + state_cwd_length + 1, path, path_length + 1);
    1255        8074 :                                 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      220788 :                 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      228873 :         add_slash = (use_realpath != CWD_REALPATH) && path_length > 0 && IS_SLASH(resolved_path[path_length-1]);
    1321      228873 :         t = CWDG(realpath_cache_ttl) ? 0 : -1;
    1322      228873 :         path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL TSRMLS_CC);
    1323             : 
    1324      228873 :         if (path_length < 0) {
    1325       13225 :                 errno = ENOENT;
    1326       13225 :                 return 1;
    1327             :         }
    1328             : 
    1329      215648 :         if (!start && !path_length) {
    1330           0 :                 resolved_path[path_length++] = '.';
    1331             :         }
    1332      215648 :         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      215648 :         resolved_path[path_length] = 0;
    1339             : 
    1340             : #ifdef TSRM_WIN32
    1341             : verify:
    1342             : #endif
    1343      215648 :         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      215648 :                 state->cwd_length = path_length;
    1369      215648 :                 tmp = erealloc(state->cwd, state->cwd_length+1);
    1370      215648 :                 if (tmp == NULL) {
    1371             : #if VIRTUAL_CWD_DEBUG
    1372             :                         fprintf (stderr, "Out of memory\n");
    1373             : #endif
    1374           0 :                         return 1;
    1375             :                 }
    1376      215648 :                 state->cwd = (char *) tmp;
    1377             : 
    1378      215648 :                 memcpy(state->cwd, resolved_path, state->cwd_length+1);
    1379      215648 :                 ret = 0;
    1380             :         }
    1381             : 
    1382             : #if VIRTUAL_CWD_DEBUG
    1383             :         fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
    1384             : #endif
    1385      215648 :         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 = 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             : #ifdef TSRM_WIN32
    1544             : static void UnixTimeToFileTime(time_t t, LPFILETIME pft) /* {{{ */
    1545             : {
    1546             :         // Note that LONGLONG is a 64-bit value
    1547             :         LONGLONG ll;
    1548             : 
    1549             :         ll = Int32x32To64(t, 10000000) + 116444736000000000;
    1550             :         pft->dwLowDateTime = (DWORD)ll;
    1551             :         pft->dwHighDateTime = ll >> 32;
    1552             : }
    1553             : /* }}} */
    1554             : 
    1555             : TSRM_API int win32_utime(const char *filename, struct utimbuf *buf) /* {{{ */
    1556             : {
    1557             :         FILETIME mtime, atime;
    1558             :         HANDLE hFile;
    1559             : 
    1560             :         hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
    1561             :                                  OPEN_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    1562             : 
    1563             :         /* OPEN_ALWAYS mode sets the last error to ERROR_ALREADY_EXISTS but
    1564             :            the CreateFile operation succeeds */
    1565             :         if (GetLastError() == ERROR_ALREADY_EXISTS) {
    1566             :                 SetLastError(0);
    1567             :         }
    1568             : 
    1569             :         if ( hFile == INVALID_HANDLE_VALUE ) {
    1570             :                 return -1;
    1571             :         }
    1572             : 
    1573             :         if (!buf) {
    1574             :                 SYSTEMTIME st;
    1575             :                 GetSystemTime(&st);
    1576             :                 SystemTimeToFileTime(&st, &mtime);
    1577             :                 atime = mtime;
    1578             :         } else {
    1579             :                 UnixTimeToFileTime(buf->modtime, &mtime);
    1580             :                 UnixTimeToFileTime(buf->actime, &atime);
    1581             :         }
    1582             :         if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
    1583             :                 CloseHandle(hFile);
    1584             :                 return -1;
    1585             :         }
    1586             :         CloseHandle(hFile);
    1587             :         return 1;
    1588             : }
    1589             : /* }}} */
    1590             : #endif
    1591             : 
    1592           0 : CWD_API int virtual_utime(const char *filename, struct utimbuf *buf TSRMLS_DC) /* {{{ */
    1593             : {
    1594             :         cwd_state new_state;
    1595             :         int ret;
    1596             : 
    1597           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1598           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH TSRMLS_CC)) {
    1599           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1600           0 :                 return -1;
    1601             :         }
    1602             : 
    1603             : #ifdef TSRM_WIN32
    1604             :         ret = win32_utime(new_state.cwd, buf);
    1605             : #else
    1606           0 :         ret = utime(new_state.cwd, buf);
    1607             : #endif
    1608             : 
    1609           0 :         CWD_STATE_FREE_ERR(&new_state);
    1610           0 :         return ret;
    1611             : }
    1612             : /* }}} */
    1613             : #endif
    1614             : 
    1615           0 : CWD_API int virtual_chmod(const char *filename, mode_t mode TSRMLS_DC) /* {{{ */
    1616             : {
    1617             :         cwd_state new_state;
    1618             :         int ret;
    1619             : 
    1620           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1621           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH TSRMLS_CC)) {
    1622           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1623           0 :                 return -1;
    1624             :         }
    1625             : 
    1626           0 :         ret = chmod(new_state.cwd, mode);
    1627             : 
    1628           0 :         CWD_STATE_FREE_ERR(&new_state);
    1629           0 :         return ret;
    1630             : }
    1631             : /* }}} */
    1632             : 
    1633             : #if !defined(TSRM_WIN32) && !defined(NETWARE)
    1634           0 : CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link TSRMLS_DC) /* {{{ */
    1635             : {
    1636             :         cwd_state new_state;
    1637             :         int ret;
    1638             : 
    1639           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1640           0 :         if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH TSRMLS_CC)) {
    1641           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1642           0 :                 return -1;
    1643             :         }
    1644             : 
    1645           0 :         if (link) {
    1646             : #if HAVE_LCHOWN
    1647           0 :                 ret = lchown(new_state.cwd, owner, group);
    1648             : #else
    1649             :                 ret = -1;
    1650             : #endif
    1651             :         } else {
    1652           0 :                 ret = chown(new_state.cwd, owner, group);
    1653             :         }
    1654             : 
    1655           0 :         CWD_STATE_FREE_ERR(&new_state);
    1656           0 :         return ret;
    1657             : }
    1658             : /* }}} */
    1659             : #endif
    1660             : 
    1661           0 : CWD_API int virtual_open(const char *path TSRMLS_DC, int flags, ...) /* {{{ */
    1662             : {
    1663             :         cwd_state new_state;
    1664             :         int f;
    1665             : 
    1666           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1667           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH TSRMLS_CC)) {
    1668           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1669           0 :                 return -1;
    1670             :         }
    1671             : 
    1672           0 :         if (flags & O_CREAT) {
    1673             :                 mode_t mode;
    1674             :                 va_list arg;
    1675             : 
    1676           0 :                 va_start(arg, flags);
    1677           0 :                 mode = (mode_t) va_arg(arg, int);
    1678           0 :                 va_end(arg);
    1679             : 
    1680           0 :                 f = open(new_state.cwd, flags, mode);
    1681             :         } else {
    1682           0 :                 f = open(new_state.cwd, flags);
    1683             :         }
    1684           0 :         CWD_STATE_FREE_ERR(&new_state);
    1685           0 :         return f;
    1686             : }
    1687             : /* }}} */
    1688             : 
    1689           0 : CWD_API int virtual_creat(const char *path, mode_t mode TSRMLS_DC) /* {{{ */
    1690             : {
    1691             :         cwd_state new_state;
    1692             :         int f;
    1693             : 
    1694           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1695           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH TSRMLS_CC)) {
    1696           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1697           0 :                 return -1;
    1698             :         }
    1699             : 
    1700           0 :         f = creat(new_state.cwd,  mode);
    1701             : 
    1702           0 :         CWD_STATE_FREE_ERR(&new_state);
    1703           0 :         return f;
    1704             : }
    1705             : /* }}} */
    1706             : 
    1707           0 : CWD_API int virtual_rename(const char *oldname, const char *newname TSRMLS_DC) /* {{{ */
    1708             : {
    1709             :         cwd_state old_state;
    1710             :         cwd_state new_state;
    1711             :         int retval;
    1712             : 
    1713           0 :         CWD_STATE_COPY(&old_state, &CWDG(cwd));
    1714           0 :         if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND TSRMLS_CC)) {
    1715           0 :                 CWD_STATE_FREE_ERR(&old_state);
    1716           0 :                 return -1;
    1717             :         }
    1718           0 :         oldname = old_state.cwd;
    1719             : 
    1720           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1721           0 :         if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND TSRMLS_CC)) {
    1722           0 :                 CWD_STATE_FREE_ERR(&old_state);
    1723           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1724           0 :                 return -1;
    1725             :         }
    1726           0 :         newname = new_state.cwd;
    1727             : 
    1728             :         /* rename on windows will fail if newname already exists.
    1729             :            MoveFileEx has to be used */
    1730             : #ifdef TSRM_WIN32
    1731             :         /* MoveFileEx returns 0 on failure, other way 'round for this function */
    1732             :         retval = (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED) == 0) ? -1 : 0;
    1733             : #else
    1734           0 :         retval = rename(oldname, newname);
    1735             : #endif
    1736             : 
    1737           0 :         CWD_STATE_FREE_ERR(&old_state);
    1738           0 :         CWD_STATE_FREE_ERR(&new_state);
    1739             : 
    1740           0 :         return retval;
    1741             : }
    1742             : /* }}} */
    1743             : 
    1744           0 : CWD_API int virtual_stat(const char *path, zend_stat_t *buf TSRMLS_DC) /* {{{ */
    1745             : {
    1746             :         cwd_state new_state;
    1747             :         int retval;
    1748             : 
    1749           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1750           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH TSRMLS_CC)) {
    1751           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1752           0 :                 return -1;
    1753             :         }
    1754             : 
    1755           0 :         retval = php_sys_stat(new_state.cwd, buf);
    1756             : 
    1757           0 :         CWD_STATE_FREE_ERR(&new_state);
    1758           0 :         return retval;
    1759             : }
    1760             : /* }}} */
    1761             : 
    1762           0 : CWD_API int virtual_lstat(const char *path, zend_stat_t *buf TSRMLS_DC) /* {{{ */
    1763             : {
    1764             :         cwd_state new_state;
    1765             :         int retval;
    1766             : 
    1767           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1768           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND TSRMLS_CC)) {
    1769           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1770           0 :                 return -1;
    1771             :         }
    1772             : 
    1773           0 :         retval = php_sys_lstat(new_state.cwd, buf);
    1774             : 
    1775           0 :         CWD_STATE_FREE_ERR(&new_state);
    1776           0 :         return retval;
    1777             : }
    1778             : /* }}} */
    1779             : 
    1780           0 : CWD_API int virtual_unlink(const char *path TSRMLS_DC) /* {{{ */
    1781             : {
    1782             :         cwd_state new_state;
    1783             :         int retval;
    1784             : 
    1785           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1786           0 :         if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND TSRMLS_CC)) {
    1787           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1788           0 :                 return -1;
    1789             :         }
    1790             : 
    1791           0 :         retval = unlink(new_state.cwd);
    1792             : 
    1793           0 :         CWD_STATE_FREE_ERR(&new_state);
    1794           0 :         return retval;
    1795             : }
    1796             : /* }}} */
    1797             : 
    1798           0 : CWD_API int virtual_mkdir(const char *pathname, mode_t mode TSRMLS_DC) /* {{{ */
    1799             : {
    1800             :         cwd_state new_state;
    1801             :         int retval;
    1802             : 
    1803           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1804           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH TSRMLS_CC)) {
    1805           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1806           0 :                 return -1;
    1807             :         }
    1808             : 
    1809             : #ifdef TSRM_WIN32
    1810             :         retval = mkdir(new_state.cwd);
    1811             : #else
    1812           0 :         retval = mkdir(new_state.cwd, mode);
    1813             : #endif
    1814           0 :         CWD_STATE_FREE_ERR(&new_state);
    1815           0 :         return retval;
    1816             : }
    1817             : /* }}} */
    1818             : 
    1819           0 : CWD_API int virtual_rmdir(const char *pathname TSRMLS_DC) /* {{{ */
    1820             : {
    1821             :         cwd_state new_state;
    1822             :         int retval;
    1823             : 
    1824           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1825           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND TSRMLS_CC)) {
    1826           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1827           0 :                 return -1;
    1828             :         }
    1829             : 
    1830           0 :         retval = rmdir(new_state.cwd);
    1831             : 
    1832           0 :         CWD_STATE_FREE_ERR(&new_state);
    1833           0 :         return retval;
    1834             : }
    1835             : /* }}} */
    1836             : 
    1837             : #ifdef TSRM_WIN32
    1838             : DIR *opendir(const char *name);
    1839             : #endif
    1840             : 
    1841           0 : CWD_API DIR *virtual_opendir(const char *pathname TSRMLS_DC) /* {{{ */
    1842             : {
    1843             :         cwd_state new_state;
    1844             :         DIR *retval;
    1845             : 
    1846           0 :         CWD_STATE_COPY(&new_state, &CWDG(cwd));
    1847           0 :         if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH TSRMLS_CC)) {
    1848           0 :                 CWD_STATE_FREE_ERR(&new_state);
    1849           0 :                 return NULL;
    1850             :         }
    1851             : 
    1852           0 :         retval = opendir(new_state.cwd);
    1853             : 
    1854           0 :         CWD_STATE_FREE_ERR(&new_state);
    1855           0 :         return retval;
    1856             : }
    1857             : /* }}} */
    1858             : 
    1859             : #ifdef TSRM_WIN32
    1860             : CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
    1861             : {
    1862             :         return popen_ex(command, type, CWDG(cwd).cwd, NULL TSRMLS_CC);
    1863             : }
    1864             : /* }}} */
    1865             : #elif defined(NETWARE)
    1866             : /* On NetWare, the trick of prepending "cd cwd; " doesn't work so we need to perform
    1867             :    a VCWD_CHDIR() and mutex it
    1868             :  */
    1869             : CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
    1870             : {
    1871             :         char prev_cwd[MAXPATHLEN];
    1872             :         char *getcwd_result;
    1873             :         FILE *retval;
    1874             : 
    1875             :         getcwd_result = VCWD_GETCWD(prev_cwd, MAXPATHLEN);
    1876             :         if (!getcwd_result) {
    1877             :                 return NULL;
    1878             :         }
    1879             : 
    1880             : #ifdef ZTS
    1881             :         tsrm_mutex_lock(cwd_mutex);
    1882             : #endif
    1883             : 
    1884             :         VCWD_CHDIR(CWDG(cwd).cwd);
    1885             :         retval = popen(command, type);
    1886             :         VCWD_CHDIR(prev_cwd);
    1887             : 
    1888             : #ifdef ZTS
    1889             :         tsrm_mutex_unlock(cwd_mutex);
    1890             : #endif
    1891             : 
    1892             :         return retval;
    1893             : }
    1894             : /* }}} */
    1895             : #else /* Unix */
    1896           0 : CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
    1897             : {
    1898             :         int command_length;
    1899           0 :         int dir_length, extra = 0;
    1900             :         char *command_line;
    1901             :         char *ptr, *dir;
    1902             :         FILE *retval;
    1903             : 
    1904           0 :         command_length = strlen(command);
    1905             : 
    1906           0 :         dir_length = CWDG(cwd).cwd_length;
    1907           0 :         dir = CWDG(cwd).cwd;
    1908           0 :         while (dir_length > 0) {
    1909           0 :                 if (*dir == '\'') extra+=3;
    1910           0 :                 dir++;
    1911           0 :                 dir_length--;
    1912             :         }
    1913           0 :         dir_length = CWDG(cwd).cwd_length;
    1914           0 :         dir = CWDG(cwd).cwd;
    1915             : 
    1916           0 :         ptr = command_line = (char *) emalloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
    1917           0 :         if (!command_line) {
    1918           0 :                 return NULL;
    1919             :         }
    1920           0 :         memcpy(ptr, "cd ", sizeof("cd ")-1);
    1921           0 :         ptr += sizeof("cd ")-1;
    1922             : 
    1923           0 :         if (CWDG(cwd).cwd_length == 0) {
    1924           0 :                 *ptr++ = DEFAULT_SLASH;
    1925             :         } else {
    1926           0 :                 *ptr++ = '\'';
    1927           0 :                 while (dir_length > 0) {
    1928           0 :                         switch (*dir) {
    1929             :                         case '\'':
    1930           0 :                                 *ptr++ = '\'';
    1931           0 :                                 *ptr++ = '\\';
    1932           0 :                                 *ptr++ = '\'';
    1933             :                                 /* fall-through */
    1934             :                         default:
    1935           0 :                                 *ptr++ = *dir;
    1936             :                         }
    1937           0 :                         dir++;
    1938           0 :                         dir_length--;
    1939             :                 }
    1940           0 :                 *ptr++ = '\'';
    1941             :         }
    1942             : 
    1943           0 :         *ptr++ = ' ';
    1944           0 :         *ptr++ = ';';
    1945           0 :         *ptr++ = ' ';
    1946             : 
    1947           0 :         memcpy(ptr, command, command_length+1);
    1948           0 :         retval = popen(command_line, type);
    1949             : 
    1950           0 :         efree(command_line);
    1951           0 :         return retval;
    1952             : }
    1953             : /* }}} */
    1954             : #endif
    1955             : 
    1956       98549 : CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{ */
    1957             : {
    1958             :         cwd_state new_state;
    1959             :         char cwd[MAXPATHLEN];
    1960             : 
    1961             :         /* realpath("") returns CWD */
    1962       98549 :         if (!*path) {
    1963           5 :                 new_state.cwd = (char*)emalloc(1);
    1964           5 :                 if (new_state.cwd == NULL) {
    1965           0 :                         return NULL;
    1966             :                 }
    1967           5 :                 new_state.cwd[0] = '\0';
    1968           5 :                 new_state.cwd_length = 0;
    1969           5 :                 if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
    1970           5 :                         path = cwd;
    1971             :                 }
    1972      111986 :         } else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
    1973        6721 :                                         VCWD_GETCWD(cwd, MAXPATHLEN)) {
    1974        6721 :                 new_state.cwd = estrdup(cwd);
    1975        6721 :                 new_state.cwd_length = strlen(cwd);
    1976             :         } else {
    1977       91823 :                 new_state.cwd = (char*)emalloc(1);
    1978       91823 :                 if (new_state.cwd == NULL) {
    1979           0 :                         return NULL;
    1980             :                 }
    1981       91823 :                 new_state.cwd[0] = '\0';
    1982       91823 :                 new_state.cwd_length = 0;
    1983             :         }
    1984             : 
    1985       98549 :         if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH TSRMLS_CC)) {
    1986       13215 :                 efree(new_state.cwd);
    1987       13215 :                 return NULL;
    1988             :         }
    1989             : 
    1990       85334 :         if (real_path) {
    1991       85121 :                 int copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
    1992       85121 :                 memcpy(real_path, new_state.cwd, copy_len);
    1993       85121 :                 real_path[copy_len] = '\0';
    1994       85121 :                 efree(new_state.cwd);
    1995       85121 :                 return real_path;
    1996             :         } else {
    1997         213 :                 return new_state.cwd;
    1998             :         }
    1999             : }
    2000             : /* }}} */
    2001             : 
    2002             : /*
    2003             :  * Local variables:
    2004             :  * tab-width: 4
    2005             :  * c-basic-offset: 4
    2006             :  * End:
    2007             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:46 +0000 (2 days ago)

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