PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/standard/dir.c

http://github.com/infusion/PHP
C | 607 lines | 421 code | 106 blank | 80 comment | 92 complexity | 00097a7d9229d7c0f5b57d942edf4735 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 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. | Author: Thies C. Arntzen <thies@thieso.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: dir.c 306939 2011-01-01 02:19:59Z felipe $ */
  19. /* {{{ includes/startup/misc */
  20. #include "php.h"
  21. #include "fopen_wrappers.h"
  22. #include "file.h"
  23. #include "php_dir.h"
  24. #include "php_string.h"
  25. #include "php_scandir.h"
  26. #include "basic_functions.h"
  27. #ifdef HAVE_DIRENT_H
  28. #include <dirent.h>
  29. #endif
  30. #if HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #include <errno.h>
  34. #ifdef PHP_WIN32
  35. #include "win32/readdir.h"
  36. #endif
  37. #ifdef HAVE_GLOB
  38. #ifndef PHP_WIN32
  39. #include <glob.h>
  40. #else
  41. #include "win32/glob.h"
  42. #endif
  43. #endif
  44. typedef struct {
  45. int default_dir;
  46. } php_dir_globals;
  47. #ifdef ZTS
  48. #define DIRG(v) TSRMG(dir_globals_id, php_dir_globals *, v)
  49. int dir_globals_id;
  50. #else
  51. #define DIRG(v) (dir_globals.v)
  52. php_dir_globals dir_globals;
  53. #endif
  54. #if 0
  55. typedef struct {
  56. int id;
  57. DIR *dir;
  58. } php_dir;
  59. static int le_dirp;
  60. #endif
  61. static zend_class_entry *dir_class_entry_ptr;
  62. #define FETCH_DIRP() \
  63. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &id) == FAILURE) { \
  64. return; \
  65. } \
  66. if (ZEND_NUM_ARGS() == 0) { \
  67. myself = getThis(); \
  68. if (myself) { \
  69. if (zend_hash_find(Z_OBJPROP_P(myself), "handle", sizeof("handle"), (void **)&tmp) == FAILURE) { \
  70. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find my handle property"); \
  71. RETURN_FALSE; \
  72. } \
  73. ZEND_FETCH_RESOURCE(dirp, php_stream *, tmp, -1, "Directory", php_file_le_stream()); \
  74. } else { \
  75. ZEND_FETCH_RESOURCE(dirp, php_stream *, 0, DIRG(default_dir), "Directory", php_file_le_stream()); \
  76. } \
  77. } else { \
  78. dirp = (php_stream *) zend_fetch_resource(&id TSRMLS_CC, -1, "Directory", NULL, 1, php_file_le_stream()); \
  79. if (!dirp) \
  80. RETURN_FALSE; \
  81. }
  82. /* {{{ arginfo */
  83. ZEND_BEGIN_ARG_INFO_EX(arginfo_dir, 0, 0, 0)
  84. ZEND_ARG_INFO(0, dir_handle)
  85. ZEND_END_ARG_INFO()
  86. /* }}} */
  87. static const zend_function_entry php_dir_class_functions[] = {
  88. PHP_FALIAS(close, closedir, arginfo_dir)
  89. PHP_FALIAS(rewind, rewinddir, arginfo_dir)
  90. PHP_NAMED_FE(read, php_if_readdir, arginfo_dir)
  91. {NULL, NULL, NULL}
  92. };
  93. static void php_set_default_dir(int id TSRMLS_DC)
  94. {
  95. if (DIRG(default_dir)!=-1) {
  96. zend_list_delete(DIRG(default_dir));
  97. }
  98. if (id != -1) {
  99. zend_list_addref(id);
  100. }
  101. DIRG(default_dir) = id;
  102. }
  103. PHP_RINIT_FUNCTION(dir)
  104. {
  105. DIRG(default_dir) = -1;
  106. return SUCCESS;
  107. }
  108. PHP_MINIT_FUNCTION(dir)
  109. {
  110. static char dirsep_str[2], pathsep_str[2];
  111. zend_class_entry dir_class_entry;
  112. INIT_CLASS_ENTRY(dir_class_entry, "Directory", php_dir_class_functions);
  113. dir_class_entry_ptr = zend_register_internal_class(&dir_class_entry TSRMLS_CC);
  114. #ifdef ZTS
  115. ts_allocate_id(&dir_globals_id, sizeof(php_dir_globals), NULL, NULL);
  116. #endif
  117. dirsep_str[0] = DEFAULT_SLASH;
  118. dirsep_str[1] = '\0';
  119. REGISTER_STRING_CONSTANT("DIRECTORY_SEPARATOR", dirsep_str, CONST_CS|CONST_PERSISTENT);
  120. pathsep_str[0] = ZEND_PATHS_SEPARATOR;
  121. pathsep_str[1] = '\0';
  122. REGISTER_STRING_CONSTANT("PATH_SEPARATOR", pathsep_str, CONST_CS|CONST_PERSISTENT);
  123. #ifdef HAVE_GLOB
  124. #ifdef GLOB_BRACE
  125. REGISTER_LONG_CONSTANT("GLOB_BRACE", GLOB_BRACE, CONST_CS | CONST_PERSISTENT);
  126. #else
  127. # define GLOB_BRACE 0
  128. #endif
  129. #ifdef GLOB_MARK
  130. REGISTER_LONG_CONSTANT("GLOB_MARK", GLOB_MARK, CONST_CS | CONST_PERSISTENT);
  131. #else
  132. # define GLOB_MARK 0
  133. #endif
  134. #ifdef GLOB_NOSORT
  135. REGISTER_LONG_CONSTANT("GLOB_NOSORT", GLOB_NOSORT, CONST_CS | CONST_PERSISTENT);
  136. #else
  137. # define GLOB_NOSORT 0
  138. #endif
  139. #ifdef GLOB_NOCHECK
  140. REGISTER_LONG_CONSTANT("GLOB_NOCHECK", GLOB_NOCHECK, CONST_CS | CONST_PERSISTENT);
  141. #else
  142. # define GLOB_NOCHECK 0
  143. #endif
  144. #ifdef GLOB_NOESCAPE
  145. REGISTER_LONG_CONSTANT("GLOB_NOESCAPE", GLOB_NOESCAPE, CONST_CS | CONST_PERSISTENT);
  146. #else
  147. # define GLOB_NOESCAPE 0
  148. #endif
  149. #ifdef GLOB_ERR
  150. REGISTER_LONG_CONSTANT("GLOB_ERR", GLOB_ERR, CONST_CS | CONST_PERSISTENT);
  151. #else
  152. # define GLOB_ERR 0
  153. #endif
  154. #ifndef GLOB_ONLYDIR
  155. # define GLOB_ONLYDIR (1<<30)
  156. # define GLOB_EMULATE_ONLYDIR
  157. # define GLOB_FLAGMASK (~GLOB_ONLYDIR)
  158. #else
  159. # define GLOB_FLAGMASK (~0)
  160. #endif
  161. /* This is used for checking validity of passed flags (passing invalid flags causes segfault in glob()!! */
  162. #define GLOB_AVAILABLE_FLAGS (0 | GLOB_BRACE | GLOB_MARK | GLOB_NOSORT | GLOB_NOCHECK | GLOB_NOESCAPE | GLOB_ERR | GLOB_ONLYDIR)
  163. REGISTER_LONG_CONSTANT("GLOB_ONLYDIR", GLOB_ONLYDIR, CONST_CS | CONST_PERSISTENT);
  164. REGISTER_LONG_CONSTANT("GLOB_AVAILABLE_FLAGS", GLOB_AVAILABLE_FLAGS, CONST_CS | CONST_PERSISTENT);
  165. #endif /* HAVE_GLOB */
  166. return SUCCESS;
  167. }
  168. /* }}} */
  169. /* {{{ internal functions */
  170. static void _php_do_opendir(INTERNAL_FUNCTION_PARAMETERS, int createobject)
  171. {
  172. char *dirname;
  173. int dir_len;
  174. zval *zcontext = NULL;
  175. php_stream_context *context = NULL;
  176. php_stream *dirp;
  177. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &dirname, &dir_len, &zcontext) == FAILURE) {
  178. RETURN_NULL();
  179. }
  180. context = php_stream_context_from_zval(zcontext, 0);
  181. dirp = php_stream_opendir(dirname, ENFORCE_SAFE_MODE|REPORT_ERRORS, context);
  182. if (dirp == NULL) {
  183. RETURN_FALSE;
  184. }
  185. dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
  186. php_set_default_dir(dirp->rsrc_id TSRMLS_CC);
  187. if (createobject) {
  188. object_init_ex(return_value, dir_class_entry_ptr);
  189. add_property_stringl(return_value, "path", dirname, dir_len, 1);
  190. add_property_resource(return_value, "handle", dirp->rsrc_id);
  191. php_stream_auto_cleanup(dirp); /* so we don't get warnings under debug */
  192. } else {
  193. php_stream_to_zval(dirp, return_value);
  194. }
  195. }
  196. /* }}} */
  197. /* {{{ proto mixed opendir(string path[, resource context])
  198. Open a directory and return a dir_handle */
  199. PHP_FUNCTION(opendir)
  200. {
  201. _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  202. }
  203. /* }}} */
  204. /* {{{ proto object dir(string directory[, resource context])
  205. Directory class with properties, handle and class and methods read, rewind and close */
  206. PHP_FUNCTION(getdir)
  207. {
  208. _php_do_opendir(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  209. }
  210. /* }}} */
  211. /* {{{ proto void closedir([resource dir_handle])
  212. Close directory connection identified by the dir_handle */
  213. PHP_FUNCTION(closedir)
  214. {
  215. zval *id = NULL, **tmp, *myself;
  216. php_stream *dirp;
  217. int rsrc_id;
  218. FETCH_DIRP();
  219. if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
  220. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid Directory resource", dirp->rsrc_id);
  221. RETURN_FALSE;
  222. }
  223. rsrc_id = dirp->rsrc_id;
  224. zend_list_delete(dirp->rsrc_id);
  225. if (rsrc_id == DIRG(default_dir)) {
  226. php_set_default_dir(-1 TSRMLS_CC);
  227. }
  228. }
  229. /* }}} */
  230. #if defined(HAVE_CHROOT) && !defined(ZTS) && ENABLE_CHROOT_FUNC
  231. /* {{{ proto bool chroot(string directory)
  232. Change root directory */
  233. PHP_FUNCTION(chroot)
  234. {
  235. char *str;
  236. int ret, str_len;
  237. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  238. RETURN_FALSE;
  239. }
  240. ret = chroot(str);
  241. if (ret != 0) {
  242. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  243. RETURN_FALSE;
  244. }
  245. php_clear_stat_cache(1, NULL, 0 TSRMLS_CC);
  246. ret = chdir("/");
  247. if (ret != 0) {
  248. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  249. RETURN_FALSE;
  250. }
  251. RETURN_TRUE;
  252. }
  253. /* }}} */
  254. #endif
  255. /* {{{ proto bool chdir(string directory)
  256. Change the current directory */
  257. PHP_FUNCTION(chdir)
  258. {
  259. char *str;
  260. int ret, str_len;
  261. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  262. RETURN_FALSE;
  263. }
  264. if (strlen(str) != str_len) {
  265. RETURN_FALSE;
  266. }
  267. if (php_check_open_basedir(str TSRMLS_CC)) {
  268. RETURN_FALSE;
  269. }
  270. ret = VCWD_CHDIR(str);
  271. if (ret != 0) {
  272. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s (errno %d)", strerror(errno), errno);
  273. RETURN_FALSE;
  274. }
  275. if (BG(CurrentStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentStatFile), strlen(BG(CurrentStatFile)))) {
  276. efree(BG(CurrentStatFile));
  277. BG(CurrentStatFile) = NULL;
  278. }
  279. if (BG(CurrentLStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentLStatFile), strlen(BG(CurrentLStatFile)))) {
  280. efree(BG(CurrentLStatFile));
  281. BG(CurrentLStatFile) = NULL;
  282. }
  283. RETURN_TRUE;
  284. }
  285. /* }}} */
  286. /* {{{ proto mixed getcwd(void)
  287. Gets the current directory */
  288. PHP_FUNCTION(getcwd)
  289. {
  290. char path[MAXPATHLEN];
  291. char *ret=NULL;
  292. if (zend_parse_parameters_none() == FAILURE) {
  293. return;
  294. }
  295. #if HAVE_GETCWD
  296. ret = VCWD_GETCWD(path, MAXPATHLEN);
  297. #elif HAVE_GETWD
  298. ret = VCWD_GETWD(path);
  299. #endif
  300. if (ret) {
  301. RETURN_STRING(path, 1);
  302. } else {
  303. RETURN_FALSE;
  304. }
  305. }
  306. /* }}} */
  307. /* {{{ proto void rewinddir([resource dir_handle])
  308. Rewind dir_handle back to the start */
  309. PHP_FUNCTION(rewinddir)
  310. {
  311. zval *id = NULL, **tmp, *myself;
  312. php_stream *dirp;
  313. FETCH_DIRP();
  314. if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
  315. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid Directory resource", dirp->rsrc_id);
  316. RETURN_FALSE;
  317. }
  318. php_stream_rewinddir(dirp);
  319. }
  320. /* }}} */
  321. /* {{{ proto string readdir([resource dir_handle])
  322. Read directory entry from dir_handle */
  323. PHP_NAMED_FUNCTION(php_if_readdir)
  324. {
  325. zval *id = NULL, **tmp, *myself;
  326. php_stream *dirp;
  327. php_stream_dirent entry;
  328. FETCH_DIRP();
  329. if (!(dirp->flags & PHP_STREAM_FLAG_IS_DIR)) {
  330. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d is not a valid Directory resource", dirp->rsrc_id);
  331. RETURN_FALSE;
  332. }
  333. if (php_stream_readdir(dirp, &entry)) {
  334. RETURN_STRINGL(entry.d_name, strlen(entry.d_name), 1);
  335. }
  336. RETURN_FALSE;
  337. }
  338. /* }}} */
  339. #ifdef HAVE_GLOB
  340. /* {{{ proto array glob(string pattern [, int flags])
  341. Find pathnames matching a pattern */
  342. PHP_FUNCTION(glob)
  343. {
  344. int cwd_skip = 0;
  345. #ifdef ZTS
  346. char cwd[MAXPATHLEN];
  347. char work_pattern[MAXPATHLEN];
  348. char *result;
  349. #endif
  350. char *pattern = NULL;
  351. int pattern_len;
  352. long flags = 0;
  353. glob_t globbuf;
  354. int n;
  355. int ret;
  356. zend_bool basedir_limit = 0;
  357. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &pattern, &pattern_len, &flags) == FAILURE) {
  358. return;
  359. }
  360. if (strlen(pattern) != pattern_len) {
  361. RETURN_FALSE;
  362. }
  363. if (pattern_len >= MAXPATHLEN) {
  364. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  365. RETURN_FALSE;
  366. }
  367. if ((GLOB_AVAILABLE_FLAGS & flags) != flags) {
  368. php_error_docref(NULL TSRMLS_CC, E_WARNING, "At least one of the passed flags is invalid or not supported on this platform");
  369. RETURN_FALSE;
  370. }
  371. #ifdef ZTS
  372. if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) {
  373. result = VCWD_GETCWD(cwd, MAXPATHLEN);
  374. if (!result) {
  375. cwd[0] = '\0';
  376. }
  377. #ifdef PHP_WIN32
  378. if (IS_SLASH(*pattern)) {
  379. cwd[2] = '\0';
  380. }
  381. #endif
  382. cwd_skip = strlen(cwd)+1;
  383. snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);
  384. pattern = work_pattern;
  385. }
  386. #endif
  387. memset(&globbuf, 0, sizeof(glob_t));
  388. globbuf.gl_offs = 0;
  389. if (0 != (ret = glob(pattern, flags & GLOB_FLAGMASK, NULL, &globbuf))) {
  390. #ifdef GLOB_NOMATCH
  391. if (GLOB_NOMATCH == ret) {
  392. /* Some glob implementation simply return no data if no matches
  393. were found, others return the GLOB_NOMATCH error code.
  394. We don't want to treat GLOB_NOMATCH as an error condition
  395. so that PHP glob() behaves the same on both types of
  396. implementations and so that 'foreach (glob() as ...'
  397. can be used for simple glob() calls without further error
  398. checking.
  399. */
  400. goto no_results;
  401. }
  402. #endif
  403. RETURN_FALSE;
  404. }
  405. /* now catch the FreeBSD style of "no matches" */
  406. if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
  407. no_results:
  408. if ((PG(open_basedir) && *PG(open_basedir))) {
  409. struct stat s;
  410. if (0 != VCWD_STAT(pattern, &s) || S_IFDIR != (s.st_mode & S_IFMT)) {
  411. RETURN_FALSE;
  412. }
  413. }
  414. array_init(return_value);
  415. return;
  416. }
  417. array_init(return_value);
  418. for (n = 0; n < globbuf.gl_pathc; n++) {
  419. if ((PG(open_basedir) && *PG(open_basedir))) {
  420. if (php_check_open_basedir_ex(globbuf.gl_pathv[n], 0 TSRMLS_CC)) {
  421. basedir_limit = 1;
  422. continue;
  423. }
  424. }
  425. /* we need to do this everytime since GLOB_ONLYDIR does not guarantee that
  426. * all directories will be filtered. GNU libc documentation states the
  427. * following:
  428. * If the information about the type of the file is easily available
  429. * non-directories will be rejected but no extra work will be done to
  430. * determine the information for each file. I.e., the caller must still be
  431. * able to filter directories out.
  432. */
  433. if (flags & GLOB_ONLYDIR) {
  434. struct stat s;
  435. if (0 != VCWD_STAT(globbuf.gl_pathv[n], &s)) {
  436. continue;
  437. }
  438. if (S_IFDIR != (s.st_mode & S_IFMT)) {
  439. continue;
  440. }
  441. }
  442. add_next_index_string(return_value, globbuf.gl_pathv[n]+cwd_skip, 1);
  443. }
  444. globfree(&globbuf);
  445. if (basedir_limit && !zend_hash_num_elements(Z_ARRVAL_P(return_value))) {
  446. zval_dtor(return_value);
  447. RETURN_FALSE;
  448. }
  449. }
  450. /* }}} */
  451. #endif
  452. /* {{{ proto array scandir(string dir [, int sorting_order [, resource context]])
  453. List files & directories inside the specified path */
  454. PHP_FUNCTION(scandir)
  455. {
  456. char *dirn;
  457. int dirn_len;
  458. long flags = 0;
  459. char **namelist;
  460. int n, i;
  461. zval *zcontext = NULL;
  462. php_stream_context *context = NULL;
  463. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &dirn, &dirn_len, &flags, &zcontext) == FAILURE) {
  464. return;
  465. }
  466. if (strlen(dirn) != dirn_len) {
  467. RETURN_FALSE;
  468. }
  469. if (dirn_len < 1) {
  470. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Directory name cannot be empty");
  471. RETURN_FALSE;
  472. }
  473. if (zcontext) {
  474. context = php_stream_context_from_zval(zcontext, 0);
  475. }
  476. if (!flags) {
  477. n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasort);
  478. } else {
  479. n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
  480. }
  481. if (n < 0) {
  482. php_error_docref(NULL TSRMLS_CC, E_WARNING, "(errno %d): %s", errno, strerror(errno));
  483. RETURN_FALSE;
  484. }
  485. array_init(return_value);
  486. for (i = 0; i < n; i++) {
  487. add_next_index_string(return_value, namelist[i], 0);
  488. }
  489. if (n) {
  490. efree(namelist);
  491. }
  492. }
  493. /* }}} */
  494. /*
  495. * Local variables:
  496. * tab-width: 4
  497. * c-basic-offset: 4
  498. * End:
  499. * vim600: sw=4 ts=4 fdm=marker
  500. * vim<600: sw=4 ts=4
  501. */