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: 2015-05-21 Functions: 18 42 42.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:58:53 +0000 (5 days ago)

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