xref: /PHP-7.1/Zend/zend_virtual_cwd.h (revision ccd4716e)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 #ifndef VIRTUAL_CWD_H
24 #define VIRTUAL_CWD_H
25 
26 #include "TSRM.h"
27 #include "tsrm_config_common.h"
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <ctype.h>
32 
33 #ifdef HAVE_UTIME_H
34 #include <utime.h>
35 #endif
36 
37 #ifdef HAVE_STDARG_H
38 #include <stdarg.h>
39 #endif
40 
41 #ifdef ZTS
42 #define VIRTUAL_DIR
43 #endif
44 
45 #ifndef ZEND_WIN32
46 #include <unistd.h>
47 #else
48 #include <direct.h>
49 #endif
50 
51 #if defined(__osf__) || defined(_AIX)
52 #include <errno.h>
53 #endif
54 
55 #ifdef ZEND_WIN32
56 #include "readdir.h"
57 #include <sys/utime.h>
58 #include "win32/ioutil.h"
59 /* mode_t isn't defined on Windows */
60 typedef unsigned short mode_t;
61 
62 #define DEFAULT_SLASH '\\'
63 #define DEFAULT_DIR_SEPARATOR	';'
64 #define IS_SLASH(c)	((c) == '/' || (c) == '\\')
65 #define IS_SLASH_P(c)	(*(c) == '/' || \
66         (*(c) == '\\' && !IsDBCSLeadByte(*(c-1))))
67 
68 /* COPY_WHEN_ABSOLUTE is 2 under Win32 because by chance both regular absolute paths
69    in the file system and UNC paths need copying of two characters */
70 #define COPY_WHEN_ABSOLUTE(path) 2
71 #define IS_UNC_PATH(path, len) \
72 	(len >= 2 && IS_SLASH(path[0]) && IS_SLASH(path[1]))
73 #define IS_ABSOLUTE_PATH(path, len) \
74 	(len >= 2 && (/* is local */isalpha(path[0]) && path[1] == ':' || /* is UNC */IS_SLASH(path[0]) && IS_SLASH(path[1])))
75 
76 #elif defined(NETWARE)
77 #ifdef HAVE_DIRENT_H
78 #include <dirent.h>
79 #endif
80 
81 #define DEFAULT_SLASH '/'
82 #define DEFAULT_DIR_SEPARATOR	';'
83 #define IS_SLASH(c)	((c) == '/' || (c) == '\\')
84 #define IS_SLASH_P(c)	IS_SLASH(*(c))
85 /* Colon indicates volume name, either first character should be forward slash or backward slash */
86 #define IS_ABSOLUTE_PATH(path, len) \
87     ((strchr(path, ':') != NULL) || ((len >= 1) && ((path[0] == '/') || (path[0] == '\\'))))
88 
89 #else
90 #ifdef HAVE_DIRENT_H
91 #include <dirent.h>
92 #endif
93 
94 #define DEFAULT_SLASH '/'
95 
96 #ifdef __riscos__
97 #define DEFAULT_DIR_SEPARATOR  ';'
98 #else
99 #define DEFAULT_DIR_SEPARATOR  ':'
100 #endif
101 
102 #define IS_SLASH(c)	((c) == '/')
103 #define IS_SLASH_P(c)	(*(c) == '/')
104 
105 #endif
106 
107 
108 #ifndef COPY_WHEN_ABSOLUTE
109 #define COPY_WHEN_ABSOLUTE(path) 0
110 #endif
111 
112 #ifndef IS_ABSOLUTE_PATH
113 #define IS_ABSOLUTE_PATH(path, len) \
114 	(IS_SLASH(path[0]))
115 #endif
116 
117 #ifdef TSRM_EXPORTS
118 #define CWD_EXPORTS
119 #endif
120 
121 #ifdef ZEND_WIN32
122 #	ifdef CWD_EXPORTS
123 #		define CWD_API __declspec(dllexport)
124 #	else
125 #		define CWD_API __declspec(dllimport)
126 #	endif
127 #elif defined(__GNUC__) && __GNUC__ >= 4
128 #	define CWD_API __attribute__ ((visibility("default")))
129 #else
130 #	define CWD_API
131 #endif
132 
133 #ifdef ZEND_WIN32
134 CWD_API int php_sys_stat_ex(const char *path, zend_stat_t *buf, int lstat);
135 # define php_sys_stat(path, buf) php_sys_stat_ex(path, buf, 0)
136 # define php_sys_lstat(path, buf) php_sys_stat_ex(path, buf, 1)
137 CWD_API int php_sys_readlink(const char *link, char *target, size_t target_len);
138 #else
139 # define php_sys_stat stat
140 # define php_sys_lstat lstat
141 # ifdef HAVE_SYMLINK
142 # define php_sys_readlink(link, target, target_len) readlink(link, target, target_len)
143 # endif
144 #endif
145 
146 typedef struct _cwd_state {
147 	char *cwd;
148 	int cwd_length;
149 } cwd_state;
150 
151 typedef int (*verify_path_func)(const cwd_state *);
152 
153 CWD_API void virtual_cwd_startup(void);
154 CWD_API void virtual_cwd_shutdown(void);
155 CWD_API int virtual_cwd_activate(void);
156 CWD_API int virtual_cwd_deactivate(void);
157 CWD_API char *virtual_getcwd_ex(size_t *length);
158 CWD_API char *virtual_getcwd(char *buf, size_t size);
159 CWD_API int virtual_chdir(const char *path);
160 CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path));
161 CWD_API int virtual_filepath(const char *path, char **filepath);
162 CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path);
163 CWD_API char *virtual_realpath(const char *path, char *real_path);
164 CWD_API FILE *virtual_fopen(const char *path, const char *mode);
165 CWD_API int virtual_open(const char *path, int flags, ...);
166 CWD_API int virtual_creat(const char *path, mode_t mode);
167 CWD_API int virtual_rename(const char *oldname, const char *newname);
168 CWD_API int virtual_stat(const char *path, zend_stat_t *buf);
169 CWD_API int virtual_lstat(const char *path, zend_stat_t *buf);
170 CWD_API int virtual_unlink(const char *path);
171 CWD_API int virtual_mkdir(const char *pathname, mode_t mode);
172 CWD_API int virtual_rmdir(const char *pathname);
173 CWD_API DIR *virtual_opendir(const char *pathname);
174 CWD_API FILE *virtual_popen(const char *command, const char *type);
175 CWD_API int virtual_access(const char *pathname, int mode);
176 #if defined(ZEND_WIN32)
177 /* these are not defined in win32 headers */
178 #ifndef W_OK
179 #define W_OK 0x02
180 #endif
181 #ifndef R_OK
182 #define R_OK 0x04
183 #endif
184 #ifndef X_OK
185 #define X_OK 0x01
186 #endif
187 #ifndef F_OK
188 #define F_OK 0x00
189 #endif
190 #endif
191 
192 #if HAVE_UTIME
193 CWD_API int virtual_utime(const char *filename, struct utimbuf *buf);
194 #endif
195 CWD_API int virtual_chmod(const char *filename, mode_t mode);
196 #if !defined(ZEND_WIN32) && !defined(NETWARE)
197 CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link);
198 #endif
199 
200 /* One of the following constants must be used as the last argument
201    in virtual_file_ex() call. */
202 
203 #define CWD_EXPAND   0 /* expand "." and ".." but dont resolve symlinks      */
204 #define CWD_FILEPATH 1 /* resolve symlinks if file is exist otherwise expand */
205 #define CWD_REALPATH 2 /* call realpath(), resolve symlinks. File must exist */
206 
207 CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath);
208 
209 CWD_API char *tsrm_realpath(const char *path, char *real_path);
210 
211 #define REALPATH_CACHE_TTL  (2*60) /* 2 minutes */
212 #define REALPATH_CACHE_SIZE 0      /* disabled while php.ini isn't loaded */
213 
214 typedef struct _realpath_cache_bucket {
215 	zend_ulong                    key;
216 	char                          *path;
217 	char                          *realpath;
218 	struct _realpath_cache_bucket *next;
219 	time_t                         expires;
220 	int                            path_len;
221 	int                            realpath_len;
222 	int                            is_dir;
223 #ifdef ZEND_WIN32
224 	unsigned char                  is_rvalid;
225 	unsigned char                  is_readable;
226 	unsigned char                  is_wvalid;
227 	unsigned char                  is_writable;
228 #endif
229 } realpath_cache_bucket;
230 
231 typedef struct _virtual_cwd_globals {
232 	cwd_state cwd;
233 	zend_long                   realpath_cache_size;
234 	zend_long                   realpath_cache_size_limit;
235 	zend_long                   realpath_cache_ttl;
236 	realpath_cache_bucket *realpath_cache[1024];
237 } virtual_cwd_globals;
238 
239 #ifdef ZTS
240 extern ts_rsrc_id cwd_globals_id;
241 # define CWDG(v) ZEND_TSRMG(cwd_globals_id, virtual_cwd_globals *, v)
242 #else
243 extern virtual_cwd_globals cwd_globals;
244 # define CWDG(v) (cwd_globals.v)
245 #endif
246 
247 CWD_API void realpath_cache_clean(void);
248 CWD_API void realpath_cache_del(const char *path, int path_len);
249 CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, int path_len, time_t t);
250 CWD_API zend_long realpath_cache_size(void);
251 CWD_API zend_long realpath_cache_max_buckets(void);
252 CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void);
253 
254 #ifdef CWD_EXPORTS
255 extern void virtual_cwd_main_cwd_init(uint8_t);
256 #endif
257 
258 /* The actual macros to be used in programs using TSRM
259  * If the program defines VIRTUAL_DIR it will use the
260  * virtual_* functions
261  */
262 
263 #ifdef VIRTUAL_DIR
264 
265 #define VCWD_GETCWD(buff, size) virtual_getcwd(buff, size)
266 #define VCWD_FOPEN(path, mode) virtual_fopen(path, mode)
267 /* Because open() has two modes, we have to macros to replace it */
268 #define VCWD_OPEN(path, flags) virtual_open(path, flags)
269 #define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path, flags, mode)
270 #define VCWD_CREAT(path, mode) virtual_creat(path, mode)
271 #define VCWD_CHDIR(path) virtual_chdir(path)
272 #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, virtual_chdir)
273 #define VCWD_GETWD(buf)
274 #define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path)
275 #define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname)
276 #define VCWD_STAT(path, buff) virtual_stat(path, buff)
277 # define VCWD_LSTAT(path, buff) virtual_lstat(path, buff)
278 #define VCWD_UNLINK(path) virtual_unlink(path)
279 #define VCWD_MKDIR(pathname, mode) virtual_mkdir(pathname, mode)
280 #define VCWD_RMDIR(pathname) virtual_rmdir(pathname)
281 #define VCWD_OPENDIR(pathname) virtual_opendir(pathname)
282 #define VCWD_POPEN(command, type) virtual_popen(command, type)
283 #define VCWD_ACCESS(pathname, mode) virtual_access(pathname, mode)
284 #if HAVE_UTIME
285 #define VCWD_UTIME(path, time) virtual_utime(path, time)
286 #endif
287 #define VCWD_CHMOD(path, mode) virtual_chmod(path, mode)
288 #if !defined(ZEND_WIN32) && !defined(NETWARE)
289 #define VCWD_CHOWN(path, owner, group) virtual_chown(path, owner, group, 0)
290 #if HAVE_LCHOWN
291 #define VCWD_LCHOWN(path, owner, group) virtual_chown(path, owner, group, 1)
292 #endif
293 #endif
294 
295 #else
296 
297 #define VCWD_CREAT(path, mode) creat(path, mode)
298 /* rename on windows will fail if newname already exists.
299    MoveFileEx has to be used */
300 #if defined(ZEND_WIN32)
301 #define VCWD_FOPEN(path, mode)  php_win32_ioutil_fopen(path, mode)
302 #define VCWD_OPEN(path, flags) php_win32_ioutil_open(path, flags)
303 #define VCWD_OPEN_MODE(path, flags, mode) php_win32_ioutil_open(path, flags, mode)
304 # define VCWD_RENAME(oldname, newname) php_win32_ioutil_rename(oldname, newname)
305 #define VCWD_MKDIR(pathname, mode) php_win32_ioutil_mkdir(pathname, mode)
306 #define VCWD_RMDIR(pathname) php_win32_ioutil_rmdir(pathname)
307 #define VCWD_UNLINK(path) php_win32_ioutil_unlink(path)
308 #define VCWD_CHDIR(path) php_win32_ioutil_chdir(path)
309 #define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode)
310 #define VCWD_GETCWD(buff, size) php_win32_ioutil_getcwd(buff, size)
311 #define VCWD_CHMOD(path, mode) php_win32_ioutil_chmod(path, mode)
312 #else
313 #define VCWD_FOPEN(path, mode)  fopen(path, mode)
314 #define VCWD_OPEN(path, flags) open(path, flags)
315 #define VCWD_OPEN_MODE(path, flags, mode)	open(path, flags, mode)
316 # define VCWD_RENAME(oldname, newname) rename(oldname, newname)
317 #define VCWD_MKDIR(pathname, mode) mkdir(pathname, mode)
318 #define VCWD_RMDIR(pathname) rmdir(pathname)
319 #define VCWD_UNLINK(path) unlink(path)
320 #define VCWD_CHDIR(path) chdir(path)
321 #define VCWD_ACCESS(pathname, mode) access(pathname, mode)
322 #define VCWD_GETCWD(buff, size) getcwd(buff, size)
323 #define VCWD_CHMOD(path, mode) chmod(path, mode)
324 #endif
325 
326 #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
327 #define VCWD_GETWD(buf) getwd(buf)
328 #define VCWD_STAT(path, buff) php_sys_stat(path, buff)
329 #define VCWD_LSTAT(path, buff) lstat(path, buff)
330 #define VCWD_OPENDIR(pathname) opendir(pathname)
331 #define VCWD_POPEN(command, type) popen(command, type)
332 
333 #define VCWD_REALPATH(path, real_path) tsrm_realpath(path, real_path)
334 
335 #if HAVE_UTIME
336 # ifdef ZEND_WIN32
337 #  define VCWD_UTIME(path, time) win32_utime(path, time)
338 # else
339 #  define VCWD_UTIME(path, time) utime(path, time)
340 # endif
341 #endif
342 
343 #if !defined(ZEND_WIN32) && !defined(NETWARE)
344 #define VCWD_CHOWN(path, owner, group) chown(path, owner, group)
345 #if HAVE_LCHOWN
346 #define VCWD_LCHOWN(path, owner, group) lchown(path, owner, group)
347 #endif
348 #endif
349 
350 #endif
351 
352 /* Global stat declarations */
353 #ifndef _S_IFDIR
354 #define _S_IFDIR S_IFDIR
355 #endif
356 
357 #ifndef _S_IFREG
358 #define _S_IFREG S_IFREG
359 #endif
360 
361 #ifndef S_IFLNK
362 # define S_IFLNK 0120000
363 #endif
364 
365 #ifndef S_ISDIR
366 #define S_ISDIR(mode)	(((mode)&S_IFMT) == S_IFDIR)
367 #endif
368 
369 #ifndef S_ISREG
370 #define S_ISREG(mode)	(((mode)&S_IFMT) == S_IFREG)
371 #endif
372 
373 #ifndef S_ISLNK
374 #define S_ISLNK(mode)	(((mode)&S_IFMT) == S_IFLNK)
375 #endif
376 
377 #ifndef S_IXROOT
378 #define S_IXROOT ( S_IXUSR | S_IXGRP | S_IXOTH )
379 #endif
380 
381 #endif /* VIRTUAL_CWD_H */
382