From 7fa0b43518ac4cee10001dbe2c19473e424b31b9 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Sun, 16 Dec 2007 06:31:00 +0000 Subject: [PATCH] fully implement Phar->copy(), fix test --- ext/phar/TODO | 2 +- ext/phar/package.php | 1 + ext/phar/phar_object.c | 40 +++++++++++++++++------------------ ext/phar/tests/phar_copy.phpt | 28 ++++++++++++++++-------- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/ext/phar/TODO b/ext/phar/TODO index 18d9e4d0f05..88f0f7ca425 100644 --- a/ext/phar/TODO +++ b/ext/phar/TODO @@ -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. diff --git a/ext/phar/package.php b/ext/phar/package.php index 2482c145e6b..078e1811fb8 100644 --- a/ext/phar/package.php +++ b/ext/phar/package.php @@ -1,6 +1,7 @@ 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); diff --git a/ext/phar/tests/phar_copy.phpt b/ext/phar/tests/phar_copy.phpt index 5ad0ccef217..8fe3471179d 100644 --- a/ext/phar/tests/phar_copy.phpt +++ b/ext/phar/tests/phar_copy.phpt @@ -11,34 +11,44 @@ phar.require_hash=1 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-- + --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=== \ No newline at end of file