]> granicus.if.org Git - php/commitdiff
A much better solution for setDefaultStub(). The convertTo*() fixes should be trivial...
authorSteph Fox <sfox@php.net>
Thu, 21 Feb 2008 00:24:38 +0000 (00:24 +0000)
committerSteph Fox <sfox@php.net>
Thu, 21 Feb 2008 00:24:38 +0000 (00:24 +0000)
ext/phar/phar.c
ext/phar/phar_object.c
ext/phar/tar.c
ext/phar/tests/tar/phar_setdefaultstub.phpt
ext/phar/tests/zip/phar_setdefaultstub.phpt
ext/phar/zip.c

index e34a37e9cddceeeb3e04a43cfc3e8630fb4ac828..cb7f3c17e93dbe79edd67512a436147fb6f6f88a 100644 (file)
@@ -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";
        }
 
index e60fdb1b4167025bf14c26bfd19df0b0aec8d9e3..aee70b48b2fe209263f0a2e9e0dfff71b352dbc7 100755 (executable)
@@ -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);
index dd754137d2d6c7a5a51d8a3f51a7333dd985e56e..bfd56fd3e0e43c8582d1d681f22decda01312a1d 100644 (file)
@@ -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);
                }
        }
 
index 4dc0898d627c5a8b7536202f58a20b8e648df514..b285cb1c9bd6a34483f05722772998dd9a914864 100644 (file)
@@ -10,6 +10,12 @@ $fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.tar';
 $phar = new Phar($fname);
 $phar['a.php'] = '<php echo "this is a\n"; ?>';
 $phar['b.php'] = '<php echo "this is b\n"; ?>';
+$phar->setStub('<?php echo "Hello World\n"; __HALT_COMPILER(); ?>');
+
+var_dump($phar->getStub());
+
+echo "============================================================================\n";
+echo "============================================================================\n";
 
 try {
        $phar->setDefaultStub();
@@ -50,18 +56,23 @@ var_dump($phar->getStub());
 <?php 
 unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.tar');
 ?>
---EXPECT--
-Stub cannot be changed in a tar-based phar
+--EXPECTF--
+string(51) "<?php echo "Hello World\n"; __HALT_COMPILER(); ?>
+"
+============================================================================
+============================================================================
 string(60) "<?php // tar-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-Stub cannot be changed in a tar-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 1 given in %sphar_setdefaultstub.php on line %d
 string(60) "<?php // tar-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-Stub cannot be changed in a tar-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 2 given in %sphar_setdefaultstub.php on line %d
 string(60) "<?php // tar-based phar archive stub file
 __HALT_COMPILER();"
 ===DONE===
index 0d5acea2f5007a1c7393b79a80d1d815b7501557..10bfc6c475608138a17423a81883271db65a5cbf 100644 (file)
@@ -10,6 +10,12 @@ $fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.zip';
 $phar = new Phar($fname);
 $phar['a.php'] = '<php echo "this is a\n"; ?>';
 $phar['b.php'] = '<php echo "this is b\n"; ?>';
+$phar->setStub('<?php echo "Hello World\n"; __HALT_COMPILER(); ?>');
+
+var_dump($phar->getStub());
+
+echo "============================================================================\n";
+echo "============================================================================\n";
 
 try {
        $phar->setDefaultStub();
@@ -50,18 +56,23 @@ var_dump($phar->getStub());
 <?php 
 unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.zip');
 ?>
---EXPECT--
-Stub cannot be changed in a zip-based phar
+--EXPECTF--
+string(51) "<?php echo "Hello World\n"; __HALT_COMPILER(); ?>
+"
+============================================================================
+============================================================================
 string(60) "<?php // zip-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-Stub cannot be changed in a zip-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 1 given in %sphar_setdefaultstub.php on line %d
 string(60) "<?php // zip-based phar archive stub file
 __HALT_COMPILER();"
 ============================================================================
 ============================================================================
-Stub cannot be changed in a zip-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 2 given in %sphar_setdefaultstub.php on line %d
 string(60) "<?php // zip-based phar archive stub file
 __HALT_COMPILER();"
 ===DONE===
index 8c64083043c8390733e3a2fe541edbf968171d9c..aae781882129142d790d880efe275d1e1ea26f15 100644 (file)
@@ -672,7 +672,6 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, char **er
                entry.uncompressed_filesize = entry.compressed_filesize = phar->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;
                        }