Skip to content

Commit 7beea2a

Browse files
committed
ext/phar: refactor phar_get_archive() to return phar_archive_data*
Instead of using an out param.
1 parent f6c24b1 commit 7beea2a

8 files changed

Lines changed: 107 additions & 125 deletions

File tree

ext/phar/dirstream.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ static php_stream *phar_make_dirstream(const char *dir, size_t dirlen, const Has
248248
php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) /* {{{ */
249249
{
250250
char *error;
251-
phar_archive_data *phar;
252251

253252
php_url *resource = phar_parse_url(wrapper, path, mode, options);
254253
if (!resource) {
@@ -276,7 +275,8 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,
276275

277276
phar_request_initialize();
278277

279-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
278+
const phar_archive_data *phar = phar_get_archive(ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error);
279+
if (!phar) {
280280
if (error) {
281281
php_stream_wrapper_log_error(wrapper, options, "%s", error);
282282
efree(error);
@@ -344,7 +344,6 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,
344344
int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mode, int options, php_stream_context *context) /* {{{ */
345345
{
346346
phar_entry_info entry;
347-
phar_archive_data *phar = NULL;
348347
char *error;
349348
php_url *resource = NULL;
350349

@@ -355,9 +354,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
355354
return 0;
356355
}
357356

358-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL)) {
359-
phar = NULL;
360-
}
357+
phar_archive_data *phar = phar_get_archive(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
361358

362359
zend_string_release_ex(arch, false);
363360

@@ -383,7 +380,8 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
383380
return 0;
384381
}
385382

386-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
383+
phar = phar_get_archive(ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error);
384+
if (!phar) {
387385
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);
388386
efree(error);
389387
php_url_free(resource);
@@ -471,7 +469,6 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
471469
*/
472470
int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) /* {{{ */
473471
{
474-
phar_archive_data *phar = NULL;
475472
char *error;
476473

477474
/* pre-readonly check, we need to know if this is a data phar */
@@ -481,9 +478,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
481478
return 0;
482479
}
483480

484-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL)) {
485-
phar = NULL;
486-
}
481+
phar_archive_data *phar = phar_get_archive(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
487482

488483
zend_string_release_ex(arch, false);
489484

@@ -510,7 +505,8 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
510505
return 0;
511506
}
512507

513-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
508+
phar = phar_get_archive(ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error);
509+
if (!phar) {
514510
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);
515511
efree(error);
516512
php_url_free(resource);

ext/phar/func_interceptors.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
9898

