1
0
mirror of https://github.com/php/php-src.git synced 2026-03-26 01:02:25 +01:00

fully implement Phar->copy(), fix test

This commit is contained in:
Greg Beaver
2007-12-16 06:31:00 +00:00
parent e1b6be2741
commit 7fa0b43518
4 changed files with 41 additions and 30 deletions

View File

@@ -68,7 +68,7 @@ Version 1.3.0
* implement PPG signing
* ability to match files containing a metadata key opendir('phar://a.phar/?mime-type=image/jpeg')
or foreach ($p->match('mime-type', 'image/jpeg') as $file)
* Phar::copy($from, $to);
X Phar::copy($from, $to); [Greg]
X Phar::delete($what) [Greg]
X Phar::buildFromIterator(Iterator $it[, string $base_directory]) [Greg]
* Layout: Option to compress all content rather than single files.

View File

@@ -1,6 +1,7 @@
<?php
$notes = '
* implement Phar::copy(string $from, string $to) [Greg]
* implement Phar::buildFromIterator(Iterator $it[, string $base_directory]) [Greg]
* add mapping of include/require from within a phar to location within phar [Greg]
solves the include_path issue without code munging

View File

@@ -1106,35 +1106,35 @@ PHP_METHOD(Phar, copy)
RETURN_FALSE;
}
if (!zend_hash_exists(&phar_obj->arc.archive->manifest, oldfile, (uint) oldfile_len)) {
if (SUCCESS != zend_hash_find(&phar_obj->arc.archive->manifest, oldfile, (uint) oldfile_len, (void**)&oldentry) || oldentry->is_deleted) {
if (!zend_hash_exists(&phar_obj->arc.archive->manifest, oldfile, (uint) oldfile_len) || SUCCESS != zend_hash_find(&phar_obj->arc.archive->manifest, oldfile, (uint) oldfile_len, (void**)&oldentry) || oldentry->is_deleted) {
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
"file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->arc.archive->fname);
RETURN_FALSE;
}
fp = oldentry->fp;
if (fp && fp != phar_obj->arc.archive->fp) {
/* new file */
newentry.fp = php_stream_temp_new();
fp = newentry.fp;
php_stream_seek(fp, 0, SEEK_SET);
if (oldentry->compressed_filesize != php_stream_copy_to_stream(oldentry->fp, fp, oldentry->compressed_filesize)) {
php_stream_close(fp);
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
"file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->arc.archive->fname);
RETURN_FALSE;
"file \"%s\" could not be copied to file \"%s\" in %s, copy failed", oldfile, newfile, phar_obj->arc.archive->fname);
}
}
if (oldentry->fp && oldentry->fp != phar_obj->arc.archive->fp) {
/* new file */
fp = oldentry->fp;
php_stream_seek(fp, 0, SEEK_SET);
} else {
fp = phar_obj->arc.archive->fp;
php_stream_seek(fp, phar_obj->arc.archive->internal_file_start + oldentry->offset_within_phar, SEEK_SET);
}
newentry.fp = php_stream_temp_new();
if (oldentry->compressed_filesize != php_stream_copy_to_stream(fp, newentry.fp, oldentry->compressed_filesize)) {
php_stream_close(newentry.fp);
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
"file \"%s\" could not be copied to file \"%s\" in %s, copy failed", oldfile, newfile, phar_obj->arc.archive->fname);
}
fp = newentry.fp;
memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
if (newentry.metadata) {
SEPARATE_ZVAL(&(newentry.metadata));
newentry.metadata_str.c = NULL;
newentry.metadata_str.len = 0;
}
newentry.fp = fp;
newentry.filename = estrndup(newfile, newfile_len);
newentry.filename_len = newfile_len;
phar_obj->arc.archive->is_modified = 1;
zend_hash_add(&phar_obj->arc.archive->manifest, newfile, newfile_len, (void*)&newentry, sizeof(phar_entry_info), NULL);
phar_flush(phar_obj->arc.archive, 0, 0, &error TSRMLS_CC);

View File

@@ -11,34 +11,44 @@ phar.require_hash=1
<?php
$fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php';
$fname2 = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '2.phar.php';
$pname = 'phar://'.$fname;
$iname = '/file.txt';
$ename = '/error/';
$p = new Phar($fname);
$p[$iname] = "foobar\n";
try
{
$p['a'] = 'hi';
$p->startBuffering();
$p->copy('a', 'b');
echo $p['b'];
echo file_get_contents($p['b']->getPathName());
$p['a']->setCompressedGZ();
$p->copy('a', 'c');
echo $p['c'];
$p['b']->setMetadata('a');
$p->copy('b', 'c');
$p->stopBuffering();
echo file_get_contents($p['c']->getPathName());
copy($fname, $fname2);
$p->copy('a', $ename);
}
catch(Exception $e)
{
echo $e->getMessage() . "\n";
}
include($pname . $iname);
ini_set('phar.readonly',1);
$p2 = new Phar($fname2);
echo "\n";
echo 'a: ' , file_get_contents($p2['a']->getPathName());
echo 'b: ' ,file_get_contents($p2['b']->getPathName());
echo 'c: ' ,file_get_contents($p2['c']->getPathName()), $p2['c']->getMetaData();
?>
===DONE===
--CLEAN--
<?php unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php'); ?>
<?php unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '2.phar.php'); ?>
--EXPECTF--
hihi
file "/error/" contains invalid characters /, cannot be copied from "a" in phar %s
===DONE===
hihifile "/error/" contains invalid characters empty directory, cannot be copied from "a" in phar %s
a: hib: hic: hia===DONE===