zend_virtual_cwd.h 12 KB

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