php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_virtual_cwd.h
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Andi Gutmans <andi@php.net> |
14 | Sascha Schumann <sascha@schumann.cx> |
15 | Pierre Joye <pierre@php.net> |
16 +----------------------------------------------------------------------+
17*/
18
19#ifndef VIRTUAL_CWD_H
20#define VIRTUAL_CWD_H
21
22#include "TSRM.h"
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <ctype.h>
27
28#ifdef HAVE_UTIME_H
29#include <utime.h>
30#endif
31
32#include <stdarg.h>
33#include <limits.h>
34
35#ifdef HAVE_SYS_PARAM_H
36# include <sys/param.h>
37#endif
38
39#ifndef MAXPATHLEN
40# ifdef _WIN32
41# include "win32/ioutil.h"
42# define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
43# elif PATH_MAX
44# define MAXPATHLEN PATH_MAX
45# elif defined(MAX_PATH)
46# define MAXPATHLEN MAX_PATH
47# else
48# define MAXPATHLEN 256
49# endif
50#endif
51
52#ifdef ZTS
53#define VIRTUAL_DIR
54#endif
55
56#ifndef ZEND_WIN32
57#include <unistd.h>
58#else
59#include <direct.h>
60#endif
61
62#if defined(__osf__) || defined(_AIX)
63#include <errno.h>
64#endif
65
66#include "zend_stream.h"
67
68#ifdef ZEND_WIN32
69#include "win32/readdir.h"
70#include <sys/utime.h>
71#include "win32/ioutil.h"
72/* mode_t isn't defined on Windows */
73typedef unsigned short mode_t;
74
75#define DEFAULT_SLASH '\\'
76#define DEFAULT_DIR_SEPARATOR ';'
77#define IS_SLASH(c) ((c) == '/' || (c) == '\\')
78// IS_SLASH_P() may read the previous char on Windows, which may be OOB; use IS_SLASH_P_EX() instead
79#define IS_SLASH_P(c) (*(c) == '/' || \
80 (*(c) == '\\' && !IsDBCSLeadByte(*(c-1))))
81#define IS_SLASH_P_EX(c, first_byte) (*(c) == '/' || \
82 (*(c) == '\\' && ((first_byte) || !IsDBCSLeadByte(*(c-1)))))
83
84/* COPY_WHEN_ABSOLUTE is 2 under Win32 because by chance both regular absolute paths
85 in the file system and UNC paths need copying of two characters */
86#define COPY_WHEN_ABSOLUTE(path) 2
87#define IS_UNC_PATH(path, len) \
88 (len >= 2 && IS_SLASH(path[0]) && IS_SLASH(path[1]))
89#define IS_ABSOLUTE_PATH(path, len) \
90 (len >= 2 && (/* is local */isalpha(path[0]) && path[1] == ':' || /* is UNC */IS_SLASH(path[0]) && IS_SLASH(path[1])))
91
92#else
93#ifdef HAVE_DIRENT_H
94#include <dirent.h>
95
96#ifndef DT_UNKNOWN
97# define DT_UNKNOWN 0
98#endif
99#ifndef DT_DIR
100# define DT_DIR 4
101#endif
102#ifndef DT_REG
103# define DT_REG 8
104#endif
105#endif
106
107#define DEFAULT_SLASH '/'
108
109#ifdef __riscos__
110#define DEFAULT_DIR_SEPARATOR ';'
111#else
112#define DEFAULT_DIR_SEPARATOR ':'
113#endif
114
115#define IS_SLASH(c) ((c) == '/')
116// IS_SLASH_P() may read the previous char on Windows, which may be OOB; use IS_SLASH_P_EX() instead
117#define IS_SLASH_P(c) (*(c) == '/')
118#define IS_SLASH_P_EX(c, first_byte) IS_SLASH_P(c)
119
120#endif
121
122
123#ifndef COPY_WHEN_ABSOLUTE
124#define COPY_WHEN_ABSOLUTE(path) 0
125#endif
126
127#ifndef IS_ABSOLUTE_PATH
128#define IS_ABSOLUTE_PATH(path, len) \
129 (IS_SLASH(path[0]))
130#endif
131
132#ifdef TSRM_EXPORTS
133#define CWD_EXPORTS
134#endif
135
136#ifdef ZEND_WIN32
137# ifdef CWD_EXPORTS
138# define CWD_API __declspec(dllexport)
139# else
140# define CWD_API __declspec(dllimport)
141# endif
142#elif defined(__GNUC__) && __GNUC__ >= 4
143# define CWD_API __attribute__ ((visibility("default")))
144#else
145# define CWD_API
146#endif
147
148#ifdef ZEND_WIN32
149# define php_sys_stat_ex php_win32_ioutil_stat_ex
150# define php_sys_stat php_win32_ioutil_stat
151# define php_sys_lstat php_win32_ioutil_lstat
152# define php_sys_fstat php_win32_ioutil_fstat
153# define php_sys_readlink php_win32_ioutil_readlink
154# define php_sys_symlink php_win32_ioutil_symlink
155# define php_sys_link php_win32_ioutil_link
156#else
157# define php_sys_stat stat
158# define php_sys_lstat lstat
159# define php_sys_fstat fstat
160# ifdef HAVE_SYMLINK
161# define php_sys_readlink(link, target, target_len) readlink(link, target, target_len)
162# define php_sys_symlink symlink
163# define php_sys_link link
164# endif
165#endif
166
167typedef struct _cwd_state {
168 char *cwd;
171
172typedef int (*verify_path_func)(const cwd_state *);
173
178CWD_API char *virtual_getcwd_ex(size_t *length);
179CWD_API char *virtual_getcwd(char *buf, size_t size);
180CWD_API zend_result virtual_chdir(const char *path);
181CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path));
182CWD_API int virtual_filepath(const char *path, char **filepath);
183CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path);
184CWD_API char *virtual_realpath(const char *path, char *real_path);
185CWD_API FILE *virtual_fopen(const char *path, const char *mode);
186CWD_API int virtual_open(const char *path, int flags, ...);
187CWD_API int virtual_creat(const char *path, mode_t mode);
188CWD_API int virtual_rename(const char *oldname, const char *newname);
189CWD_API int virtual_stat(const char *path, zend_stat_t *buf);
190CWD_API int virtual_lstat(const char *path, zend_stat_t *buf);
191CWD_API int virtual_unlink(const char *path);
192CWD_API int virtual_mkdir(const char *pathname, mode_t mode);
193CWD_API int virtual_rmdir(const char *pathname);
194CWD_API DIR *virtual_opendir(const char *pathname);
195CWD_API FILE *virtual_popen(const char *command, const char *type);
196CWD_API int virtual_access(const char *pathname, int mode);
197
198#ifdef HAVE_UTIME
199CWD_API int virtual_utime(const char *filename, struct utimbuf *buf);
200#endif
201CWD_API int virtual_chmod(const char *filename, mode_t mode);
202#if !defined(ZEND_WIN32)
203CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link);
204#endif
205
206/* One of the following constants must be used as the last argument
207 in virtual_file_ex() call. */
208
209// TODO Make this into an enum
210#define CWD_EXPAND 0 /* expand "." and ".." but don't resolve symlinks */
211#define CWD_FILEPATH 1 /* resolve symlinks if file is exist otherwise expand */
212#define CWD_REALPATH 2 /* call realpath(), resolve symlinks. File must exist */
213
214CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath);
215
216CWD_API char *tsrm_realpath(const char *path, char *real_path);
217
218#define REALPATH_CACHE_TTL (2*60) /* 2 minutes */
219#define REALPATH_CACHE_SIZE 0 /* disabled while php.ini isn't loaded */
220
223 char *path;
224 char *realpath;
226 time_t expires;
227 uint16_t path_len;
228 uint16_t realpath_len;
229 uint8_t is_dir:1;
230#ifdef ZEND_WIN32
231 uint8_t is_rvalid:1;
232 uint8_t is_readable:1;
233 uint8_t is_wvalid:1;
234 uint8_t is_writable:1;
235#endif
237
245
246#ifdef ZTS
247extern ts_rsrc_id cwd_globals_id;
248extern size_t cwd_globals_offset;
249# define CWDG(v) ZEND_TSRMG_FAST(cwd_globals_offset, virtual_cwd_globals *, v)
250#else
252# define CWDG(v) (cwd_globals.v)
253#endif
254
256CWD_API void realpath_cache_del(const char *path, size_t path_len);
257CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, size_t path_len, time_t t);
261
262#ifdef CWD_EXPORTS
263extern void virtual_cwd_main_cwd_init(uint8_t);
264#endif
265
266/* The actual macros to be used in programs using TSRM
267 * If the program defines VIRTUAL_DIR it will use the
268 * virtual_* functions
269 */
270
271#ifdef VIRTUAL_DIR
272
273#define VCWD_GETCWD(buff, size) virtual_getcwd(buff, size)
274#define VCWD_FOPEN(path, mode) virtual_fopen(path, mode)
275/* Because open() has two modes, we have to macros to replace it */
276#define VCWD_OPEN(path, flags) virtual_open(path, flags)
277#define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path, flags, mode)
278#define VCWD_CREAT(path, mode) virtual_creat(path, mode)
279#define VCWD_CHDIR(path) virtual_chdir(path)
280#define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, (int (*)(const char *)) virtual_chdir)
281#define VCWD_GETWD(buf)
282#define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path)
283#define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname)
284#define VCWD_STAT(path, buff) virtual_stat(path, buff)
285# define VCWD_LSTAT(path, buff) virtual_lstat(path, buff)
286#define VCWD_UNLINK(path) virtual_unlink(path)
287#define VCWD_MKDIR(pathname, mode) virtual_mkdir(pathname, mode)
288#define VCWD_RMDIR(pathname) virtual_rmdir(pathname)
289#define VCWD_OPENDIR(pathname) virtual_opendir(pathname)
290#define VCWD_POPEN(command, type) virtual_popen(command, type)
291#define VCWD_ACCESS(pathname, mode) virtual_access(pathname, mode)
292#ifdef HAVE_UTIME
293#define VCWD_UTIME(path, time) virtual_utime(path, time)
294#endif
295#define VCWD_CHMOD(path, mode) virtual_chmod(path, mode)
296#if !defined(ZEND_WIN32)
297#define VCWD_CHOWN(path, owner, group) virtual_chown(path, owner, group, 0)
298#ifdef HAVE_LCHOWN
299#define VCWD_LCHOWN(path, owner, group) virtual_chown(path, owner, group, 1)
300#endif
301#endif
302
303#else
304
305#define VCWD_CREAT(path, mode) creat(path, mode)
306/* rename on windows will fail if newname already exists.
307 MoveFileEx has to be used */
308#if defined(ZEND_WIN32)
309#define VCWD_FOPEN(path, mode) php_win32_ioutil_fopen(path, mode)
310#define VCWD_OPEN(path, flags) php_win32_ioutil_open(path, flags)
311#define VCWD_OPEN_MODE(path, flags, mode) php_win32_ioutil_open(path, flags, mode)
312# define VCWD_RENAME(oldname, newname) php_win32_ioutil_rename(oldname, newname)
313#define VCWD_MKDIR(pathname, mode) php_win32_ioutil_mkdir(pathname, mode)
314#define VCWD_RMDIR(pathname) php_win32_ioutil_rmdir(pathname)
315#define VCWD_UNLINK(path) php_win32_ioutil_unlink(path)
316#define VCWD_CHDIR(path) php_win32_ioutil_chdir(path)
317#define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode)
318#define VCWD_GETCWD(buff, size) php_win32_ioutil_getcwd(buff, size)
319#define VCWD_CHMOD(path, mode) php_win32_ioutil_chmod(path, mode)
320#else
321#define VCWD_FOPEN(path, mode) fopen(path, mode)
322#define VCWD_OPEN(path, flags) open(path, flags)
323#define VCWD_OPEN_MODE(path, flags, mode) open(path, flags, mode)
324# define VCWD_RENAME(oldname, newname) rename(oldname, newname)
325#define VCWD_MKDIR(pathname, mode) mkdir(pathname, mode)
326#define VCWD_RMDIR(pathname) rmdir(pathname)
327#define VCWD_UNLINK(path) unlink(path)
328#define VCWD_CHDIR(path) chdir(path)
329#define VCWD_ACCESS(pathname, mode) access(pathname, mode)
330#define VCWD_GETCWD(buff, size) getcwd(buff, size)
331#define VCWD_CHMOD(path, mode) chmod(path, mode)
332#endif
333
334#define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
335#define VCWD_GETWD(buf) getwd(buf)
336#define VCWD_STAT(path, buff) php_sys_stat(path, buff)
337#define VCWD_LSTAT(path, buff) lstat(path, buff)
338#define VCWD_OPENDIR(pathname) opendir(pathname)
339#define VCWD_POPEN(command, type) popen(command, type)
340
341#define VCWD_REALPATH(path, real_path) tsrm_realpath(path, real_path)
342
343#ifdef HAVE_UTIME
344# ifdef ZEND_WIN32
345# define VCWD_UTIME(path, time) win32_utime(path, time)
346# else
347# define VCWD_UTIME(path, time) utime(path, time)
348# endif
349#endif
350
351#if !defined(ZEND_WIN32)
352#define VCWD_CHOWN(path, owner, group) chown(path, owner, group)
353#ifdef HAVE_LCHOWN
354#define VCWD_LCHOWN(path, owner, group) lchown(path, owner, group)
355#endif
356#endif
357
358#endif
359
360/* Global stat declarations */
361#ifndef _S_IFDIR
362#define _S_IFDIR S_IFDIR
363#endif
364
365#ifndef _S_IFREG
366#define _S_IFREG S_IFREG
367#endif
368
369#ifndef S_IFLNK
370#define _IFLNK 0120000 /* symbolic link */
371#define S_IFLNK _IFLNK
372#endif
373
374#ifndef S_ISDIR
375#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
376#endif
377
378#ifndef S_ISREG
379#define S_ISREG(mode) (((mode)&S_IFMT) == S_IFREG)
380#endif
381
382#ifndef S_ISLNK
383#define S_ISLNK(mode) (((mode)&S_IFMT) == S_IFLNK)
384#endif
385
386#ifndef S_IXROOT
387#define S_IXROOT ( S_IXUSR | S_IXGRP | S_IXOTH )
388#endif
389
390/* XXX should be _S_IFIFO? */
391#ifndef S_IFIFO
392#define _IFIFO 0010000 /* fifo */
393#define S_IFIFO _IFIFO
394#endif
395
396#ifndef S_IFBLK
397#define _IFBLK 0060000 /* block special */
398#define S_IFBLK _IFBLK
399#endif
400
401#endif /* VIRTUAL_CWD_H */
is_readable(string $filename)
is_writable(string $filename)
link(string $target, string $link)
zend_ffi_type * type
Definition ffi.c:3812
new_type size
Definition ffi.c:4365
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
char * mode
unsigned short mode_t
Definition ioutil.h:72
struct DIR_W32 DIR
Definition readdir.h:41
struct _realpath_cache_bucket * next
realpath_cache_bucket * realpath_cache[1024]
zend_long realpath_cache_size_limit
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
struct stat zend_stat_t
Definition zend_stream.h:94
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
void virtual_cwd_main_cwd_init(uint8_t reinit)
virtual_cwd_globals cwd_globals
CWD_API void virtual_cwd_shutdown(void)
struct _realpath_cache_bucket realpath_cache_bucket
CWD_API char * virtual_getcwd_ex(size_t *length)
CWD_API int virtual_chmod(const char *filename, mode_t mode)
CWD_API int virtual_stat(const char *path, zend_stat_t *buf)
CWD_API void realpath_cache_del(const char *path, size_t path_len)
CWD_API void virtual_cwd_activate(void)
CWD_API realpath_cache_bucket * realpath_cache_lookup(const char *path, size_t path_len, time_t t)
int(* verify_path_func)(const cwd_state *)
CWD_API int virtual_chdir_file(const char *path, int(*p_chdir)(const char *path))
CWD_API int virtual_unlink(const char *path)
CWD_API int virtual_filepath(const char *path, char **filepath)
CWD_API int virtual_open(const char *path, int flags,...)
CWD_API zend_long realpath_cache_max_buckets(void)
CWD_API int virtual_access(const char *pathname, int mode)
CWD_API zend_result virtual_chdir(const char *path)
CWD_API FILE * virtual_fopen(const char *path, const char *mode)
CWD_API void virtual_cwd_deactivate(void)
CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path)
CWD_API int virtual_mkdir(const char *pathname, mode_t mode)
CWD_API int virtual_rmdir(const char *pathname)
CWD_API int virtual_rename(const char *oldname, const char *newname)
CWD_API char * tsrm_realpath(const char *path, char *real_path)
CWD_API int virtual_creat(const char *path, mode_t mode)
CWD_API char * virtual_realpath(const char *path, char *real_path)
CWD_API int virtual_lstat(const char *path, zend_stat_t *buf)
CWD_API realpath_cache_bucket ** realpath_cache_get_buckets(void)
CWD_API void virtual_cwd_startup(void)
CWD_API char * virtual_getcwd(char *buf, size_t size)
CWD_API FILE * virtual_popen(const char *command, const char *type)
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link)
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath)
#define CWD_API
struct _cwd_state cwd_state
struct _virtual_cwd_globals virtual_cwd_globals
CWD_API void realpath_cache_clean(void)
CWD_API DIR * virtual_opendir(const char *pathname)
CWD_API zend_long realpath_cache_size(void)