Back to index

php5  5.3.10
Functions
dirstream.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int phar_wrapper_mkdir (php_stream_wrapper *wrapper, char *url_from, int mode, int options, php_stream_context *context TSRMLS_DC)
 Make a new directory within a phar archive.
int phar_wrapper_rmdir (php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
 Remove a directory within a phar archive.
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)
 Open a directory handle within a phar archive.

Function Documentation

int phar_wrapper_mkdir ( php_stream_wrapper *  wrapper,
char *  url_from,
int  mode,
int  options,
php_stream_context *context  TSRMLS_DC 
)

Make a new directory within a phar archive.

Definition at line 435 of file dirstream.c.

{
       phar_entry_info entry, *e;
       phar_archive_data *phar = NULL;
       char *error, *arch, *entry2;
       int arch_len, entry_len;
       php_url *resource = NULL;
       uint host_len;

       /* pre-readonly check, we need to know if this is a data phar */
       if (FAILURE == phar_split_fname(url_from, strlen(url_from), &arch, &arch_len, &entry2, &entry_len, 2, 2 TSRMLS_CC)) {
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\", no phar archive specified", url_from);
              return 0;
       }

       if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL TSRMLS_CC)) {
              phar = NULL;
       }

       efree(arch);
       efree(entry2);

       if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\", write operations disabled", url_from);
              return 0;
       }

       if ((resource = phar_parse_url(wrapper, url_from, "w", options TSRMLS_CC)) == NULL) {
              return 0;
       }

       /* we must have at the very least phar://alias.phar/internalfile.php */
       if (!resource->scheme || !resource->host || !resource->path) {
              php_url_free(resource);
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: invalid url \"%s\"", url_from);
              return 0;
       }

       if (strcasecmp("phar", resource->scheme)) {
              php_url_free(resource);
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: not a phar stream url \"%s\"", url_from);
              return 0;
       }

       host_len = strlen(resource->host);

       if (FAILURE == phar_get_archive(&phar, resource->host, host_len, NULL, 0, &error TSRMLS_CC)) {
              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);
              efree(error);
              php_url_free(resource);
              return 0;
       }

       if ((e = phar_get_entry_info_dir(phar, resource->path + 1, strlen(resource->path + 1), 2, &error, 1 TSRMLS_CC))) {
              /* directory exists, or is a subdirectory of an existing file */
              if (e->is_temp_dir) {
                     efree(e->filename);
                     efree(e);
              }
              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);
              php_url_free(resource);
              return 0;
       }

       if (error) {
              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);
              efree(error);
              php_url_free(resource);
              return 0;
       }

       if (phar_get_entry_info_dir(phar, resource->path + 1, strlen(resource->path + 1), 0, &error, 1 TSRMLS_CC)) {
              /* entry exists as a file */
              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);
              php_url_free(resource);
              return 0;
       }

       if (error) {
              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);
              efree(error);
              php_url_free(resource);
              return 0;
       }

       memset((void *) &entry, 0, sizeof(phar_entry_info));

       /* strip leading "/" */
       if (phar->is_zip) {
              entry.is_zip = 1;
       }

       entry.filename = estrdup(resource->path + 1);

       if (phar->is_tar) {
              entry.is_tar = 1;
              entry.tar_type = TAR_DIR;
       }

       entry.filename_len = strlen(resource->path + 1);
       php_url_free(resource);
       entry.is_dir = 1;
       entry.phar = phar;
       entry.is_modified = 1;
       entry.is_crc_checked = 1;
       entry.flags = PHAR_ENT_PERM_DEF_DIR;
       entry.old_flags = PHAR_ENT_PERM_DEF_DIR;

       if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
              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);
              efree(error);
              efree(entry.filename);
              return 0;
       }

       phar_flush(phar, 0, 0, 0, &error TSRMLS_CC);

       if (error) {
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", entry.filename, phar->fname, error);
              zend_hash_del(&phar->manifest, entry.filename, entry.filename_len);
              efree(error);
              return 0;
       }

       phar_add_virtual_dirs(phar, entry.filename, entry.filename_len TSRMLS_CC);
       return 1;
}

