From 2513903e405a3aed3fda66ae198e7286e3f27bd6 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Sun, 28 Jan 2007 03:25:53 +0000 Subject: [PATCH] stream context for specifying meta-data --- ext/phar/TODO | 2 +- ext/phar/phar.c | 45 ++++++++++++++++++++++---------- ext/phar/phar_object.c | 4 ++- ext/phar/tests/phar_ctx_001.phpt | 42 +++++++++++++++++++++++++---- 4 files changed, 72 insertions(+), 21 deletions(-) diff --git a/ext/phar/TODO b/ext/phar/TODO index e93b6254cf..3a2ddaf18d 100644 --- a/ext/phar/TODO +++ b/ext/phar/TODO @@ -13,7 +13,7 @@ Version 1.0.0 X docs on file format/manifest description * docs on uses X stream context for specifying compression of a file [Marcus] - * stream context for specifying meta-data + X stream context for specifying meta-data [Greg] X Phar->begin()/Phar->commitWrite() for specifying a new stub to the phar, and deferring flush until all modifications are complete [Greg] X Phar->getStub() for retrieving the stub of the phar [Marcus] diff --git a/ext/phar/phar.c b/ext/phar/phar.c index 8999385c5c..c4398000d0 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -1312,11 +1312,12 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, char *pat char *error; char *filter_name; char tmpbuf[8]; + HashTable *pharcontext; php_url *resource = NULL; php_stream *fp, *fpf; php_stream_filter *filter/*, *consumed */; php_uint32 offset, read, total, toread; - zval **pzoption; + zval **pzoption, *metadata; resource = php_url_parse(path); @@ -1350,20 +1351,34 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, char *pat php_url_free(resource); return NULL; } - efree(error); + if (error) { + efree(error); + } fpf = php_stream_alloc(&phar_ops, idata, NULL, mode); php_url_free(resource); efree(internal_file); - if (idata->internal_file->uncompressed_filesize == 0 - && idata->internal_file->compressed_filesize == 0 - && context && context->options - && zend_hash_find(HASH_OF(context->options), "phar", sizeof("phar"), (void**)&pzoption) == SUCCESS - && zend_hash_find(HASH_OF(*pzoption), "compress", sizeof("compress"), (void**)&pzoption) == SUCCESS - && Z_TYPE_PP(pzoption) == IS_LONG - && (Z_LVAL_PP(pzoption) & ~PHAR_ENT_COMPRESSION_MASK) == 0 - ) { - idata->internal_file->flags &= ~PHAR_ENT_COMPRESSION_MASK; - idata->internal_file->flags |= Z_LVAL_PP(pzoption); + if (context && context->options && zend_hash_find(HASH_OF(context->options), "phar", sizeof("phar"), (void**)&pzoption) == SUCCESS) { + pharcontext = HASH_OF(*pzoption); + if (idata->internal_file->uncompressed_filesize == 0 + && idata->internal_file->compressed_filesize == 0 + && zend_hash_find(pharcontext, "compress", sizeof("compress"), (void**)&pzoption) == SUCCESS + && Z_TYPE_PP(pzoption) == IS_LONG + && (Z_LVAL_PP(pzoption) & ~PHAR_ENT_COMPRESSION_MASK) == 0 + ) { + idata->internal_file->flags &= ~PHAR_ENT_COMPRESSION_MASK; + idata->internal_file->flags |= Z_LVAL_PP(pzoption); + } + if (zend_hash_find(pharcontext, "metadata", sizeof("metadata"), (void**)&pzoption) == SUCCESS) { + if (idata->internal_file->metadata) { + zval_ptr_dtor(&idata->internal_file->metadata); + idata->internal_file->metadata = NULL; + } + + MAKE_STD_ZVAL(idata->internal_file->metadata); + metadata = *pzoption; + ZVAL_ZVAL(idata->internal_file->metadata, metadata, 1, 0); + idata->phar->is_modified = 1; + } } return fpf; } else { @@ -1992,7 +2007,7 @@ int phar_flush(phar_archive_data *archive, char *user_stub, long len TSRMLS_DC) phar_set_32(entry_buffer+16, entry->flags); phar_set_32(entry_buffer+20, metadata_str.len); if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer)) - || metadata_str.len != php_stream_write(newfile, metadata_str.c, sizeof(metadata_str.len))) { + || metadata_str.len != php_stream_write(newfile, metadata_str.c, metadata_str.len)) { smart_str_free(&metadata_str); if (oldfile) { php_stream_close(oldfile); @@ -2532,7 +2547,9 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, char *url, int optio php_url_free(resource); return FAILURE; } - efree(error); + if (error) { + efree(error); + } if (!idata) { php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: \"%s\" is not a file in phar \"%s\", cannot unlink", internal_file, resource->host); efree(internal_file); diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 2504462384..58cc636434 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -499,7 +499,9 @@ PHP_METHOD(Phar, offsetSet) zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be created", fname); } } else { - efree(error); + if (error) { + efree(error); + } if (Z_TYPE_P(contents) == IS_STRING) { contents_len = php_stream_write(data->fp, Z_STRVAL_P(contents), Z_STRLEN_P(contents)); if (contents_len != Z_STRLEN_P(contents)) { diff --git a/ext/phar/tests/phar_ctx_001.phpt b/ext/phar/tests/phar_ctx_001.phpt index 6ec812df86..11a1ba2520 100644 --- a/ext/phar/tests/phar_ctx_001.phpt +++ b/ext/phar/tests/phar_ctx_001.phpt @@ -28,29 +28,42 @@ var_dump($phar['b']->isCompressed()); var_dump(file_get_contents($pname . '/c')); var_dump($phar['c']->isCompressed()); -$context = stream_context_create(array('phar'=>array('compress'=>Phar::GZ))); +$context = stream_context_create(array('phar'=> array('compress'=>Phar::GZ, 'metadata' => array(2, 'hi' => 3)))); +$context2 = stream_context_create(array('phar' => array('metadata' => array(4)))); file_put_contents($pname . '/a', 'new a', 0); // no compression file_put_contents($pname . '/b', 'new b', 0, $context); -file_put_contents($pname . '/d', 'new d');//, 0, $context); +file_put_contents($pname . '/d', 'new d', 0, $context2); $phar = new Phar($fname); var_dump(file_get_contents($pname . '/a')); var_dump($phar['a']->isCompressed()); +var_dump($phar['a']->getMetaData()); var_dump(file_get_contents($pname . '/b')); var_dump($phar['b']->isCompressed()); +var_dump($phar['b']->getMetaData()); var_dump(file_get_contents($pname . '/c')); var_dump($phar['c']->isCompressed()); +var_dump($phar['c']->getMetaData()); var_dump(file_get_contents($pname . '/d')); var_dump($phar['d']->isCompressed()); - +var_dump($phar['d']->getMetaData()); +$context2 = stream_context_create(array('phar' => array('metadata' => array(4)))); +$fp = fopen($pname . '/b', 'r+', 0, $context2); +fclose($fp); +?> +==AFTER== +isCompressed()); +var_dump($phar['b']->getMetaData()); ?> ===DONE=== --CLEAN-- ---EXPECTF-- +--EXPECT-- string(1) "a" bool(false) string(1) "b" @@ -59,10 +72,29 @@ string(1) "c" bool(false) string(5) "new a" bool(false) +NULL string(5) "new b" bool(true) +array(2) { + [0]=> + int(2) + ["hi"]=> + int(3) +} string(1) "c" bool(false) +NULL string(5) "new d" bool(false) -===DONE=== +array(1) { + [0]=> + int(4) +} +==AFTER== +string(5) "new b" +bool(true) +array(1) { + [0]=> + int(4) +} +===DONE=== \ No newline at end of file -- 2.40.0