]> granicus.if.org Git - php/commitdiff
re-factor all-file compression. Remove compressAllFilesGZ/compressAllFilesBZ2 in...
authorGreg Beaver <cellog@php.net>
Thu, 10 Apr 2008 13:40:26 +0000 (13:40 +0000)
committerGreg Beaver <cellog@php.net>
Thu, 10 Apr 2008 13:40:26 +0000 (13:40 +0000)
unifying compressFiles which accepts Phar::GZ or Phar::BZ2 as argument.  rename uncompressAllFiles to decompressFiles

ext/phar/phar_object.c
ext/phar/tests/phar_oo_compressallbz2.phpt
ext/phar/tests/phar_oo_compressallgz.phpt
ext/phar/tests/phar_oo_getmodified.phpt
ext/phar/tests/phar_oo_uncompressall.phpt

index 28895f20e6c8d911d683f5e16e0b5f70982247bb..3ea15a613ccec1c09856849954b8b7f1c0d711f3 100755 (executable)
@@ -1908,7 +1908,7 @@ PHP_METHOD(Phar, compress)
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
                return;
        }
-
+       
        if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
                zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
                        "Cannot compress phar archive, phar is read-only");
@@ -1917,7 +1917,7 @@ PHP_METHOD(Phar, compress)
 
        if (phar_obj->arc.archive->is_zip) {
                zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
-                       "Cannot compress zip-based archives with whole-archive compression");
+                       "Cannot compress zipbased archives with whole-archive compression");
                return;
        }
 
@@ -1933,7 +1933,7 @@ PHP_METHOD(Phar, compress)
                        }
                        flags = PHAR_FILE_COMPRESSED_GZ;
                        break;
-
+       
                case PHAR_ENT_COMPRESSED_BZ2:
                        if (!phar_has_bz2) {
                                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
@@ -1961,6 +1961,7 @@ PHP_METHOD(Phar, compress)
 }
 /* }}} */
 
+
 /* {{{ proto object Phar::convertToPhar([string file_ext])
  * Convert a phar.tar or phar.zip archive to the phar file format. The 
  * optional parameter allows the user to determine the new
@@ -2601,75 +2602,67 @@ static int pharobj_cancompress(HashTable *manifest TSRMLS_DC) /* {{{ */
 }
 /* }}} */
 
-/* {{{ proto bool Phar::compressAllFilesGZ()
- * compress every file with GZip compression
+/* {{{ proto object Phar::compressFiles(int method)
+ * Compress all files within a phar or zip archive using the specified compression
+ * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
+ * the kind of compression desired
  */
-PHP_METHOD(Phar, compressAllFilesGZ)
+PHP_METHOD(Phar, compressFiles)
 {
        char *error;
+       php_uint32 flags;
+       long method;
        PHAR_ARCHIVE_OBJECT();
 
-       if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Phar is readonly, cannot change compression");
-               return;
-       }
-       if (!phar_has_zlib) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress with Gzip compression, zlib extension is not enabled");
-               return;
-       }
-       if (phar_obj->arc.archive->is_tar) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress with Gzip compression, tar archives cannot compress individual files, use compress() to compress the whole archive");
-               return;
-       }
-       if (!pharobj_cancompress(&phar_obj->arc.archive->manifest TSRMLS_CC)) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress all files as Gzip, some are compressed as bzip2 and cannot be uncompressed");
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
                return;
        }
-       pharobj_set_compression(&phar_obj->arc.archive->manifest, PHAR_ENT_COMPRESSED_GZ TSRMLS_CC);
-
-       phar_obj->arc.archive->is_modified = 1;
-
-       phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
-       if (error) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, error);
-               efree(error);
-       }
-}
-/* }}} */
-
-/* {{{ proto bool Phar::compressAllFilesBZIP2()
- * compress every file with BZip2 compression
- */
-PHP_METHOD(Phar, compressAllFilesBZIP2)
-{
-       char *error;
-       PHAR_ARCHIVE_OBJECT();
 
        if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
                        "Phar is readonly, cannot change compression");
                return;
        }
