cvs: pear /File_Archive Archive.php /File_Archive/Archive/Reader Ar.php Tar.php /File_Archive/Archive/Writer Files.php /File_Archive/tests test.php

From: Date: Sun, 29 May 2005 16:00:50 +0000
Subject: cvs: pear /File_Archive Archive.php /File_Archive/Archive/Reader Ar.php Tar.php /File_Archive/Archive/Writer Files.php /File_Archive/tests test.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-32231@lists.php.net to get a copy of this message
vincentlascaux Sun May 29 12:00:50 2005 EDT Modified files: /pear/File_Archive Archive.php /pear/File_Archive/Archive/Reader Ar.php Tar.php /pear/File_Archive/Archive/Writer Files.php /pear/File_Archive/tests test.php Log: + Better algorithm to remove block for files (do it in place with two file pointers). Uses less memory than previously http://cvs.php.net/diff.php/pear/File_Archive/Archive.php?r1=1.52&r2=1.53&ty=u Index: pear/File_Archive/Archive.php diff -u pear/File_Archive/Archive.php:1.52 pear/File_Archive/Archive.php:1.53 --- pear/File_Archive/Archive.php:1.52 Sat May 28 20:12:17 2005 +++ pear/File_Archive/Archive.php Sun May 29 12:00:49 2005 @@ -27,7 +27,7 @@ * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL - * @version CVS: $Id: Archive.php,v 1.52 2005/05/29 00:12:17 vincentlascaux Exp $ + * @version CVS: $Id: Archive.php,v 1.53 2005/05/29 16:00:49 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ @@ -901,7 +901,7 @@ * @param File_Archive_Predicate $pred The files that follow the predicate * (for which $pred->isTrue($source) is true) will be erased */ - function remove(&$source, $pred) + function remove($pred, &$source) { if (PEAR::isError($source)) { return $source; http://cvs.php.net/diff.php/pear/File_Archive/Archive/Reader/Ar.php?r1=1.11&r2=1.12&ty=u Index: pear/File_Archive/Archive/Reader/Ar.php diff -u pear/File_Archive/Archive/Reader/Ar.php:1.11 pear/File_Archive/Archive/Reader/Ar.php:1.12 --- pear/File_Archive/Archive/Reader/Ar.php:1.11 Sun May 29 08:42:18 2005 +++ pear/File_Archive/Archive/Reader/Ar.php Sun May 29 12:00:49 2005 @@ -311,7 +311,6 @@ $this->rewind(); $keep = false; - //TODO: write this data to a temp file? $data = $this->getData($blockPos); foreach ($blocks as $length) { if ($keep) { http://cvs.php.net/diff.php/pear/File_Archive/Archive/Reader/Tar.php?r1=1.25&r2=1.26&ty=u Index: pear/File_Archive/Archive/Reader/Tar.php diff -u pear/File_Archive/Archive/Reader/Tar.php:1.25 pear/File_Archive/Archive/Reader/Tar.php:1.26 --- pear/File_Archive/Archive/Reader/Tar.php:1.25 Sun May 29 08:42:18 2005 +++ pear/File_Archive/Archive/Reader/Tar.php Sun May 29 12:00:49 2005 @@ -25,7 +25,7 @@ * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL - * @version CVS: $Id: Tar.php,v 1.25 2005/05/29 12:42:18 vincentlascaux Exp $ + * @version CVS: $Id: Tar.php,v 1.26 2005/05/29 16:00:49 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ @@ -290,7 +290,6 @@ $this->rewind(); $keep = false; - //TODO: write this data to a temp file? $data = $this->getData($blockPos); foreach ($blocks as $length) { if ($keep) { http://cvs.php.net/diff.php/pear/File_Archive/Archive/Writer/Files.php?r1=1.16&r2=1.17&ty=u Index: pear/File_Archive/Archive/Writer/Files.php diff -u pear/File_Archive/Archive/Writer/Files.php:1.16 pear/File_Archive/Archive/Writer/Files.php:1.17 --- pear/File_Archive/Archive/Writer/Files.php:1.16 Sat May 28 17:34:15 2005 +++ pear/File_Archive/Archive/Writer/Files.php Sun May 29 12:00:50 2005 @@ -25,7 +25,7 @@ * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL - * @version CVS: $Id: Files.php,v 1.16 2005/05/28 21:34:15 vincentlascaux Exp $ + * @version CVS: $Id: Files.php,v 1.17 2005/05/29 16:00:50 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ @@ -94,6 +94,11 @@ /** * Open a file for writing from a given position + * + * @param string $filename The name of the file to open + * @param int $pos the initial position in the file + * @param $stat the stats of the file + * @param $mime the mime type of the file */ function openFile($filename, $pos = 0, $stat = array(), $mime = "application/octet-stream") { @@ -120,27 +125,41 @@ */ function openFileRemoveBlock($filename, $pos, $blocks, $stat = array(), $mime = "application/octet-stream") { - $this->openFile($filename, $pos, $stat, $mime); + $error = $this->openFile($filename, $pos, $stat, $mime); + if (PEAR::isError($error)) { + return $error; + } + + //This will be used to read the initial file + //The data, with the unusefull block removed will be written to $this->handle + $read = fopen($filename, 'r'); + if ($pos > 0) { + if (fseek($this->handle, $pos) == -1) { + fread($this->handle, $pos); + } + } - //TODO: Might be a lot in memory... $keep = false; $data = ''; foreach ($blocks as $length) { - if ($keep && $length > 0) { - $data .= fread($this->handle, $length); + if ($keep) { + while ($length > 0 && + ($data = fread($read, min($length, 8192))) != '') { + $length -= strlen($data); + fwrite($this->handle, $data); + } } else { - fseek($this->handle, $length, SEEK_CUR); + fseek($read, $length, SEEK_CUR); } $keep = !$keep; } if ($keep) { while(!feof($this->handle)) { - $data .= fread($this->handle, 8196); + fwrite($this->handle, fread($read, 8196)); } } - fseek($this->handle, $pos); - fwrite($this->handle, $data); + fclose($read); ftruncate($this->handle, ftell($this->handle)); } http://cvs.php.net/diff.php/pear/File_Archive/tests/test.php?r1=1.36&r2=1.37&ty=u Index: pear/File_Archive/tests/test.php diff -u pear/File_Archive/tests/test.php:1.36 pear/File_Archive/tests/test.php:1.37 --- pear/File_Archive/tests/test.php:1.36 Thu May 26 12:12:19 2005 +++ pear/File_Archive/tests/test.php Sun May 29 12:00:50 2005 @@ -316,6 +316,37 @@ while ($source->next()) echo $source->getFilename()."\n"; } + + function _testRemove($extension) + { + $writer = File_Archive::toArchive( + "test.$extension", + File_Archive::toVariable($x) + ); + $writer->newFile('foo.txt'); + $writer->writeData('ABCDEF'); + $writer->newFile('bla_to_remove.txt'); + $writer->writeData('GHIJKL'); + $writer->newFile('bar.txt'); + $writer->writeData('MNOP'); + $writer->close(); + + File_Archive::remove( + $source = File_Archive::readSource( + File_Archive::readMemory($x, "test.$extension"), + "test.$extension/"), + File_Archive::predEreg('_to_remove') + ); + $this->assertEquals( + "foo.txt\nbar.txt", + implode("\n", $source->getFileList()) + ); + } + function testRemoveTar() { return $this->_testRemove('tar'); } + function testRemoveTgz() { return $this->_testRemove('tbz'); } + function testRemoveTbz() { return $this->_testRemove('tgz'); } + function testRemoveAr() { return $this->_testRemove('ar'); } + function testRemoveZip() { return $this->_testRemove('zip'); } } $test = new PHPUnit_TestSuite("Test");

« previous php.pear.cvs (#32231) next »