1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 08:12:21 +01:00

Introduce zend_stream_init_fp() API

Reduce the amount of code that mucks around with zend_file_handle
initialization.
This commit is contained in:
Nikita Popov
2019-07-16 16:21:45 +02:00
parent 349a388b90
commit 46faf8f018
7 changed files with 42 additions and 56 deletions

View File

@@ -94,6 +94,13 @@ static size_t zend_stream_fsize(zend_file_handle *file_handle) /* {{{ */
return -1;
} /* }}} */
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename) {
memset(handle, 0, sizeof(zend_file_handle));
handle->type = ZEND_HANDLE_FP;
handle->handle.fp = fp;
handle->filename = filename;
}
ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle) /* {{{ */
{
if (zend_stream_open_function) {

View File

@@ -69,6 +69,7 @@ typedef struct _zend_file_handle {
} zend_file_handle;
BEGIN_EXTERN_C()
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename);
ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle);
ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len);
ZEND_API void zend_file_handle_dtor(zend_file_handle *fh);

View File

@@ -413,17 +413,12 @@ static int browscap_read_file(char *filename, browser_data *browdata, int persis
return FAILURE;
}
fh.handle.fp = VCWD_FOPEN(filename, "r");
fh.opened_path = NULL;
fh.free_filename = 0;
zend_stream_init_fp(&fh, VCWD_FOPEN(filename, "r"), filename);
if (!fh.handle.fp) {
zend_error(E_CORE_WARNING, "Cannot open '%s' for reading", filename);
return FAILURE;
}
fh.filename = filename;
fh.type = ZEND_HANDLE_FP;
browdata->htab = pemalloc(sizeof *browdata->htab, persistent);
zend_hash_init_ex(browdata->htab, 0, NULL,
persistent ? browscap_entry_dtor_persistent : browscap_entry_dtor, persistent, 0);

View File

