cvs: pear /PHP_Archive Archive.php package.xml package2.xml /PHP_Archive/Archive Creator.php /PHP_Archive/tests opendir.phpt
/PHP_Archive/tests/filenotfound filenotfound.phar /PHP_Archive/tests/longfilename longphar.phar /PHP_Archive/tests/opendir opendir.phar
/PHP_Archive/tests/require_once require_once.phar
| From: | Greg Beaver | Date: | Wed, 30 Nov 2005 06:41:34 +0000 |
| Subject: | cvs: pear /PHP_Archive Archive.php package.xml package2.xml /PHP_Archive/Archive Creator.php /PHP_Archive/tests opendir.phpt /PHP_Archive/tests/filenotfound filenotfound.phar /PHP_Archive/tests/longfilename longphar.phar /PHP_Archive/tests/opendir opendir.phar /PHP_Archive/tests/require_once require_once.phar |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-36617@lists.php.net to get a copy of this message | ||
cellog Wed Nov 30 01:41:34 2005 EDT
Modified files:
/pear/PHP_Archive Archive.php package.xml package2.xml
/pear/PHP_Archive/Archive Creator.php
/pear/PHP_Archive/tests/filenotfound filenotfound.phar
/pear/PHP_Archive/tests/longfilename longphar.phar
/pear/PHP_Archive/tests opendir.phpt
/pear/PHP_Archive/tests/opendir opendir.phar
/pear/PHP_Archive/tests/require_once require_once.phar
Log:
update doc comments, fix tests
remove non-existing file from package.xml
http://cvs.php.net/diff.php/pear/PHP_Archive/Archive.php?r1=1.20&r2=1.21&ty=u Index: pear/PHP_Archive/Archive.php diff -u pear/PHP_Archive/Archive.php:1.20 pear/PHP_Archive/Archive.php:1.21 --- pear/PHP_Archive/Archive.php:1.20 Wed Nov 30 00:24:54 2005 +++ pear/PHP_Archive/Archive.php Wed Nov 30 01:41:32 2005 @@ -19,18 +19,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version $Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ + * @version $Id: Archive.php,v 1.21 2005/11/30 06:41:32 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -55,10 +60,50 @@ */ protected $position = 0; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ private static $_fileStart = array(); - private $basename; + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -124,14 +169,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -139,7 +184,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -148,15 +193,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, self::$_fileStart[$this->basename] + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -168,10 +213,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -190,7 +235,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -198,7 +243,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -233,24 +278,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - self::$_fileStart[$this->basename] = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -258,15 +303,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -402,7 +461,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -449,7 +508,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -458,13 +517,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; http://cvs.php.net/diff.php/pear/PHP_Archive/package.xml?r1=1.16&r2=1.17&ty=u Index: pear/PHP_Archive/package.xml diff -u pear/PHP_Archive/package.xml:1.16 pear/PHP_Archive/package.xml:1.17 --- pear/PHP_Archive/package.xml:1.16 Wed Nov 30 00:38:37 2005 +++ pear/PHP_Archive/package.xml Wed Nov 30 01:41:32 2005 @@ -21,7 +21,7 @@ </maintainers> <release> <version>0.7.0</version> - <date>2005-11-29</date> + <date>2005-11-30</date> <license>PHP License</license> <state>alpha</state> <notes>major refactoring - .tar file format is no longer used @@ -59,7 +59,6 @@ <file role="test" name="makerequire_oncephar.php.inc"/> <file role="test" name="require_once.phar"/> </dir> <!-- /tests/require_once --> - <file role="test" name="cachestattest.php"/> <file role="test" name="eof.phpt"/> <file role="test" name="filenotfound.phpt"/> <file role="test" name="filenotfoundtest.php"/> @@ -93,7 +92,7 @@ <changelog> <release> <version>0.7.0</version> - <date>2005-11-29</date> + <date>2005-11-30</date> <license>PHP License</license> <state>alpha</state> <notes>major refactoring - .tar file format is no longer used http://cvs.php.net/diff.php/pear/PHP_Archive/package2.xml?r1=1.8&r2=1.9&ty=u Index: pear/PHP_Archive/package2.xml diff -u pear/PHP_Archive/package2.xml:1.8 pear/PHP_Archive/package2.xml:1.9 --- pear/PHP_Archive/package2.xml:1.8 Wed Nov 30 00:38:38 2005 +++ pear/PHP_Archive/package2.xml Wed Nov 30 01:41:32 2005 @@ -21,8 +21,8 @@ <email>cellog@php.net</email> <active>yes</active> </lead> - <date>2005-11-29</date> - <time>23:38:11</time> + <date>2005-11-30</date> + <time>00:36:18</time> <version> <release>0.7.0</release> <api>0.7</api> @@ -62,7 +62,6 @@ <file name="makerequire_oncephar.php.inc" role="test" /> <file name="require_once.phar" role="test" /> </dir> <!-- /tests/require_once --> - <file name="cachestattest.php" role="test" /> <file name="eof.phpt" role="test" /> <file name="filenotfound.phpt" role="test" /> <file name="filenotfoundtest.php" role="test" /> @@ -118,7 +117,7 @@ <release>alpha</release> <api>alpha</api> </stability> - <date>2005-11-29</date> + <date>2005-11-30</date> <license>PHP License</license> <notes>major refactoring - .tar file format is no longer used DRAMATIC improvement in efficiency of disk use, memory usage, http://cvs.php.net/diff.php/pear/PHP_Archive/Archive/Creator.php?r1=1.18&r2=1.19&ty=u Index: pear/PHP_Archive/Archive/Creator.php diff -u pear/PHP_Archive/Archive/Creator.php:1.18 pear/PHP_Archive/Archive/Creator.php:1.19 --- pear/PHP_Archive/Archive/Creator.php:1.18 Wed Nov 30 00:24:59 2005 +++ pear/PHP_Archive/Archive/Creator.php Wed Nov 30 01:41:33 2005 @@ -1,6 +1,33 @@ <?php +/** + * PHP_Archive Class creator (creates .phar) + * + * @package PHP_Archive + * @category PHP + */ +/** + * Needed for file manipulation + */ require_once 'System.php'; require_once 'PHP/Archive.php'; +/** + * PHP_Archive Class creator (implements .phar) + * + * PHAR files a singular archive from which an entire application can run. + * To use it, simply package it using {@see PHP_Archive_Creator} and use phar:// + * URIs to your includes. i.e. require_once 'phar://config.php' will include config.php + * from the root of the PHAR file. + * + * Gz code borrowed from the excellent File_Archive package by Vincent Lascaux. + * + * @copyright Copyright © David Shafik and Synaptic Media 2004. All rights reserved. + * @author Davey Shafik <davey@synapticmedia.net> + * @author Greg Beaver <cellog@php.net> + * @link http://www.synapticmedia.net Synaptic Media + * @version $Id: Creator.php,v 1.19 2005/11/30 06:41:33 cellog Exp $ + * @package PHP_Archive + * @category PHP + */ class PHP_Archive_Creator { /** @@ -155,7 +182,8 @@ mkdir($this->temp_path . DIRECTORY_SEPARATOR . 'contents'); } $size = strlen($file_contents); - // always use gz header/footer to help ensure validity of file + // save crc32 of file and the uncompressed file size, so we + // can do a sanity check on the file when opening it from the phar $file_contents = pack("VV",crc32($file_contents),strlen($file_contents)) . ($this->compress ? gzdeflate($file_contents, 9) : $file_contents); @@ -460,11 +488,17 @@ * $offset, // offset from start of files = 2 * $info['actualsize']); // actual size in the .phar = 3 * </code> + * @return string */ - function serializeManifest($manifest) + public function serializeManifest($manifest) { $ret = ''; foreach ($manifest as $savepath => $info) { + // save the string length, then the string, then this info + // uncompressed file size + // save timestamp + // byte offset from the start of internal files within the phar + // compressed file size (actual size in the phar) $ret .= pack('V', strlen($savepath)) . $savepath . call_user_func_array('pack', array_merge(array('VVVV'), $info)); } http://cvs.php.net/diff.php/pear/PHP_Archive/tests/filenotfound/filenotfound.phar?r1=1.4&r2=1.5&ty=u Index: pear/PHP_Archive/tests/filenotfound/filenotfound.phar diff -u pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.4 pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.5 --- pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.4 Wed Nov 30 00:25:06 2005 +++ pear/PHP_Archive/tests/filenotfound/filenotfound.phar Wed Nov 30 01:41:33 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,6 +614,6 @@ } ?> <?php __HALT_COMPILER(); ?> -!+! @fopen('phar://cachestat.phar/test2.php', 'r'); ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/PHP_Archive/tests/longfilename/longphar.phar?r1=1.6&r2=1.7&ty=u Index: pear/PHP_Archive/tests/longfilename/longphar.phar diff -u pear/PHP_Archive/tests/longfilename/longphar.phar:1.6 pear/PHP_Archive/tests/longfilename/longphar.phar:1.7 --- pear/PHP_Archive/tests/longfilename/longphar.phar:1.6 Wed Nov 30 00:25:06 2005 +++ pear/PHP_Archive/tests/longfilename/longphar.phar Wed Nov 30 01:41:33 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,7 +614,7 @@ } ?> <?php __HALT_COMPILER(); ?> -+ echo __FILE__; var_dump('hello'); http://cvs.php.net/diff.php/pear/PHP_Archive/tests/opendir.phpt?r1=1.3&r2=1.4&ty=u Index: pear/PHP_Archive/tests/opendir.phpt diff -u pear/PHP_Archive/tests/opendir.phpt:1.3 pear/PHP_Archive/tests/opendir.phpt:1.4 --- pear/PHP_Archive/tests/opendir.phpt:1.3 Sun Jul 24 15:47:28 2005 +++ pear/PHP_Archive/tests/opendir.phpt Wed Nov 30 01:41:33 2005 @@ -14,22 +14,8 @@ 'd' => is_dir('phar://opendir.phar/' . $file)); } $phpunit = new PEAR_PHPTest(true); +$phpunit->showAll(); $phpunit->assertEquals(array ( - 'cachestat' => - array ( - 'f' => false, - 'd' => true, - ), - 'cachestat.phpt' => - array ( - 'f' => true, - 'd' => false, - ), - 'cachestattest.php' => - array ( - 'f' => true, - 'd' => false, - ), 'eof.phpt' => array ( 'f' => true, http://cvs.php.net/diff.php/pear/PHP_Archive/tests/opendir/opendir.phar?r1=1.5&r2=1.6&ty=u Index: pear/PHP_Archive/tests/opendir/opendir.phar diff -u pear/PHP_Archive/tests/opendir/opendir.phar:1.5 pear/PHP_Archive/tests/opendir/opendir.phar:1.6 --- pear/PHP_Archive/tests/opendir/opendir.phar:1.5 Wed Nov 30 00:25:07 2005 +++ pear/PHP_Archive/tests/opendir/opendir.phar Wed Nov 30 01:41:33 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,4 +614,4 @@ } ?> <?php __HALT_COMPILER(); ?> -\ No newline at end of file +\ No newline at end of file http://cvs.php.net/diff.php/pear/PHP_Archive/tests/require_once/require_once.phar?r1=1.4&r2=1.5&ty=u Index: pear/PHP_Archive/tests/require_once/require_once.phar diff -u pear/PHP_Archive/tests/require_once/require_once.phar:1.4 pear/PHP_Archive/tests/require_once/require_once.phar:1.5 --- pear/PHP_Archive/tests/require_once/require_once.phar:1.4 Wed Nov 30 00:25:08 2005 +++ pear/PHP_Archive/tests/require_once/require_once.phar Wed Nov 30 01:41:34 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,4 +614,4 @@ } ?> <?php __HALT_COMPILER(); ?> -&\ No newline at end of file +&\ No newline at end of file
http://cvs.php.net/diff.php/pear/PHP_Archive/Archive.php?r1=1.20&r2=1.21&ty=u Index: pear/PHP_Archive/Archive.php diff -u pear/PHP_Archive/Archive.php:1.20 pear/PHP_Archive/Archive.php:1.21 --- pear/PHP_Archive/Archive.php:1.20 Wed Nov 30 00:24:54 2005 +++ pear/PHP_Archive/Archive.php Wed Nov 30 01:41:32 2005 @@ -19,18 +19,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version $Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ + * @version $Id: Archive.php,v 1.21 2005/11/30 06:41:32 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -55,10 +60,50 @@ */ protected $position = 0; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ private static $_fileStart = array(); - private $basename; + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -124,14 +169,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -139,7 +184,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -148,15 +193,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, self::$_fileStart[$this->basename] + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -168,10 +213,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -190,7 +235,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -198,7 +243,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -233,24 +278,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - self::$_fileStart[$this->basename] = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -258,15 +303,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -402,7 +461,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -449,7 +508,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -458,13 +517,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; http://cvs.php.net/diff.php/pear/PHP_Archive/package.xml?r1=1.16&r2=1.17&ty=u Index: pear/PHP_Archive/package.xml diff -u pear/PHP_Archive/package.xml:1.16 pear/PHP_Archive/package.xml:1.17 --- pear/PHP_Archive/package.xml:1.16 Wed Nov 30 00:38:37 2005 +++ pear/PHP_Archive/package.xml Wed Nov 30 01:41:32 2005 @@ -21,7 +21,7 @@ </maintainers> <release> <version>0.7.0</version> - <date>2005-11-29</date> + <date>2005-11-30</date> <license>PHP License</license> <state>alpha</state> <notes>major refactoring - .tar file format is no longer used @@ -59,7 +59,6 @@ <file role="test" name="makerequire_oncephar.php.inc"/> <file role="test" name="require_once.phar"/> </dir> <!-- /tests/require_once --> - <file role="test" name="cachestattest.php"/> <file role="test" name="eof.phpt"/> <file role="test" name="filenotfound.phpt"/> <file role="test" name="filenotfoundtest.php"/> @@ -93,7 +92,7 @@ <changelog> <release> <version>0.7.0</version> - <date>2005-11-29</date> + <date>2005-11-30</date> <license>PHP License</license> <state>alpha</state> <notes>major refactoring - .tar file format is no longer used http://cvs.php.net/diff.php/pear/PHP_Archive/package2.xml?r1=1.8&r2=1.9&ty=u Index: pear/PHP_Archive/package2.xml diff -u pear/PHP_Archive/package2.xml:1.8 pear/PHP_Archive/package2.xml:1.9 --- pear/PHP_Archive/package2.xml:1.8 Wed Nov 30 00:38:38 2005 +++ pear/PHP_Archive/package2.xml Wed Nov 30 01:41:32 2005 @@ -21,8 +21,8 @@ <email>cellog@php.net</email> <active>yes</active> </lead> - <date>2005-11-29</date> - <time>23:38:11</time> + <date>2005-11-30</date> + <time>00:36:18</time> <version> <release>0.7.0</release> <api>0.7</api> @@ -62,7 +62,6 @@ <file name="makerequire_oncephar.php.inc" role="test" /> <file name="require_once.phar" role="test" /> </dir> <!-- /tests/require_once --> - <file name="cachestattest.php" role="test" /> <file name="eof.phpt" role="test" /> <file name="filenotfound.phpt" role="test" /> <file name="filenotfoundtest.php" role="test" /> @@ -118,7 +117,7 @@ <release>alpha</release> <api>alpha</api> </stability> - <date>2005-11-29</date> + <date>2005-11-30</date> <license>PHP License</license> <notes>major refactoring - .tar file format is no longer used DRAMATIC improvement in efficiency of disk use, memory usage, http://cvs.php.net/diff.php/pear/PHP_Archive/Archive/Creator.php?r1=1.18&r2=1.19&ty=u Index: pear/PHP_Archive/Archive/Creator.php diff -u pear/PHP_Archive/Archive/Creator.php:1.18 pear/PHP_Archive/Archive/Creator.php:1.19 --- pear/PHP_Archive/Archive/Creator.php:1.18 Wed Nov 30 00:24:59 2005 +++ pear/PHP_Archive/Archive/Creator.php Wed Nov 30 01:41:33 2005 @@ -1,6 +1,33 @@ <?php +/** + * PHP_Archive Class creator (creates .phar) + * + * @package PHP_Archive + * @category PHP + */ +/** + * Needed for file manipulation + */ require_once 'System.php'; require_once 'PHP/Archive.php'; +/** + * PHP_Archive Class creator (implements .phar) + * + * PHAR files a singular archive from which an entire application can run. + * To use it, simply package it using {@see PHP_Archive_Creator} and use phar:// + * URIs to your includes. i.e. require_once 'phar://config.php' will include config.php + * from the root of the PHAR file. + * + * Gz code borrowed from the excellent File_Archive package by Vincent Lascaux. + * + * @copyright Copyright © David Shafik and Synaptic Media 2004. All rights reserved. + * @author Davey Shafik <davey@synapticmedia.net> + * @author Greg Beaver <cellog@php.net> + * @link http://www.synapticmedia.net Synaptic Media + * @version $Id: Creator.php,v 1.19 2005/11/30 06:41:33 cellog Exp $ + * @package PHP_Archive + * @category PHP + */ class PHP_Archive_Creator { /** @@ -155,7 +182,8 @@ mkdir($this->temp_path . DIRECTORY_SEPARATOR . 'contents'); } $size = strlen($file_contents); - // always use gz header/footer to help ensure validity of file + // save crc32 of file and the uncompressed file size, so we + // can do a sanity check on the file when opening it from the phar $file_contents = pack("VV",crc32($file_contents),strlen($file_contents)) . ($this->compress ? gzdeflate($file_contents, 9) : $file_contents); @@ -460,11 +488,17 @@ * $offset, // offset from start of files = 2 * $info['actualsize']); // actual size in the .phar = 3 * </code> + * @return string */ - function serializeManifest($manifest) + public function serializeManifest($manifest) { $ret = ''; foreach ($manifest as $savepath => $info) { + // save the string length, then the string, then this info + // uncompressed file size + // save timestamp + // byte offset from the start of internal files within the phar + // compressed file size (actual size in the phar) $ret .= pack('V', strlen($savepath)) . $savepath . call_user_func_array('pack', array_merge(array('VVVV'), $info)); } http://cvs.php.net/diff.php/pear/PHP_Archive/tests/filenotfound/filenotfound.phar?r1=1.4&r2=1.5&ty=u Index: pear/PHP_Archive/tests/filenotfound/filenotfound.phar diff -u pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.4 pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.5 --- pear/PHP_Archive/tests/filenotfound/filenotfound.phar:1.4 Wed Nov 30 00:25:06 2005 +++ pear/PHP_Archive/tests/filenotfound/filenotfound.phar Wed Nov 30 01:41:33 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,6 +614,6 @@ } ?> <?php __HALT_COMPILER(); ?> -!+! @fopen('phar://cachestat.phar/test2.php', 'r'); ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/PHP_Archive/tests/longfilename/longphar.phar?r1=1.6&r2=1.7&ty=u Index: pear/PHP_Archive/tests/longfilename/longphar.phar diff -u pear/PHP_Archive/tests/longfilename/longphar.phar:1.6 pear/PHP_Archive/tests/longfilename/longphar.phar:1.7 --- pear/PHP_Archive/tests/longfilename/longphar.phar:1.6 Wed Nov 30 00:25:06 2005 +++ pear/PHP_Archive/tests/longfilename/longphar.phar Wed Nov 30 01:41:33 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,7 +614,7 @@ } ?> <?php __HALT_COMPILER(); ?> -+ echo __FILE__; var_dump('hello'); http://cvs.php.net/diff.php/pear/PHP_Archive/tests/opendir.phpt?r1=1.3&r2=1.4&ty=u Index: pear/PHP_Archive/tests/opendir.phpt diff -u pear/PHP_Archive/tests/opendir.phpt:1.3 pear/PHP_Archive/tests/opendir.phpt:1.4 --- pear/PHP_Archive/tests/opendir.phpt:1.3 Sun Jul 24 15:47:28 2005 +++ pear/PHP_Archive/tests/opendir.phpt Wed Nov 30 01:41:33 2005 @@ -14,22 +14,8 @@ 'd' => is_dir('phar://opendir.phar/' . $file)); } $phpunit = new PEAR_PHPTest(true); +$phpunit->showAll(); $phpunit->assertEquals(array ( - 'cachestat' => - array ( - 'f' => false, - 'd' => true, - ), - 'cachestat.phpt' => - array ( - 'f' => true, - 'd' => false, - ), - 'cachestattest.php' => - array ( - 'f' => true, - 'd' => false, - ), 'eof.phpt' => array ( 'f' => true, http://cvs.php.net/diff.php/pear/PHP_Archive/tests/opendir/opendir.phar?r1=1.5&r2=1.6&ty=u Index: pear/PHP_Archive/tests/opendir/opendir.phar diff -u pear/PHP_Archive/tests/opendir/opendir.phar:1.5 pear/PHP_Archive/tests/opendir/opendir.phar:1.6 --- pear/PHP_Archive/tests/opendir/opendir.phar:1.5 Wed Nov 30 00:25:07 2005 +++ pear/PHP_Archive/tests/opendir/opendir.phar Wed Nov 30 01:41:33 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,4 +614,4 @@ } ?> <?php __HALT_COMPILER(); ?> -\ No newline at end of file +\ No newline at end of file http://cvs.php.net/diff.php/pear/PHP_Archive/tests/require_once/require_once.phar?r1=1.4&r2=1.5&ty=u Index: pear/PHP_Archive/tests/require_once/require_once.phar diff -u pear/PHP_Archive/tests/require_once/require_once.phar:1.4 pear/PHP_Archive/tests/require_once/require_once.phar:1.5 --- pear/PHP_Archive/tests/require_once/require_once.phar:1.4 Wed Nov 30 00:25:08 2005 +++ pear/PHP_Archive/tests/require_once/require_once.phar Wed Nov 30 01:41:34 2005 @@ -27,18 +27,23 @@ * @author Davey Shafik <davey@synapticmedia.net> * @author Greg Beaver <cellog@php.net> * @link http://www.synapticmedia.net Synaptic Media - * @version Id: Archive.php,v 1.19 2005/07/24 15:42:20 cellog Exp $ + * @version Id: Archive.php,v 1.20 2005/11/30 05:24:54 cellog Exp $ * @package PHP_Archive * @category PHP */ class PHP_Archive { + /** + * Whether this archive is compressed with zlib + * + * @var bool + */ private $_compressed; /** * @var string Real path to the .phar archive */ - protected $pharName = null; + private $_archiveName = null; /** * Current file name in the phar * @var string @@ -63,10 +68,50 @@ */ protected $position = 0; - private static $_pharMapping; - private static $_manifest; - private $basename; - private $_fileStart; + /** + * Map actual realpath to phars to meta-data about the phar + * + * Data is indexed by the alias that is used by internal files. In other + * words, if a file is included via: + * <code> + * require_once 'phar://PEAR.phar/PEAR/Installer.php'; + * </code> + * then the alias is "PEAR.phar" + * + * Information stored is the actual file name, a boolean indicating whether + * this .phar is compressed with zlib, and the precise offset of internal files + * within the .phar, used with the {@link $_manifest} to load actual file contents + * @var array + */ + private static $_pharMapping = array(); + /** + * File listing for the .phar + * + * The manifest is indexed per phar. + * + * Files within the .phar are indexed by their relative path within the + * .phar. Each file has this information in its internal array + * + * - 0 = uncompressed file size + * - 1 = timestamp of when file was added to phar + * - 2 = offset of file within phar relative to internal file's start + * - 3 = compressed file size (actual size in the phar) + * @var array + */ + private static $_manifest = array(); + /** + * Absolute offset of internal files within the .phar, indexed by absolute + * path to the .phar + * + * @var array + */ + private static $_fileStart = array(); + /** + * file name of the phar + * + * @var string + */ + private $_basename; /** * Map a full real file path to an alias used to refer to the .phar @@ -132,14 +177,14 @@ protected function selectFile($path, $allowdirs = true) { $std = self::processFile($path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { $this->_setCurrentFile($path); return true; } if (!$allowdirs) { - return 'Error: "' . $path . '" is not a file in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" is not a file in phar "' . $this->_basename . '"'; } - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if (empty($std) || //$std is a directory strncmp($std.'/', $path, strlen($std)+1) == 0) { @@ -147,7 +192,7 @@ return true; } } - return 'Error: "' . $path . '" not found in phar "' . $this->basename . '"'; + return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"'; } private function _setCurrentFile($path) @@ -156,15 +201,15 @@ 2 => 0100444, // file mode, readable by all, writeable by none 4 => 0, // uid 5 => 0, // gid - 7 => self::$_manifest[$this->basename][$path][0], // size - 9 => self::$_manifest[$this->basename][$path][1], // creation time + 7 => self::$_manifest[$this->_archiveName][$path][0], // size + 9 => self::$_manifest[$this->_archiveName][$path][1], // creation time ); $this->currentFilename = $path; // actual file length in file includes 8-byte header - $this->internalFileLength = self::$_manifest[$this->basename][$path][3] - 8; + $this->internalFileLength = self::$_manifest[$this->_archiveName][$path][3] - 8; // seek to offset of file header within the .phar if (is_resource(@$this->fp)) { - fseek($this->fp, $this->_fileStart + self::$_manifest[$this->basename][$path][2]); + fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][2]); } } @@ -176,10 +221,10 @@ */ public function extractFile($path) { - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); $stat = fstat($this->fp); if (!$this->fp) { - return array('Error: cannot open phar "' . $this->archiveName . '"'); + return array('Error: cannot open phar "' . $this->_archiveName . '"'); } if (($e = $this->selectFile($path, false)) === true) { $temp = unpack("Vcrc32/Visize", fread($this->fp, 8)); @@ -198,7 +243,7 @@ if ($this->_compressed) { $data = gzinflate($data); } - if (!isset(self::$_manifest[$this->basename][$path]['ok'])) { + if (!isset(self::$_manifest[$this->_archiveName][$path]['ok'])) { if ($temp['isize'] != $this->currentStat[7]) { return array("Not valid internal .phar file (size error {$size} != " . $this->currentStat[7] . ")"); @@ -206,7 +251,7 @@ if ($temp['crc32'] != crc32($data)) { return array("Not valid internal .phar file (checksum error)"); } - self::$_manifest[$this->basename][$path]['ok'] = true; + self::$_manifest[$this->_archiveName][$path]['ok'] = true; } return $data; } else { @@ -241,24 +286,24 @@ $info['path'] = substr($info['path'], 1); } if (isset(self::$_pharMapping[$info['host']])) { - $this->basename = $info['host']; - $this->archiveName = self::$_pharMapping[$info['host']][0]; + $this->_basename = $info['host']; + $this->_archiveName = self::$_pharMapping[$info['host']][0]; $this->_compressed = self::$_pharMapping[$info['host']][1]; } else { // no such phar has been included, or last opened phar is requested $pharinfo = end(self::$_pharMapping); - $this->basename = key(self::$_pharMapping); - $this->archiveName = $pharinfo[0]; + $this->_basename = key(self::$_pharMapping); + $this->_archiveName = $pharinfo[0]; $this->_compressed = $pharinfo[1]; } - if (!isset(self::$_manifest[$this->basename])) { - $fp = fopen($this->archiveName, 'rb'); + if (!isset(self::$_manifest[$this->_archiveName])) { + $fp = fopen($this->_archiveName, 'rb'); // seek to __HALT_COMPILER_OFFSET__ - fseek($fp, self::$_pharMapping[$this->basename][2]); + fseek($fp, self::$_pharMapping[$this->_basename][2]); $manifest_length = unpack('Vlen', fread($fp, 4)); - self::$_manifest[$this->basename] = - $this->unserializeManifest(fread($fp, $manifest_length['len'])); - $this->_fileStart = ftell($fp); + self::$_manifest[$this->_archiveName] = + $this->_unserializeManifest(fread($fp, $manifest_length['len'])); + self::$_fileStart[$this->_archiveName] = ftell($fp); fclose($fp); } $file = $info['path']; @@ -266,15 +311,29 @@ } - function unserializeManifest($manifest) + /** + * Enter description here... + * + * @param unknown_type $manifest + * @return unknown + */ + private function _unserializeManifest($manifest) { + // retrieve the number of files in the manifest $info = unpack('V', substr($manifest, 0, 4)); $manifest = substr($manifest, 4); $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + // file name $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + // retrieve manifest data: + // 0 = uncompressed file size + // 1 = timestamp of when file was added to phar + // 2 = offset of file within phar relative to internal file's start + // 3 = compressed file size (actual size in the phar) $ret[$savepath] = array_values(unpack('Va/Vb/Vc/Vd', substr($manifest, 0, 16))); $manifest = substr($manifest, 16); } @@ -410,7 +469,7 @@ { $std = $file ? self::processFile($file) : $this->currentFilename; if ($file) { - if (isset(self::$_manifest[$this->basename][$file])) { + if (isset(self::$_manifest[$this->_archiveName][$file])) { $this->_setCurrentFile($file); $isdir = false; } else { @@ -457,7 +516,7 @@ $path = !empty($info['path']) ? $info['host'] . $info['path'] : $info['host'] . '/'; $path = $this->initializeStream('phar://' . $path); - if (isset(self::$_manifest[$this->basename][$path])) { + if (isset(self::$_manifest[$this->_archiveName][$path])) { trigger_error('Error: "' . $path . '" is a file, and cannot be opened with opendir', E_USER_ERROR); return false; @@ -466,13 +525,13 @@ trigger_error('Error: Unknown phar in "' . $file . '"', E_USER_ERROR); return false; } - $this->fp = @fopen($this->archiveName, "rb"); + $this->fp = @fopen($this->_archiveName, "rb"); if (!$this->fp) { - trigger_error('Error: cannot open phar "' . $this->archiveName . '"'); + trigger_error('Error: cannot open phar "' . $this->_archiveName . '"'); return false; } $this->_dirFiles = array(); - foreach (self::$_manifest[$this->basename] as $file => $info) { + foreach (self::$_manifest[$this->_archiveName] as $file => $info) { if ($path == '/') { if (strpos($file, '/')) { $this->_dirFiles[array_shift($a = explode('/', $file))] = true; @@ -555,4 +614,4 @@ } ?> <?php __HALT_COMPILER(); ?> -&\ No newline at end of file +&\ No newline at end of file