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