9999
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
100100
/* retrieving a file defaults to within the current directory, so use this if possible */
101-
phar_archive_data *phar;
102-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL)) {
101+
phar_archive_data *phar = phar_get_archive(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
102+
if (!phar) {
103103
zend_string_release_ex(arch, false);
104104
return NULL;
105105
}
@@ -492,9 +492,9 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
492492
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
493493
if (arch) {
494494
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
495-
zend_result has_archive = phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
495+
phar = phar_get_archive(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
496496
zend_string_release_ex(arch, false);
497-
if (FAILURE == has_archive) {
497+
if (!phar) {
498498
goto skip_phar;
499499
}
500500
splitted:;
@@ -727,13 +727,13 @@ PHP_FUNCTION(phar_is_file) /* {{{ */
727727

728728
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
729729
if (arch) {
730-
phar_archive_data *phar;
730+
;
731731

732732
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
733733
/* retrieving a file within the current directory, so use this if possible */
734-
zend_result has_archive = phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
734+
phar_archive_data *phar = phar_get_archive(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
735735
zend_string_release_ex(arch, false);
736-
if (has_archive == SUCCESS) {
736+
if (phar) {
737737
phar_entry_info *etemp;
738738

739739
zend_string *entry = phar_fix_filepath(filename, filename_len, true);
@@ -784,13 +784,11 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
784784

785785
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
786786
if (arch) {
787-
phar_archive_data *phar;
788-
789787
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
790788
/* retrieving a file within the current directory, so use this if possible */
791-
zend_result has_archive = phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
789+
phar_archive_data *phar = phar_get_archive(ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
792790
zend_string_release_ex(arch, false);
793-
if (has_archive == SUCCESS) {
791+
if (phar) {
794792
phar_entry_info *etemp;
795793

796794
zend_string *entry = phar_fix_filepath(filename, filename_len, true);

ext/phar/phar.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ ZEND_ATTRIBUTE_NONNULL void phar_entry_remove(phar_entry_data *idata, char **err
486486
*/
487487
static zend_result phar_open_parsed_phar(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
488488
{
489-
phar_archive_data *phar;
490489
#ifdef PHP_WIN32
491490
char *save_fname;
492491
ALLOCA_FLAG(fname_use_heap)
@@ -504,12 +503,12 @@ static zend_result phar_open_parsed_phar(char *fname, size_t fname_len, char *al
504503
phar_unixify_path_separators(fname, fname_len);
505504
}
506505
#endif
507-
zend_result archive_retrieved = phar_get_archive(&phar, fname, fname_len, alias, alias_len, error);
506+
phar_archive_data *phar = phar_get_archive(fname, fname_len, alias, alias_len, error);
508507
/* logic is as follows:
509508
- If no alias was passed in, then it can match either and be valid
510509
- If an explicit alias was requested, ensure the filename passed in matches the phar's filename.
511510
*/
512-
bool process_phar = SUCCESS == archive_retrieved && (!alias || zend_string_equals_cstr(phar->fname, fname, fname_len));
511+
bool process_phar = phar && (!alias || zend_string_equals_cstr(phar->fname, fname, fname_len));
513512
#ifdef PHP_WIN32
514513
if (fname != save_fname) {
515514
free_alloca(fname, fname_use_heap);

ext/phar/phar_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 6, 7) zend_result phar_open_or_create_filename(ze
411411
ZEND_ATTRIBUTE_NONNULL_ARGS(1, 6, 7) zend_result phar_create_or_parse_filename(zend_string *fname, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error);
412412
ZEND_ATTRIBUTE_NONNULL_ARGS(3) zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **error);
413413
zend_result phar_free_alias(const phar_archive_data *phar);
414-
zend_result phar_get_archive(phar_archive_data **archive, const char *fname, size_t fname_len, const char *alias, size_t alias_len, char **error);
414+
phar_archive_data* phar_get_archive(const char *fname, size_t fname_len, const char *alias, size_t alias_len, char **error);
415415
zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type, char *sig, size_t sig_len, const char *fname, char **signature, size_t *signature_len, char **error);
416416
ZEND_ATTRIBUTE_NONNULL zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error);
417417

ext/phar/phar_object.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,8 @@ PHP_METHOD(Phar, webPhar)
752752
entry_len = sizeof("/index.php")-1;
753753
}
754754

755-
if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL) ||
756-
(info = phar_get_entry_info(phar, entry, entry_len, NULL, false)) == NULL) {
755+
phar = phar_get_archive(fname, fname_len, NULL, 0, NULL);
756+
if (!phar || (info = phar_get_entry_info(phar, entry, entry_len, NULL, false)) == NULL) {
757757
phar_do_404(phar, fname, fname_len, f404);
758758
} else {
759759
char *tmp = NULL, sa = '\0';
@@ -793,8 +793,8 @@ PHP_METHOD(Phar, webPhar)
793793
goto cleanup_skip_entry;
794794
}
795795

796-
if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL) ||
797-
(info = phar_get_entry_info(phar, entry, entry_len, NULL, false)) == NULL) {
796+
phar = phar_get_archive(fname, fname_len, NULL, 0, NULL);
797+
if (!phar || (info = phar_get_entry_info(phar, entry, entry_len, NULL, false)) == NULL) {
798798
phar_do_404(phar, fname, fname_len, f404);
799799
goto cleanup;
800800
}

ext/phar/stream.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
233233
} else {
234234
if (!*internal_file && (options & STREAM_OPEN_FOR_INCLUDE)) {
235235
/* retrieve the stub */
236-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, NULL)) {
236+
phar = phar_get_archive(ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, NULL);
237+
if (!phar) {
237238
php_stream_wrapper_log_error(wrapper, options, "file %s is not a valid phar archive", ZSTR_VAL(resource->host));
238239
efree(internal_file);
239240
php_url_free(resource);
@@ -557,7 +558,6 @@ static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int f
557558
php_stream_statbuf *ssb, php_stream_context *context) /* {{{ */
558559
{
559560
char *internal_file;
560-
phar_archive_data *phar;
561561
size_t internal_file_len;
562562

563563
php_url *resource = phar_parse_url(wrapper, url, "r", flags|PHP_STREAM_URL_STAT_QUIET);
@@ -580,7 +580,8 @@ static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int f
580580

581581
internal_file = ZSTR_VAL(resource->path) + 1; /* strip leading "/" */
582582
/* find the phar in our trusty global hash indexed by alias (host of phar://blah.phar/file.whatever) */
583-
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, NULL)) {
583+
phar_archive_data *phar = phar_get_archive(ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, NULL);
584+
if (!phar) {
584585
php_url_free(resource);
585586
return FAILURE;
586587
}
@@ -731,7 +732,6 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
731732
static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context) /* {{{ */
732733
{
733734
char *error;
734-
phar_archive_data *phar, *pfrom, *pto;
735735
bool is_dir = false;
736736
bool is_modified = false;
737737

@@ -742,7 +742,9 @@ static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from
742742
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": invalid or non-writable url \"%s\"", url_from, url_to, url_from);
743743
return 0;
744744
}
745-
if (SUCCESS != phar_get_archive(&pfrom, ZSTR_VAL(resource_from->host), ZSTR_LEN(resource_from->host), NULL, 0, &error)) {
745+
746+
phar_archive_data *pfrom = phar_get_archive(ZSTR_VAL(resource_from->host), ZSTR_LEN(resource_from->host), NULL, 0, &error);
747+
if (!pfrom) {
746748
pfrom = NULL;
747749
if (error) {
748750
efree(error);
@@ -760,7 +762,9 @@ static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from
760762
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": invalid or non-writable url \"%s\"", url_from, url_to, url_to);
761763
return 0;
762764
}
763-
if (SUCCESS != phar_get_archive(&pto, ZSTR_VAL(resource_to->host), ZSTR_LEN(resource_to->host), NULL, 0, &error)) {
765+
766+
phar_archive_data *pto = phar_get_archive(ZSTR_VAL(resource_to->host), ZSTR_LEN(resource_to->host), NULL, 0, &error);
767+
if (!pto) {
764768
if (error) {
765769
efree(error);
766770
}
@@ -809,7 +813,8 @@ static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from
809813
return 0;
810814
}
811815

812-
if (SUCCESS != phar_get_archive(&phar, ZSTR_VAL(resource_from->host), ZSTR_LEN(resource_from->host), NULL, 0, &error)) {
816+
phar_archive_data *phar = phar_get_archive(ZSTR_VAL(resource_from->host), ZSTR_LEN(resource_from->host), NULL, 0, &error);
817+
if (!phar) {
813818
php_url_free(resource_from);
814819
php_url_free(resource_to);
815820
php_error_docref(NULL, E_WARNING, "phar error: cannot rename \"%s\" to \"%s\": %s", url_from, url_to, error);

0 commit comments

Comments
 (0)