dirstream.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. +----------------------------------------------------------------------+
  3. | phar:// stream wrapper support |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2005-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: 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 TSRMLS_DC);
  24. END_EXTERN_C()
  25. 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 TSRMLS_DC) /* {{{ */
  40. {
  41. HashTable *data = (HashTable *)stream->abstract;
  42. if (data && data->arBuckets) {
  43. zend_hash_destroy(data);
  44. data->arBuckets = 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, off_t offset, int whence, off_t *newoffset TSRMLS_DC) /* {{{ */
  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 TSRMLS_DC) /* {{{ */
  82. {
  83. size_t to_read;
  84. HashTable *data = (HashTable *)stream->abstract;
  85. char *str_key;
  86. uint keylen;
  87. ulong unused;
  88. if (HASH_KEY_NON_EXISTENT == zend_hash_get_current_key_ex(data, &str_key, &keylen, &unused, 0, NULL)) {
  89. return 0;
  90. }
  91. zend_hash_move_forward(data);
  92. to_read = MIN(keylen, count);
  93. if (to_read == 0 || count < keylen) {
  94. return 0;
  95. }
  96. memset(buf, 0, sizeof(php_stream_dirent));
  97. memcpy(((php_stream_dirent *) buf)->d_name, str_key, to_read);
  98. ((php_stream_dirent *) buf)->d_name[to_read + 1] = '\0';
  99. return sizeof(php_stream_dirent);
  100. }
  101. /* }}} */
  102. /**
  103. * Dummy: Used for writing to a phar directory (i.e. not used)
  104. */
  105. static size_t phar_dir_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) /* {{{ */
  106. {
  107. return 0;
  108. }
  109. /* }}} */
  110. /**
  111. * Dummy: Used for flushing writes to a phar directory (i.e. not used)
  112. */
  113. static int phar_dir_flush(php_stream *stream TSRMLS_DC) /* {{{ */
  114. {
  115. return EOF;
  116. }
  117. /* }}} */
  118. /**
  119. * add an empty element with a char * key to a hash table, avoiding duplicates
  120. *
  121. * This is used to get a unique listing of virtual directories within a phar,
  122. * for iterating over opendir()ed phar directories.
  123. */
  124. static int phar_add_empty(HashTable *ht, char *arKey, uint nKeyLength) /* {{{ */
  125. {
  126. void *dummy = (char *) 1;
  127. return zend_hash_update(ht, arKey, nKeyLength, (void *) &dummy, sizeof(void *), NULL);
  128. }
  129. /* }}} */
  130. /**
  131. * Used for sorting directories alphabetically
  132. */
  133. static int phar_compare_dir_name(const void *a, const void *b TSRMLS_DC) /* {{{ */
  134. {
  135. Bucket *f;
  136. Bucket *s;
  137. int result;
  138. f = *((Bucket **) a);
  139. s = *((Bucket **) b);
  140. result = zend_binary_strcmp(f->arKey, f->nKeyLength, s->arKey, s->nKeyLength);
  141. if (result < 0) {
  142. return -1;
  143. } else if (result > 0) {
  144. return 1;
  145. } else {
  146. return 0;
  147. }
  148. }
  149. /* }}} */
  150. /**
  151. * Create a opendir() directory stream handle by iterating over each of the
  152. * files in a phar and retrieving its relative path. From this, construct
  153. * a list of files/directories that are "in" the directory represented by dir
  154. */
  155. static php_stream *phar_make_dirstream(char *dir, HashTable *manifest TSRMLS_DC) /* {{{ */
  156. {
  157. HashTable *data;
  158. int dirlen = strlen(dir);
  159. char *entry, *found, *save, *str_key;
  160. uint keylen;
  161. ulong unused;
  162. ALLOC_HASHTABLE(data);
  163. zend_hash_init(data, 64, NULL, NULL, 0);
  164. if ((*dir == '/' && dirlen == 1 && (manifest->nNumOfElements == 0)) || (dirlen >= sizeof(".phar")-1 && !memcmp(dir, ".phar", sizeof(".phar")-1))) {
  165. /* make empty root directory for empty phar */
  166. /* make empty directory for .phar magic directory */
  167. efree(dir);
  168. return php_stream_alloc(&phar_dir_ops, data, NULL, "r");
  169. }
  170. zend_hash_internal_pointer_reset(manifest);
  171. while (FAILURE != zend_hash_has_more_elements(manifest)) {
  172. keylen = 0;
  173. if (HASH_KEY_NON_EXISTENT == zend_hash_get_current_key_ex(manifest, &str_key, &keylen, &unused, 0, NULL)) {
  174. break;
  175. }
  176. if (keylen <= (uint)dirlen) {
  177. if (keylen == 0 || keylen < (uint)dirlen || !strncmp(str_key, dir, dirlen)) {
  178. if (SUCCESS != zend_hash_move_forward(manifest)) {
  179. break;
  180. }
  181. continue;
  182. }
  183. }
  184. if (*dir == '/') {
  185. /* root directory */
  186. if (keylen >= sizeof(".phar")-1 && !memcmp(str_key, ".phar", sizeof(".phar")-1)) {
  187. /* do not add any magic entries to this directory */
  188. if (SUCCESS != zend_hash_move_forward(manifest)) {
  189. break;
  190. }
  191. continue;
  192. }
  193. if (NULL != (found = (char *) memchr(str_key, '/', keylen))) {
  194. /* the entry has a path separator and is a subdirectory */
  195. entry = (char *) safe_emalloc(found - str_key, 1, 1);
  196. memcpy(entry, str_key, found - str_key);
  197. keylen = found - str_key;
  198. entry[keylen] = '\0';
  199. } else {
  200. entry = (char *) safe_emalloc(keylen, 1, 1);
  201. memcpy(entry, str_key, keylen);
  202. entry[keylen] = '\0';
  203. }
  204. goto PHAR_ADD_ENTRY;
  205. } else {
  206. if (0 != memcmp(str_key, dir, dirlen)) {
  207. /* entry in directory not found */
  208. if (SUCCESS != zend_hash_move_forward(manifest)) {
  209. break;
  210. }
  211. continue;
  212. } else {
  213. if (str_key[dirlen] != '/') {
  214. if (SUCCESS != zend_hash_move_forward(manifest)) {
  215. break;
  216. }
  217. continue;
  218. }
  219. }
  220. }
  221. save = str_key;
  222. save += dirlen + 1; /* seek to just past the path separator */
  223. if (NULL != (found = (char *) memchr(save, '/', keylen - dirlen - 1))) {
  224. /* is subdirectory */
  225. save -= dirlen + 1;
  226. entry = (char *) safe_emalloc(found - save + dirlen, 1, 1);
  227. memcpy(entry, save + dirlen + 1, found - save - dirlen - 1);
  228. keylen = found - save - dirlen - 1;
  229. entry[keylen] = '\0';
  230. } else {
  231. /* is file */
  232. save -= dirlen + 1;
  233. entry = (char *) safe_emalloc(keylen - dirlen, 1, 1);
  234. memcpy(entry, save + dirlen + 1, keylen - dirlen - 1);
  235. entry[keylen - dirlen - 1] = '\0';
  236. keylen = keylen - dirlen - 1;
  237. }
  238. PHAR_ADD_ENTRY:
  239. if (keylen) {
  240. phar_add_empty(data, entry, keylen);
  241. }
  242. efree(entry);
  243. if (SUCCESS != zend_hash_move_forward(manifest)) {
  244. break;
  245. }
  246. }
  247. if (FAILURE != zend_hash_has_more_elements(data)) {
  248. efree(dir);
  249. if (zend_hash_sort(data, zend_qsort, phar_compare_dir_name, 0 TSRMLS_CC) == FAILURE) {
  250. FREE_HASHTABLE(data);
  251. return NULL;
  252. }
  253. return php_stream_alloc(&phar_dir_ops, data, NULL, "r");
  254. } else {
  255. efree(dir);
  256. return php_stream_alloc(&phar_dir_ops, data, NULL, "r");
  257. }
  258. }
  259. /* }}}*/
  260. /**
  261. * Open a directory handle within a phar archive
  262. */
  263. php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC) /* {{{ */
  264. {
  265. php_url *resource = NULL;
  266. php_stream *ret;
  267. char *internal_file, *error, *str_key;
  268. uint keylen;
  269. ulong unused;
  270. phar_archive_data *phar;
  271. phar_entry_info *entry = NULL;
  272. uint host_len;
  273. if ((resource = phar_parse_url(wrapper, path, mode, options TSRMLS_CC)) == NULL) {
  274. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar url \"%s\" is unknown", path);
  275. return NULL;
  276. }
  277. /* we must have at the very least phar://alias.phar/ */
  278. if (!resource->scheme || !resource->host || !resource->path) {
  279. if (resource->host && !resource->path) {
  280. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: no directory in \"%s\", must have at least phar://%s/ for root directory (always use full path to a new phar)", path, resource->host);
  281. php_url_free(resource);
  282. return NULL;
  283. }
  284. php_url_free(resource);
  285. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: invalid url \"%s\", must have at least phar://%s/", path, path);
  286. return NULL;
  287. }
  288. if (strcasecmp("phar", resource->scheme)) {
  289. php_url_free(resource);
  290. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: not a phar url \"%s\"", path);
  291. return NULL;
  292. }
  293. host_len = strlen(resource->host);
  294. phar_request_initialize(TSRMLS_C);
  295. internal_file = resource->path + 1; /* strip leading "/" */
  296. if (FAILURE == phar_get_archive(&phar, resource->host, host_len, NULL, 0, &error TSRMLS_CC)) {
  297. if (error) {
  298. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "%s", error);
  299. efree(error);
  300. } else {
  301. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar file \"%s\" is unknown", resource->host);
  302. }
  303. php_url_free(resource);
  304. return NULL;
  305. }
  306. if (error) {
  307. efree(error);
  308. }
  309. if (*internal_file == '\0') {
  310. /* root directory requested */
  311. internal_file = estrndup(internal_file - 1, 1);
  312. ret = phar_make_dirstream(internal_file, &phar->manifest TSRMLS_CC);
  313. php_url_free(resource);
  314. return ret;
  315. }
  316. if (!phar->manifest.arBuckets) {
  317. php_url_free(resource);
  318. return NULL;
  319. }
  320. if (SUCCESS == zend_hash_find(&phar->manifest, internal_file, strlen(internal_file), (void**)&entry) && !entry->is_dir) {
  321. php_url_free(resource);
  322. return NULL;
  323. } else if (entry && entry->is_dir) {
  324. if (entry->is_mounted) {
  325. php_url_free(resource);
  326. return php_stream_opendir(entry->tmp, options, context);
  327. }
  328. internal_file = estrdup(internal_file);
  329. php_url_free(resource);
  330. return phar_make_dirstream(internal_file, &phar->manifest TSRMLS_CC);
  331. } else {
  332. int i_len = strlen(internal_file);
  333. /* search for directory */
  334. zend_hash_internal_pointer_reset(&phar->manifest);
  335. while (FAILURE != zend_hash_has_more_elements(&phar->manifest)) {
  336. if (HASH_KEY_NON_EXISTENT !=
  337. zend_hash_get_current_key_ex(
  338. &phar->manifest, &str_key, &keylen, &unused, 0, NULL)) {
  339. if (keylen > (uint)i_len && 0 == memcmp(str_key, internal_file, i_len)) {
  340. /* directory found */
  341. internal_file = estrndup(internal_file,
  342. i_len);
  343. php_url_free(resource);
  344. return phar_make_dirstream(internal_file, &phar->manifest TSRMLS_CC);
  345. }
  346. }
  347. if (SUCCESS != zend_hash_move_forward(&phar->manifest)) {
  348. break;
  349. }
  350. }
  351. }
  352. php_url_free(resource);
  353. return NULL;
  354. }
  355. /* }}} */
  356. /**
  357. * Make a new directory within a phar archive
  358. */
  359. int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mode, int options, php_stream_context *context TSRMLS_DC) /* {{{ */
  360. {
  361. phar_entry_info entry, *e;
  362. phar_archive_data *phar = NULL;
  363. char *error, *arch, *entry2;
  364. int arch_len, entry_len;
  365. php_url *resource = NULL;
  366. uint host_len;
  367. /* pre-readonly check, we need to know if this is a data phar */
  368. if (FAILURE == phar_split_fname(url_from, strlen(url_from), &arch, &arch_len, &entry2, &entry_len, 2, 2 TSRMLS_CC)) {
  369. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\", no phar archive specified", url_from);
  370. return 0;
  371. }
  372. if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL TSRMLS_CC)) {
  373. phar = NULL;
  374. }
  375. efree(arch);
  376. efree(entry2);
  377. if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
  378. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\", write operations disabled", url_from);
  379. return 0;
  380. }
  381. if ((resource = phar_parse_url(wrapper, url_from, "w", options TSRMLS_CC)) == NULL) {
  382. return 0;
  383. }
  384. /* we must have at the very least phar://alias.phar/internalfile.php */
  385. if (!resource->scheme || !resource->host || !resource->path) {
  386. php_url_free(resource);
  387. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: invalid url \"%s\"", url_from);
  388. return 0;
  389. }
  390. if (strcasecmp("phar", resource->scheme)) {
  391. php_url_free(resource);
  392. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: not a phar stream url \"%s\"", url_from);
  393. return 0;
  394. }
  395. host_len = strlen(resource->host);
  396. if (FAILURE == phar_get_archive(&phar, resource->host, host_len, NULL, 0, &error TSRMLS_CC)) {
  397. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", error retrieving phar information: %s", resource->path+1, resource->host, error);
  398. efree(error);
  399. php_url_free(resource);
  400. return 0;
  401. }
  402. if ((e = phar_get_entry_info_dir(phar, resource->path + 1, strlen(resource->path + 1), 2, &error, 1 TSRMLS_CC))) {
  403. /* directory exists, or is a subdirectory of an existing file */
  404. if (e->is_temp_dir) {
  405. efree(e->filename);
  406. efree(e);
  407. }
  408. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", directory already exists", resource->path+1, resource->host);
  409. php_url_free(resource);
  410. return 0;
  411. }
  412. if (error) {
  413. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", resource->path+1, resource->host, error);
  414. efree(error);
  415. php_url_free(resource);
  416. return 0;
  417. }
  418. if (phar_get_entry_info_dir(phar, resource->path + 1, strlen(resource->path + 1), 0, &error, 1 TSRMLS_CC)) {
  419. /* entry exists as a file */
  420. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", file already exists", resource->path+1, resource->host);
  421. php_url_free(resource);
  422. return 0;
  423. }
  424. if (error) {
  425. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", resource->path+1, resource->host, error);
  426. efree(error);
  427. php_url_free(resource);
  428. return 0;
  429. }
  430. memset((void *) &entry, 0, sizeof(phar_entry_info));
  431. /* strip leading "/" */
  432. if (phar->is_zip) {
  433. entry.is_zip = 1;
  434. }
  435. entry.filename = estrdup(resource->path + 1);
  436. if (phar->is_tar) {
  437. entry.is_tar = 1;
  438. entry.tar_type = TAR_DIR;
  439. }
  440. entry.filename_len = strlen(resource->path + 1);
  441. php_url_free(resource);
  442. entry.is_dir = 1;
  443. entry.phar = phar;
  444. entry.is_modified = 1;
  445. entry.is_crc_checked = 1;
  446. entry.flags = PHAR_ENT_PERM_DEF_DIR;
  447. entry.old_flags = PHAR_ENT_PERM_DEF_DIR;
  448. if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
  449. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", adding to manifest failed", entry.filename, phar->fname);
  450. efree(error);
  451. efree(entry.filename);
  452. return 0;
  453. }
  454. phar_flush(phar, 0, 0, 0, &error TSRMLS_CC);
  455. if (error) {
  456. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", entry.filename, phar->fname, error);
  457. zend_hash_del(&phar->manifest, entry.filename, entry.filename_len);
  458. efree(error);
  459. return 0;
  460. }
  461. phar_add_virtual_dirs(phar, entry.filename, entry.filename_len TSRMLS_CC);
  462. return 1;
  463. }
  464. /* }}} */
  465. /**
  466. * Remove a directory within a phar archive
  467. */
  468. int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context TSRMLS_DC) /* {{{ */
  469. {
  470. phar_entry_info *entry;
  471. phar_archive_data *phar = NULL;
  472. char *error, *arch, *entry2;
  473. int arch_len, entry_len;
  474. php_url *resource = NULL;
  475. uint host_len;
  476. char *str_key;
  477. uint key_len;
  478. ulong unused;
  479. uint 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 TSRMLS_CC)) {
  482. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "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 TSRMLS_CC)) {
  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 TSRMLS_CC, "phar error: cannot rmdir directory \"%s\", write operations disabled", url);
  492. return 0;
  493. }
  494. if ((resource = phar_parse_url(wrapper, url, "w", options TSRMLS_CC)) == 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 TSRMLS_CC, "phar error: invalid url \"%s\"", url);
  501. return 0;
  502. }
  503. if (strcasecmp("phar", resource->scheme)) {
  504. php_url_free(resource);
  505. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: not a phar stream url \"%s\"", url);
  506. return 0;
  507. }
  508. host_len = strlen(resource->host);
  509. if (FAILURE == phar_get_archive(&phar, resource->host, host_len, NULL, 0, &error TSRMLS_CC)) {
  510. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot remove directory \"%s\" in phar \"%s\", error retrieving phar information: %s", resource->path+1, resource->host, error);
  511. efree(error);
  512. php_url_free(resource);
  513. return 0;
  514. }
  515. path_len = strlen(resource->path+1);
  516. if (!(entry = phar_get_entry_info_dir(phar, resource->path + 1, path_len, 2, &error, 1 TSRMLS_CC))) {
  517. if (error) {
  518. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", resource->path+1, resource->host, error);
  519. efree(error);
  520. } else {
  521. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot remove directory \"%s\" in phar \"%s\", directory does not exist", resource->path+1, 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_ex(&phar->manifest, &str_key, &key_len, &unused, 0, NULL);
  529. zend_hash_move_forward(&phar->manifest)
  530. ) {
  531. if (key_len > path_len &&
  532. memcmp(str_key, resource->path+1, path_len) == 0 &&
  533. IS_SLASH(str_key[path_len])) {
  534. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "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_ex(&phar->virtual_dirs, &str_key, &key_len, &unused, 0, NULL);
  545. zend_hash_move_forward(&phar->virtual_dirs)) {
  546. if (key_len > path_len &&
  547. memcmp(str_key, resource->path+1, path_len) == 0 &&
  548. IS_SLASH(str_key[path_len])) {
  549. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "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_del(&phar->virtual_dirs, 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 TSRMLS_CC);
  567. if (error) {
  568. php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "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. */