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