zend_virtual_cwd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* $Id$ */
  21. #ifndef VIRTUAL_CWD_H
  22. #define VIRTUAL_CWD_H
  23. #include "TSRM.h"
  24. #include "tsrm_config_common.h"
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <ctype.h>
  28. #ifdef HAVE_UTIME_H
  29. #include <utime.h>
  30. #endif
  31. #ifdef HAVE_STDARG_H
  32. #include <stdarg.h>
  33. #endif
  34. #ifdef ZTS
  35. #define VIRTUAL_DIR
  36. #endif
  37. #ifndef TSRM_WIN32
  38. #include <unistd.h>
  39. #else
  40. #include <direct.h>
  41. #endif
  42. #if defined(__osf__) || defined(_AIX)
  43. #include <errno.h>
  44. #endif
  45. #ifdef TSRM_WIN32
  46. #include "readdir.h"
  47. #include <sys/utime.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 && ((isalpha(path[0]) && path[1] == ':') || IS_UNC_PATH(path, len)))
  62. #elif defined(NETWARE)
  63. #ifdef HAVE_DIRENT_H
  64. #include <dirent.h>
  65. #endif
  66. #define DEFAULT_SLASH '/'
  67. #define DEFAULT_DIR_SEPARATOR ';'
  68. #define IS_SLASH(c) ((c) == '/' || (c) == '\\')
  69. #define IS_SLASH_P(c) IS_SLASH(*(c))
  70. /* Colon indicates volume name, either first character should be forward slash or backward slash */
  71. #define IS_ABSOLUTE_PATH(path, len) \
  72. ((strchr(path, ':') != NULL) || ((len >= 1) && ((path[0] == '/') || (path[0] == '\\'))))
  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 TSRM_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 TSRM_WIN32
  108. CWD_API int php_sys_stat_ex(const char *path, struct stat *buf, int lstat);
  109. # define php_sys_stat(path, buf) php_sys_stat_ex(path, buf, 0)
  110. # define php_sys_lstat(path, buf) php_sys_stat_ex(path, buf, 1)
  111. CWD_API int php_sys_readlink(const char *link, char *target, size_t target_len);
  112. #else
  113. # define php_sys_stat stat
  114. # define php_sys_lstat lstat
  115. # ifdef HAVE_SYMLINK
  116. # define php_sys_readlink(link, target, target_len) readlink(link, target, target_len)
  117. # endif
  118. #endif
  119. typedef struct _cwd_state {
  120. char *cwd;
  121. int cwd_length;
  122. } cwd_state;
  123. typedef int (*verify_path_func)(const cwd_state *);
  124. CWD_API void virtual_cwd_startup(void);
  125. CWD_API void virtual_cwd_shutdown(void);
  126. CWD_API int virtual_cwd_activate(TSRMLS_D);
  127. CWD_API int virtual_cwd_deactivate(TSRMLS_D);
  128. CWD_API char *virtual_getcwd_ex(size_t *length TSRMLS_DC);
  129. CWD_API char *virtual_getcwd(char *buf, size_t size TSRMLS_DC);
  130. CWD_API int virtual_chdir(const char *path TSRMLS_DC);
  131. CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path TSRMLS_DC) TSRMLS_DC);
  132. CWD_API int virtual_filepath(const char *path, char **filepath TSRMLS_DC);
  133. CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path TSRMLS_DC);
  134. CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC);
  135. CWD_API FILE *virtual_fopen(const char *path, const char *mode TSRMLS_DC);
  136. CWD_API int virtual_open(const char *path TSRMLS_DC, int flags, ...);
  137. CWD_API int virtual_creat(const char *path, mode_t mode TSRMLS_DC);
  138. CWD_API int virtual_rename(const char *oldname, const char *newname TSRMLS_DC);
  139. CWD_API int virtual_stat(const char *path, struct stat *buf TSRMLS_DC);
  140. CWD_API int virtual_lstat(const char *path, struct stat *buf TSRMLS_DC);
  141. CWD_API int virtual_unlink(const char *path TSRMLS_DC);
  142. CWD_API int virtual_mkdir(const char *pathname, mode_t mode TSRMLS_DC);
  143. CWD_API int virtual_rmdir(const char *pathname TSRMLS_DC);
  144. CWD_API DIR *virtual_opendir(const char *pathname TSRMLS_DC);
  145. CWD_API FILE *virtual_popen(const char *command, const char *type TSRMLS_DC);
  146. CWD_API int virtual_access(const char *pathname, int mode TSRMLS_DC);
  147. #if defined(TSRM_WIN32)
  148. /* these are not defined in win32 headers */
  149. #ifndef W_OK
  150. #define W_OK 0x02
  151. #endif
  152. #ifndef R_OK
  153. #define R_OK 0x04
  154. #endif
  155. #ifndef X_OK
  156. #define X_OK 0x01
  157. #endif
  158. #ifndef F_OK
  159. #define F_OK 0x00
  160. #endif
  161. #endif
  162. #if HAVE_UTIME
  163. CWD_API int virtual_utime(const char *filename, struct utimbuf *buf TSRMLS_DC);
  164. #endif
  165. CWD_API int virtual_chmod(const char *filename, mode_t mode TSRMLS_DC);
  166. #if !defined(TSRM_WIN32) && !defined(NETWARE)
  167. CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link TSRMLS_DC);
  168. #endif
  169. /* One of the following constants must be used as the last argument
  170. in virtual_file_ex() call. */
  171. #define CWD_EXPAND 0 /* expand "." and ".." but dont resolve symlinks */
  172. #define CWD_FILEPATH 1 /* resolve symlinks if file is exist otherwise expand */
  173. #define CWD_REALPATH 2 /* call realpath(), resolve symlinks. File must exist */
  174. CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath TSRMLS_DC);
  175. CWD_API char *tsrm_realpath(const char *path, char *real_path TSRMLS_DC);
  176. #define REALPATH_CACHE_TTL (2*60) /* 2 minutes */
  177. #define REALPATH_CACHE_SIZE 0 /* disabled while php.ini isn't loaded */
  178. typedef struct _realpath_cache_bucket {
  179. unsigned long key;
  180. char *path;
  181. int path_len;
  182. char *realpath;
  183. int realpath_len;
  184. int is_dir;
  185. time_t expires;
  186. #ifdef PHP_WIN32
  187. unsigned char is_rvalid;
  188. unsigned char is_readable;
  189. unsigned char is_wvalid;
  190. unsigned char is_writable;
  191. #endif
  192. struct _realpath_cache_bucket *next;
  193. } realpath_cache_bucket;
  194. typedef struct _virtual_cwd_globals {
  195. cwd_state cwd;
  196. long realpath_cache_size;
  197. long realpath_cache_size_limit;
  198. long realpath_cache_ttl;
  199. realpath_cache_bucket *realpath_cache[1024];
  200. } virtual_cwd_globals;
  201. #ifdef ZTS
  202. extern ts_rsrc_id cwd_globals_id;
  203. # define CWDG(v) TSRMG(cwd_globals_id, virtual_cwd_globals *, v)
  204. #else
  205. extern virtual_cwd_globals cwd_globals;
  206. # define CWDG(v) (cwd_globals.v)
  207. #endif
  208. CWD_API void realpath_cache_clean(TSRMLS_D);
  209. CWD_API void realpath_cache_del(const char *path, int path_len TSRMLS_DC);
  210. CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, int path_len, time_t t TSRMLS_DC);
  211. CWD_API int realpath_cache_size(TSRMLS_D);
  212. CWD_API int realpath_cache_max_buckets(TSRMLS_D);
  213. CWD_API realpath_cache_bucket** realpath_cache_get_buckets(TSRMLS_D);
  214. /* The actual macros to be used in programs using TSRM
  215. * If the program defines VIRTUAL_DIR it will use the
  216. * virtual_* functions
  217. */
  218. #ifdef VIRTUAL_DIR
  219. #define VCWD_GETCWD(buff, size) virtual_getcwd(buff, size TSRMLS_CC)
  220. #define VCWD_FOPEN(path, mode) virtual_fopen(path, mode TSRMLS_CC)
  221. /* Because open() has two modes, we have to macros to replace it */
  222. #define VCWD_OPEN(path, flags) virtual_open(path TSRMLS_CC, flags)
  223. #define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path TSRMLS_CC, flags, mode)
  224. #define VCWD_CREAT(path, mode) virtual_creat(path, mode TSRMLS_CC)
  225. #define VCWD_CHDIR(path) virtual_chdir(path TSRMLS_CC)
  226. #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, virtual_chdir TSRMLS_CC)
  227. #define VCWD_GETWD(buf)
  228. #define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path TSRMLS_CC)
  229. #define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname TSRMLS_CC)
  230. #define VCWD_STAT(path, buff) virtual_stat(path, buff TSRMLS_CC)
  231. # define VCWD_LSTAT(path, buff) virtual_lstat(path, buff TSRMLS_CC)
  232. #define VCWD_UNLINK(path) virtual_unlink(path TSRMLS_CC)
  233. #define VCWD_MKDIR(pathname, mode) virtual_mkdir(pathname, mode TSRMLS_CC)
  234. #define VCWD_RMDIR(pathname) virtual_rmdir(pathname TSRMLS_CC)
  235. #define VCWD_OPENDIR(pathname) virtual_opendir(pathname TSRMLS_CC)
  236. #define VCWD_POPEN(command, type) virtual_popen(command, type TSRMLS_CC)
  237. #define VCWD_ACCESS(pathname, mode) virtual_access(pathname, mode TSRMLS_CC)
  238. #if HAVE_UTIME
  239. #define VCWD_UTIME(path, time) virtual_utime(path, time TSRMLS_CC)
  240. #endif
  241. #define VCWD_CHMOD(path, mode) virtual_chmod(path, mode TSRMLS_CC)
  242. #if !defined(TSRM_WIN32) && !defined(NETWARE)
  243. #define VCWD_CHOWN(path, owner, group) virtual_chown(path, owner, group, 0 TSRMLS_CC)
  244. #if HAVE_LCHOWN
  245. #define VCWD_LCHOWN(path, owner, group) virtual_chown(path, owner, group, 1 TSRMLS_CC)
  246. #endif
  247. #endif
  248. #else
  249. #define VCWD_GETCWD(buff, size) getcwd(buff, size)
  250. #define VCWD_FOPEN(path, mode) fopen(path, mode)
  251. #define VCWD_OPEN(path, flags) open(path, flags)
  252. #define VCWD_OPEN_MODE(path, flags, mode) open(path, flags, mode)
  253. #define VCWD_CREAT(path, mode) creat(path, mode)
  254. /* rename on windows will fail if newname already exists.
  255. MoveFileEx has to be used */
  256. #if defined(TSRM_WIN32)
  257. # define VCWD_RENAME(oldname, newname) (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED) == 0 ? -1 : 0)
  258. #else
  259. # define VCWD_RENAME(oldname, newname) rename(oldname, newname)
  260. #endif
  261. #define VCWD_CHDIR(path) chdir(path)
  262. #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
  263. #define VCWD_GETWD(buf) getwd(buf)
  264. #define VCWD_STAT(path, buff) php_sys_stat(path, buff)
  265. #define VCWD_LSTAT(path, buff) lstat(path, buff)
  266. #define VCWD_UNLINK(path) unlink(path)
  267. #define VCWD_MKDIR(pathname, mode) mkdir(pathname, mode)
  268. #define VCWD_RMDIR(pathname) rmdir(pathname)
  269. #define VCWD_OPENDIR(pathname) opendir(pathname)
  270. #define VCWD_POPEN(command, type) popen(command, type)
  271. #if defined(TSRM_WIN32)
  272. #define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode TSRMLS_CC)
  273. #else
  274. #define VCWD_ACCESS(pathname, mode) access(pathname, mode)
  275. #endif
  276. #define VCWD_REALPATH(path, real_path) tsrm_realpath(path, real_path TSRMLS_CC)
  277. #if HAVE_UTIME
  278. # ifdef TSRM_WIN32
  279. # define VCWD_UTIME(path, time) win32_utime(path, time)
  280. # else
  281. # define VCWD_UTIME(path, time) utime(path, time)
  282. # endif
  283. #endif
  284. #define VCWD_CHMOD(path, mode) chmod(path, mode)
  285. #if !defined(TSRM_WIN32) && !defined(NETWARE)
  286. #define VCWD_CHOWN(path, owner, group) chown(path, owner, group)
  287. #if HAVE_LCHOWN
  288. #define VCWD_LCHOWN(path, owner, group) lchown(path, owner, group)
  289. #endif
  290. #endif
  291. #endif
  292. #endif /* VIRTUAL_CWD_H */