2000-09-04 02:18:13 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
2000-09-04 02:18:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2007-01-01 17:36:18 +08:00
|
|
|
| Copyright (c) 1997-2007 The PHP Group |
|
2000-09-04 02:18:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2000-09-04 02:18:13 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-25 13:05:06 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2000-09-04 02:18:13 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Sascha Schumann <sascha@schumann.cx> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
2004-10-07 07:56:29 +08:00
|
|
|
#include <time.h>
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2000-09-04 02:47:35 +08:00
|
|
|
#include "tsrm_virtual_cwd.h"
|
2000-09-04 02:18:13 +08:00
|
|
|
#include "tsrm_strtok_r.h"
|
2000-09-04 02:47:35 +08:00
|
|
|
|
2000-09-04 02:18:13 +08:00
|
|
|
#ifdef TSRM_WIN32
|
2000-09-04 02:47:35 +08:00
|
|
|
#include <io.h>
|
2001-04-28 00:41:53 +08:00
|
|
|
#include "tsrm_win32.h"
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
2005-07-07 15:10:50 +08:00
|
|
|
#ifdef NETWARE
|
|
|
|
#include <fsio.h>
|
|
|
|
#endif
|
|
|
|
|
2002-11-07 02:07:23 +08:00
|
|
|
#ifndef HAVE_REALPATH
|
2002-10-05 19:26:17 +08:00
|
|
|
#define realpath(x,y) strcpy(y,x)
|
|
|
|
#endif
|
|
|
|
|
2000-09-04 02:18:13 +08:00
|
|
|
#define VIRTUAL_CWD_DEBUG 0
|
|
|
|
|
|
|
|
#include "TSRM.h"
|
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
/* Only need mutex for popen() in Windows and NetWare because it doesn't chdir() on UNIX */
|
2002-05-29 16:41:21 +08:00
|
|
|
#if (defined(TSRM_WIN32) || defined(NETWARE)) && defined(ZTS)
|
2000-09-04 02:18:13 +08:00
|
|
|
MUTEX_T cwd_mutex;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ZTS
|
2004-10-05 08:42:25 +08:00
|
|
|
ts_rsrc_id cwd_globals_id;
|
2000-09-04 02:18:13 +08:00
|
|
|
#else
|
2004-10-05 08:42:25 +08:00
|
|
|
virtual_cwd_globals cwd_globals;
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cwd_state main_cwd_state; /* True global */
|
|
|
|
|
|
|
|
#ifndef TSRM_WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <direct.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISDIR
|
|
|
|
#define S_ISDIR(mode) ((mode) & _S_IFDIR)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(mode) ((mode) & _S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TSRM_WIN32
|
2002-04-22 06:20:14 +08:00
|
|
|
#include <tchar.h>
|
|
|
|
#define tsrm_strtok_r(a,b,c) _tcstok((a),(b))
|
2000-09-04 02:18:13 +08:00
|
|
|
#define TOKENIZER_STRING "/\\"
|
|
|
|
|
|
|
|
static int php_check_dots(const char *element, int n)
|
|
|
|
{
|
|
|
|
while (n-- > 0) if (element[n] != '.') break;
|
|
|
|
|
|
|
|
return (n != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define IS_DIRECTORY_UP(element, len) \
|
|
|
|
(len >= 2 && !php_check_dots(element, len))
|
|
|
|
|
|
|
|
#define IS_DIRECTORY_CURRENT(element, len) \
|
2003-06-04 08:01:21 +08:00
|
|
|
(len == 1 && element[0] == '.')
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2002-05-29 16:41:21 +08:00
|
|
|
#elif defined(NETWARE)
|
|
|
|
/* NetWare has strtok() (in LibC) and allows both slashes in paths, like Windows --
|
|
|
|
but rest of the stuff is like Unix */
|
|
|
|
/* strtok() call in LibC is abending when used in a different address space -- hence using
|
|
|
|
PHP's version itself for now */
|
|
|
|
/*#define tsrm_strtok_r(a,b,c) strtok((a),(b))*/
|
|
|
|
#define TOKENIZER_STRING "/\\"
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
#define TOKENIZER_STRING "/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* default macros */
|
|
|
|
|
|
|
|
#ifndef IS_DIRECTORY_UP
|
|
|
|
#define IS_DIRECTORY_UP(element, len) \
|
2003-06-04 08:01:21 +08:00
|
|
|
(len == 2 && element[0] == '.' && element[1] == '.')
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef IS_DIRECTORY_CURRENT
|
|
|
|
#define IS_DIRECTORY_CURRENT(element, len) \
|
2003-06-04 08:01:21 +08:00
|
|
|
(len == 1 && element[0] == '.')
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* define this to check semantics */
|
|
|
|
#define IS_DIR_OK(s) (1)
|
|
|
|
|
|
|
|
#ifndef IS_DIR_OK
|
|
|
|
#define IS_DIR_OK(state) (php_is_dir_ok(state) == 0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define CWD_STATE_COPY(d, s) \
|
|
|
|
(d)->cwd_length = (s)->cwd_length; \
|
|
|
|
(d)->cwd = (char *) malloc((s)->cwd_length+1); \
|
|
|
|
memcpy((d)->cwd, (s)->cwd, (s)->cwd_length+1);
|
|
|
|
|
|
|
|
#define CWD_STATE_FREE(s) \
|
|
|
|
free((s)->cwd);
|
|
|
|
|
2006-11-10 23:04:07 +08:00
|
|
|
#ifdef TSRM_WIN32
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int php_sys_stat(const char *path, struct stat *buf) /* {{{ */
|
2006-11-10 23:04:07 +08:00
|
|
|
{
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA data;
|
|
|
|
__int64 t;
|
|
|
|
|
|
|
|
if (!GetFileAttributesEx(path, GetFileExInfoStandard, &data)) {
|
|
|
|
return stat(path, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path[1] == ':') {
|
|
|
|
if (path[0] >= 'A' && path[0] <= 'Z') {
|
|
|
|
buf->st_dev = buf->st_rdev = path[0] - 'A';
|
|
|
|
} else {
|
|
|
|
buf->st_dev = buf->st_rdev = path[0] - 'a';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char cur_path[MAXPATHLEN+1];
|
|
|
|
DWORD len = sizeof(cur_path);
|
|
|
|
char *tmp = cur_path;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
DWORD r = GetCurrentDirectory(len, tmp);
|
|
|
|
if (r < len) {
|
|
|
|
if (tmp[1] == ':') {
|
|
|
|
if (path[0] >= 'A' && path[0] <= 'Z') {
|
|
|
|
buf->st_dev = buf->st_rdev = path[0] - 'A';
|
|
|
|
} else {
|
|
|
|
buf->st_dev = buf->st_rdev = path[0] - 'a';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf->st_dev = buf->st_rdev = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (!r) {
|
|
|
|
buf->st_dev = buf->st_rdev = -1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
len = r+1;
|
|
|
|
tmp = (char*)malloc(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tmp != cur_path) {
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf->st_uid = buf->st_gid = buf->st_ino = 0;
|
|
|
|
buf->st_mode = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? (S_IFDIR|S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6)) : S_IFREG;
|
|
|
|
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));
|
|
|
|
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
|
|
|
|
int len = strlen(path);
|
|
|
|
|
|
|
|
if (path[len-4] == '.') {
|
|
|
|
if (_memicmp(path+len-3, "exe", 3) == 0 ||
|
|
|
|
_memicmp(path+len-3, "com", 3) == 0 ||
|
|
|
|
_memicmp(path+len-3, "bat", 3) == 0 ||
|
|
|
|
_memicmp(path+len-3, "cmd", 3) == 0) {
|
|
|
|
buf->st_mode |= (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf->st_nlink = 1;
|
|
|
|
t = data.nFileSizeHigh;
|
|
|
|
t = t << 32;
|
|
|
|
t |= data.nFileSizeLow;
|
|
|
|
buf->st_size = t;
|
|
|
|
t = data.ftLastAccessTime.dwHighDateTime;
|
|
|
|
t = t << 32;
|
|
|
|
t |= data.ftLastAccessTime.dwLowDateTime;
|
|
|
|
buf->st_atime = (unsigned long)((t / 10000000) - 11644473600);
|
|
|
|
t = data.ftCreationTime.dwHighDateTime;
|
|
|
|
t = t << 32;
|
|
|
|
t |= data.ftCreationTime.dwLowDateTime;
|
|
|
|
buf->st_ctime = (unsigned long)((t / 10000000) - 11644473600);
|
|
|
|
t = data.ftLastWriteTime.dwHighDateTime;
|
|
|
|
t = t << 32;
|
|
|
|
t |= data.ftLastWriteTime.dwLowDateTime;
|
|
|
|
buf->st_mtime = (unsigned long)((t / 10000000) - 11644473600);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2006-11-10 23:04:07 +08:00
|
|
|
#endif
|
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static int php_is_dir_ok(const cwd_state *state) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
2006-11-10 23:04:07 +08:00
|
|
|
if (php_sys_stat(state->cwd, &buf) == 0 && S_ISDIR(buf.st_mode))
|
2000-09-04 02:18:13 +08:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static int php_is_file_ok(const cwd_state *state) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
2006-11-10 23:04:07 +08:00
|
|
|
if (php_sys_stat(state->cwd, &buf) == 0 && S_ISREG(buf.st_mode))
|
2000-09-04 02:18:13 +08:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static void cwd_globals_ctor(virtual_cwd_globals *cwd_globals TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
CWD_STATE_COPY(&cwd_globals->cwd, &main_cwd_state);
|
2004-10-05 08:42:25 +08:00
|
|
|
cwd_globals->realpath_cache_size = 0;
|
|
|
|
cwd_globals->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
|
|
|
|
cwd_globals->realpath_cache_ttl = REALPATH_CACHE_TTL;
|
|
|
|
memset(cwd_globals->realpath_cache, 0, sizeof(cwd_globals->realpath_cache));
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static void cwd_globals_dtor(virtual_cwd_globals *cwd_globals TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
CWD_STATE_FREE(&cwd_globals->cwd);
|
2007-01-22 17:31:46 +08:00
|
|
|
realpath_cache_clean(TSRMLS_C);
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static char *tsrm_strndup(const char *s, size_t length) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = (char *) malloc(length+1);
|
|
|
|
if (!p) {
|
|
|
|
return (char *)NULL;
|
|
|
|
}
|
|
|
|
if (length) {
|
|
|
|
memcpy(p,s,length);
|
|
|
|
}
|
|
|
|
p[length]=0;
|
|
|
|
return p;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API void virtual_cwd_startup(void) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
char cwd[MAXPATHLEN];
|
|
|
|
char *result;
|
|
|
|
|
2005-07-07 15:10:50 +08:00
|
|
|
#ifdef NETWARE
|
|
|
|
result = getcwdpath(cwd, NULL, 1);
|
|
|
|
if(result)
|
|
|
|
{
|
|
|
|
char *c=cwd;
|
|
|
|
while(c = strchr(c, '\\'))
|
|
|
|
{
|
|
|
|
*c='/';
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2000-09-04 02:18:13 +08:00
|
|
|
result = getcwd(cwd, sizeof(cwd));
|
2005-07-07 15:10:50 +08:00
|
|
|
#endif
|
2000-09-04 02:18:13 +08:00
|
|
|
if (!result) {
|
|
|
|
cwd[0] = '\0';
|
|
|
|
}
|
|
|
|
main_cwd_state.cwd = strdup(cwd);
|
|
|
|
main_cwd_state.cwd_length = strlen(cwd);
|
|
|
|
|
|
|
|
#ifdef ZTS
|
2001-07-27 18:08:26 +08:00
|
|
|
ts_allocate_id(&cwd_globals_id, sizeof(virtual_cwd_globals), (ts_allocate_ctor) cwd_globals_ctor, (ts_allocate_dtor) cwd_globals_dtor);
|
2000-09-04 02:18:13 +08:00
|
|
|
#else
|
2001-07-27 18:08:26 +08:00
|
|
|
cwd_globals_ctor(&cwd_globals TSRMLS_CC);
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
#if (defined(TSRM_WIN32) || defined(NETWARE)) && defined(ZTS)
|
2000-09-04 02:18:13 +08:00
|
|
|
cwd_mutex = tsrm_mutex_alloc();
|
|
|
|
#endif
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API void virtual_cwd_shutdown(void) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
#ifndef ZTS
|
2001-07-27 18:08:26 +08:00
|
|
|
cwd_globals_dtor(&cwd_globals TSRMLS_CC);
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
2003-06-25 13:05:06 +08:00
|
|
|
#if (defined(TSRM_WIN32) || defined(NETWARE)) && defined(ZTS)
|
2000-09-04 02:18:13 +08:00
|
|
|
tsrm_mutex_free(cwd_mutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free(main_cwd_state.cwd); /* Don't use CWD_STATE_FREE because the non global states will probably use emalloc()/efree() */
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state *state;
|
|
|
|
|
|
|
|
state = &CWDG(cwd);
|
|
|
|
|
|
|
|
if (state->cwd_length == 0) {
|
|
|
|
char *retval;
|
|
|
|
|
|
|
|
*length = 1;
|
|
|
|
retval = (char *) malloc(2);
|
|
|
|
retval[0] = DEFAULT_SLASH;
|
|
|
|
retval[1] = '\0';
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TSRM_WIN32
|
|
|
|
/* If we have something like C: */
|
|
|
|
if (state->cwd_length == 2 && state->cwd[state->cwd_length-1] == ':') {
|
|
|
|
char *retval;
|
|
|
|
|
|
|
|
*length = state->cwd_length+1;
|
|
|
|
retval = (char *) malloc(*length+1);
|
|
|
|
memcpy(retval, state->cwd, *length);
|
|
|
|
retval[*length-1] = DEFAULT_SLASH;
|
|
|
|
retval[*length] = '\0';
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*length = state->cwd_length;
|
|
|
|
return strdup(state->cwd);
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
/* Same semantics as UNIX getcwd() */
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API char *virtual_getcwd(char *buf, size_t size TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
char *cwd;
|
|
|
|
|
2001-08-05 09:34:40 +08:00
|
|
|
cwd = virtual_getcwd_ex(&length TSRMLS_CC);
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
if (buf == NULL) {
|
|
|
|
return cwd;
|
|
|
|
}
|
|
|
|
if (length > size-1) {
|
|
|
|
free(cwd);
|
|
|
|
errno = ERANGE; /* Is this OK? */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(buf, cwd, length+1);
|
|
|
|
free(cwd);
|
|
|
|
return buf;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static inline unsigned long realpath_cache_key(const char *path, int path_len) /* {{{ */
|
2004-10-05 08:42:25 +08:00
|
|
|
{
|
2007-01-21 00:22:51 +08:00
|
|
|
register unsigned long h;
|
|
|
|
const char *e = path + path_len;
|
|
|
|
|
|
|
|
for (h = 2166136261U; path < e;) {
|
|
|
|
h *= 16777619;
|
|
|
|
h ^= *path++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
2004-10-05 08:42:25 +08:00
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2004-10-05 08:42:25 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API void realpath_cache_clean(TSRMLS_D) /* {{{ */
|
2007-01-22 17:31:46 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(CWDG(realpath_cache))/sizeof(CWDG(realpath_cache)[0]); i++) {
|
|
|
|
realpath_cache_bucket *p = CWDG(realpath_cache)[i];
|
|
|
|
while (p != NULL) {
|
|
|
|
realpath_cache_bucket *r = p;
|
|
|
|
p = p->next;
|
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
CWDG(realpath_cache)[i] = NULL;
|
|
|
|
}
|
|
|
|
CWDG(realpath_cache_size) = 0;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2007-01-22 17:31:46 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API void realpath_cache_del(const char *path, int path_len TSRMLS_DC) /* {{{ */
|
2007-01-22 17:31:46 +08:00
|
|
|
{
|
|
|
|
unsigned long key = realpath_cache_key(path, path_len);
|
|
|
|
unsigned long n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
|
|
|
|
realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
|
|
|
|
|
|
|
|
while (*bucket != NULL) {
|
|
|
|
if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
|
|
|
|
memcmp(path, (*bucket)->path, path_len) == 0) {
|
|
|
|
realpath_cache_bucket *r = *bucket;
|
|
|
|
*bucket = (*bucket)->next;
|
|
|
|
CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
|
|
|
|
free(r);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
bucket = &(*bucket)->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2007-01-22 17:31:46 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static inline void realpath_cache_add(const char *path, int path_len, const char *realpath, int realpath_len, time_t t TSRMLS_DC) /* {{{ */
|
2004-10-05 08:42:25 +08:00
|
|
|
{
|
|
|
|
long size = sizeof(realpath_cache_bucket) + path_len + 1 + realpath_len + 1;
|
|
|
|
if (CWDG(realpath_cache_size) + size <= CWDG(realpath_cache_size_limit)) {
|
|
|
|
realpath_cache_bucket *bucket = malloc(size);
|
|
|
|
unsigned long n;
|
|
|
|
|
|
|
|
bucket->key = realpath_cache_key(path, path_len);
|
|
|
|
bucket->path = (char*)bucket + sizeof(realpath_cache_bucket);
|
|
|
|
memcpy(bucket->path, path, path_len+1);
|
|
|
|
bucket->path_len = path_len;
|
|
|
|
bucket->realpath = bucket->path + (path_len + 1);
|
|
|
|
memcpy(bucket->realpath, realpath, realpath_len+1);
|
|
|
|
bucket->realpath_len = realpath_len;
|
|
|
|
bucket->expires = t + CWDG(realpath_cache_ttl);
|
|
|
|
n = bucket->key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
|
|
|
|
bucket->next = CWDG(realpath_cache)[n];
|
|
|
|
CWDG(realpath_cache)[n] = bucket;
|
2007-01-21 00:22:51 +08:00
|
|
|
CWDG(realpath_cache_size) += size;
|
2004-10-05 08:42:25 +08:00
|
|
|
}
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2004-10-05 08:42:25 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
static inline realpath_cache_bucket* realpath_cache_find(const char *path, int path_len, time_t t TSRMLS_DC) /* {{{ */
|
2004-10-05 08:42:25 +08:00
|
|
|
{
|
|
|
|
unsigned long key = realpath_cache_key(path, path_len);
|
|
|
|
unsigned long n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
|
|
|
|
realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
|
|
|
|
|
|
|
|
while (*bucket != NULL) {
|
|
|
|
if (CWDG(realpath_cache_ttl) && (*bucket)->expires < t) {
|
|
|
|
realpath_cache_bucket *r = *bucket;
|
2006-01-16 17:09:37 +08:00
|
|
|
*bucket = (*bucket)->next;
|
|
|
|
CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
|
|
|
|
free(r);
|
2004-10-05 08:42:25 +08:00
|
|
|
} else if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
|
|
|
|
memcmp(path, (*bucket)->path, path_len) == 0) {
|
|
|
|
return *bucket;
|
|
|
|
} else {
|
2006-01-16 17:09:37 +08:00
|
|
|
bucket = &(*bucket)->next;
|
2004-10-05 08:42:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2004-10-05 08:42:25 +08:00
|
|
|
|
2000-09-04 02:18:13 +08:00
|
|
|
/* Resolve path relatively to state and put the real path into state */
|
|
|
|
/* returns 0 for ok, 1 for error */
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
int path_length = strlen(path);
|
2004-10-05 08:42:25 +08:00
|
|
|
cwd_state old_state;
|
|
|
|
char orig_path[MAXPATHLEN];
|
|
|
|
realpath_cache_bucket *bucket;
|
2006-02-07 04:37:11 +08:00
|
|
|
time_t t = 0;
|
2006-09-20 15:35:59 +08:00
|
|
|
int ret;
|
2007-04-04 04:20:56 +08:00
|
|
|
int use_cache;
|
|
|
|
int use_relative_path = 0;
|
2004-10-05 08:42:25 +08:00
|
|
|
TSRMLS_FETCH();
|
2007-07-10 21:21:11 +08:00
|
|
|
|
2007-04-04 04:20:56 +08:00
|
|
|
use_cache = ((use_realpath != CWD_EXPAND) && CWDG(realpath_cache_size_limit));
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
if (path_length == 0)
|
2007-07-10 21:21:11 +08:00
|
|
|
return (1);
|
2004-12-02 06:56:05 +08:00
|
|
|
if (path_length >= MAXPATHLEN)
|
|
|
|
return (1);
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2006-09-20 15:35:59 +08:00
|
|
|
#if VIRTUAL_CWD_DEBUG
|
|
|
|
fprintf(stderr,"cwd = %s path = %s\n", state->cwd, path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* cwd_length can be 0 when getcwd() fails.
|
|
|
|
* This can happen under solaris when a dir does not have read permissions
|
|
|
|
* but *does* have execute permissions */
|
2007-04-04 04:20:56 +08:00
|
|
|
if (!IS_ABSOLUTE_PATH(path, path_length)) {
|
|
|
|
if (state->cwd_length == 0) {
|
|
|
|
use_cache = 0;
|
|
|
|
use_relative_path = 1;
|
|
|
|
} else {
|
|
|
|
int orig_path_len;
|
|
|
|
int state_cwd_length = state->cwd_length;
|
2006-09-20 15:35:59 +08:00
|
|
|
|
2006-10-05 16:03:11 +08:00
|
|
|
#ifdef TSRM_WIN32
|
2007-04-04 04:20:56 +08:00
|
|
|
if (IS_SLASH(path[0])) {
|
|
|
|
state_cwd_length = 2;
|
|
|
|
}
|
2006-10-05 16:03:11 +08:00
|
|
|
#endif
|
2007-04-04 04:20:56 +08:00
|
|
|
orig_path_len = path_length + state_cwd_length + 1;
|
|
|
|
if (orig_path_len >= MAXPATHLEN) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
memcpy(orig_path, state->cwd, state_cwd_length);
|
|
|
|
orig_path[state_cwd_length] = DEFAULT_SLASH;
|
|
|
|
memcpy(orig_path + state_cwd_length + 1, path, path_length + 1);
|
|
|
|
path = orig_path;
|
|
|
|
path_length = orig_path_len;
|
2004-10-05 08:42:25 +08:00
|
|
|
}
|
2006-09-20 15:35:59 +08:00
|
|
|
}
|
|
|
|
|
2007-04-04 04:20:56 +08:00
|
|
|
if (use_cache) {
|
2007-05-26 06:53:34 +08:00
|
|
|
t = CWDG(realpath_cache_ttl)?time(0):0;
|
2006-09-20 15:35:59 +08:00
|
|
|
if ((bucket = realpath_cache_find(path, path_length, t TSRMLS_CC)) != NULL) {
|
2004-10-05 08:42:25 +08:00
|
|
|
int len = bucket->realpath_len;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&old_state, state);
|
|
|
|
state->cwd = (char *) realloc(state->cwd, len+1);
|
|
|
|
memcpy(state->cwd, bucket->realpath, len+1);
|
|
|
|
state->cwd_length = len;
|
|
|
|
if (verify_path && verify_path(state)) {
|
|
|
|
CWD_STATE_FREE(state);
|
|
|
|
*state = old_state;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
CWD_STATE_FREE(&old_state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-20 15:35:59 +08:00
|
|
|
|
2007-01-20 05:00:35 +08:00
|
|
|
if (use_realpath != CWD_EXPAND) {
|
2002-10-05 19:26:17 +08:00
|
|
|
#if !defined(TSRM_WIN32) && !defined(NETWARE)
|
2006-09-20 15:35:59 +08:00
|
|
|
char resolved_path[MAXPATHLEN];
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2006-09-20 15:35:59 +08:00
|
|
|
if (!realpath(path, resolved_path)) { /* Note: Not threadsafe on older *BSD's */
|
2006-11-13 23:13:20 +08:00
|
|
|
if (use_realpath == CWD_REALPATH) {
|
2006-11-11 01:23:58 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
goto no_realpath;
|
2003-10-30 05:33:05 +08:00
|
|
|
}
|
2007-07-03 22:48:01 +08:00
|
|
|
use_realpath = CWD_REALPATH;
|
2006-09-20 15:35:59 +08:00
|
|
|
CWD_STATE_COPY(&old_state, state);
|
2005-12-20 22:24:14 +08:00
|
|
|
|
2006-09-20 15:35:59 +08:00
|
|
|
state->cwd_length = strlen(resolved_path);
|
|
|
|
state->cwd = (char *) realloc(state->cwd, state->cwd_length+1);
|
|
|
|
memcpy(state->cwd, resolved_path, state->cwd_length+1);
|
|
|
|
#else
|
|
|
|
goto no_realpath;
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
2006-09-20 15:35:59 +08:00
|
|
|
} else {
|
|
|
|
char *ptr, *path_copy, *free_path;
|
2007-02-19 22:58:07 +08:00
|
|
|
char *tok;
|
2006-09-20 15:35:59 +08:00
|
|
|
int ptr_length;
|
2007-02-21 21:09:03 +08:00
|
|
|
#ifdef TSRM_WIN32
|
|
|
|
int is_unc;
|
|
|
|
#endif
|
2006-09-20 15:35:59 +08:00
|
|
|
no_realpath:
|
|
|
|
|
2007-07-24 17:07:33 +08:00
|
|
|
#ifdef TSRM_WIN32
|
|
|
|
if (memchr(path, '*', path_length) ||
|
|
|
|
memchr(path, '?', path_length)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-09-20 15:35:59 +08:00
|
|
|
free_path = path_copy = tsrm_strndup(path, path_length);
|
|
|
|
CWD_STATE_COPY(&old_state, state);
|
|
|
|
|
2007-02-21 21:09:03 +08:00
|
|
|
#ifdef TSRM_WIN32
|
2007-04-12 23:28:58 +08:00
|
|
|
ret = 0;
|
2007-02-21 21:09:03 +08:00
|
|
|
is_unc = 0;
|
2006-09-20 15:35:59 +08:00
|
|
|
if (path_length >= 2 && path[1] == ':') {
|
|
|
|
state->cwd = (char *) realloc(state->cwd, 2 + 1);
|
|
|
|
state->cwd[0] = toupper(path[0]);
|
|
|
|
state->cwd[1] = ':';
|
|
|
|
state->cwd[2] = '\0';
|
|
|
|
state->cwd_length = 2;
|
|
|
|
path_copy += 2;
|
|
|
|
} else if (IS_UNC_PATH(path, path_length)) {
|
|
|
|
state->cwd = (char *) realloc(state->cwd, 1 + 1);
|
|
|
|
state->cwd[0] = DEFAULT_SLASH;
|
|
|
|
state->cwd[1] = '\0';
|
|
|
|
state->cwd_length = 1;
|
|
|
|
path_copy += 2;
|
2007-02-21 21:09:03 +08:00
|
|
|
is_unc = 2;
|
2006-09-20 15:35:59 +08:00
|
|
|
} else {
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
2006-09-20 15:35:59 +08:00
|
|
|
state->cwd = (char *) realloc(state->cwd, 1);
|
|
|
|
state->cwd[0] = '\0';
|
|
|
|
state->cwd_length = 0;
|
2000-09-04 02:18:13 +08:00
|
|
|
#ifdef TSRM_WIN32
|
|
|
|
}
|
2006-09-20 15:35:59 +08:00
|
|
|
#endif
|
|
|
|
|
2007-02-19 22:58:07 +08:00
|
|
|
tok = NULL;
|
2003-03-17 21:40:45 +08:00
|
|
|
ptr = tsrm_strtok_r(path_copy, TOKENIZER_STRING, &tok);
|
|
|
|
while (ptr) {
|
|
|
|
ptr_length = strlen(ptr);
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2003-03-17 21:40:45 +08:00
|
|
|
if (IS_DIRECTORY_UP(ptr, ptr_length)) {
|
|
|
|
char save;
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-04-04 04:20:56 +08:00
|
|
|
if (use_relative_path) {
|
|
|
|
CWD_STATE_FREE(state);
|
|
|
|
*state = old_state;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-03-17 21:40:45 +08:00
|
|
|
save = DEFAULT_SLASH;
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
#define PREVIOUS state->cwd[state->cwd_length - 1]
|
|
|
|
|
2003-03-17 21:40:45 +08:00
|
|
|
while (IS_ABSOLUTE_PATH(state->cwd, state->cwd_length) &&
|
|
|
|
!IS_SLASH(PREVIOUS)) {
|
|
|
|
save = PREVIOUS;
|
|
|
|
PREVIOUS = '\0';
|
|
|
|
state->cwd_length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_ABSOLUTE_PATH(state->cwd, state->cwd_length)) {
|
|
|
|
state->cwd[state->cwd_length++] = save;
|
|
|
|
state->cwd[state->cwd_length] = '\0';
|
|
|
|
} else {
|
|
|
|
PREVIOUS = '\0';
|
|
|
|
state->cwd_length--;
|
|
|
|
}
|
|
|
|
} else if (!IS_DIRECTORY_CURRENT(ptr, ptr_length)) {
|
2007-04-04 04:20:56 +08:00
|
|
|
if (use_relative_path) {
|
|
|
|
state->cwd = (char *) realloc(state->cwd, state->cwd_length+ptr_length+1);
|
|
|
|
use_relative_path = 0;
|
|
|
|
} else {
|
|
|
|
state->cwd = (char *) realloc(state->cwd, state->cwd_length+ptr_length+1+1);
|
2001-05-03 23:50:37 +08:00
|
|
|
#ifdef TSRM_WIN32
|
2007-04-04 04:20:56 +08:00
|
|
|
/* Windows 9x will consider C:\\Foo as a network path. Avoid it. */
|
|
|
|
if (state->cwd_length < 2 ||
|
|
|
|
(state->cwd[state->cwd_length-1]!='\\' && state->cwd[state->cwd_length-1]!='/') ||
|
|
|
|
IsDBCSLeadByte(state->cwd[state->cwd_length-2])) {
|
|
|
|
state->cwd[state->cwd_length++] = DEFAULT_SLASH;
|
|
|
|
}
|
2002-05-29 16:41:21 +08:00
|
|
|
#elif defined(NETWARE)
|
2007-04-04 04:20:56 +08:00
|
|
|
/*
|
|
|
|
Below code keeps appending to state->cwd a File system seperator
|
|
|
|
cases where this appending should not happen is given below,
|
|
|
|
a) sys: should just be left as it is
|
|
|
|
b) sys:system should just be left as it is,
|
|
|
|
Colon is allowed only in the first token as volume names alone can have the : in their names.
|
|
|
|
Files and Directories cannot have : in their names
|
|
|
|
So the check goes like this,
|
|
|
|
For second token and above simply append the DEFAULT_SLASH to the state->cwd.
|
|
|
|
For first token check for the existence of :
|
|
|
|
if it exists don't append the DEFAULT_SLASH to the state->cwd.
|
|
|
|
*/
|
|
|
|
if(((state->cwd_length == 0) && (strchr(ptr, ':') == NULL)) || (state->cwd_length > 0)) {
|
|
|
|
state->cwd[state->cwd_length++] = DEFAULT_SLASH;
|
|
|
|
}
|
2001-05-06 00:04:14 +08:00
|
|
|
#else
|
2007-04-04 04:20:56 +08:00
|
|
|
state->cwd[state->cwd_length++] = DEFAULT_SLASH;
|
2001-05-03 23:50:37 +08:00
|
|
|
#endif
|
2007-04-04 04:20:56 +08:00
|
|
|
}
|
2003-03-17 21:40:45 +08:00
|
|
|
memcpy(&state->cwd[state->cwd_length], ptr, ptr_length+1);
|
2006-09-20 15:35:59 +08:00
|
|
|
|
|
|
|
#ifdef TSRM_WIN32
|
2007-01-20 05:00:35 +08:00
|
|
|
if (use_realpath != CWD_EXPAND) {
|
2006-09-20 15:35:59 +08:00
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
HANDLE hFind;
|
|
|
|
|
|
|
|
if ((hFind = FindFirstFile(state->cwd, &data)) != INVALID_HANDLE_VALUE) {
|
|
|
|
int length = strlen(data.cFileName);
|
|
|
|
|
|
|
|
if (length != ptr_length) {
|
|
|
|
state->cwd = (char *) realloc(state->cwd, state->cwd_length+length+1);
|
|
|
|
}
|
|
|
|
memcpy(&state->cwd[state->cwd_length], data.cFileName, length+1);
|
|
|
|
ptr_length = length;
|
|
|
|
FindClose(hFind);
|
2007-02-19 22:58:07 +08:00
|
|
|
ret = 0;
|
2007-01-24 01:03:48 +08:00
|
|
|
} else if (use_realpath == CWD_REALPATH) {
|
2007-02-21 21:09:03 +08:00
|
|
|
if (is_unc) {
|
|
|
|
/* skip share name */
|
|
|
|
is_unc--;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
}
|
2006-09-20 15:35:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-03-17 21:40:45 +08:00
|
|
|
state->cwd_length += ptr_length;
|
|
|
|
}
|
|
|
|
ptr = tsrm_strtok_r(NULL, TOKENIZER_STRING, &tok);
|
2001-05-06 00:05:19 +08:00
|
|
|
}
|
2007-07-23 16:57:04 +08:00
|
|
|
#if defined(TSRM_WIN32) || defined(NETWARE)
|
|
|
|
if (path[path_length-1] == '\\' || path[path_length-1] == '/') {
|
|
|
|
#else
|
|
|
|
if (path[path_length-1] == '/') {
|
|
|
|
#endif
|
|
|
|
state->cwd = (char*)realloc(state->cwd, state->cwd_length + 2);
|
|
|
|
state->cwd[state->cwd_length++] = DEFAULT_SLASH;
|
|
|
|
state->cwd[state->cwd_length] = 0;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2006-09-20 15:35:59 +08:00
|
|
|
free(free_path);
|
|
|
|
|
2007-02-19 22:58:07 +08:00
|
|
|
if ((use_realpath == CWD_REALPATH) && ret) {
|
|
|
|
CWD_STATE_FREE(state);
|
|
|
|
*state = old_state;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-03-17 21:40:45 +08:00
|
|
|
if (state->cwd_length == COPY_WHEN_ABSOLUTE(state->cwd)) {
|
|
|
|
state->cwd = (char *) realloc(state->cwd, state->cwd_length+1+1);
|
|
|
|
state->cwd[state->cwd_length] = DEFAULT_SLASH;
|
|
|
|
state->cwd[state->cwd_length+1] = '\0';
|
|
|
|
state->cwd_length++;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
|
|
|
|
2007-06-18 17:50:40 +08:00
|
|
|
if (use_cache && (use_realpath == CWD_REALPATH)) {
|
2006-09-20 15:35:59 +08:00
|
|
|
realpath_cache_add(path, path_length, state->cwd, state->cwd_length, t TSRMLS_CC);
|
2004-10-05 08:42:25 +08:00
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2004-10-05 08:42:25 +08:00
|
|
|
if (verify_path && verify_path(state)) {
|
|
|
|
CWD_STATE_FREE(state);
|
|
|
|
*state = old_state;
|
2000-09-04 02:18:13 +08:00
|
|
|
ret = 1;
|
|
|
|
} else {
|
2004-10-05 08:42:25 +08:00
|
|
|
CWD_STATE_FREE(&old_state);
|
2000-09-04 02:18:13 +08:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if VIRTUAL_CWD_DEBUG
|
|
|
|
fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
|
|
|
|
#endif
|
|
|
|
return (ret);
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_chdir(const char *path TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
2006-11-13 23:13:20 +08:00
|
|
|
return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH)?-1:0;
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path TSRMLS_DC) TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
int length = strlen(path);
|
|
|
|
char *temp;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (length == 0) {
|
|
|
|
return 1; /* Can't cd to empty string */
|
|
|
|
}
|
|
|
|
while(--length >= 0 && !IS_SLASH(path[length])) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == -1) {
|
2000-10-03 23:08:37 +08:00
|
|
|
/* No directory only file name */
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
|
|
|
|
2002-05-29 16:41:21 +08:00
|
|
|
if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
|
2000-09-04 02:18:13 +08:00
|
|
|
length++;
|
|
|
|
}
|
2000-09-07 04:27:12 +08:00
|
|
|
temp = (char *) tsrm_do_alloca(length+1);
|
2000-09-04 02:18:13 +08:00
|
|
|
memcpy(temp, path, length);
|
|
|
|
temp[length] = 0;
|
|
|
|
#if VIRTUAL_CWD_DEBUG
|
|
|
|
fprintf (stderr, "Changing directory to %s\n", temp);
|
|
|
|
#endif
|
2001-08-05 09:34:40 +08:00
|
|
|
retval = p_chdir(temp TSRMLS_CC);
|
2000-09-07 04:27:12 +08:00
|
|
|
tsrm_free_alloca(temp);
|
2000-09-04 02:18:13 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
2003-06-25 13:05:06 +08:00
|
|
|
char *retval;
|
2007-07-10 21:21:11 +08:00
|
|
|
char cwd[MAXPATHLEN];
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-07-10 21:21:11 +08:00
|
|
|
/* realpath("") returns CWD */
|
|
|
|
if (!*path) {
|
|
|
|
new_state.cwd = (char*)malloc(1);
|
|
|
|
new_state.cwd[0] = '\0';
|
|
|
|
new_state.cwd_length = 0;
|
|
|
|
if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
|
|
|
|
path = cwd;
|
|
|
|
}
|
|
|
|
} else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
|
|
|
} else {
|
|
|
|
new_state.cwd = (char*)malloc(1);
|
|
|
|
new_state.cwd[0] = '\0';
|
|
|
|
new_state.cwd_length = 0;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)==0) {
|
2000-09-04 02:18:13 +08:00
|
|
|
int len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
|
2003-06-25 13:05:06 +08:00
|
|
|
|
2000-09-04 02:18:13 +08:00
|
|
|
memcpy(real_path, new_state.cwd, len);
|
|
|
|
real_path[len] = '\0';
|
2003-06-25 13:05:06 +08:00
|
|
|
retval = real_path;
|
|
|
|
} else {
|
|
|
|
retval = NULL;
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
|
|
|
|
return retval;
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH);
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
*filepath = new_state.cwd;
|
|
|
|
|
|
|
|
return retval;
|
2000-11-22 12:59:32 +08:00
|
|
|
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-11-22 12:59:32 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_filepath(const char *path, char **filepath TSRMLS_DC) /* {{{ */
|
2000-11-22 12:59:32 +08:00
|
|
|
{
|
2001-08-05 09:34:40 +08:00
|
|
|
return virtual_filepath_ex(path, filepath, php_is_file_ok TSRMLS_CC);
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API FILE *virtual_fopen(const char *path, const char *mode TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (path[0] == '\0') { /* Fail to open empty path */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
f = fopen(new_state.cwd, mode);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return f;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_access(const char *pathname, int mode TSRMLS_DC) /* {{{ */
|
2002-10-04 09:04:00 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-08-05 21:17:50 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2002-10-04 09:04:00 +08:00
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
#if defined(TSRM_WIN32)
|
|
|
|
ret = tsrm_win32_access(new_state.cwd, mode);
|
|
|
|
#else
|
2002-10-04 09:04:00 +08:00
|
|
|
ret = access(new_state.cwd, mode);
|
2003-06-25 13:05:06 +08:00
|
|
|
#endif
|
2002-10-04 09:04:00 +08:00
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2002-10-04 09:04:00 +08:00
|
|
|
|
2000-09-04 02:58:46 +08:00
|
|
|
#if HAVE_UTIME
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_utime(const char *filename, struct utimbuf *buf TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
ret = utime(new_state.cwd, buf);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_chmod(const char *filename, mode_t mode TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
ret = chmod(new_state.cwd, mode);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2002-05-29 16:41:21 +08:00
|
|
|
#if !defined(TSRM_WIN32) && !defined(NETWARE)
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2006-03-06 02:57:54 +08:00
|
|
|
if (link) {
|
|
|
|
#if HAVE_LCHOWN
|
|
|
|
ret = lchown(new_state.cwd, owner, group);
|
|
|
|
#else
|
|
|
|
ret = -1;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
ret = chown(new_state.cwd, owner, group);
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_open(const char *path TSRMLS_DC, int flags, ...) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int f;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
if (flags & O_CREAT) {
|
|
|
|
mode_t mode;
|
|
|
|
va_list arg;
|
|
|
|
|
|
|
|
va_start(arg, flags);
|
2001-11-18 05:15:38 +08:00
|
|
|
mode = (mode_t) va_arg(arg, int);
|
2000-09-04 02:18:13 +08:00
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
f = open(new_state.cwd, flags, mode);
|
|
|
|
} else {
|
|
|
|
f = open(new_state.cwd, flags);
|
|
|
|
}
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return f;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_creat(const char *path, mode_t mode TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int f;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
f = creat(new_state.cwd, mode);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return f;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_rename(char *oldname, char *newname TSRMLS_DC) /* {{{ */
|
2000-10-26 13:21:52 +08:00
|
|
|
{
|
|
|
|
cwd_state old_state;
|
|
|
|
cwd_state new_state;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&old_state, &CWDG(cwd));
|
2007-07-23 16:57:04 +08:00
|
|
|
if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&old_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-10-26 13:21:52 +08:00
|
|
|
oldname = old_state.cwd;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2007-07-23 16:57:04 +08:00
|
|
|
if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&old_state);
|
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-10-26 13:21:52 +08:00
|
|
|
newname = new_state.cwd;
|
|
|
|
|
|
|
|
retval = rename(oldname, newname);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&old_state);
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_stat(const char *path, struct stat *buf TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2005-12-20 22:24:14 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2006-11-10 23:04:07 +08:00
|
|
|
retval = php_sys_stat(new_state.cwd, buf);
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2005-02-21 17:56:28 +08:00
|
|
|
#if !defined(TSRM_WIN32)
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_lstat(const char *path, struct stat *buf TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
2002-11-05 07:24:15 +08:00
|
|
|
cwd_state new_state;
|
2000-09-04 02:18:13 +08:00
|
|
|
int retval;
|
|
|
|
|
2002-11-05 07:24:15 +08:00
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2007-05-30 18:50:47 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2002-11-05 07:24:15 +08:00
|
|
|
|
|
|
|
retval = lstat(new_state.cwd, buf);
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2002-11-05 07:24:15 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2000-09-04 02:18:13 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_unlink(const char *path TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
2002-11-05 07:24:15 +08:00
|
|
|
cwd_state new_state;
|
2000-09-04 02:18:13 +08:00
|
|
|
int retval;
|
|
|
|
|
2002-11-05 07:24:15 +08:00
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2002-11-05 07:24:15 +08:00
|
|
|
retval = unlink(new_state.cwd);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
2000-09-04 02:18:13 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_mkdir(const char *pathname, mode_t mode TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2000-09-04 02:47:35 +08:00
|
|
|
#ifdef TSRM_WIN32
|
|
|
|
retval = mkdir(new_state.cwd);
|
|
|
|
#else
|
2000-09-04 02:18:13 +08:00
|
|
|
retval = mkdir(new_state.cwd, mode);
|
2000-09-04 02:47:35 +08:00
|
|
|
#endif
|
2000-09-04 02:18:13 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API int virtual_rmdir(const char *pathname TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
retval = rmdir(new_state.cwd);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2000-09-04 02:47:35 +08:00
|
|
|
#ifdef TSRM_WIN32
|
|
|
|
DIR *opendir(const char *name);
|
|
|
|
#endif
|
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API DIR *virtual_opendir(const char *pathname TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
cwd_state new_state;
|
|
|
|
DIR *retval;
|
|
|
|
|
|
|
|
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
|
2007-01-16 22:02:19 +08:00
|
|
|
CWD_STATE_FREE(&new_state);
|
2006-09-28 23:48:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
retval = opendir(new_state.cwd);
|
|
|
|
|
|
|
|
CWD_STATE_FREE(&new_state);
|
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
2002-05-29 16:41:21 +08:00
|
|
|
#ifdef TSRM_WIN32
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
2002-10-29 15:32:52 +08:00
|
|
|
return popen_ex(command, type, CWDG(cwd).cwd, NULL);
|
2000-09-04 02:18:13 +08:00
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2002-05-29 16:41:21 +08:00
|
|
|
#elif defined(NETWARE)
|
|
|
|
/* On NetWare, the trick of prepending "cd cwd; " doesn't work so we need to perform
|
|
|
|
a VCWD_CHDIR() and mutex it
|
2000-09-04 02:18:13 +08:00
|
|
|
*/
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
|
2000-09-04 02:18:13 +08:00
|
|
|
{
|
|
|
|
char prev_cwd[MAXPATHLEN];
|
|
|
|
char *getcwd_result;
|
|
|
|
FILE *retval;
|
|
|
|
|
2002-05-29 16:41:21 +08:00
|
|
|
getcwd_result = VCWD_GETCWD(prev_cwd, MAXPATHLEN);
|
2000-09-04 02:18:13 +08:00
|
|
|
if (!getcwd_result) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
tsrm_mutex_lock(cwd_mutex);
|
|
|
|
#endif
|
|
|
|
|
2002-05-29 16:41:21 +08:00
|
|
|
VCWD_CHDIR(CWDG(cwd).cwd);
|
2001-04-28 00:41:53 +08:00
|
|
|
retval = popen(command, type);
|
2002-05-29 16:41:21 +08:00
|
|
|
VCWD_CHDIR(prev_cwd);
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
tsrm_mutex_unlock(cwd_mutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2002-05-29 16:41:21 +08:00
|
|
|
#else /* Unix */
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC) /* {{{ */
|
2002-05-29 16:41:21 +08:00
|
|
|
{
|
|
|
|
int command_length;
|
2004-12-02 09:04:23 +08:00
|
|
|
int dir_length, extra = 0;
|
2002-05-29 16:41:21 +08:00
|
|
|
char *command_line;
|
2004-12-02 09:04:23 +08:00
|
|
|
char *ptr, *dir;
|
2002-05-29 16:41:21 +08:00
|
|
|
FILE *retval;
|
|
|
|
|
|
|
|
command_length = strlen(command);
|
|
|
|
|
2004-12-02 09:04:23 +08:00
|
|
|
dir_length = CWDG(cwd).cwd_length;
|
|
|
|
dir = CWDG(cwd).cwd;
|
|
|
|
while (dir_length > 0) {
|
|
|
|
if (*dir == '\'') extra+=3;
|
|
|
|
dir++;
|
|
|
|
dir_length--;
|
|
|
|
}
|
|
|
|
dir_length = CWDG(cwd).cwd_length;
|
|
|
|
dir = CWDG(cwd).cwd;
|
|
|
|
|
2005-01-16 02:19:49 +08:00
|
|
|
ptr = command_line = (char *) malloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
|
2002-05-29 16:41:21 +08:00
|
|
|
if (!command_line) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(ptr, "cd ", sizeof("cd ")-1);
|
|
|
|
ptr += sizeof("cd ")-1;
|
|
|
|
|
|
|
|
if (CWDG(cwd).cwd_length == 0) {
|
|
|
|
*ptr++ = DEFAULT_SLASH;
|
|
|
|
} else {
|
2004-12-02 09:04:23 +08:00
|
|
|
*ptr++ = '\'';
|
|
|
|
while (dir_length > 0) {
|
|
|
|
switch (*dir) {
|
|
|
|
case '\'':
|
|
|
|
*ptr++ = '\'';
|
|
|
|
*ptr++ = '\\';
|
|
|
|
*ptr++ = '\'';
|
|
|
|
/* fall-through */
|
|
|
|
default:
|
|
|
|
*ptr++ = *dir;
|
|
|
|
}
|
|
|
|
dir++;
|
|
|
|
dir_length--;
|
|
|
|
}
|
|
|
|
*ptr++ = '\'';
|
2002-05-29 16:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*ptr++ = ' ';
|
|
|
|
*ptr++ = ';';
|
|
|
|
*ptr++ = ' ';
|
|
|
|
|
|
|
|
memcpy(ptr, command, command_length+1);
|
|
|
|
retval = popen(command_line, type);
|
|
|
|
|
|
|
|
free(command_line);
|
|
|
|
return retval;
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
#endif
|
|
|
|
|
2007-08-10 17:09:46 +08:00
|
|
|
CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC) /* {{{ */
|
2006-02-09 04:50:03 +08:00
|
|
|
{
|
2006-11-10 20:59:27 +08:00
|
|
|
cwd_state new_state;
|
|
|
|
char cwd[MAXPATHLEN];
|
2006-02-09 04:50:03 +08:00
|
|
|
|
2007-07-10 21:21:11 +08:00
|
|
|
/* realpath("") returns CWD */
|
|
|
|
if (!*path) {
|
|
|
|
new_state.cwd = (char*)malloc(1);
|
|
|
|
new_state.cwd[0] = '\0';
|
|
|
|
new_state.cwd_length = 0;
|
|
|
|
if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
|
|
|
|
path = cwd;
|
|
|
|
}
|
|
|
|
} else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
|
2006-11-10 20:59:27 +08:00
|
|
|
VCWD_GETCWD(cwd, MAXPATHLEN)) {
|
|
|
|
new_state.cwd = strdup(cwd);
|
|
|
|
new_state.cwd_length = strlen(cwd);
|
2006-02-09 04:50:03 +08:00
|
|
|
} else {
|
2006-11-10 20:59:27 +08:00
|
|
|
new_state.cwd = (char*)malloc(1);
|
|
|
|
new_state.cwd[0] = '\0';
|
|
|
|
new_state.cwd_length = 0;
|
|
|
|
}
|
|
|
|
|
2006-11-13 23:13:20 +08:00
|
|
|
if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
|
2006-11-10 20:59:27 +08:00
|
|
|
free(new_state.cwd);
|
|
|
|
return NULL;
|
2006-02-09 04:50:03 +08:00
|
|
|
}
|
|
|
|
|
2006-11-10 20:59:27 +08:00
|
|
|
if (real_path) {
|
|
|
|
int copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
|
|
|
|
memcpy(real_path, new_state.cwd, copy_len);
|
|
|
|
real_path[copy_len] = '\0';
|
|
|
|
free(new_state.cwd);
|
|
|
|
return real_path;
|
|
|
|
} else {
|
|
|
|
return new_state.cwd;
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 17:09:46 +08:00
|
|
|
/* }}} */
|
2000-09-04 02:18:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|