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 556 46.8 %
Date: 2015-06-27 Functions: 18 42 42.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:07 +0000 (6 days ago)

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