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

LCOV - code coverage report
Current view: top level - Zend - zend_virtual_cwd.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 260 551 47.2 %
Date: 2014-07-21 Functions: 18 42 42.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:07 +0000 (6 days ago)

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