/ext/spl/spl_directory.c
C | 2922 lines | 2286 code | 318 blank | 318 comment | 366 complexity | 008ab01183c483dd353e82d7af6a7ef3 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | Copyright (c) The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Author: Marcus Boerger <helly@php.net> |
- +----------------------------------------------------------------------+
- */
- #ifdef HAVE_CONFIG_H
- # include "config.h"
- #endif
- #include "php.h"
- #include "php_ini.h"
- #include "ext/standard/info.h"
- #include "ext/standard/file.h"
- #include "ext/standard/php_string.h"
- #include "zend_compile.h"
- #include "zend_exceptions.h"
- #include "zend_interfaces.h"
- #include "php_spl.h"
- #include "spl_functions.h"
- #include "spl_engine.h"
- #include "spl_iterators.h"
- #include "spl_directory.h"
- #include "spl_directory_arginfo.h"
- #include "spl_exceptions.h"
- #include "php.h"
- #include "fopen_wrappers.h"
- #include "ext/standard/basic_functions.h"
- #include "ext/standard/php_filestat.h"
- #define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
- /* declare the class handlers */
- static zend_object_handlers spl_filesystem_object_handlers;
- /* includes handler to validate object state when retrieving methods */
- static zend_object_handlers spl_filesystem_object_check_handlers;
- /* decalre the class entry */
- PHPAPI zend_class_entry *spl_ce_SplFileInfo;
- PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
- PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
- PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
- PHPAPI zend_class_entry *spl_ce_GlobIterator;
- PHPAPI zend_class_entry *spl_ce_SplFileObject;
- PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
- static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ */
- {
- if (intern->u.file.current_line) {
- efree(intern->u.file.current_line);
- intern->u.file.current_line = NULL;
- }
- if (!Z_ISUNDEF(intern->u.file.current_zval)) {
- zval_ptr_dtor(&intern->u.file.current_zval);
- ZVAL_UNDEF(&intern->u.file.current_zval);
- }
- } /* }}} */
- static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */
- {
- spl_filesystem_object *intern = spl_filesystem_from_obj(object);
- zend_objects_destroy_object(object);
- switch(intern->type) {
- case SPL_FS_DIR:
- if (intern->u.dir.dirp) {
- php_stream_close(intern->u.dir.dirp);
- intern->u.dir.dirp = NULL;
- }
- break;
- case SPL_FS_FILE:
- if (intern->u.file.stream) {
- /*
- if (intern->u.file.zcontext) {
- zend_list_delref(Z_RESVAL_P(intern->zcontext));
- }
- */
- if (!intern->u.file.stream->is_persistent) {
- php_stream_close(intern->u.file.stream);
- } else {
- php_stream_pclose(intern->u.file.stream);
- }
- intern->u.file.stream = NULL;
- }
- break;
- default:
- break;
- }
- } /* }}} */
- static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
- {
- spl_filesystem_object *intern = spl_filesystem_from_obj(object);
- if (intern->oth_handler && intern->oth_handler->dtor) {
- intern->oth_handler->dtor(intern);
- }
- zend_object_std_dtor(&intern->std);
- if (intern->_path) {
- efree(intern->_path);
- }
- if (intern->file_name) {
- efree(intern->file_name);
- }
- switch(intern->type) {
- case SPL_FS_INFO:
- break;
- case SPL_FS_DIR:
- if (intern->u.dir.sub_path) {
- efree(intern->u.dir.sub_path);
- }
- break;
- case SPL_FS_FILE:
- if (intern->u.file.open_mode) {
- efree(intern->u.file.open_mode);
- }
- if (intern->orig_path) {
- efree(intern->orig_path);
- }
- spl_filesystem_file_free_line(intern);
- break;
- }
- } /* }}} */
- /* {{{ spl_ce_dir_object_new */
- /* creates the object by
- - allocating memory
- - initializing the object members
- - storing the object
- - setting it's handlers
- called from
- - clone
- - new
- */
- static zend_object *spl_filesystem_object_new_ex(zend_class_entry *class_type)
- {
- spl_filesystem_object *intern;
- intern = zend_object_alloc(sizeof(spl_filesystem_object), class_type);
- /* intern->type = SPL_FS_INFO; done by set 0 */
- intern->file_class = spl_ce_SplFileObject;
- intern->info_class = spl_ce_SplFileInfo;
- zend_object_std_init(&intern->std, class_type);
- object_properties_init(&intern->std, class_type);
- intern->std.handlers = &spl_filesystem_object_handlers;
- return &intern->std;
- }
- /* }}} */
- /* {{{ spl_filesystem_object_new */
- /* See spl_filesystem_object_new_ex */
- static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
- {
- return spl_filesystem_object_new_ex(class_type);
- }
- /* }}} */
- /* {{{ spl_filesystem_object_new_check */
- static zend_object *spl_filesystem_object_new_check(zend_class_entry *class_type)
- {
- spl_filesystem_object *ret = spl_filesystem_from_obj(spl_filesystem_object_new_ex(class_type));
- ret->std.handlers = &spl_filesystem_object_check_handlers;
- return &ret->std;
- }
- /* }}} */
- PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_t *len) /* {{{ */
- {
- #ifdef HAVE_GLOB
- if (intern->type == SPL_FS_DIR) {
- if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
- return php_glob_stream_get_path(intern->u.dir.dirp, len);
- }
- }
- #endif
- if (len) {
- *len = intern->_path_len;
- }
- return intern->_path;
- } /* }}} */
- static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
- {
- char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
- switch (intern->type) {
- case SPL_FS_INFO:
- case SPL_FS_FILE:
- if (!intern->file_name) {
- php_error_docref(NULL, E_ERROR, "Object not initialized");
- }
- break;
- case SPL_FS_DIR:
- {
- size_t path_len = 0;
- char *path = spl_filesystem_object_get_path(intern, &path_len);
- if (intern->file_name) {
- efree(intern->file_name);
- }
- /* if there is parent path, amend it, otherwise just use the given path as is */
- if (path_len == 0) {
- intern->file_name_len = spprintf(
- &intern->file_name, 0, "%s", intern->u.dir.entry.d_name);
- } else {
- intern->file_name_len = spprintf(
- &intern->file_name, 0, "%s%c%s", path, slash, intern->u.dir.entry.d_name);
- }
- }
- break;
- }
- } /* }}} */
- static int spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
- {
- if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
- intern->u.dir.entry.d_name[0] = '\0';
- return 0;
- } else {
- return 1;
- }
- }
- /* }}} */
- #define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
- static inline int spl_filesystem_is_dot(const char * d_name) /* {{{ */
- {
- return !strcmp(d_name, ".") || !strcmp(d_name, "..");
- }
- /* }}} */
- /* {{{ spl_filesystem_dir_open */
- /* open a directory resource */
- static void spl_filesystem_dir_open(spl_filesystem_object* intern, char *path)
- {
- int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
- intern->type = SPL_FS_DIR;
- intern->_path_len = strlen(path);
- intern->u.dir.dirp = php_stream_opendir(path, REPORT_ERRORS, FG(default_context));
- if (intern->_path_len > 1 && IS_SLASH_AT(path, intern->_path_len-1)) {
- intern->_path = estrndup(path, --intern->_path_len);
- } else {
- intern->_path = estrndup(path, intern->_path_len);
- }
- intern->u.dir.index = 0;
- if (EG(exception) || intern->u.dir.dirp == NULL) {
- intern->u.dir.entry.d_name[0] = '\0';
- if (!EG(exception)) {
- /* open failed w/out notice (turned to exception due to EH_THROW) */
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
- "Failed to open directory \"%s\"", path);
- }
- } else {
- do {
- spl_filesystem_dir_read(intern);
- } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
- }
- }
- /* }}} */
- static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_include_path, int silent) /* {{{ */
- {
- zval tmp;
- intern->type = SPL_FS_FILE;
- php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, &tmp);
- if (Z_TYPE(tmp) == IS_TRUE) {
- intern->u.file.open_mode = NULL;
- intern->file_name = NULL;
- zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
- return FAILURE;
- }
- intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
- intern->u.file.stream = php_stream_open_wrapper_ex(intern->file_name, intern->u.file.open_mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
- if (!intern->file_name_len || !intern->u.file.stream) {
- if (!EG(exception)) {
- zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot open file '%s'", intern->file_name_len ? intern->file_name : "");
- }
- intern->file_name = NULL; /* until here it is not a copy */
- intern->u.file.open_mode = NULL;
- return FAILURE;
- }
- /*
- if (intern->u.file.zcontext) {
- //zend_list_addref(Z_RES_VAL(intern->u.file.zcontext));
- Z_ADDREF_P(intern->u.file.zcontext);
- }
- */
- if (intern->file_name_len > 1 && IS_SLASH_AT(intern->file_name, intern->file_name_len-1)) {
- intern->file_name_len--;
- }
- intern->orig_path = estrndup(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path));
- intern->file_name = estrndup(intern->file_name, intern->file_name_len);
- intern->u.file.open_mode = estrndup(intern->u.file.open_mode, intern->u.file.open_mode_len);
- /* avoid reference counting in debug mode, thus do it manually */
- ZVAL_RES(&intern->u.file.zresource, intern->u.file.stream->res);
- /*!!! TODO: maybe bug?
- Z_SET_REFCOUNT(intern->u.file.zresource, 1);
- */
- intern->u.file.delimiter = ',';
- intern->u.file.enclosure = '"';
- intern->u.file.escape = (unsigned char) '\\';
- intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1);
- return SUCCESS;
- } /* }}} */
- /* {{{ spl_filesystem_object_clone */
- /* Local zend_object creation (on stack)
- Load the 'other' object
- Create a new empty object (See spl_filesystem_object_new_ex)
- Open the directory
- Clone other members (properties)
- */
- static zend_object *spl_filesystem_object_clone(zend_object *old_object)
- {
- zend_object *new_object;
- spl_filesystem_object *intern;
- spl_filesystem_object *source;
- int index, skip_dots;
- source = spl_filesystem_from_obj(old_object);
- new_object = spl_filesystem_object_new_ex(old_object->ce);
- intern = spl_filesystem_from_obj(new_object);
- intern->flags = source->flags;
- switch (source->type) {
- case SPL_FS_INFO:
- intern->_path_len = source->_path_len;
- intern->_path = estrndup(source->_path, source->_path_len);
- intern->file_name_len = source->file_name_len;
- intern->file_name = estrndup(source->file_name, intern->file_name_len);
- break;
- case SPL_FS_DIR:
- spl_filesystem_dir_open(intern, source->_path);
- /* read until we hit the position in which we were before */
- skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
- for(index = 0; index < source->u.dir.index; ++index) {
- do {
- spl_filesystem_dir_read(intern);
- } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
- }
- intern->u.dir.index = index;
- break;
- case SPL_FS_FILE:
- ZEND_ASSERT(0);
- }
- intern->file_class = source->file_class;
- intern->info_class = source->info_class;
- intern->oth = source->oth;
- intern->oth_handler = source->oth_handler;
- zend_objects_clone_members(new_object, old_object);
- if (intern->oth_handler && intern->oth_handler->clone) {
- intern->oth_handler->clone(source, intern);
- }
- return new_object;
- }
- /* }}} */
- void spl_filesystem_info_set_filename(spl_filesystem_object *intern, char *path, size_t len, size_t use_copy) /* {{{ */
- {
- char *p1, *p2;
- if (intern->file_name) {
- efree(intern->file_name);
- }
- intern->file_name = use_copy ? estrndup(path, len) : path;
- intern->file_name_len = len;
- while (intern->file_name_len > 1 && IS_SLASH_AT(intern->file_name, intern->file_name_len-1)) {
- intern->file_name[intern->file_name_len-1] = 0;
- intern->file_name_len--;
- }
- p1 = strrchr(intern->file_name, '/');
- #if defined(PHP_WIN32)
- p2 = strrchr(intern->file_name, '\\');
- #else
- p2 = 0;
- #endif
- if (p1 || p2) {
- intern->_path_len = ((p1 > p2 ? p1 : p2) - intern->file_name);
- } else {
- intern->_path_len = 0;
- }
- if (intern->_path) {
- efree(intern->_path);
- }
- intern->_path = estrndup(path, intern->_path_len);
- } /* }}} */
- static spl_filesystem_object *spl_filesystem_object_create_info(spl_filesystem_object *source, char *file_path, size_t file_path_len, int use_copy, zend_class_entry *ce, zval *return_value) /* {{{ */
- {
- spl_filesystem_object *intern;
- zval arg1;
- zend_error_handling error_handling;
- if (!file_path || !file_path_len) {
- #if defined(PHP_WIN32)
- zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot create SplFileInfo for empty path");
- if (file_path && !use_copy) {
- efree(file_path);
- }
- #else
- if (file_path && !use_copy) {
- efree(file_path);
- }
- file_path_len = 1;
- file_path = "/";
- #endif
- return NULL;
- }
- zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
- ce = ce ? ce : source->info_class;
- zend_update_class_constants(ce);
- intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
- ZVAL_OBJ(return_value, &intern->std);
- if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
- ZVAL_STRINGL(&arg1, file_path, file_path_len);
- zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
- zval_ptr_dtor(&arg1);
- } else {
- spl_filesystem_info_set_filename(intern, file_path, file_path_len, use_copy);
- }
- zend_restore_error_handling(&error_handling);
- return intern;
- } /* }}} */
- static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, spl_filesystem_object *source, int type, zend_class_entry *ce, zval *return_value) /* {{{ */
- {
- spl_filesystem_object *intern;
- zend_bool use_include_path = 0;
- zval arg1, arg2;
- zend_error_handling error_handling;
- zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
- switch (source->type) {
- case SPL_FS_INFO:
- case SPL_FS_FILE:
- break;
- case SPL_FS_DIR:
- if (!source->u.dir.entry.d_name[0]) {
- zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Could not open file");
- zend_restore_error_handling(&error_handling);
- return NULL;
- }
- }
- switch (type) {
- case SPL_FS_INFO:
- ce = ce ? ce : source->info_class;
- if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
- break;
- }
- intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
- ZVAL_OBJ(return_value, &intern->std);
- spl_filesystem_object_get_file_name(source);
- if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
- ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
- zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
- zval_ptr_dtor(&arg1);
- } else {
- intern->file_name = estrndup(source->file_name, source->file_name_len);
- intern->file_name_len = source->file_name_len;
- intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len);
- intern->_path = estrndup(intern->_path, intern->_path_len);
- }
- break;
- case SPL_FS_FILE:
- {
- ce = ce ? ce : source->file_class;
- if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
- break;
- }
- char *open_mode = "r";
- size_t open_mode_len = 1;
- zval *resource = NULL;
- if (zend_parse_parameters(num_args, "|sbr",
- &open_mode, &open_mode_len, &use_include_path, &resource) == FAILURE
- ) {
- zend_restore_error_handling(&error_handling);
- return NULL;
- }
- intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
- ZVAL_OBJ(return_value, &intern->std);
- spl_filesystem_object_get_file_name(source);
- if (ce->constructor->common.scope != spl_ce_SplFileObject) {
- ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
- ZVAL_STRINGL(&arg2, open_mode, open_mode_len);
- zend_call_method_with_2_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1, &arg2);
- zval_ptr_dtor(&arg1);
- zval_ptr_dtor(&arg2);
- } else {
- intern->file_name = source->file_name;
- intern->file_name_len = source->file_name_len;
- intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len);
- intern->_path = estrndup(intern->_path, intern->_path_len);
- intern->u.file.open_mode = open_mode;
- intern->u.file.open_mode_len = open_mode_len;
- intern->u.file.zcontext = resource;
- if (spl_filesystem_file_open(intern, use_include_path, 0) == FAILURE) {
- zend_restore_error_handling(&error_handling);
- zval_ptr_dtor(return_value);
- ZVAL_NULL(return_value);
- return NULL;
- }
- }
- break;
- }
- case SPL_FS_DIR:
- zend_restore_error_handling(&error_handling);
- zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Operation not supported");
- return NULL;
- }
- zend_restore_error_handling(&error_handling);
- return NULL;
- } /* }}} */
- static int spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
- {
- return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
- }
- /* }}} */
- static char *spl_filesystem_object_get_pathname(spl_filesystem_object *intern, size_t *len) { /* {{{ */
- switch (intern->type) {
- case SPL_FS_INFO:
- case SPL_FS_FILE:
- *len = intern->file_name_len;
- return intern->file_name;
- case SPL_FS_DIR:
- if (intern->u.dir.entry.d_name[0]) {
- spl_filesystem_object_get_file_name(intern);
- *len = intern->file_name_len;
- return intern->file_name;
- }
- }
- *len = 0;
- return NULL;
- }
- /* }}} */
- static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *object) /* {{{ */
- {
- spl_filesystem_object *intern = spl_filesystem_from_obj(object);
- zval tmp;
- HashTable *rv;
- zend_string *pnstr;
- char *path;
- size_t path_len;
- char stmp[2];
- if (!intern->std.properties) {
- rebuild_object_properties(&intern->std);
- }
- rv = zend_array_dup(intern->std.properties);
- pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "pathName", sizeof("pathName")-1);
- path = spl_filesystem_object_get_pathname(intern, &path_len);
- ZVAL_STRINGL(&tmp, path ? path : "", path_len);
- zend_symtable_update(rv, pnstr, &tmp);
- zend_string_release_ex(pnstr, 0);
- if (intern->file_name) {
- pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "fileName", sizeof("fileName")-1);
- spl_filesystem_object_get_path(intern, &path_len);
- if (path_len && path_len < intern->file_name_len) {
- ZVAL_STRINGL(&tmp, intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1));
- } else {
- ZVAL_STRINGL(&tmp, intern->file_name, intern->file_name_len);
- }
- zend_symtable_update(rv, pnstr, &tmp);
- zend_string_release_ex(pnstr, 0);
- }
- if (intern->type == SPL_FS_DIR) {
- #ifdef HAVE_GLOB
- pnstr = spl_gen_private_prop_name(spl_ce_DirectoryIterator, "glob", sizeof("glob")-1);
- if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
- ZVAL_STRINGL(&tmp, intern->_path, intern->_path_len);
- } else {
- ZVAL_FALSE(&tmp);
- }
- zend_symtable_update(rv, pnstr, &tmp);
- zend_string_release_ex(pnstr, 0);
- #endif
- pnstr = spl_gen_private_prop_name(spl_ce_RecursiveDirectoryIterator, "subPathName", sizeof("subPathName")-1);
- if (intern->u.dir.sub_path) {
- ZVAL_STRINGL(&tmp, intern->u.dir.sub_path, intern->u.dir.sub_path_len);
- } else {
- ZVAL_EMPTY_STRING(&tmp);
- }
- zend_symtable_update(rv, pnstr, &tmp);
- zend_string_release_ex(pnstr, 0);
- }
- if (intern->type == SPL_FS_FILE) {
- pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "openMode", sizeof("openMode")-1);
- ZVAL_STRINGL(&tmp, intern->u.file.open_mode, intern->u.file.open_mode_len);
- zend_symtable_update(rv, pnstr, &tmp);
- zend_string_release_ex(pnstr, 0);
- stmp[1] = '\0';
- stmp[0] = intern->u.file.delimiter;
- pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "delimiter", sizeof("delimiter")-1);
- ZVAL_STRINGL(&tmp, stmp, 1);
- zend_symtable_update(rv, pnstr, &tmp);
- zend_string_release_ex(pnstr, 0);
- stmp[0] = intern->u.file.enclosure;
- pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "enclosure", sizeof("enclosure")-1);
- ZVAL_STRINGL(&tmp, stmp, 1);
- zend_symtable_update(rv, pnstr, &tmp);
- zend_string_release_ex(pnstr, 0);
- }
- return rv;
- }
- /* }}} */
- zend_function *spl_filesystem_object_get_method_check(zend_object **object, zend_string *method, const zval *key) /* {{{ */
- {
- spl_filesystem_object *fsobj = spl_filesystem_from_obj(*object);
- if (fsobj->u.dir.dirp == NULL && fsobj->orig_path == NULL) {
- zend_function *func;
- zend_string *tmp = zend_string_init("_bad_state_ex", sizeof("_bad_state_ex") - 1, 0);
- func = zend_std_get_method(object, tmp, NULL);
- zend_string_release_ex(tmp, 0);
- return func;
- }
- return zend_std_get_method(object, method, key);
- }
- /* }}} */
- #define DIT_CTOR_FLAGS 0x00000001
- #define DIT_CTOR_GLOB 0x00000002
- void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long ctor_flags) /* {{{ */
- {
- spl_filesystem_object *intern;
- char *path;
- int parsed;
- size_t len;
- zend_long flags;
- zend_error_handling error_handling;
- if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
- flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
- parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &path, &len, &flags);
- } else {
- flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
- parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &len);
- }
- if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_SKIPDOTS)) {
- flags |= SPL_FILE_DIR_SKIPDOTS;
- }
- if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_UNIXPATHS)) {
- flags |= SPL_FILE_DIR_UNIXPATHS;
- }
- if (parsed == FAILURE) {
- return;
- }
- if (!len) {
- zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Directory name must not be empty.");
- return;
- }
- intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (intern->_path) {
- /* object is already initialized */
- php_error_docref(NULL, E_WARNING, "Directory object is already initialized");
- return;
- }
- intern->flags = flags;
- zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
- #ifdef HAVE_GLOB
- if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && strstr(path, "glob://") != path) {
- spprintf(&path, 0, "glob://%s", path);
- spl_filesystem_dir_open(intern, path);
- efree(path);
- } else
- #endif
- {
- spl_filesystem_dir_open(intern, path);
- }
- intern->u.dir.is_recursive = instanceof_function(intern->std.ce, spl_ce_RecursiveDirectoryIterator) ? 1 : 0;
- zend_restore_error_handling(&error_handling);
- }
- /* }}} */
- /* {{{ proto DirectoryIterator::__construct(string path)
- Cronstructs a new dir iterator from a path. */
- PHP_METHOD(DirectoryIterator, __construct)
- {
- spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- /* }}} */
- /* {{{ proto void DirectoryIterator::rewind()
- Rewind dir back to the start */
- PHP_METHOD(DirectoryIterator, rewind)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- intern->u.dir.index = 0;
- if (intern->u.dir.dirp) {
- php_stream_rewinddir(intern->u.dir.dirp);
- }
- spl_filesystem_dir_read(intern);
- }
- /* }}} */
- /* {{{ proto string DirectoryIterator::key()
- Return current dir entry */
- PHP_METHOD(DirectoryIterator, key)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- if (intern->u.dir.dirp) {
- RETURN_LONG(intern->u.dir.index);
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto DirectoryIterator DirectoryIterator::current()
- Return this (needed for Iterator interface) */
- PHP_METHOD(DirectoryIterator, current)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- ZVAL_OBJ(return_value, Z_OBJ_P(ZEND_THIS));
- Z_ADDREF_P(return_value);
- }
- /* }}} */
- /* {{{ proto void DirectoryIterator::next()
- Move to next entry */
- PHP_METHOD(DirectoryIterator, next)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- intern->u.dir.index++;
- do {
- spl_filesystem_dir_read(intern);
- } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
- if (intern->file_name) {
- efree(intern->file_name);
- intern->file_name = NULL;
- }
- }
- /* }}} */
- /* {{{ proto void DirectoryIterator::seek(int position)
- Seek to the given position */
- PHP_METHOD(DirectoryIterator, seek)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- zval retval;
- zend_long pos;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pos) == FAILURE) {
- RETURN_THROWS();
- }
- if (intern->u.dir.index > pos) {
- /* we first rewind */
- zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_rewind, "rewind", NULL);
- }
- while (intern->u.dir.index < pos) {
- int valid = 0;
- zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_valid, "valid", &retval);
- valid = zend_is_true(&retval);
- zval_ptr_dtor(&retval);
- if (!valid) {
- zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", pos);
- RETURN_THROWS();
- }
- zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_next, "next", NULL);
- }
- } /* }}} */
- /* {{{ proto string DirectoryIterator::valid()
- Check whether dir contains more entries */
- PHP_METHOD(DirectoryIterator, valid)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- RETURN_BOOL(intern->u.dir.entry.d_name[0] != '\0');
- }
- /* }}} */
- /* {{{ proto string SplFileInfo::getPath()
- Return the path */
- PHP_METHOD(SplFileInfo, getPath)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- char *path;
- size_t path_len;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- path = spl_filesystem_object_get_path(intern, &path_len);
- if (path) {
- RETURN_STRINGL(path, path_len);
- } else {
- RETURN_EMPTY_STRING();
- }
- }
- /* }}} */
- /* {{{ proto string SplFileInfo::getFilename()
- Return filename only */
- PHP_METHOD(SplFileInfo, getFilename)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- size_t path_len;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- spl_filesystem_object_get_path(intern, &path_len);
- if (path_len && path_len < intern->file_name_len) {
- RETURN_STRINGL(intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1));
- } else {
- RETURN_STRINGL(intern->file_name, intern->file_name_len);
- }
- }
- /* }}} */
- /* {{{ proto string DirectoryIterator::getFilename()
- Return filename of current dir entry */
- PHP_METHOD(DirectoryIterator, getFilename)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- RETURN_STRING(intern->u.dir.entry.d_name);
- }
- /* }}} */
- /* {{{ proto string SplFileInfo::getExtension()
- Returns file extension component of path */
- PHP_METHOD(SplFileInfo, getExtension)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- char *fname = NULL;
- const char *p;
- size_t flen;
- size_t path_len;
- size_t idx;
- zend_string *ret;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- spl_filesystem_object_get_path(intern, &path_len);
- if (path_len && path_len < intern->file_name_len) {
- fname = intern->file_name + path_len + 1;
- flen = intern->file_name_len - (path_len + 1);
- } else {
- fname = intern->file_name;
- flen = intern->file_name_len;
- }
- ret = php_basename(fname, flen, NULL, 0);
- p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
- if (p) {
- idx = p - ZSTR_VAL(ret);
- RETVAL_STRINGL(ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
- zend_string_release_ex(ret, 0);
- return;
- } else {
- zend_string_release_ex(ret, 0);
- RETURN_EMPTY_STRING();
- }
- }
- /* }}}*/
- /* {{{ proto string DirectoryIterator::getExtension()
- Returns the file extension component of path */
- PHP_METHOD(DirectoryIterator, getExtension)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- const char *p;
- size_t idx;
- zend_string *fname;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0);
- p = zend_memrchr(ZSTR_VAL(fname), '.', ZSTR_LEN(fname));
- if (p) {
- idx = p - ZSTR_VAL(fname);
- RETVAL_STRINGL(ZSTR_VAL(fname) + idx + 1, ZSTR_LEN(fname) - idx - 1);
- zend_string_release_ex(fname, 0);
- } else {
- zend_string_release_ex(fname, 0);
- RETURN_EMPTY_STRING();
- }
- }
- /* }}} */
- /* {{{ proto string SplFileInfo::getBasename([string $suffix])
- Returns filename component of path */
- PHP_METHOD(SplFileInfo, getBasename)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- char *fname, *suffix = 0;
- size_t flen;
- size_t slen = 0, path_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
- RETURN_THROWS();
- }
- spl_filesystem_object_get_path(intern, &path_len);
- if (path_len && path_len < intern->file_name_len) {
- fname = intern->file_name + path_len + 1;
- flen = intern->file_name_len - (path_len + 1);
- } else {
- fname = intern->file_name;
- flen = intern->file_name_len;
- }
- RETURN_STR(php_basename(fname, flen, suffix, slen));
- }
- /* }}}*/
- /* {{{ proto string DirectoryIterator::getBasename([string $suffix])
- Returns filename component of current dir entry */
- PHP_METHOD(DirectoryIterator, getBasename)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- char *suffix = 0;
- size_t slen = 0;
- zend_string *fname;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
- RETURN_THROWS();
- }
- fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), suffix, slen);
- RETVAL_STR(fname);
- }
- /* }}} */
- /* {{{ proto string SplFileInfo::getPathname()
- Return path and filename */
- PHP_METHOD(SplFileInfo, getPathname)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- char *path;
- size_t path_len;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- path = spl_filesystem_object_get_pathname(intern, &path_len);
- if (path != NULL) {
- RETURN_STRINGL(path, path_len);
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto string FilesystemIterator::key()
- Return getPathname() or getFilename() depending on flags */
- PHP_METHOD(FilesystemIterator, key)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
- RETURN_STRING(intern->u.dir.entry.d_name);
- } else {
- spl_filesystem_object_get_file_name(intern);
- RETURN_STRINGL(intern->file_name, intern->file_name_len);
- }
- }
- /* }}} */
- /* {{{ proto string FilesystemIterator::current()
- Return getFilename(), getFileInfo() or $this depending on flags */
- PHP_METHOD(FilesystemIterator, current)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
- spl_filesystem_object_get_file_name(intern);
- RETURN_STRINGL(intern->file_name, intern->file_name_len);
- } else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
- spl_filesystem_object_get_file_name(intern);
- spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value);
- } else {
- ZVAL_OBJ(return_value, Z_OBJ_P(ZEND_THIS));
- Z_ADDREF_P(return_value);
- }
- }
- /* }}} */
- /* {{{ proto bool DirectoryIterator::isDot()
- Returns true if current entry is '.' or '..' */
- PHP_METHOD(DirectoryIterator, isDot)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- RETURN_BOOL(spl_filesystem_is_dot(intern->u.dir.entry.d_name));
- }
- /* }}} */
- /* {{{ proto SplFileInfo::__construct(string file_name)
- Cronstructs a new SplFileInfo from a path. */
- /* When the constructor gets called the object is already created
- by the engine, so we must only call 'additional' initializations.
- */
- PHP_METHOD(SplFileInfo, __construct)
- {
- spl_filesystem_object *intern;
- char *path;
- size_t len;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &path, &len) == FAILURE) {
- RETURN_THROWS();
- }
- intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- spl_filesystem_info_set_filename(intern, path, len, 1);
- /* intern->type = SPL_FS_INFO; already set */
- }
- /* }}} */
- /* {{{ FileInfoFunction */
- #define FileInfoFunction(func_name, func_num) \
- PHP_METHOD(SplFileInfo, func_name) \
- { \
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS); \
- zend_error_handling error_handling; \
- if (zend_parse_parameters_none() == FAILURE) { \
- RETURN_THROWS(); \
- } \
- \
- zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);\
- spl_filesystem_object_get_file_name(intern); \
- php_stat(intern->file_name, intern->file_name_len, func_num, return_value); \
- zend_restore_error_handling(&error_handling); \
- }
- /* }}} */
- /* {{{ proto int SplFileInfo::getPerms()
- Get file permissions */
- FileInfoFunction(getPerms, FS_PERMS)
- /* }}} */
- /* {{{ proto int SplFileInfo::getInode()
- Get file inode */
- FileInfoFunction(getInode, FS_INODE)
- /* }}} */
- /* {{{ proto int SplFileInfo::getSize()
- Get file size */
- FileInfoFunction(getSize, FS_SIZE)
- /* }}} */
- /* {{{ proto int SplFileInfo::getOwner()
- Get file owner */
- FileInfoFunction(getOwner, FS_OWNER)
- /* }}} */
- /* {{{ proto int SplFileInfo::getGroup()
- Get file group */
- FileInfoFunction(getGroup, FS_GROUP)
- /* }}} */
- /* {{{ proto int SplFileInfo::getATime()
- Get last access time of file */
- FileInfoFunction(getATime, FS_ATIME)
- /* }}} */
- /* {{{ proto int SplFileInfo::getMTime()
- Get last modification time of file */
- FileInfoFunction(getMTime, FS_MTIME)
- /* }}} */
- /* {{{ proto int SplFileInfo::getCTime()
- Get inode modification time of file */
- FileInfoFunction(getCTime, FS_CTIME)
- /* }}} */
- /* {{{ proto string SplFileInfo::getType()
- Get file type */
- FileInfoFunction(getType, FS_TYPE)
- /* }}} */
- /* {{{ proto bool SplFileInfo::isWritable()
- Returns true if file can be written */
- FileInfoFunction(isWritable, FS_IS_W)
- /* }}} */
- /* {{{ proto bool SplFileInfo::isReadable()
- Returns true if file can be read */
- FileInfoFunction(isReadable, FS_IS_R)
- /* }}} */
- /* {{{ proto bool SplFileInfo::isExecutable()
- Returns true if file is executable */
- FileInfoFunction(isExecutable, FS_IS_X)
- /* }}} */
- /* {{{ proto bool SplFileInfo::isFile()
- Returns true if file is a regular file */
- FileInfoFunction(isFile, FS_IS_FILE)
- /* }}} */
- /* {{{ proto bool SplFileInfo::isDir()
- Returns true if file is directory */
- FileInfoFunction(isDir, FS_IS_DIR)
- /* }}} */
- /* {{{ proto bool SplFileInfo::isLink()
- Returns true if file is symbolic link */
- FileInfoFunction(isLink, FS_IS_LINK)
- /* }}} */
- /* {{{ proto string SplFileInfo::getLinkTarget()
- Return the target of a symbolic link */
- PHP_METHOD(SplFileInfo, getLinkTarget)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- ssize_t ret;
- char buff[MAXPATHLEN];
- zend_error_handling error_handling;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
- if (intern->file_name == NULL) {
- spl_filesystem_object_get_file_name(intern);
- }
- #if defined(PHP_WIN32) || HAVE_SYMLINK
- if (intern->file_name == NULL) {
- php_error_docref(NULL, E_WARNING, "Empty filename");
- RETURN_FALSE;
- } else if (!IS_ABSOLUTE_PATH(intern->file_name, intern->file_name_len)) {
- char expanded_path[MAXPATHLEN];
- if (!expand_filepath_with_mode(intern->file_name, expanded_path, NULL, 0, CWD_EXPAND )) {
- php_error_docref(NULL, E_WARNING, "No such file or directory");
- RETURN_FALSE;
- }
- ret = php_sys_readlink(expanded_path, buff, MAXPATHLEN - 1);
- } else {
- ret = php_sys_readlink(intern->file_name, buff, MAXPATHLEN-1);
- }
- #else
- ret = -1; /* always fail if not implemented */
- #endif
- if (ret == -1) {
- zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Unable to read link %s, error: %s", intern->file_name, strerror(errno));
- RETVAL_FALSE;
- } else {
- /* Append NULL to the end of the string */
- buff[ret] = '\0';
- RETVAL_STRINGL(buff, ret);
- }
- zend_restore_error_handling(&error_handling);
- }
- /* }}} */
- /* {{{ proto string SplFileInfo::getRealPath()
- Return the resolved path */
- PHP_METHOD(SplFileInfo, getRealPath)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- char buff[MAXPATHLEN];
- char *filename;
- zend_error_handling error_handling;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
- if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
- spl_filesystem_object_get_file_name(intern);
- }
- if (intern->orig_path) {
- filename = intern->orig_path;
- } else {
- filename = intern->file_name;
- }
- if (filename && VCWD_REALPATH(filename, buff)) {
- #ifdef ZTS
- if (VCWD_ACCESS(buff, F_OK)) {
- RETVAL_FALSE;
- } else
- #endif
- RETVAL_STRING(buff);
- } else {
- RETVAL_FALSE;
- }
- zend_restore_error_handling(&error_handling);
- }
- /* }}} */
- /* {{{ proto SplFileObject SplFileInfo::openFile([string mode = 'r' [, bool use_include_path [, resource context]]])
- Open the current file */
- PHP_METHOD(SplFileInfo, openFile)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_FILE, NULL, return_value);
- }
- /* }}} */
- /* {{{ proto void SplFileInfo::setFileClass([string class_name])
- Class to use in openFile() */
- PHP_METHOD(SplFileInfo, setFileClass)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- zend_class_entry *ce = spl_ce_SplFileObject;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
- RETURN_THROWS();
- }
- intern->file_class = ce;
- }
- /* }}} */
- /* {{{ proto void SplFileInfo::setInfoClass([string class_name])
- Class to use in getFileInfo(), getPathInfo() */
- PHP_METHOD(SplFileInfo, setInfoClass)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- zend_class_entry *ce = spl_ce_SplFileInfo;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
- RETURN_THROWS();
- }
- intern->info_class = ce;
- }
- /* }}} */
- /* {{{ proto SplFileInfo SplFileInfo::getFileInfo([string $class_name])
- Get/copy file info */
- PHP_METHOD(SplFileInfo, getFileInfo)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- zend_class_entry *ce = intern->info_class;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
- RETURN_THROWS();
- }
- spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_INFO, ce, return_value);
- }
- /* }}} */
- /* {{{ proto SplFileInfo SplFileInfo::getPathInfo([string $class_name])
- Get/copy file info */
- PHP_METHOD(SplFileInfo, getPathInfo)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- zend_class_entry *ce = intern->info_class;
- size_t path_len;
- char *path;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
- RETURN_THROWS();
- }
- path = spl_filesystem_object_get_pathname(intern, &path_len);
- if (path) {
- char *dpath = estrndup(path, path_len);
- path_len = php_dirname(dpath, path_len);
- spl_filesystem_object_create_info(intern, dpath, path_len, 1, ce, return_value);
- efree(dpath);
- }
- }
- /* }}} */
- /* {{{ proto void SplFileInfo::__debugInfo() */
- PHP_METHOD(SplFileInfo, __debugInfo)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
- RETURN_ARR(spl_filesystem_object_get_debug_info(Z_OBJ_P(ZEND_THIS)));
- } /* }}} */
- /* {{{ proto SplFileInfo::_bad_state_ex(void) */
- PHP_METHOD(SplFileInfo, _bad_state_ex)
- {
- zend_throw_exception_ex(spl_ce_LogicException, 0,
- "The parent constructor was not called: the object is in an "
- "invalid state ");
- }
- /* }}} */
- /* {{{ proto FilesystemIterator::__construct(string path [, int flags])
- Cronstructs a new dir iterator from a path. */
- PHP_METHOD(FilesystemIterator, __construct)
- {
- spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS | SPL_FILE_DIR_SKIPDOTS);
- }
- /* }}} */
- /* {{{ proto void FilesystemIterator::rewind()
- Rewind dir back to the start */
- PHP_METHOD(FilesystemIterator, rewind)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- intern->u.dir.index = 0;
- if (intern->u.dir.dirp) {
- php_stream_rewinddir(intern->u.dir.dirp);
- }
- do {
- spl_filesystem_dir_read(intern);
- } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
- }
- /* }}} */
- /* {{{ proto int FilesystemIterator::getFlags()
- Get handling flags */
- PHP_METHOD(FilesystemIterator, getFlags)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- RETURN_LONG(intern->flags & (SPL_FILE_DIR_KEY_MODE_MASK | SPL_FILE_DIR_CURRENT_MODE_MASK | SPL_FILE_DIR_OTHERS_MASK));
- } /* }}} */
- /* {{{ proto void FilesystemIterator::setFlags(long $flags)
- Set handling flags */
- PHP_METHOD(FilesystemIterator, setFlags)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- zend_long flags;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &flags) == FAILURE) {
- RETURN_THROWS();
- }
- intern->flags &= ~(SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK);
- intern->flags |= ((SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK) & flags);
- } /* }}} */
- /* {{{ proto bool RecursiveDirectoryIterator::hasChildren([bool $allow_links = false])
- Returns whether current entry is a directory and not '.' or '..' */
- PHP_METHOD(RecursiveDirectoryIterator, hasChildren)
- {
- zend_bool allow_links = 0;
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &allow_links) == FAILURE) {
- RETURN_THROWS();
- }
- if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
- RETURN_FALSE;
- } else {
- spl_filesystem_object_get_file_name(intern);
- if (!allow_links && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
- php_stat(intern->file_name, intern->file_name_len, FS_IS_LINK, return_value);
- if (zend_is_true(return_value)) {
- RETURN_FALSE;
- }
- }
- php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, return_value);
- }
- }
- /* }}} */
- /* {{{ proto RecursiveDirectoryIterator DirectoryIterator::getChildren()
- Returns an iterator for the current entry if it is a directory */
- PHP_METHOD(RecursiveDirectoryIterator, getChildren)
- {
- zval zpath, zflags;
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- spl_filesystem_object *subdir;
- char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- spl_filesystem_object_get_file_name(intern);
- ZVAL_LONG(&zflags, intern->flags);
- ZVAL_STRINGL(&zpath, intern->file_name, intern->file_name_len);
- spl_instantiate_arg_ex2(Z_OBJCE_P(ZEND_THIS), return_value, &zpath, &zflags);
- zval_ptr_dtor(&zpath);
- subdir = Z_SPLFILESYSTEM_P(return_value);
- if (subdir) {
- if (intern->u.dir.sub_path && intern->u.dir.sub_path[0]) {
- subdir->u.dir.sub_path_len = spprintf(&subdir->u.dir.sub_path, 0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name);
- } else {
- subdir->u.dir.sub_path_len = strlen(intern->u.dir.entry.d_name);
- subdir->u.dir.sub_path = estrndup(intern->u.dir.entry.d_name, subdir->u.dir.sub_path_len);
- }
- subdir->info_class = intern->info_class;
- subdir->file_class = intern->file_class;
- subdir->oth = intern->oth;
- }
- }
- /* }}} */
- /* {{{ proto void RecursiveDirectoryIterator::getSubPath()
- Get sub path */
- PHP_METHOD(RecursiveDirectoryIterator, getSubPath)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- if (intern->u.dir.sub_path) {
- RETURN_STRINGL(intern->u.dir.sub_path, intern->u.dir.sub_path_len);
- } else {
- RETURN_EMPTY_STRING();
- }
- }
- /* }}} */
- /* {{{ proto void RecursiveDirectoryIterator::getSubPathname()
- Get sub path and file name */
- PHP_METHOD(RecursiveDirectoryIterator, getSubPathname)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- if (intern->u.dir.sub_path) {
- RETURN_NEW_STR(strpprintf(0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name));
- } else {
- RETURN_STRING(intern->u.dir.entry.d_name);
- }
- }
- /* }}} */
- /* {{{ proto RecursiveDirectoryIterator::__construct(string path [, int flags])
- Cronstructs a new dir iterator from a path. */
- PHP_METHOD(RecursiveDirectoryIterator, __construct)
- {
- spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS);
- }
- /* }}} */
- #ifdef HAVE_GLOB
- /* {{{ proto GlobIterator::__construct(string path [, int flags])
- Cronstructs a new dir iterator from a glob expression (no glob:// needed). */
- PHP_METHOD(GlobIterator, __construct)
- {
- spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS|DIT_CTOR_GLOB);
- }
- /* }}} */
- /* {{{ proto int GlobIterator::count()
- Return the number of directories and files found by globbing */
- PHP_METHOD(GlobIterator, count)
- {
- spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
- RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
- } else {
- /* should not happen */
- php_error_docref(NULL, E_ERROR, "GlobIterator lost glob state");
- }
- }
- /* }}} */
- #endif /* HAVE_GLOB */
- /* {{{ forward declarations to the iterator handlers */
- static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter);
- static int spl_filesystem_dir_it_valid(zend_object_iterator *iter);
- static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter);
- static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key);
- static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter);
- static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter);
- /* iterator handler table */
- static const zend_object_iterator_funcs spl_filesystem_dir_it_funcs = {
- spl_filesystem_dir_it_dtor,
- spl_filesystem_dir_it_valid,
- spl_filesystem_dir_it_current_data,
- spl_filesystem_dir_it_current_key,
- spl_filesystem_dir_it_move_forward,
- spl_filesystem_dir_it_rewind,
- NULL
- };
- /* }}} */
- /* {{{ spl_ce_dir_get_iterator */
- zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
- {
- spl_filesystem_iterator *iterator;
- spl_filesystem_object *dir_object;
- if (by_ref) {
- zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
- return NULL;
- }
- dir_object = Z_SPLFILESYSTEM_P(object);
- iterator = spl_filesystem_object_to_iterator(dir_object);
- Z_ADDREF_P(object);
- ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
- iterator->intern.funcs = &spl_filesystem_dir_it_funcs;
- /* ->current must be initialized; rewind doesn't set it and valid
- * doesn't check whether it's set */
- iterator->current = *object;
- return &iterator->intern;
- }
- /* }}} */
- /* {{{ spl_filesystem_dir_it_dtor */
- static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter)
- {
- spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
- if (!Z_ISUNDEF(iterator->intern.data)) {
- zval *object = &iterator->intern.data;
- zval_ptr_dtor(object);
- }
- /* Otherwise we were called from the owning object free storage handler as
- * it sets iterator->intern.data to IS_UNDEF.
- * We don't even need to destroy iterator->current as we didn't add a
- * reference to it in move_forward or get_iterator */
- }
- /* }}} */
- /* {{{ spl_filesystem_dir_it_valid */
- static int spl_filesystem_dir_it_valid(zend_object_iterator *iter)
- {
- spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
- return object->u.dir.entry.d_name[0] != '\0' ? SUCCESS : FAILURE;
- }
- /* }}} */
- /* {{{ spl_filesystem_dir_it_current_data */
- static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter)
- {
- spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
- return &iterator->current;
- }
- /* }}} */
- /* {{{ spl_filesystem_dir_it_current_key */
- static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key)
- {
- spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
- ZVAL_LONG(key, object->u.dir.index);
- }
- /* }}} */
- /* {{{ spl_filesystem_dir_it_move_forward */
- static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter)
- {
- spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
- object->u.dir.index++;
- spl_filesystem_dir_read(object);
- if (object->file_name) {
- efree(object->file_name);
- object->file_name = NULL;
- }
- }
- /* }}} */
- /* {{{ spl_filesyste…
Large files files are truncated, but you can click here to view the full file