Skip to content

ext/phar: Do some refactoring to make the code easier to understand #15426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 23, 2024
12 changes: 4 additions & 8 deletions ext/phar/dirstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,
zend_ulong unused;
phar_archive_data *phar;
phar_entry_info *entry = NULL;
uint32_t host_len;

if ((resource = phar_parse_url(wrapper, path, mode, options)) == NULL) {
php_stream_wrapper_log_error(wrapper, options, "phar url \"%s\" is unknown", path);
Expand All @@ -320,7 +319,7 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,
return NULL;
}

host_len = ZSTR_LEN(resource->host);
size_t host_len = ZSTR_LEN(resource->host);
phar_request_initialize();
internal_file = ZSTR_VAL(resource->path) + 1; /* strip leading "/" */

Expand Down Expand Up @@ -401,7 +400,6 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
char *error, *arch, *entry2;
size_t arch_len, entry_len;
php_url *resource = NULL;
uint32_t 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)) {
Expand Down Expand Up @@ -438,7 +436,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
return 0;
}

host_len = ZSTR_LEN(resource->host);
size_t host_len = ZSTR_LEN(resource->host);

if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), host_len, NULL, 0, &error)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", error retrieving phar information: %s", ZSTR_VAL(resource->path) + 1, ZSTR_VAL(resource->host), error);
Expand Down Expand Up @@ -533,10 +531,8 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
char *error, *arch, *entry2;
size_t arch_len, entry_len;
php_url *resource = NULL;
uint32_t host_len;
zend_string *str_key;
zend_ulong unused;
uint32_t 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)) {
Expand Down Expand Up @@ -573,7 +569,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
return 0;
}

host_len = ZSTR_LEN(resource->host);
size_t host_len = ZSTR_LEN(resource->host);

if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), host_len, NULL, 0, &error)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", error retrieving phar information: %s", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
Expand All @@ -582,7 +578,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
return 0;
}

path_len = ZSTR_LEN(resource->path) - 1;
size_t path_len = ZSTR_LEN(resource->path) - 1;

if (!(entry = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, path_len, 2, &error, 1))) {
if (error) {
Expand Down
2 changes: 2 additions & 0 deletions ext/phar/dirstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);

#ifdef PHAR_DIRSTREAM
#include "ext/standard/url.h"

php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options);

/* directory handlers */
Expand Down
21 changes: 10 additions & 11 deletions ext/phar/func_interceptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
*/

#include "phar_internal.h"
#include "ext/standard/php_filestat.h"
#include "ext/standard/file.h" /* For php_le_stream_context() */

#define PHAR_FUNC(name) \
static PHP_NAMED_FUNCTION(name)

PHAR_FUNC(phar_opendir) /* {{{ */
PHP_FUNCTION(phar_opendir) /* {{{ */
{
char *filename;
size_t filename_len;
Expand Down Expand Up @@ -156,7 +155,7 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
return name;
}

PHAR_FUNC(phar_file_get_contents) /* {{{ */
PHP_FUNCTION(phar_file_get_contents) /* {{{ */
{
zend_string *filename;
zend_string *contents;
Expand Down Expand Up @@ -233,7 +232,7 @@ PHAR_FUNC(phar_file_get_contents) /* {{{ */
}
/* }}} */

PHAR_FUNC(phar_readfile) /* {{{ */
PHP_FUNCTION(phar_readfile) /* {{{ */
{
zend_string *filename;
bool use_include_path = 0;
Expand Down Expand Up @@ -277,7 +276,7 @@ PHAR_FUNC(phar_readfile) /* {{{ */
}
/* }}} */

PHAR_FUNC(phar_fopen) /* {{{ */
PHP_FUNCTION(phar_fopen) /* {{{ */
{
zend_string *filename;
char *mode;
Expand Down Expand Up @@ -653,7 +652,7 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
/* }}} */

#define PharFileFunction(fname, funcnum, orig) \
ZEND_NAMED_FUNCTION(fname) { \
PHP_FUNCTION(fname) { \
if (!PHAR_G(intercepted)) { \
PHAR_G(orig)(INTERNAL_FUNCTION_PARAM_PASSTHRU); \
} else { \
Expand Down Expand Up @@ -725,7 +724,7 @@ PharFileFunction(phar_file_exists, FS_EXISTS, orig_file_exists)
PharFileFunction(phar_is_dir, FS_IS_DIR, orig_is_dir)
/* }}} */

PHAR_FUNC(phar_is_file) /* {{{ */
PHP_FUNCTION(phar_is_file) /* {{{ */
{
char *filename;
size_t filename_len;
Expand Down Expand Up @@ -791,7 +790,7 @@ PHAR_FUNC(phar_is_file) /* {{{ */
}
/* }}} */

PHAR_FUNC(phar_is_link) /* {{{ */
PHP_FUNCTION(phar_is_link) /* {{{ */
{
char *filename;
size_t filename_len;
Expand Down Expand Up @@ -886,7 +885,7 @@ void phar_release_functions(void)
PHAR_G(orig_##func) = NULL; \
if (NULL != (orig = zend_hash_str_find_ptr(CG(function_table), #func, sizeof(#func)-1))) { \
PHAR_G(orig_##func) = orig->internal_function.handler; \
orig->internal_function.handler = phar_##func; \
orig->internal_function.handler = PHP_FN(phar_##func); \
}

void phar_intercept_functions_init(void)
Expand Down
Loading
Loading