Back to index

php5  5.3.10
tsrm_virtual_cwd.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Sascha Schumann <sascha@schumann.cx>                        |
00017    |          Pierre Joye <pierre@php.net>                                |
00018    +----------------------------------------------------------------------+
00019 */
00020 
00021 /* $Id: tsrm_virtual_cwd.h 321634 2012-01-01 13:15:04Z felipe $ */
00022 
00023 #ifndef VIRTUAL_CWD_H
00024 #define VIRTUAL_CWD_H
00025 
00026 #include "TSRM.h"
00027 #include "tsrm_config_common.h"
00028 
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <ctype.h>
00032 
00033 #ifdef HAVE_UTIME_H
00034 #include <utime.h>
00035 #endif
00036 
00037 #ifdef HAVE_STDARG_H
00038 #include <stdarg.h>
00039 #endif
00040 
00041 #ifdef ZTS
00042 #define VIRTUAL_DIR
00043 #endif
00044 
00045 #ifndef TSRM_WIN32
00046 #include <unistd.h>
00047 #else
00048 #include <direct.h>
00049 #endif
00050 
00051 #if defined(__osf__) || defined(_AIX)
00052 #include <errno.h>
00053 #endif
00054 
00055 #ifdef TSRM_WIN32
00056 #include "readdir.h"
00057 #include <sys/utime.h>
00058 /* mode_t isn't defined on Windows */
00059 typedef unsigned short mode_t;
00060 
00061 #define DEFAULT_SLASH '\\'
00062 #define DEFAULT_DIR_SEPARATOR      ';'
00063 #define IS_SLASH(c)  ((c) == '/' || (c) == '\\')
00064 #define IS_SLASH_P(c)       (*(c) == '/' || \
00065         (*(c) == '\\' && !IsDBCSLeadByte(*(c-1))))
00066 
00067 /* COPY_WHEN_ABSOLUTE is 2 under Win32 because by chance both regular absolute paths
00068    in the file system and UNC paths need copying of two characters */
00069 #define COPY_WHEN_ABSOLUTE(path) 2
00070 #define IS_UNC_PATH(path, len) \
00071        (len >= 2 && IS_SLASH(path[0]) && IS_SLASH(path[1]))
00072 #define IS_ABSOLUTE_PATH(path, len) \
00073        (len >= 2 && ((isalpha(path[0]) && path[1] == ':') || IS_UNC_PATH(path, len)))
00074 
00075 #elif defined(NETWARE)
00076 #ifdef HAVE_DIRENT_H
00077 #include <dirent.h>
00078 #endif
00079 
00080 #define DEFAULT_SLASH '/'
00081 #define DEFAULT_DIR_SEPARATOR      ';'
00082 #define IS_SLASH(c)  ((c) == '/' || (c) == '\\')
00083 #define IS_SLASH_P(c)       IS_SLASH(*(c))
00084 /* Colon indicates volume name, either first character should be forward slash or backward slash */
00085 #define IS_ABSOLUTE_PATH(path, len) \
00086     ((strchr(path, ':') != NULL) || ((len >= 1) && ((path[0] == '/') || (path[0] == '\\'))))
00087 
00088 #else
00089 #ifdef HAVE_DIRENT_H
00090 #include <dirent.h>
00091 #endif
00092 
00093 #define DEFAULT_SLASH '/'
00094 
00095 #ifdef __riscos__
00096 #define DEFAULT_DIR_SEPARATOR  ';'
00097 #else
00098 #define DEFAULT_DIR_SEPARATOR  ':'
00099 #endif
00100 
00101 #define IS_SLASH(c)  ((c) == '/')
00102 #define IS_SLASH_P(c)       (*(c) == '/')
00103 
00104 #endif
00105 
00106 
00107 #ifndef COPY_WHEN_ABSOLUTE
00108 #define COPY_WHEN_ABSOLUTE(path) 0
00109 #endif
00110 
00111 #ifndef IS_ABSOLUTE_PATH
00112 #define IS_ABSOLUTE_PATH(path, len) \
00113        (IS_SLASH(path[0]))
00114 #endif
00115 
00116 #ifdef TSRM_EXPORTS
00117 #define CWD_EXPORTS
00118 #endif
00119 
00120 #ifdef TSRM_WIN32
00121 #      ifdef CWD_EXPORTS
00122 #             define CWD_API __declspec(dllexport)
00123 #      else
00124 #             define CWD_API __declspec(dllimport)
00125 #      endif
00126 #elif defined(__GNUC__) && __GNUC__ >= 4
00127 #      define CWD_API __attribute__ ((visibility("default")))
00128 #else
00129 #      define CWD_API
00130 #endif
00131 
00132 #ifdef TSRM_WIN32
00133 CWD_API int php_sys_stat_ex(const char *path, struct stat *buf, int lstat);
00134 CWD_API int php_sys_stat(const char *path, struct stat *buf);
00135 CWD_API int php_sys_lstat(const char *path, struct stat *buf);
00136 CWD_API int php_sys_readlink(const char *link, char *target, size_t target_len);
00137 #else
00138 # define php_sys_stat stat
00139 # define php_sys_lstat lstat
00140 # ifdef HAVE_SYMLINK
00141 # define php_sys_readlink(link, target, target_len) readlink(link, target, target_len)
00142 # endif
00143 #endif
00144 
00145 typedef struct _cwd_state {
00146        char *cwd;
00147        int cwd_length;
00148 } cwd_state;
00149 
00150 typedef int (*verify_path_func)(const cwd_state *);
00151 
00152 CWD_API void virtual_cwd_startup(void);
00153 CWD_API void virtual_cwd_shutdown(void);
00154 CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC);
00155 CWD_API char *virtual_getcwd(char *buf, size_t size TSRMLS_DC);
00156 CWD_API int virtual_chdir(const char *path TSRMLS_DC);
00157 CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path TSRMLS_DC) TSRMLS_DC);
00158 CWD_API int virtual_filepath(const char *path, char **filepath TSRMLS_DC);
00159 CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path TSRMLS_DC);
00160 CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC);
00161 CWD_API FILE *virtual_fopen(const char *path, const char *mode TSRMLS_DC);
00162 CWD_API int virtual_open(const char *path TSRMLS_DC, int flags, ...);
00163 CWD_API int virtual_creat(const char *path, mode_t mode TSRMLS_DC);
00164 CWD_API int virtual_rename(char *oldname, char *newname TSRMLS_DC);
00165 CWD_API int virtual_stat(const char *path, struct stat *buf TSRMLS_DC);
00166 CWD_API int virtual_lstat(const char *path, struct stat *buf TSRMLS_DC);
00167 CWD_API int virtual_unlink(const char *path TSRMLS_DC);
00168 CWD_API int virtual_mkdir(const char *pathname, mode_t mode TSRMLS_DC);
00169 CWD_API int virtual_rmdir(const char *pathname TSRMLS_DC);
00170 CWD_API DIR *virtual_opendir(const char *pathname TSRMLS_DC);
00171 CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC);
00172 CWD_API int virtual_access(const char *pathname, int mode TSRMLS_DC);
00173 #if defined(TSRM_WIN32)
00174 /* these are not defined in win32 headers */
00175 #ifndef W_OK
00176 #define W_OK 0x02
00177 #endif
00178 #ifndef R_OK
00179 #define R_OK 0x04
00180 #endif
00181 #ifndef X_OK
00182 #define X_OK 0x01
00183 #endif
00184 #ifndef F_OK
00185 #define F_OK 0x00
00186 #endif
00187 #endif
00188 
00189 #if HAVE_UTIME
00190 CWD_API int virtual_utime(const char *filename, struct utimbuf *buf TSRMLS_DC);
00191 #endif
00192 CWD_API int virtual_chmod(const char *filename, mode_t mode TSRMLS_DC);
00193 #if !defined(TSRM_WIN32) && !defined(NETWARE)
00194 CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link TSRMLS_DC);
00195 #endif
00196 
00197 /* One of the following constants must be used as the last argument
00198    in virtual_file_ex() call. */
00199 
00200 #define CWD_EXPAND   0 /* expand "." and ".." but dont resolve symlinks      */
00201 #define CWD_FILEPATH 1 /* resolve symlinks if file is exist otherwise expand */
00202 #define CWD_REALPATH 2 /* call realpath(), resolve symlinks. File must exist */
00203 
00204 CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath);
00205 
00206 CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC);
00207 
00208 #define REALPATH_CACHE_TTL  (2*60) /* 2 minutes */
00209 #define REALPATH_CACHE_SIZE 0      /* disabled while php.ini isn't loaded */
00210 
00211 typedef struct _realpath_cache_bucket {
00212        unsigned long                  key;
00213        char                          *path;
00214        int                            path_len;
00215        char                          *realpath;
00216        int                            realpath_len;
00217        int                            is_dir;
00218        time_t                         expires;
00219 #ifdef PHP_WIN32
00220        unsigned char                  is_rvalid;
00221        unsigned char                  is_readable;
00222        unsigned char                  is_wvalid;
00223        unsigned char                  is_writable;
00224 #endif
00225        struct _realpath_cache_bucket *next;
00226 } realpath_cache_bucket;
00227 
00228 typedef struct _virtual_cwd_globals {
00229        cwd_state cwd;
00230        long                   realpath_cache_size;
00231        long                   realpath_cache_size_limit;
00232        long                   realpath_cache_ttl;
00233        realpath_cache_bucket *realpath_cache[1024];
00234 } virtual_cwd_globals;
00235 
00236 #ifdef ZTS
00237 extern ts_rsrc_id cwd_globals_id;
00238 # define CWDG(v) TSRMG(cwd_globals_id, virtual_cwd_globals *, v)
00239 #else
00240 extern virtual_cwd_globals cwd_globals;
00241 # define CWDG(v) (cwd_globals.v)
00242 #endif
00243 
00244 CWD_API void realpath_cache_clean(TSRMLS_D);
00245 CWD_API void realpath_cache_del(const char *path, int path_len TSRMLS_DC);
00246 CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, int path_len, time_t t TSRMLS_DC);
00247 CWD_API int realpath_cache_size(TSRMLS_D);
00248 CWD_API int realpath_cache_max_buckets(TSRMLS_D);
00249 CWD_API realpath_cache_bucket** realpath_cache_get_buckets(TSRMLS_D);
00250 
00251 /* The actual macros to be used in programs using TSRM
00252  * If the program defines VIRTUAL_DIR it will use the
00253  * virtual_* functions
00254  */
00255 
00256 #ifdef VIRTUAL_DIR
00257 
00258 #define VCWD_GETCWD(buff, size) virtual_getcwd(buff, size TSRMLS_CC)
00259 #define VCWD_FOPEN(path, mode) virtual_fopen(path, mode TSRMLS_CC)
00260 /* Because open() has two modes, we have to macros to replace it */
00261 #define VCWD_OPEN(path, flags) virtual_open(path TSRMLS_CC, flags)
00262 #define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path TSRMLS_CC, flags, mode)
00263 #define VCWD_CREAT(path, mode) virtual_creat(path, mode TSRMLS_CC)
00264 #define VCWD_CHDIR(path) virtual_chdir(path TSRMLS_CC)
00265 #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, virtual_chdir TSRMLS_CC)
00266 #define VCWD_GETWD(buf)
00267 #define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path TSRMLS_CC)
00268 #define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname TSRMLS_CC)
00269 #define VCWD_STAT(path, buff) virtual_stat(path, buff TSRMLS_CC)
00270 #define VCWD_LSTAT(path, buff) virtual_lstat(path, buff TSRMLS_CC)
00271 #define VCWD_UNLINK(path) virtual_unlink(path TSRMLS_CC)
00272 #define VCWD_MKDIR(pathname, mode) virtual_mkdir(pathname, mode TSRMLS_CC)
00273 #define VCWD_RMDIR(pathname) virtual_rmdir(pathname TSRMLS_CC)
00274 #define VCWD_OPENDIR(pathname) virtual_opendir(pathname TSRMLS_CC)
00275 #define VCWD_POPEN(command, type) virtual_popen(command, type TSRMLS_CC)
00276 #define VCWD_ACCESS(pathname, mode) virtual_access(pathname, mode TSRMLS_CC)
00277 #if HAVE_UTIME
00278 #define VCWD_UTIME(path, time) virtual_utime(path, time TSRMLS_CC)
00279 #endif
00280 #define VCWD_CHMOD(path, mode) virtual_chmod(path, mode TSRMLS_CC)
00281 #if !defined(TSRM_WIN32) && !defined(NETWARE)
00282 #define VCWD_CHOWN(path, owner, group) virtual_chown(path, owner, group, 0 TSRMLS_CC)
00283 #if HAVE_LCHOWN
00284 #define VCWD_LCHOWN(path, owner, group) virtual_chown(path, owner, group, 1 TSRMLS_CC)
00285 #endif
00286 #endif
00287 
00288 #else
00289 
00290 #define VCWD_GETCWD(buff, size) getcwd(buff, size)
00291 #define VCWD_FOPEN(path, mode)  fopen(path, mode)
00292 #define VCWD_OPEN(path, flags) open(path, flags)
00293 #define VCWD_OPEN_MODE(path, flags, mode) open(path, flags, mode)
00294 #define VCWD_CREAT(path, mode) creat(path, mode)
00295 /* rename on windows will fail if newname already exists.
00296    MoveFileEx has to be used */
00297 #if defined(TSRM_WIN32)
00298 # define VCWD_RENAME(oldname, newname) (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED) == 0 ? -1 : 0)
00299 #else
00300 # define VCWD_RENAME(oldname, newname) rename(oldname, newname)
00301 #endif
00302 #define VCWD_CHDIR(path) chdir(path)
00303 #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
00304 #define VCWD_GETWD(buf) getwd(buf)
00305 #define VCWD_STAT(path, buff) php_sys_stat(path, buff)
00306 #define VCWD_LSTAT(path, buff) lstat(path, buff)
00307 #define VCWD_UNLINK(path) unlink(path)
00308 #define VCWD_MKDIR(pathname, mode) mkdir(pathname, mode)
00309 #define VCWD_RMDIR(pathname) rmdir(pathname)
00310 #define VCWD_OPENDIR(pathname) opendir(pathname)
00311 #define VCWD_POPEN(command, type) popen(command, type)
00312 #if defined(TSRM_WIN32)
00313 #define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode)
00314 #else
00315 #define VCWD_ACCESS(pathname, mode) access(pathname, mode)
00316 #endif
00317 
00318 #define VCWD_REALPATH(path, real_path) tsrm_realpath(path, real_path TSRMLS_CC)
00319 
00320 #if HAVE_UTIME
00321 # ifdef TSRM_WIN32
00322 #  define VCWD_UTIME(path, time) win32_utime(path, time)
00323 # else
00324 #  define VCWD_UTIME(path, time) utime(path, time)
00325 # endif
00326 #endif
00327 
00328 #define VCWD_CHMOD(path, mode) chmod(path, mode)
00329 #if !defined(TSRM_WIN32) && !defined(NETWARE)
00330 #define VCWD_CHOWN(path, owner, group) chown(path, owner, group)
00331 #if HAVE_LCHOWN
00332 #define VCWD_LCHOWN(path, owner, group) lchown(path, owner, group)
00333 #endif
00334 #endif
00335 
00336 #endif
00337 
00338 #endif /* VIRTUAL_CWD_H */