PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/phar - dirstream.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 260 337 77.2 %
Date: 2014-04-19 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 20 Apr 2014 03:52:13 +0000 (3 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.