mirror of
https://github.com/php/php-src.git
synced 2024-12-25 17:59:52 +08:00
fix gzipped phars in phar file format, add test
This commit is contained in:
parent
38a9da1071
commit
21ab2ca8e9
@ -2981,7 +2981,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, char **error
|
||||
if (!phar->fp) {
|
||||
phar->fp = newfile;
|
||||
if (error) {
|
||||
spprintf(error, 0, "unable to open new phar \"%s\" for writing", phar->fname);
|
||||
spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname);
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
@ -2991,9 +2991,15 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, char **error
|
||||
zval filterparams;
|
||||
|
||||
array_init(&filterparams);
|
||||
add_assoc_long(&filterparams, "window", MAX_WBITS);
|
||||
add_assoc_long(&filterparams, "window", MAX_WBITS+16);
|
||||
filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp) TSRMLS_CC);
|
||||
zval_dtor(&filterparams);
|
||||
if (!filter) {
|
||||
if (error) {
|
||||
spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname);
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
php_stream_filter_append(&phar->fp->writefilters, filter);
|
||||
php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL);
|
||||
php_stream_filter_flush(filter, 1);
|
||||
@ -3052,6 +3058,12 @@ static void php_phar_init_globals_module(zend_phar_globals *phar_globals)
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static long stream_fteller_for_zend(void *handle TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
return (long)php_stream_tell((php_stream*)handle);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC) /* {{{ */
|
||||
{
|
||||
zend_op_array *res;
|
||||
@ -3083,6 +3095,17 @@ static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type
|
||||
*file_handle = f;
|
||||
}
|
||||
goto skip_phar;
|
||||
} else if (phar->flags & PHAR_FILE_COMPRESSION_MASK) {
|
||||
/* compressed phar */
|
||||
file_handle->type = ZEND_HANDLE_STREAM;
|
||||
file_handle->free_filename = 0;
|
||||
file_handle->handle.stream.handle = phar->fp;
|
||||
file_handle->handle.stream.reader = (zend_stream_reader_t)_php_stream_read;
|
||||
file_handle->handle.stream.closer = NULL; /* don't close - let phar handle this one */
|
||||
file_handle->handle.stream.fteller = stream_fteller_for_zend;
|
||||
file_handle->handle.stream.interactive = 0;
|
||||
php_stream_rewind(phar->fp);
|
||||
goto skip_phar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -569,9 +569,15 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, long len, char **er
|
||||
zval filterparams;
|
||||
|
||||
array_init(&filterparams);
|
||||
add_assoc_long(&filterparams, "window", MAX_WBITS);
|
||||
add_assoc_long(&filterparams, "window", MAX_WBITS + 16);
|
||||
filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp) TSRMLS_CC);
|
||||
zval_dtor(&filterparams);
|
||||
if (!filter) {
|
||||
if (error) {
|
||||
spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname);
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
php_stream_filter_append(&phar->fp->writefilters, filter);
|
||||
php_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL);
|
||||
php_stream_filter_flush(filter, 1);
|
||||
|
53
ext/phar/tests/phar_gzip.phpt
Normal file
53
ext/phar/tests/phar_gzip.phpt
Normal file
@ -0,0 +1,53 @@
|
||||
--TEST--
|
||||
Phar: gzipped phar
|
||||
--SKIPIF--
|
||||
<?php if (!extension_loaded('phar')) die('skip'); ?>
|
||||
<?php if (!extension_loaded("spl")) die("skip SPL not available"); ?>
|
||||
<?php if (!extension_loaded("zlib")) die("skip zlib not available"); ?>
|
||||
<?php if (version_compare(phpversion(), '5.2.6', '<')) die("skip zlib is buggy in PHP < 5.2.6"); ?>
|
||||
--INI--
|
||||
phar.readonly=0
|
||||
phar.require_hash=0
|
||||
--FILE--
|
||||
<?php
|
||||
$fname = dirname(__FILE__) . '/phar_gzip.phar';
|
||||
$pname = 'phar://' . $fname;
|
||||
$fname2 = dirname(__FILE__) . '/phar_gzip.2.phar';
|
||||
$pname2 = 'phar://' . $fname2;
|
||||
|
||||
$file = '<?php
|
||||
Phar::mapPhar();
|
||||
var_dump("it worked");
|
||||
include "phar://" . __FILE__ . "/tar_004.php";
|
||||
__HALT_COMPILER();';
|
||||
|
||||
$files = array();
|
||||
$files['tar_004.php'] = '<?php var_dump(__FILE__);';
|
||||
$files['internal/file/here'] = "hi there!\n";
|
||||
$files['internal/dir/'] = '';
|
||||
$files['dir/'] = '';
|
||||
$gzip = true;
|
||||
|
||||
include 'phar_test.inc';
|
||||
|
||||
include $fname;
|
||||
|
||||
$a = new Phar($fname);
|
||||
$a['test'] = 'hi';
|
||||
copy($fname, $fname2);
|
||||
$b = new Phar($fname2);
|
||||
var_dump($b->isPhar());
|
||||
var_dump($b->isCompressed() == Phar::GZ);
|
||||
?>
|
||||
===DONE===
|
||||
--CLEAN--
|
||||
<?php
|
||||
@unlink(dirname(__FILE__) . '/phar_gzip.phar');
|
||||
@unlink(dirname(__FILE__) . '/phar_gzip.2.phar');
|
||||
?>
|
||||
--EXPECTF--
|
||||
string(9) "it worked"
|
||||
string(%d) "phar://%sphar_gzip.phar/tar_004.php"
|
||||
bool(true)
|
||||
bool(true)
|
||||
===DONE===
|
@ -61,4 +61,10 @@ foreach($files as $cont)
|
||||
|
||||
file_put_contents($fname, $file);
|
||||
|
||||
if (@$gzip) {
|
||||
$fp = gzopen($fname, 'w');
|
||||
fwrite($fp, $file);
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
?>
|
@ -4,6 +4,7 @@ Phar: tar-based phar, gzipped tar
|
||||
<?php if (!extension_loaded('phar')) die('skip'); ?>
|
||||
<?php if (!extension_loaded("spl")) die("skip SPL not available"); ?>
|
||||
<?php if (!extension_loaded("zlib")) die("skip zlib not available"); ?>
|
||||
<?php if (version_compare(phpversion(), '5.2.6', '<')) die("skip zlib is buggy in PHP < 5.2.6"); ?>
|
||||
--INI--
|
||||
phar.readonly=0
|
||||
--FILE--
|
||||
@ -33,9 +34,8 @@ $a = new Phar($fname);
|
||||
$a['test'] = 'hi';
|
||||
copy($fname, $fname2);
|
||||
$b = new Phar($fname2);
|
||||
var_dump($b->isCompressed == Phar::GZ);
|
||||
|
||||
__HALT_COMPILER();
|
||||
var_dump($b->isTar());
|
||||
var_dump($b->isCompressed() == Phar::GZ);
|
||||
?>
|
||||
===DONE===
|
||||
--CLEAN--
|
||||
@ -47,4 +47,5 @@ __HALT_COMPILER();
|
||||
string(9) "it worked"
|
||||
string(%d) "phar://%star_gzip.phar/tar_004.php"
|
||||
bool(true)
|
||||
bool(true)
|
||||
===DONE===
|
Loading…
Reference in New Issue
Block a user