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