Here is the call graph for this function:

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 
)

Open a directory handle within a phar archive.

Definition at line 322 of file dirstream.c.

{
       php_url *resource = NULL;
       php_stream *ret;
       char *internal_file, *error, *str_key;
       phar_zstr key;
       uint keylen;
       ulong unused;
       phar_archive_data *phar;
       phar_entry_info *entry = NULL;
       uint host_len;

       if ((resource = phar_parse_url(wrapper, path, mode, options TSRMLS_CC)) == NULL) {
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar url \"%s\" is unknown", path);
              return NULL;
       }

       /* we must have at the very least phar://alias.phar/ */
       if (!resource->scheme || !resource->host || !resource->path) {
              if (resource->host && !resource->path) {
                     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);
                     php_url_free(resource);
                     return NULL;
              }
              php_url_free(resource);
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: invalid url \"%s\", must have at least phar://%s/", path, path);
              return NULL;
       }

       if (strcasecmp("phar", resource->scheme)) {
              php_url_free(resource);
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: not a phar url \"%s\"", path);
              return NULL;
       }

       host_len = strlen(resource->host);
       phar_request_initialize(TSRMLS_C);
       internal_file = resource->path + 1; /* strip leading "/" */

       if (FAILURE == phar_get_archive(&phar, resource->host, host_len, NULL, 0, &error TSRMLS_CC)) {
              if (error) {
                     php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "%s", error);
                     efree(error);
              } else {
                     php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar file \"%s\" is unknown", resource->host);
              }
              php_url_free(resource);
              return NULL;
       }

       if (error) {
              efree(error);
       }

       if (*internal_file == '\0') {
              /* root directory requested */
              internal_file = estrndup(internal_file - 1, 1);
              ret = phar_make_dirstream(internal_file, &phar->manifest TSRMLS_CC);
              php_url_free(resource);
              return ret;
       }

       if (!phar->manifest.arBuckets) {
              php_url_free(resource);
              return NULL;
       }

       if (SUCCESS == zend_hash_find(&phar->manifest, internal_file, strlen(internal_file), (void**)&entry) && !entry->is_dir) {
              php_url_free(resource);
              return NULL;
       } else if (entry && entry->is_dir) {
              if (entry->is_mounted) {
                     php_url_free(resource);
                     return php_stream_opendir(entry->tmp, options, context);
              }
              internal_file = estrdup(internal_file);
              php_url_free(resource);
              return phar_make_dirstream(internal_file, &phar->manifest TSRMLS_CC);
       } else {
              int i_len = strlen(internal_file);

              /* search for directory */
              zend_hash_internal_pointer_reset(&phar->manifest);
              while (FAILURE != zend_hash_has_more_elements(&phar->manifest)) {
                     if (HASH_KEY_NON_EXISTANT != 
                                   zend_hash_get_current_key_ex(
                                          &phar->manifest, &key, &keylen, &unused, 0, NULL)) {
                            PHAR_STR(key, str_key);
                            if (keylen > (uint)i_len && 0 == memcmp(str_key, internal_file, i_len)) {
                                   PHAR_STR_FREE(str_key);
                                   /* directory found */
                                   internal_file = estrndup(internal_file,
                                                 i_len);
                                   php_url_free(resource);
                                   return phar_make_dirstream(internal_file, &phar->manifest TSRMLS_CC);
                            }
                            PHAR_STR_FREE(str_key);
                     }

                     if (SUCCESS != zend_hash_move_forward(&phar->manifest)) {
                            break;
                     }
              }
       }

       php_url_free(resource);
       return NULL;
}

Here is the call graph for this function:

int phar_wrapper_rmdir ( php_stream_wrapper *  wrapper,
char *  url,
int  options,
php_stream_context *context  TSRMLS_DC 
)

Remove a directory within a phar archive.

Definition at line 567 of file dirstream.c.

