Skip to content

Commit efc84af

Browse files
committed
- Implement get_used_files() and get_imported_files()
1 parent ad30b19 commit efc84af

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

Zend/zend-scanner.l

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ ZEND_API void zend_open_file_dtor(zend_file_handle *fh)
189189
case ZEND_HANDLE_FP:
190190
fclose(fh->handle.fp);
191191
break;
192+
case ZEND_HANDLE_FILENAME:
193+
/* We're only supposed to get here when destructing the used_files hash,
194+
* which doesn't really contain open files, but references to their names/paths
195+
*/
196+
break;
192197
#ifdef ZTS
193198
case ZEND_HANDLE_FSTREAM:
194199
delete ((ifstream *) fh->handle.is);
@@ -521,10 +526,21 @@ int require_file(zend_file_handle *file_handle, zend_bool unique CLS_DC)
521526
}
522527
if (file_handle->opened_path) {
523528
if (unique) {
524-
if (zend_hash_add(&CG(used_files), file_handle->opened_path, strlen(file_handle->opened_path)+1, file_handle, sizeof(zend_file_handle), NULL)==FAILURE) {
529+
zend_file_handle *pfh;
530+
531+
if (zend_hash_add(&CG(used_files), file_handle->opened_path, strlen(file_handle->opened_path)+1, file_handle, sizeof(zend_file_handle), (void **) &pfh)==FAILURE) {
525532
zend_close_file_handle(file_handle CLS_CC);
526533
restore_lexical_state(&original_lex_state CLS_CC);
527534
return SUCCESS;
535+
} else {
536+
/* pfh is a copy we only save for get_used_files() */
537+
pfh->type = ZEND_HANDLE_FILENAME;
538+
if (pfh->filename) {
539+
pfh->filename = estrdup(pfh->filename);
540+
}
541+
if (pfh->opened_path) {
542+
pfh->opened_path = strdup(pfh->opened_path);
543+
}
528544
}
529545
}
530546
}

Zend/zend_builtin_functions.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ static ZEND_FUNCTION(get_class);
4040
static ZEND_FUNCTION(get_parent_class);
4141
static ZEND_FUNCTION(method_exists);
4242
static ZEND_FUNCTION(leak);
43+
static ZEND_FUNCTION(get_used_files);
44+
static ZEND_FUNCTION(get_imported_files);
4345

4446
extern unsigned char first_arg_force_ref[];
4547

@@ -59,6 +61,8 @@ static zend_function_entry builtin_functions[] = {
5961
ZEND_FE(get_parent_class, NULL)
6062
ZEND_FE(method_exists, NULL)
6163
ZEND_FE(leak, NULL)
64+
ZEND_FE(get_used_files, NULL)
65+
ZEND_FE(get_imported_files, NULL)
6266
{ NULL, NULL, NULL }
6367
};
6468

@@ -421,3 +425,41 @@ ZEND_FUNCTION(leak)
421425

422426
emalloc(leakbytes);
423427
}
428+
429+
430+
static int copy_import_use_file(zend_file_handle *fh, zval *array)
431+
{
432+
if (fh->filename) {
433+
char *extension_start;
434+
435+
extension_start = strstr(fh->filename, zend_uv.import_use_extension);
436+
if (extension_start) {
437+
*extension_start = 0;
438+
if (fh->opened_path) {
439+
add_assoc_string(array, fh->filename, fh->opened_path, 1);
440+
} else {
441+
add_assoc_stringl(array, fh->filename, "N/A", sizeof("N/A")-1, 1);
442+
}
443+
*extension_start = zend_uv.import_use_extension[0];
444+
}
445+
}
446+
return 0;
447+
}
448+
449+
450+
ZEND_FUNCTION(get_used_files)
451+
{
452+
CLS_FETCH();
453+
454+
array_init(return_value);
455+
zend_hash_apply_with_argument(&CG(used_files), (int (*)(void *, void *)) copy_import_use_file, return_value);
456+
}
457+
458+
459+
ZEND_FUNCTION(get_imported_files)
460+
{
461+
ELS_FETCH();
462+
463+
array_init(return_value);
464+
zend_hash_apply_with_argument(&EG(imported_files), (int (*)(void *, void *)) copy_import_use_file, return_value);
465+
}

Zend/zend_compile.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ static void build_runtime_defined_function_key(zval *result, zval *name, zend_op
7777
}
7878

7979

80+
static int zend_open_file_dtor_wrapper(zend_file_handle *fh)
81+
{
82+
zend_open_file_dtor(fh);
83+
return 1;
84+
}
85+
86+
8087
void init_compiler(CLS_D ELS_DC)
8188
{
8289
zend_stack_init(&CG(bp_stack));
@@ -95,7 +102,7 @@ void init_compiler(CLS_D ELS_DC)
95102
init_resource_list(ELS_C);
96103
CG(unclean_shutdown) = 0;
97104
zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) zend_open_file_dtor, 0);
98-
zend_hash_init(&CG(used_files), 5, NULL, NULL , 0);
105+
zend_hash_init(&CG(used_files), 5, NULL, (int (*)(void *)) zend_open_file_dtor_wrapper, 0);
99106
}
100107

101108

0 commit comments

Comments
 (0)