From 0db641af3ef109c3aec054c25907ab6d1090bb68 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Thu, 24 Apr 2008 19:57:22 +0000 Subject: [PATCH] rework PharFileInfo->decompress and add failing test --- ext/phar/phar_object.c | 20 +++-- ext/phar/tests/pharfileinfo_compression.phpt | 88 ++++++++++++++++++++ 2 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 ext/phar/tests/pharfileinfo_compression.phpt diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 019bc1b6926..810da7a7343 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -3741,7 +3741,7 @@ PHP_METHOD(PharFileInfo, compress) return; } } - if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !phar_has_zlib) { + 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; @@ -3769,6 +3769,11 @@ PHP_METHOD(PharFileInfo, compress) 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; + } 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; @@ -3795,8 +3800,7 @@ PHP_METHOD(PharFileInfo, compress) */ PHP_METHOD(PharFileInfo, decompress) { - char *fname, *error; - int fname_len; + char *error; PHAR_ENTRY_OBJECT(); if (entry_obj->ent.entry->is_dir) { @@ -3810,7 +3814,7 @@ PHP_METHOD(PharFileInfo, decompress) } 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"); + "Phar is readonly, cannot decompress"); return; } if (entry_obj->ent.entry->is_deleted) { @@ -3829,9 +3833,11 @@ PHP_METHOD(PharFileInfo, decompress) return; } if (!entry_obj->ent.entry->fp) { - fname_len = spprintf(&fname, 0, "phar://%s/%s", entry_obj->ent.entry->phar->fname, entry_obj->ent.entry->filename); - entry_obj->ent.entry->fp = php_stream_open_wrapper_ex(fname, "rb", 0, 0, 0); - efree(fname); + if (FAILURE == phar_open_archive_fp(entry_obj->ent.entry->phar TSRMLS_CC)) { + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot decompress entry \"%s\", phar error: Cannot open phar archive \"%s\" for reading", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname); + return; + } + entry_obj->ent.entry->fp_type = PHAR_FP; } entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags; entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK; diff --git a/ext/phar/tests/pharfileinfo_compression.phpt b/ext/phar/tests/pharfileinfo_compression.phpt new file mode 100644 index 00000000000..694846b80fa --- /dev/null +++ b/ext/phar/tests/pharfileinfo_compression.phpt @@ -0,0 +1,88 @@ +--TEST-- +Phar: PharFileInfo compression-related methods +--SKIPIF-- + + + +--INI-- +phar.readonly=0 +--FILE-- +isCompressed(array()); +try { +$b->isCompressed(25); +} catch (Exception $e) { +echo $e->getMessage() . "\n"; +} +try { +$b->compress(25); +} catch (Exception $e) { +echo $e->getMessage() . "\n"; +} +$tar = $phar->convertToData(Phar::TAR); + +$c = $tar['a/b']; +try { +$c->compress(Phar::GZ); +} catch (Exception $e) { +echo $e->getMessage() . "\n"; +} +try { +$phar['a']->compress(Phar::GZ); +} catch (Exception $e) { +echo $e->getMessage() . "\n"; +} +ini_set('phar.readonly', 1); +try { +$b->compress(Phar::GZ); +} catch (Exception $e) { +echo $e->getMessage() . "\n"; +} +ini_set('phar.readonly', 0); +var_dump($b->compress(Phar::GZ)); +var_dump($b->compress(Phar::GZ)); +var_dump($b->compress(Phar::BZ2)); +var_dump($b->compress(Phar::BZ2)); + +echo "decompress\n"; + +try { +$phar['a/b']->decompress(); +} catch (Exception $e) { +echo $e->getMessage() . "\n"; +} +ini_set('phar.readonly', 1); +try { +$b->decompress(); +} catch (Exception $e) { +echo $e->getMessage() . "\n"; +} +ini_set('phar.readonly', 0); + +?> +===DONE=== +--CLEAN-- + + +--EXPECT-- +Warning: PharFileInfo::isCompressed() expects parameter 1 to be long, array given in %spharfileinfo_compression.php on line 11 +Unknown compression type specified +Unknown compression type specified +Cannot compress with Gzip compression, not possible with tar-based phar archives +Phar entry is a directory, cannot set compression +Phar is readonly, cannot change compression +bool(true) +bool(true) +bool(true) +bool(true) +decompress +===DONE=== \ No newline at end of file