From: Steph Fox Date: Thu, 21 Feb 2008 00:24:38 +0000 (+0000) Subject: A much better solution for setDefaultStub(). The convertTo*() fixes should be trivial... X-Git-Tag: RELEASE_2_0_0a1~406 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6cc2426ff17770581b410809cfb5a27425597f7c;p=php A much better solution for setDefaultStub(). The convertTo*() fixes should be trivial now. --- diff --git a/ext/phar/phar.c b/ext/phar/phar.c index e34a37e9cd..cb7f3c17e9 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -1712,7 +1712,7 @@ char *phar_create_default_stub(const char *index_php, const char *web_index, siz *error = NULL; } - if (!index_php || strlen(index_php) == 0) { + if (!index_php) { index_php = "index.php"; } diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index e60fdb1b41..aee70b48b2 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -2212,24 +2212,18 @@ PHP_METHOD(Phar, setStub) */ PHP_METHOD(Phar, setDefaultStub) { - char *index = NULL, *webindex = NULL, *error = NULL, *stub = NULL; + char *index = NULL, *webindex = NULL, *error = NULL; + char *stub = "dummy"; int index_len = 0, webindex_len = 0; - size_t stub_len; + size_t stub_len = 0; PHAR_ARCHIVE_OBJECT(); - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &index, &index_len, &webindex, &webindex_len) == FAILURE) { - RETURN_FALSE; - } - - if (phar_obj->arc.archive->is_tar) { - zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, - "Stub cannot be changed in a tar-based phar"); + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s", &index, &index_len, &webindex, &webindex_len) == FAILURE) { RETURN_FALSE; } - if (phar_obj->arc.archive->is_zip) { - zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, - "Stub cannot be changed in a zip-based phar"); + if (ZEND_NUM_ARGS() > 0 && (phar_obj->arc.archive->is_tar || phar_obj->arc.archive->is_zip)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "method accepts no arguments for a tar- or zip-based phar stub, %d given", ZEND_NUM_ARGS()); RETURN_FALSE; } @@ -2239,12 +2233,14 @@ PHP_METHOD(Phar, setStub) RETURN_FALSE; } - stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC); + if (!phar_obj->arc.archive->is_tar && !phar_obj->arc.archive->is_zip) { + stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC); - if (error) { - zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, error); - efree(error); - RETURN_FALSE; + if (error) { + zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, error); + efree(error); + RETURN_FALSE; + } } phar_flush(phar_obj->arc.archive, stub, stub_len, &error TSRMLS_CC); diff --git a/ext/phar/tar.c b/ext/phar/tar.c index dd754137d2..bfd56fd3e0 100644 --- a/ext/phar/tar.c +++ b/ext/phar/tar.c @@ -449,6 +449,8 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, long len, char **er entry.tar_type = '0'; entry.phar = phar; entry.fp_type = PHAR_MOD; + + /* set alias */ if (!phar->is_temporary_alias && phar->alias_len) { entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1); @@ -473,7 +475,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, long len, char **er } /* set stub */ - if (user_stub) { + if (user_stub && user_stub != "dummy") { char *pos; if (len < 0) { /* resource passed in */ @@ -531,19 +533,38 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, long len, char **er efree(user_stub); } } else { - if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { - /* this is a brand new phar */ - entry.fp = php_stream_fopen_tmpfile(); - if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) { + /* Either this is a brand new phar (add the stub), or setDefaultStub() is the caller (overwrite the stub) */ + entry.fp = php_stream_fopen_tmpfile(); + if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) { + if (error) { + spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname); + } + return EOF; + } + entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1; + entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); + entry.filename_len = sizeof(".phar/stub.php")-1; + + if (!user_stub) { + if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { + if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) { + php_stream_close(entry.fp); + efree(entry.filename); + if (error) { + spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname); + } + return EOF; + } + } + } else { + if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) { + php_stream_close(entry.fp); + efree(entry.filename); if (error) { - spprintf(error, 0, "unable to create stub in new tar-based phar \"%s\"", phar->fname); + spprintf(error, 0, "unable to overwrite stub in zip-based phar \"%s\"", phar->fname); } return EOF; } - entry.uncompressed_filesize = sizeof(newstub) - 1; - entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); - entry.filename_len = sizeof(".phar/stub.php")-1; - zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL); } } diff --git a/ext/phar/tests/tar/phar_setdefaultstub.phpt b/ext/phar/tests/tar/phar_setdefaultstub.phpt index 4dc0898d62..b285cb1c9b 100644 --- a/ext/phar/tests/tar/phar_setdefaultstub.phpt +++ b/ext/phar/tests/tar/phar_setdefaultstub.phpt @@ -10,6 +10,12 @@ $fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.tar'; $phar = new Phar($fname); $phar['a.php'] = ''; $phar['b.php'] = ''; +$phar->setStub(''); + +var_dump($phar->getStub()); + +echo "============================================================================\n"; +echo "============================================================================\n"; try { $phar->setDefaultStub(); @@ -50,18 +56,23 @@ var_dump($phar->getStub()); ---EXPECT-- -Stub cannot be changed in a tar-based phar +--EXPECTF-- +string(51) " +" +============================================================================ +============================================================================ string(60) "'; $phar['b.php'] = ''; +$phar->setStub(''); + +var_dump($phar->getStub()); + +echo "============================================================================\n"; +echo "============================================================================\n"; try { $phar->setDefaultStub(); @@ -50,18 +56,23 @@ var_dump($phar->getStub()); ---EXPECT-- -Stub cannot be changed in a zip-based phar +--EXPECTF-- +string(51) " +" +============================================================================ +============================================================================ string(60) "alias_len; entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1); entry.filename_len = sizeof(".phar/alias.txt")-1; - entry.is_modified = 1; if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) { if (error) { spprintf(error, 0, "unable to set alias in zip-based phar \"%s\"", phar->fname); @@ -690,7 +689,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, char **er } /* set stub */ - if (user_stub) { + if (user_stub && user_stub != "dummy") { if (len < 0) { /* resource passed in */ if (!(php_stream_from_zval_no_verify(stubfile, (zval **)user_stub))) { @@ -742,7 +741,6 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, char **er } entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); entry.filename_len = sizeof(".phar/stub.php")-1; - entry.is_modified = 1; if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) { if (free_user_stub) { efree(user_stub); @@ -756,24 +754,35 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, char **er efree(user_stub); } } else { - if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { - /* this is a brand new phar, add the stub */ - entry.fp = php_stream_fopen_tmpfile(); - if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) { - if (error) { - spprintf(error, 0, "unable to create stub in new zip-based phar \"%s\"", phar->fname); + /* Either this is a brand new phar (add the stub), or setDefaultStub() is the caller (overwrite the stub) */ + entry.fp = php_stream_fopen_tmpfile(); + if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) { + if (error) { + spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname); + } + return EOF; + } + entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1; + entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); + entry.filename_len = sizeof(".phar/stub.php")-1; + + if (!user_stub) { + if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { + if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) { + php_stream_close(entry.fp); + efree(entry.filename); + if (error) { + spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname); + } + return EOF; } - return EOF; } - entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1; - entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); - entry.filename_len = sizeof(".phar/stub.php")-1; - entry.is_modified = 1; - if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) { + } else { + if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) { php_stream_close(entry.fp); efree(entry.filename); if (error) { - spprintf(error, 0, "unable to create stub in new zip-based phar \"%s\"", phar->fname); + spprintf(error, 0, "unable to overwrite stub in zip-based phar \"%s\"", phar->fname); } return EOF; }