]> granicus.if.org Git - php/commitdiff
combine PharFileInfo->setCompressedGZ/setCompressedBZIP2 into compress() with paramet...
authorGreg Beaver <cellog@php.net>
Fri, 11 Apr 2008 13:26:03 +0000 (13:26 +0000)
committerGreg Beaver <cellog@php.net>
Fri, 11 Apr 2008 13:26:03 +0000 (13:26 +0000)
use ZEND_ACC_PUBLIC in PharFileInfo definitions, to be consistent
[DOC]

ext/phar/phar_object.c
ext/phar/tests/phar_copy.phpt
ext/phar/tests/phar_oo_compressed_001.phpt
ext/phar/tests/phar_oo_compressed_001b.phpt
ext/phar/tests/phar_oo_getcontentsgz.phpt

index 082fb8308e94691482503f06819ab6bd52a7a578..283504e9f50fb210be582fa883ebafb0f093f7f6 100755 (executable)
@@ -3305,7 +3305,7 @@ PHP_METHOD(PharFileInfo, isCompressed)
        /* a number that is not Phar::GZ or Phar::BZ2 */
        long method = 9021976;
        PHAR_ENTRY_OBJECT();
-       
+
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &method) == FAILURE) {
                return;
        }
@@ -3324,28 +3324,6 @@ PHP_METHOD(PharFileInfo, isCompressed)
 }
 /* }}} */
 
-/* {{{ proto bool PharFileInfo::isCompressedGZ()
- * Returns whether the entry is compressed using gz
- */
-PHP_METHOD(PharFileInfo, isCompressedGZ)
-{
-       PHAR_ENTRY_OBJECT();
-       
-       RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ);
-}
-/* }}} */
-
-/* {{{ proto bool PharFileInfo::isCompressedBZIP2()
- * Returns whether the entry is compressed using bzip2
- */
-PHP_METHOD(PharFileInfo, isCompressedBZIP2)
-{
-       PHAR_ENTRY_OBJECT();
-       
-       RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2);
-}
-/* }}} */
-
 /* {{{ proto int PharFileInfo::getCRC32()
  * Returns CRC32 code or throws an exception if not CRC checked
  */
@@ -3572,14 +3550,19 @@ PHP_METHOD(PharFileInfo, getContent)
 }
 /* }}} */
 
-/* {{{ proto int PharFileInfo::setCompressedGZ()
- * Instructs the Phar class to compress the current file using zlib
+/* {{{ proto int PharFileInfo::compress(int compression_type)
+ * Instructs the Phar class to compress the current file using zlib or bzip2 compression
  */
-PHP_METHOD(PharFileInfo, setCompressedGZ)
+PHP_METHOD(PharFileInfo, compress)
 {
+       long method = 9021976;
        char *error;
        PHAR_ENTRY_OBJECT();
 
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
+               return;
+       }
+
        if (entry_obj->ent.entry->is_tar) {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
                        "Cannot compress with Gzip compression, not possible with tar-based phar archives");
@@ -3590,10 +3573,6 @@ PHP_METHOD(PharFileInfo, setCompressedGZ)
                        "Phar entry is a directory, cannot set compression"); \
                return;
        }
-       if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) {
-               RETURN_TRUE;
-               return;
-       }
        if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
                        "Phar is readonly, cannot change compression");
@@ -3604,65 +3583,64 @@ PHP_METHOD(PharFileInfo, setCompressedGZ)
                        "Cannot compress deleted file");
                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;
-       }
-       entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
-       entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
-       entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_GZ;
-       entry_obj->ent.entry->phar->is_modified = 1;
-       entry_obj->ent.entry->is_modified = 1;
 
