Skip to content

Commit 4e09456

Browse files
committed
ext/phar: Refactor flushing of archive to only take string stub file
1 parent 1facbc3 commit 4e09456

File tree

7 files changed

+102
-215
lines changed

7 files changed

+102
-215
lines changed

ext/phar/dirstream.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
507507
return 0;
508508
}
509509

510-
phar_flush(phar, 0, 0, 0, &error);
510+
phar_flush(phar, &error);
511511

512512
if (error) {
513513
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", entry.filename, phar->fname, error);
@@ -634,7 +634,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
634634
} else {
635635
entry->is_deleted = 1;
636636
entry->is_modified = 1;
637-
phar_flush(phar, 0, 0, 0, &error);
637+
phar_flush(phar, &error);
638638

639639
if (error) {
640640
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", entry->filename, phar->fname, error);

ext/phar/phar.c

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */
433433
}
434434

435435
if (!phar->donotflush) {
436-
phar_flush(phar, 0, 0, 0, error);
436+
phar_flush(phar, error);
437437
}
438438
}
439439
/* }}} */
@@ -2522,27 +2522,31 @@ zend_string *phar_create_default_stub(const char *index_php, const char *web_ind
25222522
}
25232523
/* }}} */
25242524

2525+
int phar_flush(phar_archive_data *phar, char **error) {
2526+
return phar_flush_ex(phar, NULL, false, error);
2527+
}
2528+
25252529
/**
25262530
* Save phar contents to disk
25272531
*
25282532
* user_stub contains either a string, or a resource pointer, if len is a negative length.
25292533
* user_stub and len should be both 0 if the default or existing stub should be used
25302534
*/
2531-
int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int convert, char **error) /* {{{ */
2535+
int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */
25322536
{
25332537
char halt_stub[] = "__HALT_COMPILER();";
25342538
zend_string *newstub;
25352539
phar_entry_info *entry, *newentry;
25362540
size_t halt_offset;
25372541
int restore_alias_len, global_flags = 0, closeoldfile;
2538-
char *pos, has_dirs = 0;
2542+
bool has_dirs = 0;
25392543
char manifest[18], entry_buffer[24];
25402544
zend_off_t manifest_ftell;
25412545
zend_long offset;
25422546
size_t wrote;
25432547
uint32_t manifest_len, mytime, new_manifest_count;
25442548
uint32_t newcrc32;
2545-
php_stream *file, *oldfile, *newfile, *stubfile;
2549+
php_stream *file, *oldfile, *newfile;
25462550
php_stream_filter *filter;
25472551
php_serialize_data_t metadata_hash;
25482552
smart_str main_metadata_str = {0};
@@ -2568,11 +2572,11 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
25682572
zend_hash_clean(&phar->virtual_dirs);
25692573

25702574
if (phar->is_zip) {
2571-
return phar_zip_flush(phar, user_stub, len, convert, error);
2575+
return phar_zip_flush(phar, user_stub, is_default_stub, error);
25722576
}
25732577

25742578
if (phar->is_tar) {
2575-
return phar_tar_flush(phar, user_stub, len, convert, error);
2579+
return phar_tar_flush(phar, user_stub, is_default_stub, error);
25762580
}
25772581

25782582
if (PHAR_G(readonly)) {
@@ -2599,75 +2603,37 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
25992603
}
26002604

26012605
if (user_stub) {
2602-
zend_string *suser_stub;
2603-
bool free_user_stub = false;
2604-
2605-
if (len < 0) {
2606-
/* resource passed in */
2607-
if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) {
2608-
if (closeoldfile) {
2609-
php_stream_close(oldfile);
2610-
}
2611-
php_stream_close(newfile);
2612-
if (error) {
2613-
spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", phar->fname);
2614-
}
2615-
return EOF;
2616-
}
2617-
if (len == -1) {
2618-
len = PHP_STREAM_COPY_ALL;
2619-
} else {
2620-
len = -len;
2621-
}
2622-
user_stub = 0;
2623-
2624-
if (!(suser_stub = php_stream_copy_to_mem(stubfile, len, 0))) {
2625-
if (closeoldfile) {
2626-
php_stream_close(oldfile);
2627-
}
2628-
php_stream_close(newfile);
2629-
if (error) {
2630-
spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", phar->fname);
2631-
}
2632-
return EOF;
2633-
}
2634-
free_user_stub = true;
2635-
user_stub = ZSTR_VAL(suser_stub);
2636-
len = ZSTR_LEN(suser_stub);
2637-
}
2606+
char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), strlen(halt_stub));
26382607

2639-
if ((pos = php_stristr(user_stub, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
2608+
if (pos == NULL) {
26402609
if (closeoldfile) {
26412610
php_stream_close(oldfile);
26422611
}
26432612
php_stream_close(newfile);
26442613
if (error) {
26452614
spprintf(error, 0, "illegal stub for phar \"%s\" (__HALT_COMPILER(); is missing)", phar->fname);
26462615
}
2647-
if (free_user_stub) {
2648-
zend_string_free(suser_stub);
2649-
}
26502616
return EOF;
26512617
}
2652-
len = pos - user_stub + 18;
2653-
if ((size_t)len != php_stream_write(newfile, user_stub, len)
2654-
|| 5 != php_stream_write(newfile, " ?>\r\n", 5)) {
2618+
2619+
size_t len = pos - ZSTR_VAL(user_stub) + strlen(halt_stub);
2620+
const char end_sequence[] = " ?>\r\n";
2621+
size_t end_sequence_len = strlen(end_sequence);
2622+
2623+
if (
2624+
len != php_stream_write(newfile, ZSTR_VAL(user_stub), len)
2625+
|| end_sequence_len != php_stream_write(newfile, end_sequence, end_sequence_len)
2626+
) {
26552627
if (closeoldfile) {
26562628
php_stream_close(oldfile);
26572629
}
26582630
php_stream_close(newfile);
26592631
if (error) {
26602632
spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname);
26612633
}
2662-
if (free_user_stub) {
2663-
zend_string_free(suser_stub);
2664-
}
26652634
return EOF;
26662635
}
2667-
phar->halt_offset = len + 5;
2668-
if (free_user_stub) {
2669-
zend_string_free(suser_stub);
2670-
}
2636+
phar->halt_offset = len + end_sequence_len;
26712637
} else {
26722638
size_t written;
26732639

ext/phar/phar_internal.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,12 @@ zend_result phar_copy_on_write(phar_archive_data **pphar);
447447
bool phar_is_tar(char *buf, char *fname);
448448
zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, uint32_t compression, char **error);
449449
zend_result phar_open_or_create_tar(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error);
450-
int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error);
450+
int phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error);
451451

452452
/* zip functions in zip.c */
453453
int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, char **error);
454454
int phar_open_or_create_zip(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error);
455-
int phar_zip_flush(phar_archive_data *archive, char *user_stub, zend_long len, int defaultstub, char **error);
455+
int phar_zip_flush(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
456456

457457
#ifdef PHAR_MAIN
458458
extern const php_stream_wrapper php_stream_phar_wrapper;
@@ -468,7 +468,8 @@ phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t
468468
phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security);
469469
phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
470470
zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
471-
int phar_flush(phar_archive_data *archive, char *user_stub, zend_long len, int convert, char **error);
471+
int phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
472+
int phar_flush(phar_archive_data *archive, char **error);
472473
zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, int is_complete);
473474
zend_result phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, char **entry, size_t *entry_len, int executable, int for_create);
474475

0 commit comments

Comments
 (0)