{
       phar_entry_info *entry;
       phar_archive_data *phar = NULL;
       char *error, *arch, *entry2;
       int arch_len, entry_len;
       php_url *resource = NULL;
       uint host_len;
       phar_zstr key;
       char *str_key;
       uint key_len;
       ulong unused;
       uint path_len;

       /* pre-readonly check, we need to know if this is a data phar */
       if (FAILURE == phar_split_fname(url, strlen(url), &arch, &arch_len, &entry2, &entry_len, 2, 2 TSRMLS_CC)) {
              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);
              return 0;
       }

       if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL TSRMLS_CC)) {
              phar = NULL;
       }

       efree(arch);
       efree(entry2);

       if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot rmdir directory \"%s\", write operations disabled", url);
              return 0;
       }

       if ((resource = phar_parse_url(wrapper, url, "w", options TSRMLS_CC)) == NULL) {
              return 0;
       }

       /* we must have at the very least phar://alias.phar/internalfile.php */
       if (!resource->scheme || !resource->host || !resource->path) {
              php_url_free(resource);
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: invalid url \"%s\"", url);
              return 0;
       }

       if (strcasecmp("phar", resource->scheme)) {
              php_url_free(resource);
              php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: not a phar stream url \"%s\"", url);
              return 0;
       }

       host_len = strlen(resource->host);

       if (FAILURE == phar_get_archive(&phar, resource->host, host_len, NULL, 0, &error TSRMLS_CC)) {
              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);
              efree(error);
              php_url_free(resource);
              return 0;
       }

       path_len = strlen(resource->path+1);

       if (!(entry = phar_get_entry_info_dir(phar, resource->path + 1, path_len, 2, &error, 1 TSRMLS_CC))) {
              if (error) {
                     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);
                     efree(error);
              } else {
                     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);
              }
              php_url_free(resource);
              return 0;
       }

       if (!entry->is_deleted) {
              for (zend_hash_internal_pointer_reset(&phar->manifest);
              HASH_KEY_NON_EXISTANT != zend_hash_get_current_key_ex(&phar->manifest, &key, &key_len, &unused, 0, NULL);
              zend_hash_move_forward(&phar->manifest)) {

                     PHAR_STR(key, str_key);

                     if (key_len > path_len && 
                            memcmp(str_key, resource->path+1, path_len) == 0 && 
                            IS_SLASH(str_key[path_len])) {
                            PHAR_STR_FREE(str_key);
                            php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: Directory not empty");
                            if (entry->is_temp_dir) {
                                   efree(entry->filename);
                                   efree(entry);
                            }
                            php_url_free(resource);
                            return 0;
                     }
                     PHAR_STR_FREE(str_key);
              }

              for (zend_hash_internal_pointer_reset(&phar->virtual_dirs);
                     HASH_KEY_NON_EXISTANT != zend_hash_get_current_key_ex(&phar->virtual_dirs, &key, &key_len, &unused, 0, NULL);
                     zend_hash_move_forward(&phar->virtual_dirs)) {
       
                     PHAR_STR(key, str_key);
       
                     if (key_len > path_len && 
                            memcmp(str_key, resource->path+1, path_len) == 0 && 
                            IS_SLASH(str_key[path_len])) {
                            PHAR_STR_FREE(str_key);
                            php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: Directory not empty");
                            if (entry->is_temp_dir) {
                                   efree(entry->filename);
                                   efree(entry);
                            }
                            php_url_free(resource);
                            return 0;
                     }
                     PHAR_STR_FREE(str_key);
              }
       }

       if (entry->is_temp_dir) {
              zend_hash_del(&phar->virtual_dirs, resource->path+1, path_len);
              efree(entry->filename);
              efree(entry);
       } else {
              entry->is_deleted = 1;
              entry->is_modified = 1;
              phar_flush(phar, 0, 0, 0, &error TSRMLS_CC);

              if (error) {
                     php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", entry->filename, phar->fname, error);
                     php_url_free(resource);
                     efree(error);
                     return 0;
              }
       }

       php_url_free(resource);
       return 1;
}

Here is the call graph for this function: