Skip to content

Commit 46faf8f

Browse files
committed
Introduce zend_stream_init_fp() API
Reduce the amount of code that mucks around with zend_file_handle initialization.
1 parent 349a388 commit 46faf8f

File tree

7 files changed

+42
-56
lines changed

7 files changed

+42
-56
lines changed

Zend/zend_stream.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ static size_t zend_stream_fsize(zend_file_handle *file_handle) /* {{{ */
9494
return -1;
9595
} /* }}} */
9696

97+
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename) {
98+
memset(handle, 0, sizeof(zend_file_handle));
99+
handle->type = ZEND_HANDLE_FP;
100+
handle->handle.fp = fp;
101+
handle->filename = filename;
102+
}
103+
97104
ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle) /* {{{ */
98105
{
99106
if (zend_stream_open_function) {

Zend/zend_stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct _zend_file_handle {
6969
} zend_file_handle;
7070

7171
BEGIN_EXTERN_C()
72+
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename);
7273
ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle);
7374
ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len);
7475
ZEND_API void zend_file_handle_dtor(zend_file_handle *fh);

ext/standard/browscap.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,12 @@ static int browscap_read_file(char *filename, browser_data *browdata, int persis
413413
return FAILURE;
414414
}
415415

416-
fh.handle.fp = VCWD_FOPEN(filename, "r");
417-
fh.opened_path = NULL;
418-
fh.free_filename = 0;
416+
zend_stream_init_fp(&fh, VCWD_FOPEN(filename, "r"), filename);
419417
if (!fh.handle.fp) {
420418
zend_error(E_CORE_WARNING, "Cannot open '%s' for reading", filename);
421419
return FAILURE;
422420
}
423421

424-
fh.filename = filename;
425-
fh.type = ZEND_HANDLE_FP;
426-
427422
browdata->htab = pemalloc(sizeof *browdata->htab, persistent);
428423
zend_hash_init_ex(browdata->htab, 0, NULL,
429424
persistent ? browscap_entry_dtor_persistent : browscap_entry_dtor, persistent, 0);

main/php_ini.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,9 @@ int php_init_config(void)
419419
int php_ini_scanned_path_len;
420420
char *open_basedir;
421421
int free_ini_search_path = 0;
422-
zend_file_handle fh;
423422
zend_string *opened_path = NULL;
423+
FILE *fp;
424+
const char *filename;
424425

425426
zend_hash_init(&configuration_hash, 8, NULL, config_zval_dtor, 1);
426427

@@ -572,7 +573,8 @@ int php_init_config(void)
572573
* Find and open actual ini file
573574
*/
574575

575-
memset(&fh, 0, sizeof(fh));
576+
fp = NULL;
577+
filename = NULL;
576578

577579
/* If SAPI does not want to ignore all ini files OR an overriding file/path is given.
578580
* 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)
585587

586588
if (!VCWD_STAT(php_ini_file_name, &statbuf)) {
587589
if (!((statbuf.st_mode & S_IFMT) == S_IFDIR)) {
588-
fh.handle.fp = VCWD_FOPEN(php_ini_file_name, "r");
589-
if (fh.handle.fp) {
590-
fh.filename = expand_filepath(php_ini_file_name, NULL);
590+
fp = VCWD_FOPEN(php_ini_file_name, "r");
591+
if (fp) {
592+
filename = expand_filepath(php_ini_file_name, NULL);
591593
}
592594
}
593595
}
594596
}
595597

596598
/* Otherwise search for php-%sapi-module-name%.ini file in search path */
597-
if (!fh.handle.fp) {
599+
if (!fp) {
598600
const char *fmt = "php-%s.ini";
599601
char *ini_fname;
600602
spprintf(&ini_fname, 0, fmt, sapi_module.name);
601-
fh.handle.fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &opened_path);
603+
fp = php_fopen_with_path(ini_fname, "r", php_ini_search_path, &opened_path);
602604
efree(ini_fname);
603-
if (fh.handle.fp) {
604-
fh.filename = ZSTR_VAL(opened_path);
605+
if (fp) {
606+
filename = ZSTR_VAL(opened_path);
605607
}
606608
}
607609

608610
/* If still no ini file found, search for php.ini file in search path */
609-
if (!fh.handle.fp) {
610-
fh.handle.fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &opened_path);
611-
if (fh.handle.fp) {
612-
fh.filename = ZSTR_VAL(opened_path);
611+
if (!fp) {
612+
fp = php_fopen_with_path("php.ini", "r", php_ini_search_path, &opened_path);
613+
if (fp) {
614+
filename = ZSTR_VAL(opened_path);
613615
}
614616
}
615617
}
@@ -620,8 +622,9 @@ int php_init_config(void)
620622

621623
PG(open_basedir) = open_basedir;
622624

623-
if (fh.handle.fp) {
624-
fh.type = ZEND_HANDLE_FP;
625+
if (fp) {
626+
zend_file_handle fh;
627+
zend_stream_init_fp(&fh, fp, filename);
625628
RESET_ACTIVE_INI_HASH();
626629

627630
zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
@@ -655,15 +658,13 @@ int php_init_config(void)
655658
zend_stat_t sb;
656659
char ini_file[MAXPATHLEN];
657660
char *p;
658-
zend_file_handle fh2;
659661
zend_llist scanned_ini_list;
660662
zend_llist_element *element;
661663
int l, total_l = 0;
662664
char *bufpath, *debpath, *endpath;
663665
int lenpath;
664666

665667
zend_llist_init(&scanned_ini_list, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
666-
memset(&fh2, 0, sizeof(fh2));
667668

668669
bufpath = estrdup(php_ini_scanned_path);
669670
for (debpath = bufpath ; debpath ; debpath=endpath) {
@@ -697,11 +698,10 @@ int php_init_config(void)
697698
}
698699
if (VCWD_STAT(ini_file, &sb) == 0) {
699700
if (S_ISREG(sb.st_mode)) {
700-
if ((fh2.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
701-
fh2.filename = ini_file;
702-
fh2.type = ZEND_HANDLE_FP;
703-
704-
if (zend_parse_ini_file(&fh2, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash) == SUCCESS) {
701+
zend_file_handle fh;
702+
zend_stream_init_fp(&fh, VCWD_FOPEN(ini_file, "r"), ini_file);
703+
if (fh.handle.fp) {
704+
if (zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash) == SUCCESS) {
705705
/* Here, add it to the list of ini files read */
706706
l = (int)strlen(ini_file);
707707
total_l += l + 2;
@@ -784,17 +784,14 @@ PHPAPI int php_parse_user_ini_file(const char *dirname, char *ini_filename, Hash
784784
{
785785
zend_stat_t sb;
786786
char ini_file[MAXPATHLEN];
787-
zend_file_handle fh;
788787

789788
snprintf(ini_file, MAXPATHLEN, "%s%c%s", dirname, DEFAULT_SLASH, ini_filename);
790789

791790
if (VCWD_STAT(ini_file, &sb) == 0) {
792791
if (S_ISREG(sb.st_mode)) {
793-
memset(&fh, 0, sizeof(fh));
794-
if ((fh.handle.fp = VCWD_FOPEN(ini_file, "r"))) {
795-
fh.filename = ini_file;
796-
fh.type = ZEND_HANDLE_FP;
797-
792+
zend_file_handle fh;
793+
zend_stream_init_fp(&fh, VCWD_FOPEN(ini_file, "r"), ini_file);
794+
if (fh.handle.fp) {
798795
/* Reset active ini section */
799796
RESET_ACTIVE_INI_HASH();
800797

sapi/cgi/cgi_main.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,15 +2479,12 @@ consult the installation file that came with this distribution, or visit \n\
24792479
file_handle.type = ZEND_HANDLE_FILENAME;
24802480
file_handle.filename = SG(request_info).path_translated;
24812481
file_handle.handle.fp = NULL;
2482+
file_handle.opened_path = NULL;
2483+
file_handle.free_filename = 0;
24822484
} else {
2483-
file_handle.filename = "Standard input code";
2484-
file_handle.type = ZEND_HANDLE_FP;
2485-
file_handle.handle.fp = stdin;
2485+
zend_stream_init_fp(&file_handle, stdin, "Standard input code");
24862486
}
24872487

2488-
file_handle.opened_path = NULL;
2489-
file_handle.free_filename = 0;
2490-
24912488
/* request startup only after we've done all we can to
24922489
* get path_translated */
24932490
if (php_request_startup() == FAILURE) {

sapi/cli/php_cli.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -589,18 +589,13 @@ static const char *param_mode_conflict = "Either execute direct code, process st
589589
*/
590590
static int cli_seek_file_begin(zend_file_handle *file_handle, char *script_file)
591591
{
592-
// TODO: Is this still needed?
593-
file_handle->type = ZEND_HANDLE_FP;
594-
file_handle->opened_path = NULL;
595-
file_handle->free_filename = 0;
596-
if (!(file_handle->handle.fp = VCWD_FOPEN(script_file, "rb"))) {
592+
FILE *fp = VCWD_FOPEN(script_file, "rb");
593+
if (!fp) {
597594
php_printf("Could not open input file: %s\n", script_file);
598595
return FAILURE;
599596
}
600-
file_handle->filename = script_file;
601-
602-
rewind(file_handle->handle.fp);
603597

598+
zend_stream_init_fp(file_handle, fp, script_file);
604599
return SUCCESS;
605600
}
606601
/* }}} */
@@ -916,12 +911,8 @@ static int do_cli(int argc, char **argv) /* {{{ */
916911
/* here but this would make things only more complicated. And it */
917912
/* is consitent with the way -R works where the stdin file handle*/
918913
/* is also accessible. */
919-
file_handle.filename = "Standard input code";
920-
file_handle.handle.fp = stdin;
914+
zend_stream_init_fp(&file_handle, stdin, "Standard input code");
921915
}
922-
file_handle.type = ZEND_HANDLE_FP;
923-
file_handle.opened_path = NULL;
924-
file_handle.free_filename = 0;
925916
php_self = (char*)file_handle.filename;
926917

927918
/* before registering argv to module exchange the *new* argv[0] */

sapi/litespeed/lsapi_main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,9 +1296,7 @@ static int cli_main( int argc, char * argv[] )
12961296
if ( ret == -1 ) {
12971297
if ( *p ) {
12981298
zend_file_handle file_handle;
1299-
memset(&file_handle, 0, sizeof(file_handle));
1300-
file_handle.type = ZEND_HANDLE_FP;
1301-
file_handle.handle.fp = VCWD_FOPEN(*p, "rb");
1299+
zend_stream_init_fp(&file_handle, VCWD_FOPEN(*p, "rb"), NULL);
13021300

13031301
if ( file_handle.handle.fp ) {
13041302
script_filename = *p;

0 commit comments

Comments
 (0)