From 61b5fc48e33d8c42ec4b03561670d0d3245e5b13 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Thu, 10 Apr 2008 13:40:26 +0000 Subject: [PATCH] re-factor all-file compression. Remove compressAllFilesGZ/compressAllFilesBZ2 in favor of unifying compressFiles which accepts Phar::GZ or Phar::BZ2 as argument. rename uncompressAllFiles to decompressFiles --- ext/phar/phar_object.c | 108 ++++++++++----------- ext/phar/tests/phar_oo_compressallbz2.phpt | 4 +- ext/phar/tests/phar_oo_compressallgz.phpt | 4 +- ext/phar/tests/phar_oo_getmodified.phpt | 2 +- ext/phar/tests/phar_oo_uncompressall.phpt | 6 +- 5 files changed, 58 insertions(+), 66 deletions(-) diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 28895f20e6..3ea15a613c 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -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) diff --git a/ext/phar/tests/phar_oo_compressallbz2.phpt b/ext/phar/tests/phar_oo_compressallbz2.phpt index ef91272cdb..dc1d5c6e30 100644 --- a/ext/phar/tests/phar_oo_compressallbz2.phpt +++ b/ext/phar/tests/phar_oo_compressallbz2.phpt @@ -1,5 +1,5 @@ --TEST-- -Phar::compressAllFilesBZIP2() +Phar::compressFiles(Phar::BZ2) --SKIPIF-- @@ -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()); diff --git a/ext/phar/tests/phar_oo_compressallgz.phpt b/ext/phar/tests/phar_oo_compressallgz.phpt index 3cce2b4363..221a376aea 100644 --- a/ext/phar/tests/phar_oo_compressallgz.phpt +++ b/ext/phar/tests/phar_oo_compressallgz.phpt @@ -1,5 +1,5 @@ --TEST-- -Phar::compressAllFilesGZ() +Phar::compressFiles(Phar::GZ) --SKIPIF-- @@ -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()); diff --git a/ext/phar/tests/phar_oo_getmodified.phpt b/ext/phar/tests/phar_oo_getmodified.phpt index 90eb056fa2..d531393281 100644 --- a/ext/phar/tests/phar_oo_getmodified.phpt +++ b/ext/phar/tests/phar_oo_getmodified.phpt @@ -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=== diff --git a/ext/phar/tests/phar_oo_uncompressall.phpt b/ext/phar/tests/phar_oo_uncompressall.phpt index 4e3e73875e..deaadb37c1 100644 --- a/ext/phar/tests/phar_oo_uncompressall.phpt +++ b/ext/phar/tests/phar_oo_uncompressall.phpt @@ -1,5 +1,5 @@ --TEST-- -Phar::uncompressAllFiles() +Phar::decompressAllFiles() --SKIPIF-- @@ -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')); -- 2.50.1