PageRenderTime 41ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/zend_virtual_cwd.h

http://github.com/php/php-src
C Header | 383 lines | 294 code | 58 blank | 31 comment | 22 complexity | 7561815e1741620f33ee18926d05a3f8 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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. | http://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. #define CWD_EXPAND 0 /* expand "." and ".." but don't resolve symlinks */
  163. #define CWD_FILEPATH 1 /* resolve symlinks if file is exist otherwise expand */
  164. #define CWD_REALPATH 2 /* call realpath(), resolve symlinks. File must exist */
  165. CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath);
  166. CWD_API char *tsrm_realpath(const char *path, char *real_path);
  167. #define REALPATH_CACHE_TTL (2*60) /* 2 minutes */
  168. #define REALPATH_CACHE_SIZE 0 /* disabled while php.ini isn't loaded */
  169. typedef struct _realpath_cache_bucket {
  170. zend_ulong key;
  171. char *path;
  172. char *realpath;
  173. struct _realpath_cache_bucket *next;
  174. time_t expires;
  175. uint16_t path_len;
  176. uint16_t realpath_len;
  177. uint8_t is_dir:1;
  178. #ifdef ZEND_WIN32
  179. uint8_t is_rvalid:1;
  180. uint8_t is_readable:1;
  181. uint8_t is_wvalid:1;
  182. uint8_t is_writable:1;
  183. #endif
  184. } realpath_cache_bucket;
  185. typedef struct _virtual_cwd_globals {
  186. cwd_state cwd;
  187. zend_long realpath_cache_size;
  188. zend_long realpath_cache_size_limit;
  189. zend_long realpath_cache_ttl;
  190. realpath_cache_bucket *realpath_cache[1024];
  191. } virtual_cwd_globals;
  192. #ifdef ZTS
  193. extern ts_rsrc_id cwd_globals_id;
  194. extern size_t cwd_globals_offset;
  195. # define CWDG(v) ZEND_TSRMG_FAST(cwd_globals_offset, virtual_cwd_globals *, v)
  196. #else
  197. extern virtual_cwd_globals cwd_globals;
  198. # define CWDG(v) (cwd_globals.v)
  199. #endif
  200. CWD_API void realpath_cache_clean(void);
  201. CWD_API void realpath_cache_del(const char *path, size_t path_len);
  202. CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, size_t path_len, time_t t);
  203. CWD_API zend_long realpath_cache_size(void);
  204. CWD_API zend_long realpath_cache_max_buckets(void);
  205. CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void);
  206. #ifdef CWD_EXPORTS
  207. extern void virtual_cwd_main_cwd_init(uint8_t);
  208. #endif
  209. /* The actual macros to be used in programs using TSRM
  210. * If the program defines VIRTUAL_DIR it will use the
  211. * virtual_* functions
  212. */
  213. #ifdef VIRTUAL_DIR
  214. #define VCWD_GETCWD(buff, size) virtual_getcwd(buff, size)
  215. #define VCWD_FOPEN(path, mode) virtual_fopen(path, mode)
  216. /* Because open() has two modes, we have to macros to replace it */
  217. #define VCWD_OPEN(path, flags) virtual_open(path, flags)
  218. #define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path, flags, mode)
  219. #define VCWD_CREAT(path, mode) virtual_creat(path, mode)
  220. #define VCWD_CHDIR(path) virtual_chdir(path)
  221. #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, virtual_chdir)
  222. #define VCWD_GETWD(buf)
  223. #define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path)
  224. #define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname)
  225. #define VCWD_STAT(path, buff) virtual_stat(path, buff)
  226. # define VCWD_LSTAT(path, buff) virtual_lstat(path, buff)
  227. #define VCWD_UNLINK(path) virtual_unlink(path)
  228. #define VCWD_MKDIR(pathname, mode) virtual_mkdir(pathname, mode)
  229. #define VCWD_RMDIR(pathname) virtual_rmdir(pathname)
  230. #define VCWD_OPENDIR(pathname) virtual_opendir(pathname)
  231. #define VCWD_POPEN(command, type) virtual_popen(command, type)
  232. #define VCWD_ACCESS(pathname, mode) virtual_access(pathname, mode)
  233. #if HAVE_UTIME
  234. #define VCWD_UTIME(path, time) virtual_utime(path, time)
  235. #endif
  236. #define VCWD_CHMOD(path, mode) virtual_chmod(path, mode)
  237. #if !defined(ZEND_WIN32)
  238. #define VCWD_CHOWN(path, owner, group) virtual_chown(path, owner, group, 0)
  239. #if HAVE_LCHOWN
  240. #define VCWD_LCHOWN(path, owner, group) virtual_chown(path, owner, group, 1)
  241. #endif
  242. #endif
  243. #else
  244. #define VCWD_CREAT(path, mode) creat(path, mode)
  245. /* rename on windows will fail if newname already exists.
  246. MoveFileEx has to be used */
  247. #if defined(ZEND_WIN32)
  248. #define VCWD_FOPEN(path, mode) php_win32_ioutil_fopen(path, mode)
  249. #define VCWD_OPEN(path, flags) php_win32_ioutil_open(path, flags)
  250. #define VCWD_OPEN_MODE(path, flags, mode) php_win32_ioutil_open(path, flags, mode)
  251. # define VCWD_RENAME(oldname, newname) php_win32_ioutil_rename(oldname, newname)
  252. #define VCWD_MKDIR(pathname, mode) php_win32_ioutil_mkdir(pathname, mode)
  253. #define VCWD_RMDIR(pathname) php_win32_ioutil_rmdir(pathname)
  254. #define VCWD_UNLINK(path) php_win32_ioutil_unlink(path)
  255. #define VCWD_CHDIR(path) php_win32_ioutil_chdir(path)
  256. #define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode)
  257. #define VCWD_GETCWD(buff, size) php_win32_ioutil_getcwd(buff, size)
  258. #define VCWD_CHMOD(path, mode) php_win32_ioutil_chmod(path, mode)
  259. #else
  260. #define VCWD_FOPEN(path, mode) fopen(path, mode)
  261. #define VCWD_OPEN(path, flags) open(path, flags)
  262. #define VCWD_OPEN_MODE(path, flags, mode) open(path, flags, mode)
  263. # define VCWD_RENAME(oldname, newname) rename(oldname, newname)
  264. #define VCWD_MKDIR(pathname, mode) mkdir(pathname, mode)
  265. #define VCWD_RMDIR(pathname) rmdir(pathname)
  266. #define VCWD_UNLINK(path) unlink(path)
  267. #define VCWD_CHDIR(path) chdir(path)
  268. #define VCWD_ACCESS(pathname, mode) access(pathname, mode)
  269. #define VCWD_GETCWD(buff, size) getcwd(buff, size)
  270. #define VCWD_CHMOD(path, mode) chmod(path, mode)
  271. #endif
  272. #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
  273. #define VCWD_GETWD(buf) getwd(buf)
  274. #define VCWD_STAT(path, buff) php_sys_stat(path, buff)
  275. #define VCWD_LSTAT(path, buff) lstat(path, buff)
  276. #define VCWD_OPENDIR(pathname) opendir(pathname)
  277. #define VCWD_POPEN(command, type) popen(command, type)
  278. #define VCWD_REALPATH(path, real_path) tsrm_realpath(path, real_path)
  279. #if HAVE_UTIME
  280. # ifdef ZEND_WIN32
  281. # define VCWD_UTIME(path, time) win32_utime(path, time)
  282. # else
  283. # define VCWD_UTIME(path, time) utime(path, time)
  284. # endif
  285. #endif
  286. #if !defined(ZEND_WIN32)
  287. #define VCWD_CHOWN(path, owner, group) chown(path, owner, group)
  288. #if HAVE_LCHOWN
  289. #define VCWD_LCHOWN(path, owner, group) lchown(path, owner, group)
  290. #endif
  291. #endif
  292. #endif
  293. /* Global stat declarations */
  294. #ifndef _S_IFDIR
  295. #define _S_IFDIR S_IFDIR
  296. #endif
  297. #ifndef _S_IFREG
  298. #define _S_IFREG S_IFREG
  299. #endif
  300. #ifndef S_IFLNK
  301. #define _IFLNK 0120000 /* symbolic link */
  302. #define S_IFLNK _IFLNK
  303. #endif
  304. #ifndef S_ISDIR
  305. #define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
  306. #endif
  307. #ifndef S_ISREG
  308. #define S_ISREG(mode) (((mode)&S_IFMT) == S_IFREG)
  309. #endif
  310. #ifndef S_ISLNK
  311. #define S_ISLNK(mode) (((mode)&S_IFMT) == S_IFLNK)
  312. #endif
  313. #ifndef S_IXROOT
  314. #define S_IXROOT ( S_IXUSR | S_IXGRP | S_IXOTH )
  315. #endif
  316. /* XXX should be _S_IFIFO? */
  317. #ifndef S_IFIFO
  318. #define _IFIFO 0010000 /* fifo */
  319. #define S_IFIFO _IFIFO
  320. #endif
  321. #ifndef S_IFBLK
  322. #define _IFBLK 0060000 /* block special */
  323. #define S_IFBLK _IFBLK
  324. #endif
  325. #endif /* VIRTUAL_CWD_H */