zend_virtual_cwd.h 12 KB

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