-       phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
-       if (error) {
-               zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, error);
-               efree(error);
+       switch (method) {
+               case PHAR_ENT_COMPRESSED_GZ:
+                       if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) {
+                               RETURN_TRUE;
+                               return;
+                       }
+                       if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0) {
+                               if (!phar_has_bz2) {
+                                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                                               "Cannot compress with gzip compression, file is already compressed with bzip2 compression and bz2 extension is not enabled, cannot decompress");
+                                       return;
+                               }
+                               /* decompress this file indirectly */
+                               if (SUCCESS != phar_open_entry_fp(entry_obj->ent.entry, &error TSRMLS_CC)) {
+                                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                                               "Phar error: Cannot decompress bzip2-compressed file \"%s\" in phar \"%s\" in order to compress with gzip: %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error);
+                                       efree(error);
+                                       return;
+                               }
+                       }
+                       if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !phar_has_zlib) {
+                               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                                       "Cannot compress with gzip compression, zlib extension is not enabled");
+                               return;
+                       }
+                       entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
+                       entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
+                       entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_GZ;
+                       break;
+               case PHAR_ENT_COMPRESSED_BZ2:
+                       if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
+                               RETURN_TRUE;
+                               return;
+                       }
+                       if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0) {
+                               if (!phar_has_zlib) {
+                                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                                               "Cannot compress with bzip2 compression, file is already compressed with gzip compression and zlib extension is not enabled, cannot decompress");
+                                       return;
+                               }
+                               /* decompress this file indirectly */
+                               if (SUCCESS != phar_open_entry_fp(entry_obj->ent.entry, &error TSRMLS_CC)) {
+                                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
+                                               "Phar error: Cannot decompress gzip-compressed file \"%s\" in phar \"%s\" in order to compress with bzip2: %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error);
+                                       efree(error);
+                                       return;
+                               }
+                       }
+                       entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
+                       entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
+                       entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_BZ2;
+                       break;
+               default:
+                       zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
+                               "Unknown compression type specified"); \
        }
-       RETURN_TRUE;
-}
-/* }}} */
 
-/* {{{ proto int PharFileInfo::setCompressedBZIP2()
- * Instructs the Phar class to compress the current file using bzip2
- */
-PHP_METHOD(PharFileInfo, setCompressedBZIP2)
-{
-       char *error;
-       PHAR_ENTRY_OBJECT();
-
-       if (entry_obj->ent.entry->is_tar) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress with Bzip2 compression, not possible with tar-based phar archives");
-               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;
-       }
-       if (entry_obj->ent.entry->is_dir) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
-                       "Phar entry is a directory, cannot set compression"); \
-               return;
-       }
-       if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
-               RETURN_TRUE;
-       }
-       if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Phar is readonly, cannot change compression");
-               return;
-       }
-       if (entry_obj->ent.entry->is_deleted) {
-               zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
-                       "Cannot compress deleted file");
-               return;
-       }
-       entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
-       entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
-       entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_BZ2;
        entry_obj->ent.entry->phar->is_modified = 1;
        entry_obj->ent.entry->is_modified = 1;
 
@@ -3703,12 +3681,12 @@ PHP_METHOD(PharFileInfo, decompress)
                        "Cannot compress deleted file");
                return;
        }
-       if (!phar_has_zlib) {
+       if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !phar_has_zlib) {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
                        "Cannot decompress Gzip-compressed file, zlib extension is not enabled");
                return;
        }
-       if (!phar_has_bz2) {
+       if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0 && !phar_has_bz2) {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
                        "Cannot decompress Bzip2-compressed file, bz2 extension is not enabled");
                return;
@@ -3801,6 +3779,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comp, 0, 0, 1)
        ZEND_ARG_INFO(0, compression_type)
 ZEND_END_ARG_INFO();
 
+static
+ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_compo, 0, 0, 0)
+       ZEND_ARG_INFO(0, compression_type)
+ZEND_END_ARG_INFO();
+
 static
 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_copy, 0, 0, 2)
        ZEND_ARG_INFO(0, newfile)
