From e56beeeefd8c8c35fb5044f4e2d6857b463e6b23 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Wed, 9 Apr 2008 18:12:41 +0000 Subject: [PATCH] add PharFileInfo->getContents() to retrieve file contents [DOC] --- ext/phar/phar_object.c | 32 ++++++++++++++++++++ ext/phar/tests/phar_oo_compressallbz2.phpt | 1 + ext/phar/tests/phar_oo_compressallgz.phpt | 1 + ext/phar/tests/phar_oo_getcontents.phpt | 26 +++++++++++++++++ ext/phar/tests/phar_oo_getcontentsgz.phpt | 34 ++++++++++++++++++++++ ext/phar/tests/phar_oo_getmodified.phpt | 1 + ext/phar/tests/phar_oo_uncompressall.phpt | 1 + 7 files changed, 96 insertions(+) create mode 100644 ext/phar/tests/phar_oo_getcontents.phpt create mode 100644 ext/phar/tests/phar_oo_getcontentsgz.phpt diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index ce2356b5e4..bc7f7ec065 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -3466,6 +3466,37 @@ PHP_METHOD(PharFileInfo, delMetadata) } /* }}} */ +/* {{{ proto string PharFileInfo::getContents() + * return the complete file contents of the entry (like file_get_contents) + */ +PHP_METHOD(PharFileInfo, getContents) +{ + char *error; + php_stream *fp; + PHAR_ENTRY_OBJECT(); + + if (entry_obj->ent.entry->is_dir) { + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, + "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\" is a directory", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname); + return; + } + 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 retrieve contents, \"%s\" in phar \"%s\": %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error); + efree(error); + return; + } + if (!(fp = phar_get_efp(entry_obj->ent.entry TSRMLS_CC))) { + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, + "Phar error: Cannot retrieve contents of \"%s\" in phar \"%s\"", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname); + return; + } + phar_seek_efp(entry_obj->ent.entry, 0, SEEK_SET, 0 TSRMLS_CC); + Z_TYPE_P(return_value) = IS_STRING; + Z_STRLEN_P(return_value) = php_stream_copy_to_mem(fp, &(Z_STRVAL_P(return_value)), entry_obj->ent.entry->uncompressed_filesize, 0); +} +/* }}} */ + /* {{{ proto int PharFileInfo::setCompressedGZ() * Instructs the Phar class to compress the current file using zlib */ @@ -3822,6 +3853,7 @@ zend_function_entry php_entry_methods[] = { PHP_ME(PharFileInfo, __destruct, NULL, 0) PHP_ME(PharFileInfo, chmod, arginfo_entry_chmod, 0) PHP_ME(PharFileInfo, delMetadata, NULL, 0) + PHP_ME(PharFileInfo, getContents, NULL, 0) PHP_ME(PharFileInfo, getCompressedSize, NULL, 0) PHP_ME(PharFileInfo, getCRC32, NULL, 0) PHP_ME(PharFileInfo, getMetadata, NULL, 0) diff --git a/ext/phar/tests/phar_oo_compressallbz2.phpt b/ext/phar/tests/phar_oo_compressallbz2.phpt index 7120f6411d..ef91272cdb 100644 --- a/ext/phar/tests/phar_oo_compressallbz2.phpt +++ b/ext/phar/tests/phar_oo_compressallbz2.phpt @@ -45,6 +45,7 @@ var_dump($phar['b']->isCompressedBZIP2()); --CLEAN-- --EXPECTF-- string(1) "a" diff --git a/ext/phar/tests/phar_oo_compressallgz.phpt b/ext/phar/tests/phar_oo_compressallgz.phpt index 71274491f1..3cce2b4363 100644 --- a/ext/phar/tests/phar_oo_compressallgz.phpt +++ b/ext/phar/tests/phar_oo_compressallgz.phpt @@ -45,6 +45,7 @@ var_dump($phar['b']->isCompressedBZIP2()); --CLEAN-- --EXPECTF-- string(1) "a" diff --git a/ext/phar/tests/phar_oo_getcontents.phpt b/ext/phar/tests/phar_oo_getcontents.phpt new file mode 100644 index 0000000000..3f093adb31 --- /dev/null +++ b/ext/phar/tests/phar_oo_getcontents.phpt @@ -0,0 +1,26 @@ +--TEST-- +Phar object: getContents() +--SKIPIF-- + + +--INI-- +phar.readonly=0 +--FILE-- +getContents() . "\n"; +?> +===DONE=== +--CLEAN-- + +--EXPECT-- +file contents +this works +===DONE=== \ No newline at end of file diff --git a/ext/phar/tests/phar_oo_getcontentsgz.phpt b/ext/phar/tests/phar_oo_getcontentsgz.phpt new file mode 100644 index 0000000000..c2a16f23d7 --- /dev/null +++ b/ext/phar/tests/phar_oo_getcontentsgz.phpt @@ -0,0 +1,34 @@ +--TEST-- +Phar object: getContents() (verify it works with compression) +--SKIPIF-- + + + +--INI-- +phar.readonly=0 +--FILE-- +setCompressedGZ(); +copy($fname, $fname2); +$phar2 = new Phar($fname2); +var_dump($phar2['a']->isCompressed()); +echo $phar2['a']->getContents() . "\n"; +?> +===DONE=== +--CLEAN-- + +--EXPECT-- +bool(true) +file contents +this works +===DONE=== \ No newline at end of file diff --git a/ext/phar/tests/phar_oo_getmodified.phpt b/ext/phar/tests/phar_oo_getmodified.phpt index c537291631..90eb056fa2 100644 --- a/ext/phar/tests/phar_oo_getmodified.phpt +++ b/ext/phar/tests/phar_oo_getmodified.phpt @@ -28,6 +28,7 @@ var_dump($phar->getModified()); --CLEAN-- --EXPECTF-- bool(false) diff --git a/ext/phar/tests/phar_oo_uncompressall.phpt b/ext/phar/tests/phar_oo_uncompressall.phpt index b447e96d02..4e3e73875e 100644 --- a/ext/phar/tests/phar_oo_uncompressall.phpt +++ b/ext/phar/tests/phar_oo_uncompressall.phpt @@ -53,6 +53,7 @@ var_dump($phar['a']->isCompressed()); --CLEAN-- --EXPECTF-- string(1) "a" -- 2.50.1