-       if (!phar_has_bz2) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress with Bzip2 compression, bz2 extension is not enabled");
-               return;
+
+       switch (method) {
+               case PHAR_ENT_COMPRESSED_GZ:
+                       if (!phar_has_zlib) {
+                               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                                       "Cannot compress files within archive with gzip, enable ext/zlib in php.ini");
+                               return;
+                       }
+                       flags = PHAR_ENT_COMPRESSED_GZ;
+                       break;
+
+               case PHAR_ENT_COMPRESSED_BZ2:
+                       if (!phar_has_bz2) {
+                               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                                       "Cannot compress files within archive with bz2, enable ext/bz2 in php.ini");
+                               return;
+                       }
+                       flags = PHAR_ENT_COMPRESSED_BZ2;
+                       break;
+               default:
+                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                               "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
+                       return;
        }
        if (phar_obj->arc.archive->is_tar) {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress with Bzip2 compression, tar archives cannot compress individual files, use compress() to compress the whole archive");
+                       "Cannot compress with Gzip compression, tar archives cannot compress individual files, use compress() to compress the whole archive");
                return;
        }
        if (!pharobj_cancompress(&phar_obj->arc.archive->manifest TSRMLS_CC)) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress all files as Bzip2, some are compressed as gzip and cannot be uncompressed");
+               if (flags == PHAR_FILE_COMPRESSED_GZ) {
+                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                               "Cannot compress all files as Gzip, some are compressed as bzip2 and cannot be decompressed");
+               } else {
+                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                               "Cannot compress all files as Bzip2, some are compressed as gzip and cannot be decompressed");
+               }
                return;
        }
-       pharobj_set_compression(&phar_obj->arc.archive->manifest, PHAR_ENT_COMPRESSED_BZ2 TSRMLS_CC);
+       pharobj_set_compression(&phar_obj->arc.archive->manifest, flags TSRMLS_CC);
 
        phar_obj->arc.archive->is_modified = 1;
 
@@ -2681,10 +2674,10 @@ PHP_METHOD(Phar, compressAllFilesBZIP2)
 }
 /* }}} */
 
-/* {{{ proto bool Phar::uncompressAllFiles()
- * uncompress every file
+/* {{{ proto bool Phar::decompressFiles()
+ * decompress every file
  */
