Skip to content

Commit

Permalink
prepare for sync with ext/phar
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/PHP_Archive/trunk@260968 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Greg Beaver committed Jun 9, 2008
1 parent 6f247a6 commit 50298f4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
83 changes: 59 additions & 24 deletions Archive.php
Expand Up @@ -23,14 +23,17 @@
* @package PHP_Archive
* @category PHP
*/

class PHP_Archive
{
const GZ = 0x00001000;
const BZ2 = 0x00002000;
const SIG = 0x00010000;
const SHA1 = 0x0002;
const MD5 = 0x0001;
const SHA256 = 0x0003;
const SHA512 = 0x0004;
const OPENSSL = 0x0010;
/**
* Whether this archive is compressed with zlib
*
Expand All @@ -51,6 +54,11 @@ class PHP_Archive
* @var string
*/
protected $internalFileLength = null;
/**
* true if the current file is an empty directory
* @var string
*/
protected $isDir = false;
/**
* Current file statistics (size, creation date, etc.)
* @var string
Expand All @@ -74,7 +82,7 @@ class PHP_Archive
* require_once 'phar://PEAR.phar/PEAR/Installer.php';
* </code>
* then the alias is "PEAR.phar"
*
*
* Information stored is a boolean indicating whether this .phar is compressed
* with zlib, another for bzip2, phar-specific meta-data, and
* the precise offset of internal files
Expand All @@ -90,9 +98,9 @@ class PHP_Archive
private static $_pharFiles = 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
*
Expand Down Expand Up @@ -205,7 +213,7 @@ public static function viewSource($archive, $file)
header("HTTP/1.0 404 Not Found");
return false;
}

}

public static function introspect($archive, $dir)
Expand Down Expand Up @@ -435,7 +443,7 @@ private static function _mapPhar($file, $dataoffset, $alias = NULL)
$manifest .= $last;
}
if (strlen($manifest) < $manifest_length['len']) {
throw new Exception('ERROR: manifest length read was "' .
throw new Exception('ERROR: manifest length read was "' .
strlen($manifest) .'" should be "' .
$manifest_length['len'] . '"');
}
Expand Down Expand Up @@ -579,7 +587,15 @@ protected function selectFile($path, $allowdirs = true)
{
$std = self::processFile($path);
if (isset(self::$_manifest[$this->_archiveName][$path])) {
$this->_setCurrentFile($path);
if ($path[strlen($path)-1] == '/') {
// directory
if (!$allowdirs) {
return 'Error: "' . $path . '" is a directory in phar "' . $this->_basename . '"';
}
$this->_setCurrentFile($path, true);
} else {
$this->_setCurrentFile($path);
}
return true;
}
if (!$allowdirs) {
Expand All @@ -596,17 +612,30 @@ protected function selectFile($path, $allowdirs = true)
return 'Error: "' . $path . '" not found in phar "' . $this->_basename . '"';
}

private function _setCurrentFile($path)
private function _setCurrentFile($path, $dir = false)
{
$this->currentStat = array(
2 => 0100444, // file mode, readable by all, writeable by none
4 => 0, // uid
5 => 0, // gid
7 => self::$_manifest[$this->_archiveName][$path][0], // size
9 => self::$_manifest[$this->_archiveName][$path][1], // creation time
);
if ($dir) {
$this->currentStat = array(
2 => 040777, // directory mode, readable by all, writeable by none
4 => 0, // uid
5 => 0, // gid
7 => 0, // size
9 => self::$_manifest[$this->_archiveName][$path][1], // creation time
);
$this->internalFileLength = 0;
$this->isDir = true;
} else {
$this->currentStat = array(
2 => 0100444, // file mode, readable by all, writeable by none
4 => 0, // uid
5 => 0, // gid
7 => self::$_manifest[$this->_archiveName][$path][0], // size
9 => self::$_manifest[$this->_archiveName][$path][1], // creation time
);
$this->internalFileLength = self::$_manifest[$this->_archiveName][$path][2];
$this->isDir = false;
}
$this->currentFilename = $path;
$this->internalFileLength = self::$_manifest[$this->_archiveName][$path][2];
// seek to offset of file header within the .phar
if (is_resource(@$this->fp)) {
fseek($this->fp, self::$_fileStart[$this->_archiveName] + self::$_manifest[$this->_archiveName][$path][7]);
Expand Down Expand Up @@ -683,7 +712,7 @@ static protected function parseUrl($file)
return false;
}
$file = substr($file, 7);

$ret = array('scheme' => 'phar');
$pos_p = strpos($file, '.phar.php');
$pos_z = strpos($file, '.phar.gz');
Expand Down Expand Up @@ -711,7 +740,7 @@ static protected function parseUrl($file)
}
return $ret;
}

/**
* Locate the .phar archive in the include_path and detect the file to open within
* the archive.
Expand Down Expand Up @@ -807,7 +836,7 @@ private function _streamOpen($file, $searchForDir = false)
return false;
}
}

/**
* Read the data - PHP streams API
*
Expand All @@ -820,7 +849,7 @@ public function stream_read($count)
$this->position += strlen($ret);
return $ret;
}

/**
* Whether we've hit the end of the file - PHP streams API
* @access private
Expand All @@ -829,7 +858,7 @@ function stream_eof()
{
return $this->position >= $this->currentStat[7];
}

/**
* For seeking the stream - PHP streams API
* @param int
Expand Down Expand Up @@ -862,7 +891,7 @@ public function stream_seek($pos, $whence)
}
return true;
}

/**
* The current position in the stream - PHP streams API
* @access private
Expand Down Expand Up @@ -988,6 +1017,9 @@ public function dir_opendir($path)
}
} elseif (strpos($file, $path) === 0) {
$fname = substr($file, strlen($path) + 1);
if ($fname == '/' || $fname[strlen($fname)-1] == '/') {
continue; // empty directory
}
if (strpos($fname, '/')) {
// this is a directory
$a = explode('/', $fname);
Expand Down Expand Up @@ -1098,14 +1130,17 @@ public static function getFileMetadata($phar, $file)
/**
* @return list of supported signature algorithmns.
*/
public static function getsupportedsignatures()
public static function getSupportedSignatures()
{
$ret = array('MD5', 'SHA-1');
if (extension_loaded('hash')) {
$ret[] = 'SHA-256';
$ret[] = 'SHA-512';
}
if (extension_loaded('openssl')) {
$ret[] = 'OpenSSL';
}
return $ret;
}
}
?>
?>
10 changes: 5 additions & 5 deletions package.php
Expand Up @@ -2,11 +2,11 @@
require_once 'PEAR/PackageFileManager.php';
require_once 'PEAR/PackageFileManager2.php';
PEAR::setErrorHAndling(PEAR_ERROR_DIE);
$version = '0.11.4';
$apiversion = '1.0.0';
$version = '0.12.0';
$apiversion = '1.1.0';
$notes = '
minor bugfix release
* E_STRICT/E_DEPRECATED errors displayed';
sync with ext/phar
* add empty directory support';


$package = PEAR_PackageFileManager2::importOptions(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'package.xml',
Expand Down Expand Up @@ -46,4 +46,4 @@
}


?>
?>

0 comments on commit 50298f4

Please sign in to comment.