Browse Source

stream context for specifying meta-data

migration/RELEASE_1_0_0
Greg Beaver 20 years ago
parent
commit
2513903e40
  1. 2
      ext/phar/TODO
  2. 45
      ext/phar/phar.c
  3. 4
      ext/phar/phar_object.c
  4. 42
      ext/phar/tests/phar_ctx_001.phpt

2
ext/phar/TODO

@ -13,7 +13,7 @@ Version 1.0.0
X docs on file format/manifest description X docs on file format/manifest description
* docs on uses * docs on uses
X stream context for specifying compression of a file [Marcus] 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, X Phar->begin()/Phar->commitWrite() for specifying a new stub to the phar,
and deferring flush until all modifications are complete [Greg] and deferring flush until all modifications are complete [Greg]
X Phar->getStub() for retrieving the stub of the phar [Marcus] X Phar->getStub() for retrieving the stub of the phar [Marcus]

45
ext/phar/phar.c

@ -1312,11 +1312,12 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, char *pat
char *error; char *error;
char *filter_name; char *filter_name;
char tmpbuf[8]; char tmpbuf[8];
HashTable *pharcontext;
php_url *resource = NULL; php_url *resource = NULL;
php_stream *fp, *fpf; php_stream *fp, *fpf;
php_stream_filter *filter/*, *consumed */; php_stream_filter *filter/*, *consumed */;
php_uint32 offset, read, total, toread; php_uint32 offset, read, total, toread;
zval **pzoption;
zval **pzoption, *metadata;
resource = php_url_parse(path); 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); php_url_free(resource);
return NULL; return NULL;
} }
efree(error);
if (error) {
efree(error);
}
fpf = php_stream_alloc(&phar_ops, idata, NULL, mode); fpf = php_stream_alloc(&phar_ops, idata, NULL, mode);
php_url_free(resource); php_url_free(resource);
efree(internal_file); 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; return fpf;
} else { } 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+16, entry->flags);
phar_set_32(entry_buffer+20, metadata_str.len); phar_set_32(entry_buffer+20, metadata_str.len);
if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer)) 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); smart_str_free(&metadata_str);
if (oldfile) { if (oldfile) {
php_stream_close(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); php_url_free(resource);
return FAILURE; return FAILURE;
} }
efree(error);
if (error) {
efree(error);
}
if (!idata) { 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); 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); efree(internal_file);

4
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); zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be created", fname);
} }
} else { } else {
efree(error);
if (error) {
efree(error);
}
if (Z_TYPE_P(contents) == IS_STRING) { if (Z_TYPE_P(contents) == IS_STRING) {
contents_len = php_stream_write(data->fp, Z_STRVAL_P(contents), Z_STRLEN_P(contents)); contents_len = php_stream_write(data->fp, Z_STRVAL_P(contents), Z_STRLEN_P(contents));
if (contents_len != Z_STRLEN_P(contents)) { if (contents_len != Z_STRLEN_P(contents)) {

42
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(file_get_contents($pname . '/c'));
var_dump($phar['c']->isCompressed()); 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 . '/a', 'new a', 0); // no compression
file_put_contents($pname . '/b', 'new b', 0, $context); 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); $phar = new Phar($fname);
var_dump(file_get_contents($pname . '/a')); var_dump(file_get_contents($pname . '/a'));
var_dump($phar['a']->isCompressed()); var_dump($phar['a']->isCompressed());
var_dump($phar['a']->getMetaData());
var_dump(file_get_contents($pname . '/b')); var_dump(file_get_contents($pname . '/b'));
var_dump($phar['b']->isCompressed()); var_dump($phar['b']->isCompressed());
var_dump($phar['b']->getMetaData());
var_dump(file_get_contents($pname . '/c')); var_dump(file_get_contents($pname . '/c'));
var_dump($phar['c']->isCompressed()); var_dump($phar['c']->isCompressed());
var_dump($phar['c']->getMetaData());
var_dump(file_get_contents($pname . '/d')); var_dump(file_get_contents($pname . '/d'));
var_dump($phar['d']->isCompressed()); 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==
<?php
var_dump(file_get_contents($pname . '/b'));
var_dump($phar['b']->isCompressed());
var_dump($phar['b']->getMetaData());
?> ?>
===DONE=== ===DONE===
--CLEAN-- --CLEAN--
<?php <?php
unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php'); unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
?> ?>
--EXPECTF--
--EXPECT--
string(1) "a" string(1) "a"
bool(false) bool(false)
string(1) "b" string(1) "b"
@ -59,10 +72,29 @@ string(1) "c"
bool(false) bool(false)
string(5) "new a" string(5) "new a"
bool(false) bool(false)
NULL
string(5) "new b" string(5) "new b"
bool(true) bool(true)
array(2) {
[0]=>
int(2)
["hi"]=>
int(3)
}
string(1) "c" string(1) "c"
bool(false) bool(false)
NULL
string(5) "new d" string(5) "new d"
bool(false) bool(false)
===DONE===
array(1) {
[0]=>
int(4)
}
==AFTER==
string(5) "new b"
bool(true)
array(1) {
[0]=>
int(4)
}
===DONE===
Loading…
Cancel
Save