-PHP_METHOD(Phar, uncompressAllFiles)
+PHP_METHOD(Phar, decompressFiles)
 {
        char *error;
        PHAR_ARCHIVE_OBJECT();
@@ -2696,7 +2689,7 @@ PHP_METHOD(Phar, uncompressAllFiles)
        }
        if (!pharobj_cancompress(&phar_obj->arc.archive->manifest TSRMLS_CC)) {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot uncompress all files, some are compressed as bzip2 or gzip and cannot be uncompressed");
+                       "Cannot decompress all files, some are compressed as bzip2 or gzip and cannot be decompressed");
                return;
        }
        if (phar_obj->arc.archive->is_tar) {
@@ -3928,8 +3921,8 @@ zend_function_entry php_archive_methods[] = {
        PHP_ME(Phar, addFromString,         arginfo_phar_fromstring,   ZEND_ACC_PUBLIC)
        PHP_ME(Phar, buildFromIterator,     arginfo_phar_build,        ZEND_ACC_PUBLIC)
        PHP_ME(Phar, compress,              arginfo_phar_comp,         ZEND_ACC_PUBLIC)
-       PHP_ME(Phar, compressAllFilesBZIP2, NULL,                      ZEND_ACC_PUBLIC)
-       PHP_ME(Phar, compressAllFilesGZ,    NULL,                      ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, compressFiles,         arginfo_phar_comp,         ZEND_ACC_PUBLIC)
+       PHP_ME(Phar, decompressFiles,       NULL,                      ZEND_ACC_PUBLIC)
        PHP_ME(Phar, convertToPhar,         arginfo_phar_conv,         ZEND_ACC_PUBLIC)
        PHP_ME(Phar, convertToTar,          arginfo_phar_conv,         ZEND_ACC_PUBLIC)
        PHP_ME(Phar, convertToZip,          arginfo_phar_conv,         ZEND_ACC_PUBLIC)
@@ -3964,7 +3957,6 @@ zend_function_entry php_archive_methods[] = {
        PHP_ME(Phar, setStub,               arginfo_phar_setStub,      ZEND_ACC_PUBLIC)
        PHP_ME(Phar, startBuffering,        NULL,                      ZEND_ACC_PUBLIC)
        PHP_ME(Phar, stopBuffering,         NULL,                      ZEND_ACC_PUBLIC)
-       PHP_ME(Phar, uncompressAllFiles,    NULL,                      ZEND_ACC_PUBLIC)
 #endif
        /* static member functions */
        PHP_ME(Phar, apiVersion,            NULL,                      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
index ef91272cdbc86c63a23d4d4ddbc7f84e4297f453..dc1d5c6e30010ebdd197599a0fba4fd4bb1e3c05 100644 (file)
@@ -1,5 +1,5 @@
 --TEST--
-Phar::compressAllFilesBZIP2()
+Phar::compressFiles(Phar::BZ2)
 --SKIPIF--
 <?php if (!extension_loaded("phar")) die("skip"); ?>
 <?php if (!extension_loaded("bz2")) die("skip bz2 not present"); ?>
@@ -29,7 +29,7 @@ var_dump(file_get_contents($pname . '/c'));
 var_dump($phar['c']->isCompressed());
 
 $phar = new Phar($fname);
-$phar->compressAllFilesBZIP2();
+$phar->compressFiles(Phar::BZ2);
 var_dump(file_get_contents($pname . '/a'));
 var_dump($phar['a']->isCompressedGZ());
 var_dump($phar['a']->isCompressedBZIP2());
index 3cce2b4363c8b23ddadc8edd73a361b5074bc722..221a376aea95e30d132e77e99b9346dc2b740bc6 100644 (file)
@@ -1,5 +1,5 @@
 --TEST--
-Phar::compressAllFilesGZ()
+Phar::compressFiles(Phar::GZ)
 --SKIPIF--
 <?php if (!extension_loaded("phar")) die("skip"); ?>
 <?php if (!extension_loaded("zlib")) die("skip zlib not present"); ?>
@@ -29,7 +29,7 @@ var_dump(file_get_contents($pname . '/c'));
 var_dump($phar['c']->isCompressed());
 
 $phar = new Phar($fname);
-$phar->compressAllFilesGZ();
+$phar->compressFiles(Phar::GZ);
 var_dump(file_get_contents($pname . '/a'));
 var_dump($phar['a']->isCompressedGZ());
 var_dump($phar['a']->isCompressedBZIP2());
index 90eb056fa2a6bada8883cc23f85c8c99856664fc..d531393281bee674f34926e075393dd9fc69cd2f 100644 (file)
@@ -21,7 +21,7 @@ include 'files/phar_test.inc';
 
 $phar = new Phar($fname);
 var_dump($phar->getModified());
-$phar->compressAllFilesGZ();
+$phar->compressFiles(Phar::GZ);
 var_dump($phar->getModified());
 ?>
 ===DONE===
index 4e3e73875e27efb679d17d7767789b3668ba54fb..deaadb37c10b8e1f88369705c1103d8b8950f4a9 100644 (file)
@@ -1,5 +1,5 @@
 --TEST--
-Phar::uncompressAllFiles()
+Phar::decompressAllFiles()
 --SKIPIF--
 <?php if (!extension_loaded("phar")) die("skip"); ?>
 <?php if (!extension_loaded("zlib")) die("skip zlib not present"); ?>
@@ -29,7 +29,7 @@ var_dump(file_get_contents($pname . '/c'));
 var_dump($phar['c']->isCompressed());
 
 $phar = new Phar($fname);
-$phar->compressAllFilesGZ();
+$phar->compressFiles(Phar::GZ);
 var_dump(file_get_contents($pname . '/a'));
 var_dump($phar['a']->isCompressedGZ());
 var_dump($phar['a']->isCompressedBZIP2());
@@ -40,7 +40,7 @@ var_dump(file_get_contents($pname . '/c'));
 var_dump($phar['c']->isCompressedGZ());
 var_dump($phar['b']->isCompressedBZIP2());
 
-$phar->uncompressAllFiles();
+$phar->decompressFiles();
 var_dump(file_get_contents($pname . '/a'));
 var_dump($phar['a']->isCompressed());
 var_dump(file_get_contents($pname . '/b'));