dirstream.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. +----------------------------------------------------------------------+
  3. | phar:// stream wrapper support |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2005-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: Gregory Beaver <cellog@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #define PHAR_DIRSTREAM 1
  20. #include "phar_internal.h"
  21. #include "dirstream.h"
  22. BEGIN_EXTERN_C()
  23. void phar_dostat(phar_archive_data *phar, phar_entry_info *data, php_stream_statbuf *ssb, zend_bool is_dir);
  24. END_EXTERN_C()
  25. const php_stream_ops phar_dir_ops = {
  26. phar_dir_write, /* write */
  27. phar_dir_read, /* read */
  28. phar_dir_close, /* close */
  29. phar_dir_flush, /* flush */
  30. "phar dir",
  31. phar_dir_seek, /* seek */
  32. NULL, /* cast */
  33. NULL, /* stat */
  34. NULL, /* set option */
  35. };
  36. /**
  37. * Used for closedir($fp) where $fp is an opendir('phar://...') directory handle
  38. */
  39. static int phar_dir_close(php_stream *stream, int close_handle) /* {{{ */
  40. {
  41. HashTable *data = (HashTable *)stream->abstract;
  42. if (data && HT_FLAGS(data)) {
  43. zend_hash_destroy(data);
  44. HT_FLAGS(data) = 0;
  45. FREE_HASHTABLE(data);
  46. stream->abstract = NULL;
  47. }
  48. return 0;
  49. }
  50. /* }}} */
  51. /**
  52. * Used for seeking on a phar directory handle
  53. */
  54. static int phar_dir_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset) /* {{{ */
  55. {
  56. HashTable *data = (HashTable *)stream->abstract;
  57. if (!data) {
  58. return -1;
  59. }
  60. if (whence == SEEK_END) {
  61. whence = SEEK_SET;
  62. offset = zend_hash_num_elements(data) + offset;
  63. }
  64. if (whence == SEEK_SET) {
  65. zend_hash_internal_pointer_reset(data);
  66. }
  67. if (offset < 0) {
  68. return -1;
  69. } else {
  70. *newoffset = 0;
  71. while (*newoffset < offset && zend_hash_move_forward(data) == SUCCESS) {
  72. ++(*newoffset);
  73. }
  74. return 0;
  75. }
  76. }
  77. /* }}} */
  78. /**
  79. * Used for readdir() on an opendir()ed phar directory handle
  80. */
  81. static size_t phar_dir_read(php_stream *stream, char *buf, size_t count) /* {{{ */
  82. {
  83. size_t to_read;
  84. HashTable *data = (HashTable *)stream->abstract;
  85. zend_string *str_key;
  86. zend_ulong unused;
  87. if (HASH_KEY_NON_EXISTENT == zend_hash_get_current_key(data, &str_key, &unused)) {
  88. return 0;
  89. }
  90. zend_hash_move_forward(data);
  91. to_read = MIN(ZSTR_LEN(str_key), count);
  92. if (to_read == 0 || count < ZSTR_LEN(str_key)) {
  93. return 0;
  94. }
  95. memset(buf, 0, sizeof(php_stream_dirent));
  96. memcpy(((php_stream_dirent *) buf)->d_name, ZSTR_VAL(str_key), to_read);
  97. ((php_stream_dirent *) buf)->d_name[to_read + 1] = '\0';
  98. return sizeof(php_stream_dirent);
  99. }
  100. /* }}} */
  101. /**
  102. * Dummy: Used for writing to a phar directory (i.e. not used)
  103. */
  104. static size_t phar_dir_write(php_stream *stream, const char *buf, size_t count) /* {{{ */
  105. {
  106. return 0;
  107. }
  108. /* }}} */
  109. /**
  110. * Dummy: Used for flushing writes to a phar directory (i.e. not used)
  111. */
  112. static int phar_dir_flush(php_stream *stream) /* {{{ */
  113. {
  114. return EOF;
  115. }
  116. /* }}} */
  117. /**
  118. * add an empty element with a char * key to a hash table, avoiding duplicates
  119. *
  120. * This is used to get a unique listing of virtual directories within a phar,
  121. * for iterating over opendir()ed phar directories.
  122. */
  123. static int phar_add_empty(HashTable *ht, char *arKey, uint32_t nKeyLength) /* {{{ */
  124. {
  125. zval dummy;
  126. ZVAL_NULL(&dummy);
  127. zend_hash_str_update(ht, arKey, nKeyLength, &dummy);
  128. return SUCCESS;
  129. }
  130. /* }}} */
  131. /**
  132. * Used for sorting directories alphabetically
  133. */
  134. static int phar_compare_dir_name(const void *a, const void *b) /* {{{ */
  135. {
  136. Bucket *f;
  137. Bucket *s;
  138. int result;
  139. f = (Bucket *) a;
  140. s = (Bucket *) b;
  141. result = zend_binary_strcmp(ZSTR_VAL(f->key), ZSTR_LEN(f->key), ZSTR_VAL(s->key), ZSTR_LEN(s->key));
  142. if (result < 0) {
  143. return -1;
  144. } else if (result > 0) {
  145. return 1;
  146. } else {
  147. return 0;
  148. }
  149. }
  150. /* }}} */
  151. /**
  152. * Create a opendir() directory stream handle by iterating over each of the
  153. * files in a phar and retrieving its relative path. From this, construct
  154. * a list of files/directories that are "in" the directory represented by dir
  155. */
  156. static php_stream *phar_make_dirstream(char *dir, HashTable *manifest) /* {{{ */
  157. {
  158. HashTable *data;
  159. size_t dirlen = strlen(dir);
  160. char *entry, *found, *save;
  161. zend_string *str_key;
  162. size_t keylen;
  163. zend_ulong unused;
  164. ALLOC_HASHTABLE(data);
  165. zend_hash_init(data, 64, NULL, NULL, 0);
  166. if ((*dir == '/' && dirlen == 1 && (manifest->nNumOfElements == 0)) || (dirlen >= sizeof(".phar")-1 && !memcmp(dir, ".phar", sizeof(".phar")-1))) {
  167. /* make empty root directory for empty phar */
  168. /* make empty directory for .phar magic directory */
  169. efree(dir);
  170. return php_stream_alloc(&phar_dir_ops, data, NULL, "r");
  171. }
  172. zend_hash_internal_pointer_reset(manifest);
  173. while (FAILURE != zend_hash_has_more_elements(manifest)) {
  174. if (HASH_KEY_NON_EXISTENT == zend_hash_get_current_key(manifest, &str_key, &unused)) {
  175. break;
  176. }
  177. keylen = ZSTR_LEN(str_key);
  178. if (keylen <= dirlen) {
  179. if (keylen == 0 || keylen < dirlen || !strncmp(ZSTR_VAL(str_key), dir, dirlen)) {
  180. if (SUCCESS != zend_hash_move_forward(manifest)) {
  181. break;
  182. }
  183. continue;
  184. }
  185. }
  186. if (*dir == '/') {
  187. /* root directory */
  188. if (keylen >= sizeof(".phar")-1 && !memcmp(ZSTR_VAL(str_key), ".phar", sizeof(".phar")-1)) {
  189. /* do not add any magic entries to this directory */
  190. if (SUCCESS != zend_hash_move_forward(manifest)) {
  191. break;
  192. }
  193. continue;
  194. }
  195. if (NULL != (found = (char *) memchr(ZSTR_VAL(str_key), '/', keylen))) {
  196. /* the entry has a path separator and is a subdirectory */
  197. entry = (char *) safe_emalloc(found - ZSTR_VAL(str_key), 1, 1);
  198. memcpy(entry, ZSTR_VAL(str_key), found - ZSTR_VAL(str_key));
  199. keylen = found - ZSTR_VAL(str_key);
  200. entry[keylen] = '\0';
  201. } else {
  202. entry = (char *) safe_emalloc(keylen, 1, 1);
  203. memcpy(entry, ZSTR_VAL(str_key), keylen);
  204. entry[keylen] = '\0';
  205. }
  206. goto PHAR_ADD_ENTRY;
  207. } else {
  208. if (0 != memcmp(ZSTR_VAL(str_key), dir, dirlen)) {
  209. /* entry in directory not found */
  210. if (SUCCESS != zend_hash_move_forward(manifest)) {
  211. break;
  212. }
  213. continue;
  214. } else {
  215. if (ZSTR_VAL(str_key)[dirlen] != '/') {
  216. if (SUCCESS != zend_hash_move_forward(manifest)) {
  217. break;
  218. }
  219. continue;
  220. }
  221. }
  222. }
  223. save = ZSTR_VAL(str_key);
  224. save += dirlen + 1; /* seek to just past the path separator */
  225. if (NULL != (found = (char *) memchr(save, '/', keylen - dirlen - 1))) {
  226. /* is subdirectory */
  227. save -= dirlen + 1;
  228. entry = (char *) safe_emalloc(found - save + dirlen, 1, 1);
  229. memcpy(entry, save + dirlen + 1, found - save - dirlen - 1);
  230. keylen = found - save - dirlen - 1;
  231. entry[keylen] = '\0';
  232. } else {
  233. /* is file */
  234. save -= dirlen + 1;
  235. entry = (char *) safe_emalloc(keylen - dirlen, 1, 1);
  236. memcpy(entry, save + dirlen + 1, keylen - dirlen - 1);
  237. entry[keylen - dirlen - 1] = '\0';
  238. keylen = keylen - dirlen - 1;
  239. }
  240. PHAR_ADD_ENTRY:
  241. if (keylen) {
  242. phar_add_empty(data, entry, keylen);
  243. }
  244. efree(entry);
  245. if (SUCCESS != zend_hash_move_forward(manifest)) {
  246. break;
  247. }
  248. }
  249. if (FAILURE != zend_hash_has_more_elements(data)) {
  250. efree(dir);
  251. if (zend_hash_sort(data, phar_compare_dir_name, 0) == FAILURE) {
  252. FREE_HASHTABLE(data);
  253. return NULL;
  254. }
  255. return php_stream_alloc(&phar_dir_ops, data, NULL, "r");
  256. } else {
  257. efree(dir);
  258. return php_stream_alloc(&phar_dir_ops, data, NULL, "r");
  259. }
  260. }
  261. /* }}}*/
  262. /**
  263. * Open a directory handle within a phar archive
  264. */
  265. php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) /* {{{ */
  266. {
  267. php_url *resource = NULL;
  268. php_stream *ret;
  269. char *internal_file, *error;
  270. zend_string *str_key;
  271. zend_ulong unused;
  272. phar_archive_data *phar;
  273. phar_entry_info *entry = NULL;
  274. uint32_t host_len;
  275. if ((resource = phar_parse_url(wrapper, path, mode, options)) == NULL) {
  276. php_stream_wrapper_log_error(wrapper, options, "phar url \"%s\" is unknown", path);
  277. return NULL;
  278. }
  279. /* we must have at the very least phar://alias.phar/ */
  280. if (!resource->scheme || !resource->host || !resource->path) {
  281. if (resource->host && !resource->path) {
  282. php_stream_wrapper_log_error(wrapper, options, "phar error: no directory in \"%s\", must have at least phar://%s/ for root directory (always use full path to a new phar)", path, ZSTR_VAL(resource->host));
  283. php_url_free(resource);
  284. return NULL;
  285. }
  286. php_url_free(resource);
  287. php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\", must have at least phar://%s/", path, path);
  288. return NULL;
  289. }
  290. if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
  291. php_url_free(resource);
  292. php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar url \"%s\"", path);
  293. return NULL;
  294. }
  295. host_len = ZSTR_LEN(resource->host);
  296. phar_request_initialize();
  297. internal_file = ZSTR_VAL(resource->path) + 1; /* strip leading "/" */
  298. if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), host_len, NULL, 0, &error)) {
  299. if (error) {
  300. php_stream_wrapper_log_error(wrapper, options, "%s", error);
  301. efree(error);
  302. } else {
  303. php_stream_wrapper_log_error(wrapper, options, "phar file \"%s\" is unknown", ZSTR_VAL(resource->host));
  304. }
  305. php_url_free(resource);
  306. return NULL;
  307. }
  308. if (error) {
  309. efree(error);
  310. }
  311. if (*internal_file == '\0') {
  312. /* root directory requested */
  313. internal_file = estrndup(internal_file - 1, 1);
  314. ret = phar_make_dirstream(internal_file, &phar->manifest);
  315. php_url_free(resource);
  316. return ret;
  317. }
  318. if (!HT_FLAGS(&phar->manifest)) {
  319. php_url_free(resource);
  320. return NULL;
  321. }
  322. if (NULL != (entry = zend_hash_str_find_ptr(&phar->manifest, internal_file, strlen(internal_file))) && !entry->is_dir) {
  323. php_url_free(resource);
  324. return NULL;
  325. } else if (entry && entry->is_dir) {
  326. if (entry->is_mounted) {
  327. php_url_free(resource);
  328. return php_stream_opendir(entry->tmp, options, context);
  329. }
  330. internal_file = estrdup(internal_file);
  331. php_url_free(resource);
  332. return phar_make_dirstream(internal_file, &phar->manifest);
  333. } else {
  334. size_t i_len = strlen(internal_file);
  335. /* search for directory */
  336. zend_hash_internal_pointer_reset(&phar->manifest);
  337. while (FAILURE != zend_hash_has_more_elements(&phar->manifest)) {
  338. if (HASH_KEY_NON_EXISTENT !=
  339. zend_hash_get_current_key(&phar->manifest, &str_key, &unused)) {
  340. if (ZSTR_LEN(str_key) > i_len && 0 == memcmp(ZSTR_VAL(str_key), internal_file, i_len)) {
  341. /* directory found */
  342. internal_file = estrndup(internal_file,
  343. i_len);
  344. php_url_free(resource);
  345. return phar_make_dirstream(internal_file, &phar->manifest);
  346. }
  347. }
  348. if (SUCCESS != zend_hash_move_forward(&phar->manifest)) {
  349. break;
  350. }
  351. }
  352. }
  353. php_url_free(resource);
  354. return NULL;
  355. }
  356. /* }}} */
  357. /**
  358. * Make a new directory within a phar archive
  359. */
  360. int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mode, int options, php_stream_context *context) /* {{{ */
  361. {
  362. phar_entry_info entry, *e;
  363. phar_archive_data *phar = NULL;
  364. char *error, *arch, *entry2;
  365. size_t arch_len, entry_len;
  366. php_url *resource = NULL;
  367. uint32_t host_len;
  368. /* pre-readonly check, we need to know if this is a data phar */
  369. if (FAILURE == phar_split_fname(url_from, strlen(url_from), &arch, &arch_len, &entry2, &entry_len, 2, 2)) {
  370. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\", no phar archive specified", url_from);
  371. return 0;
  372. }
  373. if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
  374. phar = NULL;
  375. }
  376. efree(arch);
  377. efree(entry2);
  378. if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
  379. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\", write operations disabled", url_from);
  380. return 0;
  381. }
  382. if ((resource = phar_parse_url(wrapper, url_from, "w", options)) == NULL) {
  383. return 0;
  384. }
  385. /* we must have at the very least phar://alias.phar/internalfile.php */
  386. if (!resource->scheme || !resource->host || !resource->path) {
  387. php_url_free(resource);
  388. php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\"", url_from);
  389. return 0;
  390. }
  391. if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
  392. php_url_free(resource);
  393. php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar stream url \"%s\"", url_from);
  394. return 0;
  395. }
  396. host_len = ZSTR_LEN(resource->host);
  397. if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), host_len, NULL, 0, &error)) {
  398. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", error retrieving phar information: %s", ZSTR_VAL(resource->path) + 1, ZSTR_VAL(resource->host), error);
  399. efree(error);
  400. php_url_free(resource);
  401. return 0;
  402. }
  403. if ((e = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, ZSTR_LEN(resource->path) - 1, 2, &error, 1))) {
  404. /* directory exists, or is a subdirectory of an existing file */
  405. if (e->is_temp_dir) {
  406. efree(e->filename);
  407. efree(e);
  408. }
  409. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", directory already exists", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host));
  410. php_url_free(resource);
  411. return 0;
  412. }
  413. if (error) {
  414. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
  415. efree(error);
  416. php_url_free(resource);
  417. return 0;
  418. }
  419. if (phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, ZSTR_LEN(resource->path) - 1, 0, &error, 1)) {
  420. /* entry exists as a file */
  421. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", file already exists", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host));
  422. php_url_free(resource);
  423. return 0;
  424. }
  425. if (error) {
  426. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
  427. efree(error);
  428. php_url_free(resource);
  429. return 0;
  430. }
  431. memset((void *) &entry, 0, sizeof(phar_entry_info));
  432. /* strip leading "/" */
  433. if (phar->is_zip) {
  434. entry.is_zip = 1;
  435. }
  436. entry.filename = estrdup(ZSTR_VAL(resource->path) + 1);
  437. if (phar->is_tar) {
  438. entry.is_tar = 1;
  439. entry.tar_type = TAR_DIR;
  440. }
  441. entry.filename_len = ZSTR_LEN(resource->path) - 1;
  442. php_url_free(resource);
  443. entry.is_dir = 1;
  444. entry.phar = phar;
  445. entry.is_modified = 1;
  446. entry.is_crc_checked = 1;
  447. entry.flags = PHAR_ENT_PERM_DEF_DIR;
  448. entry.old_flags = PHAR_ENT_PERM_DEF_DIR;
  449. if (NULL == zend_hash_str_add_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) {
  450. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", adding to manifest failed", entry.filename, phar->fname);
  451. efree(error);
  452. efree(entry.filename);
  453. return 0;
  454. }
  455. phar_flush(phar, 0, 0, 0, &error);
  456. if (error) {
  457. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", entry.filename, phar->fname, error);
  458. zend_hash_str_del(&phar->manifest, entry.filename, entry.filename_len);
  459. efree(error);
  460. return 0;
  461. }
  462. phar_add_virtual_dirs(phar, entry.filename, entry.filename_len);
  463. return 1;
  464. }
  465. /* }}} */
  466. /**
  467. * Remove a directory within a phar archive
  468. */
  469. int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) /* {{{ */
  470. {
  471. phar_entry_info *entry;
  472. phar_archive_data *phar = NULL;
  473. char *error, *arch, *entry2;
  474. size_t arch_len, entry_len;
  475. php_url *resource = NULL;
  476. uint32_t host_len;
  477. zend_string *str_key;
  478. zend_ulong unused;
  479. uint32_t path_len;
  480. /* pre-readonly check, we need to know if this is a data phar */
  481. if (FAILURE == phar_split_fname(url, strlen(url), &arch, &arch_len, &entry2, &entry_len, 2, 2)) {
  482. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\", no phar archive specified, or phar archive does not exist", url);
  483. return 0;
  484. }
  485. if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
  486. phar = NULL;
  487. }
  488. efree(arch);
  489. efree(entry2);
  490. if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
  491. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot rmdir directory \"%s\", write operations disabled", url);
  492. return 0;
  493. }
  494. if ((resource = phar_parse_url(wrapper, url, "w", options)) == NULL) {
  495. return 0;
  496. }
  497. /* we must have at the very least phar://alias.phar/internalfile.php */
  498. if (!resource->scheme || !resource->host || !resource->path) {
  499. php_url_free(resource);
  500. php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url \"%s\"", url);
  501. return 0;
  502. }
  503. if (!zend_string_equals_literal_ci(resource->scheme, "phar")) {
  504. php_url_free(resource);
  505. php_stream_wrapper_log_error(wrapper, options, "phar error: not a phar stream url \"%s\"", url);
  506. return 0;
  507. }
  508. host_len = ZSTR_LEN(resource->host);
  509. if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), host_len, NULL, 0, &error)) {
  510. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", error retrieving phar information: %s", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
  511. efree(error);
  512. php_url_free(resource);
  513. return 0;
  514. }
  515. path_len = ZSTR_LEN(resource->path) - 1;
  516. if (!(entry = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, path_len, 2, &error, 1))) {
  517. if (error) {
  518. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
  519. efree(error);
  520. } else {
  521. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", directory does not exist", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host));
  522. }
  523. php_url_free(resource);
  524. return 0;
  525. }
  526. if (!entry->is_deleted) {
  527. for (zend_hash_internal_pointer_reset(&phar->manifest);
  528. HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&phar->manifest, &str_key, &unused);
  529. zend_hash_move_forward(&phar->manifest)
  530. ) {
  531. if (ZSTR_LEN(str_key) > path_len &&
  532. memcmp(ZSTR_VAL(str_key), ZSTR_VAL(resource->path)+1, path_len) == 0 &&
  533. IS_SLASH(ZSTR_VAL(str_key)[path_len])) {
  534. php_stream_wrapper_log_error(wrapper, options, "phar error: Directory not empty");
  535. if (entry->is_temp_dir) {
  536. efree(entry->filename);
  537. efree(entry);
  538. }
  539. php_url_free(resource);
  540. return 0;
  541. }
  542. }
  543. for (zend_hash_internal_pointer_reset(&phar->virtual_dirs);
  544. HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&phar->virtual_dirs, &str_key, &unused);
  545. zend_hash_move_forward(&phar->virtual_dirs)) {
  546. if (ZSTR_LEN(str_key) > path_len &&
  547. memcmp(ZSTR_VAL(str_key), ZSTR_VAL(resource->path)+1, path_len) == 0 &&
  548. IS_SLASH(ZSTR_VAL(str_key)[path_len])) {
  549. php_stream_wrapper_log_error(wrapper, options, "phar error: Directory not empty");
  550. if (entry->is_temp_dir) {
  551. efree(entry->filename);
  552. efree(entry);
  553. }
  554. php_url_free(resource);
  555. return 0;
  556. }
  557. }
  558. }
  559. if (entry->is_temp_dir) {
  560. zend_hash_str_del(&phar->virtual_dirs, ZSTR_VAL(resource->path)+1, path_len);
  561. efree(entry->filename);
  562. efree(entry);
  563. } else {
  564. entry->is_deleted = 1;
  565. entry->is_modified = 1;
  566. phar_flush(phar, 0, 0, 0, &error);
  567. if (error) {
  568. php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", entry->filename, phar->fname, error);
  569. php_url_free(resource);
  570. efree(error);
  571. return 0;
  572. }
  573. }
  574. php_url_free(resource);
  575. return 1;
  576. }
  577. /* }}} */
  578. /*
  579. * Local variables:
  580. * tab-width: 4
  581. * c-basic-offset: 4
  582. * End:
  583. * vim600: noet sw=4 ts=4 fdm=marker
  584. * vim<600: noet sw=4 ts=4
  585. */