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

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:54 +0000 (8 days ago)

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