@@ -3942,22 +3925,21 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_entry_chmod, 0, 0, 1)
 ZEND_END_ARG_INFO();
 
 zend_function_entry php_entry_methods[] = {
-       PHP_ME(PharFileInfo, __construct,        arginfo_entry___construct,  0)
-       PHP_ME(PharFileInfo, __destruct,         NULL,                       0)
-       PHP_ME(PharFileInfo, chmod,              arginfo_entry_chmod,        0)
-       PHP_ME(PharFileInfo, delMetadata,        NULL,                       0)
-       PHP_ME(PharFileInfo, getContent,         NULL,                       0)
-       PHP_ME(PharFileInfo, getCompressedSize,  NULL,                       0)
-       PHP_ME(PharFileInfo, getCRC32,           NULL,                       0)
-       PHP_ME(PharFileInfo, getMetadata,        NULL,                       0)
-       PHP_ME(PharFileInfo, getPharFlags,       NULL,                       0)
-       PHP_ME(PharFileInfo, hasMetadata,        NULL,                       0)
-       PHP_ME(PharFileInfo, isCompressed,       arginfo_phar_comp,          0)
-       PHP_ME(PharFileInfo, isCRCChecked,       NULL,                       0)
-       PHP_ME(PharFileInfo, setCompressedBZIP2, NULL,                       0)
-       PHP_ME(PharFileInfo, setCompressedGZ,    NULL,                       0)
-       PHP_ME(PharFileInfo, setMetadata,        arginfo_phar_setMetadata,   0)
-       PHP_ME(PharFileInfo, decompress,         NULL,                       0)
+       PHP_ME(PharFileInfo, __construct,        arginfo_entry___construct,  ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, __destruct,         NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, chmod,              arginfo_entry_chmod,        ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, delMetadata,        NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, getContent,         NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, getCompressedSize,  NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, getCRC32,           NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, getMetadata,        NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, getPharFlags,       NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, hasMetadata,        NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, isCompressed,       arginfo_phar_compo,         ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, isCRCChecked,       NULL,                       ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, compress,           arginfo_phar_comp,          ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, setMetadata,        arginfo_phar_setMetadata,   ZEND_ACC_PUBLIC)
+       PHP_ME(PharFileInfo, decompress,         NULL,                       ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };
 #endif /* HAVE_SPL */
index 372d850cf724a85c8734804f3dd4557e39eca9c0..74a342dbb2a598a1e04a48f12e935fbe17f6d3ea 100644 (file)
@@ -25,7 +25,7 @@ try
        $p->startBuffering();
        $p->copy('a', 'b');
        echo file_get_contents($p['b']->getPathName());
-       $p['a']->setCompressedGZ();
+       $p['a']->compress(Phar::GZ);
        $p['b']->setMetadata('a');
        $p->copy('b', 'c');
        $p->stopBuffering();
index 9db5dedfd7fd067483a47649e24fb82510733066..af02012573e9d06dcd10d52980ae494406a2ef53 100644 (file)
@@ -1,5 +1,5 @@
 --TEST--
-Phar::setCompressedGZ()
+Phar: PharFileInfo::compress(Phar::GZ)
 --SKIPIF--
 <?php if (!extension_loaded("phar")) die("skip"); ?>
 <?php if (!extension_loaded("zlib")) die("skip zlib not present"); ?>
@@ -31,7 +31,7 @@ var_dump($phar['c']->isCompressed());
 $phar['a'] = 'new a';
 $phar['a']->decompress();
 $phar['b'] = 'new b';
-$phar['b']->setCompressedGZ();
+$phar['b']->compress(Phar::GZ);
 $phar['d'] = 'new d';
 
 $phar = new Phar($fname);
index 3171244912fcfc886d1092d952dd5bec5c4b45ed..6d4c73286220bc0a17993bd077c5003ca8f19bab 100755 (executable)
@@ -1,5 +1,5 @@
 --TEST--
-Phar::setCompressedBZip2()
+Phar: PharFileInfo::compress(Phar::BZ2)
 --SKIPIF--
 <?php if (!extension_loaded("phar")) die("skip"); ?>
 <?php if (!extension_loaded("bz2")) die("skip bz2 not present"); ?>
@@ -31,7 +31,7 @@ var_dump($phar['c']->isCompressed());
 $phar['a'] = 'new a';
 $phar['a']->decompress();
 $phar['b'] = 'new b';
-$phar['b']->setCompressedBZip2();
+$phar['b']->compress(Phar::BZ2);
 $phar['d'] = 'new d';
 
 $phar = new Phar($fname);
index d63480d0177ad4b4f5c6ecccf27b0e0f408d89f5..a480a6963779d3b80d4f57fd1998d13d728f80b8 100644 (file)
@@ -14,7 +14,7 @@ $fname2 = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.2.phar.php';
 $phar = new Phar($fname);
 $phar['a'] = 'file contents
 this works';
-$phar['a']->setCompressedGZ();
+$phar['a']->compress(Phar::GZ);
 copy($fname, $fname2);
 $phar2 = new Phar($fname2);
 var_dump($phar2['a']->isCompressed());