@@ -419,8 +419,9 @@ int php_init_config(void)
int php_ini_scanned_path_len;
char *open_basedir;
int free_ini_search_path = 0;
zend_file_handle fh;
zend_string *opened_path = NULL;
FILE *fp;
const char *filename;
zend_hash_init(&configuration_hash, 8, NULL, config_zval_dtor, 1);
@@ -572,7 +573,8 @@ int php_init_config(void)
* Find and open actual ini file
*/
memset(&fh, 0, sizeof(fh));
fp = NULL;
filename = NULL;
/* If SAPI does not want to ignore all ini files OR an overriding file/path is given.
* This allows disabling scanning for ini files in the PHP_CONFIG_FILE_SCAN_DIR but still
@@ -585,31 +587,31 @@ int php_init_config(void)
if (!VCWD_STAT(php_ini_file_name, &statbuf)) {
if (!((statbuf.st_mode & S_IFMT) == S_IFDIR)) {
fh.handle.fp = VCWD_FOPEN(php_ini_file_name, "r");
if (fh.handle.fp) {
fh.filename = expand_filepath(php_ini_file_name, NULL);
fp = VCWD_FOPEN(php_ini_file_name, "r");
if (fp) {
filename = expand_filepath(php_ini_file_name, NULL);
}
}
}
}
/* Otherwise search for php-%sapi-module-name%.ini file in search path */
if (!fh.handle.fp) {
if (!fp) {
const char *fmt = "php-%s.ini";
char *ini_fname;
spprintf(&ini_fname, 0, fmt, sapi_module.name);
fh.handle.fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &opened_path);
fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &opened_path);
efree(ini_fname);
if (fh.handle.fp) {
fh.filename = ZSTR_VAL(opened_path);
if (fp) {
filename = ZSTR_VAL(opened_path);
}
}
/* If still no ini file found, search for php.ini file in search path */
if (!fh.handle.fp) {
fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &opened_path);
if (fh.handle.fp) {
fh.filename = ZSTR_VAL(opened_path);
if (!fp) {
fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &opened_path);
if (fp) {
filename = ZSTR_VAL(opened_path);
}
}
}
@@ -620,8 +622,9 @@ int php_init_config(void)
PG(open_basedir) = open_basedir;
if (fh.handle.fp) {
fh.type = ZEND_HANDLE_FP;
if (fp) {
zend_file_handle fh;
zend_stream_init_fp(&fh, fp, filename);
RESET_ACTIVE_INI_HASH();
zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
@@ -655,7 +658,6 @@ int php_init_config(void)
zend_stat_t sb;
char ini_file[MAXPATHLEN];
char *p;
zend_file_handle fh2;
zend_llist scanned_ini_list;
zend_llist_element *element;
int l, total_l = 0;
@@ -663,7 +665,6 @@ int php_init_config(void)
int lenpath;
zend_llist_init(&scanned_ini_list, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
memset(&fh2, 0, sizeof(fh2));
bufpath = estrdup(php_ini_scanned_path);
for (debpath = bufpath ; debpath ; debpath=endpath) {
@@ -697,11 +698,10 @@ int php_init_config(void)
}
if (VCWD_STAT(ini_file, &sb) == 0) {
if (S_ISREG(sb.st_mode)) {
if ((fh2.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
fh2.filename = ini_file;
fh2.type = ZEND_HANDLE_FP;
if (zend_parse_ini_file(&fh2, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash) == SUCCESS) {
zend_file_handle fh;
zend_stream_init_fp(&fh, VCWD_FOPEN(ini_file, "r"), ini_file);
if (fh.handle.fp) {
if (zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash) == SUCCESS) {
/* Here, add it to the list of ini files read */
l = (int)strlen(ini_file);
total_l += l + 2;
@@ -784,17 +784,14 @@ PHPAPI int php_parse_user_ini_file(const char *dirname, char *ini_filename, Hash
{
zend_stat_t sb;
char ini_file[MAXPATHLEN];
zend_file_handle fh;
snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, ini_filename);
if (VCWD_STAT(ini_file, &sb) == 0) {
if (S_ISREG(sb.st_mode)) {
memset(&fh, 0, sizeof(fh));
if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
fh.filename = ini_file;
fh.type = ZEND_HANDLE_FP;
zend_file_handle fh;
zend_stream_init_fp(&fh, VCWD_FOPEN(ini_file, "r"), ini_file);
if (fh.handle.fp) {
/* Reset active ini section */
RESET_ACTIVE_INI_HASH();

View File

@@ -2479,15 +2479,12 @@ parent_loop_end:
file_handle.type = ZEND_HANDLE_FILENAME;
file_handle.filename = SG(request_info).path_translated;
file_handle.handle.fp = NULL;
file_handle.opened_path = NULL;
file_handle.free_filename = 0;
} else {
file_handle.filename = "Standard input code";
file_handle.type = ZEND_HANDLE_FP;
file_handle.handle.fp = stdin;
zend_stream_init_fp(&file_handle, stdin, "Standard input code");
}
file_handle.opened_path = NULL;
file_handle.free_filename = 0;
/* request startup only after we've done all we can to
* get path_translated */
if (php_request_startup() == FAILURE) {

View File

@@ -589,18 +589,13 @@ static const char *param_mode_conflict = "Either execute direct code, process st
*/
static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file)
{
// TODO: Is this still needed?
file_handle->type = ZEND_HANDLE_FP;
file_handle->opened_path = NULL;
file_handle->free_filename = 0;
if (!(file_handle->handle.fp = VCWD_FOPEN(script_file, "rb"))) {
FILE *fp = VCWD_FOPEN(script_file, "rb");
if (!fp) {
php_printf("Could not open input file: %s\n", script_file);
return FAILURE;
}
file_handle->filename = script_file;
rewind(file_handle->handle.fp);
zend_stream_init_fp(file_handle, fp, script_file);
return SUCCESS;
}
/* }}} */
@@ -916,12 +911,8 @@ static int do_cli(int argc, char **argv) /* {{{ */
/* here but this would make things only more complicated. And it */
/* is consitent with the way -R works where the stdin file handle*/
/* is also accessible. */
file_handle.filename = "Standard input code";
file_handle.handle.fp = stdin;
zend_stream_init_fp(&file_handle, stdin, "Standard input code");
}
file_handle.type = ZEND_HANDLE_FP;
file_handle.opened_path = NULL;
file_handle.free_filename = 0;
php_self = (char*)file_handle.filename;
/* before registering argv to module exchange the *new* argv[0] */

View File

@@ -1296,9 +1296,7 @@ static int cli_main( int argc, char * argv[] )
if ( ret == -1 ) {
if ( *p ) {
zend_file_handle file_handle;
memset(&file_handle, 0, sizeof(file_handle));
file_handle.type = ZEND_HANDLE_FP;
file_handle.handle.fp = VCWD_FOPEN(*p, "rb");
zend_stream_init_fp(&file_handle, VCWD_FOPEN(*p, "rb"), NULL);
if ( file_handle.handle.fp ) {
script_filename = *p;