Skip to content

Commit 9cf133c

Browse files
committed
ext/phar: restructure code to be more legible
1 parent 9d30bd5 commit 9cf133c

File tree

2 files changed

+40
-44
lines changed

2 files changed

+40
-44
lines changed

ext/phar/func_interceptors.c

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
5757
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
5858
entry_len = filename_len;
5959
/* retrieving a file within the current directory, so use this if possible */
60-
entry = phar_fix_filepath(entry, &entry_len, 1);
60+
entry = phar_fix_filepath(entry, &entry_len, true);
6161

6262
if (entry[0] == '/') {
6363
spprintf(&name, 4096, "phar://%s%s", arch, entry);
@@ -117,19 +117,18 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
117117
}
118118
} else {
119119
size_t entry_len = ZSTR_LEN(filename);
120-
char *entry = phar_fix_filepath(estrndup(ZSTR_VAL(filename), ZSTR_LEN(filename)), &entry_len, 1);
120+
char *entry = phar_fix_filepath(estrndup(ZSTR_VAL(filename), ZSTR_LEN(filename)), &entry_len, true);
121+
bool is_in_phar;
121122
if (entry[0] == '/') {
122-
if (!zend_hash_str_exists(&(phar->manifest), entry + 1, entry_len - 1)) {
123-
/* this file is not in the phar, use the original path */
124-
notfound:
125-
efree(entry);
126-
efree(arch);
127-
return NULL;
128-
}
123+
is_in_phar = zend_hash_str_exists(&(phar->manifest), entry + 1, entry_len - 1);
129124
} else {
130-
if (!zend_hash_str_exists(&(phar->manifest), entry, entry_len)) {
131-
goto notfound;
132-
}
125+
is_in_phar = zend_hash_str_exists(&(phar->manifest), entry, entry_len);
126+
}
127+
/* this file is not in the phar, use the original path */
128+
if (!is_in_phar) {
129+
efree(entry);
130+
efree(arch);
131+
return NULL;
133132
}
134133
/* auto-convert to phar:// */
135134
if (entry[0] == '/') {
@@ -483,7 +482,6 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
483482
size_t arch_len, entry_len;
484483
zend_string *fname;
485484
zend_stat_t sb = {0};
486-
phar_entry_info *data = NULL;
487485
phar_archive_data *phar;
488486

489487
fname = zend_get_executed_filename_ex();
@@ -513,15 +511,18 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
513511
goto skip_phar;
514512
}
515513
splitted:
516-
entry = phar_fix_filepath(entry, &entry_len, 1);
514+
entry = phar_fix_filepath(entry, &entry_len, true);
515+
const phar_entry_info *data = NULL;
517516
if (entry[0] == '/') {
518-
if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
517+
data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1);
518+
if (data) {
519519
efree(entry);
520520
goto stat_entry;
521521
}
522522
goto notfound;
523523
}
524-
if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len))) {
524+
data = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len);
525+
if (data) {
525526
efree(entry);
526527
goto stat_entry;
527528
}
@@ -552,8 +553,9 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
552553
PHAR_G(cwd) = "/";
553554
PHAR_G(cwd_len) = 0;
554555
/* clean path without cwd */
555-
entry = phar_fix_filepath(entry, &entry_len, 1);
556-
if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
556+
entry = phar_fix_filepath(entry, &entry_len, true);
557+
data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1);
558+
if (NULL != data) {
557559
PHAR_G(cwd) = save;
558560
PHAR_G(cwd_len) = save_len;
559561
efree(entry);
@@ -755,20 +757,18 @@ PHP_FUNCTION(phar_is_file) /* {{{ */
755757
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
756758
phar_entry_info *etemp;
757759

758-
entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
760+
entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, true);
759761
if (entry[0] == '/') {
760-
if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
761-
/* this file is not in the current directory, use the original path */
762-
found_it:
763-
efree(entry);
764-
efree(arch);
765-
RETURN_BOOL(!etemp->is_dir);
766-
}
762+
etemp = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1);
767763
} else {
768-
if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len))) {
769-
goto found_it;
770-
}
764+
etemp = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len);
771765
}
766+
if (etemp) {
767+
efree(entry);
768+
efree(arch);
769+
RETURN_BOOL(!etemp->is_dir);
770+
}
771+
/* this file is not in the current directory, use the original path */
772772
}
773773
if (entry != filename) {
774774
efree(entry);
@@ -800,7 +800,7 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
800800
goto skip_phar;
801801
}
802802
if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
803-
char *arch, *entry;
803+
char *arch;
804804
size_t arch_len, entry_len;
805805
zend_string *fname = zend_get_executed_filename_ex();
806806

@@ -813,29 +813,25 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
813813
if (SUCCESS == phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), &arch, &arch_len, NULL, NULL, 2, 0)) {
814814
phar_archive_data *phar;
815815

816-
entry = filename;
816+
char *entry = filename;
817817
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
818818
entry_len = filename_len;
819819
/* retrieving a file within the current directory, so use this if possible */
820820
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
821821
phar_entry_info *etemp;
822822

823-
entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
823+
entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, true);
824824
if (entry[0] == '/') {
825-
if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) {
826-
/* this file is not in the current directory, use the original path */
827-
found_it:
828-
efree(entry);
829-
efree(arch);
830-
RETURN_BOOL(etemp->link);
831-
}
825+
etemp = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1);
832826
} else {
833-
if (NULL != (etemp = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len))) {
834-
goto found_it;
835-
}
827+
etemp = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len);
828+
}
829+
efree(entry);
830+
if (etemp) {
831+
efree(arch);
832+
RETURN_BOOL(etemp->link);
836833
}
837834
}
838-
efree(entry);
839835
efree(arch);
840836
RETURN_FALSE;
841837
}

ext/phar/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ zend_string *phar_find_in_include_path(zend_string *filename, phar_archive_data
317317
}
318318

319319
try_len = ZSTR_LEN(filename);
320-
test = phar_fix_filepath(estrndup(ZSTR_VAL(filename), ZSTR_LEN(filename)), &try_len, 1);
320+
test = phar_fix_filepath(estrndup(ZSTR_VAL(filename), ZSTR_LEN(filename)), &try_len, true);
321321

322322
if (*test == '/') {
323323
if (zend_hash_str_exists(&(phar->manifest), test + 1, try_len - 1)) {

0 commit comments

Comments
 (0)