mirror of
https://github.com/php/php-src.git
synced 2025-08-16 14:08:47 +02:00
* made rebuildFileMap method public, added caching and checkFileMap method
This commit is contained in:
parent
8619108154
commit
81e5b17f91
2 changed files with 202 additions and 44 deletions
|
@ -29,7 +29,9 @@ TODO:
|
||||||
require_once "System.php";
|
require_once "System.php";
|
||||||
require_once "PEAR.php";
|
require_once "PEAR.php";
|
||||||
|
|
||||||
define("PEAR_REGISTRY_ERROR_LOCK", -2);
|
define('PEAR_REGISTRY_ERROR_LOCK', -2);
|
||||||
|
define('PEAR_REGISTRY_ERROR_FORMAT', -3);
|
||||||
|
define('PEAR_REGISTRY_ERROR_FILE', -4);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Administration class used to maintain the installed package database.
|
* Administration class used to maintain the installed package database.
|
||||||
|
@ -63,6 +65,20 @@ class PEAR_Registry extends PEAR
|
||||||
*/
|
*/
|
||||||
var $lock_mode = 0; // XXX UNUSED
|
var $lock_mode = 0; // XXX UNUSED
|
||||||
|
|
||||||
|
/** Cache of package information. Structure:
|
||||||
|
* array(
|
||||||
|
* 'package' => array('id' => ... ),
|
||||||
|
* ... )
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
var $pkginfo_cache = array();
|
||||||
|
|
||||||
|
/** Cache of file map. Structure:
|
||||||
|
* array( '/path/to/file' => 'package', ... )
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
var $filemap_cache = array();
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// {{{ constructor
|
// {{{ constructor
|
||||||
|
@ -78,11 +94,12 @@ class PEAR_Registry extends PEAR
|
||||||
{
|
{
|
||||||
parent::PEAR();
|
parent::PEAR();
|
||||||
$ds = DIRECTORY_SEPARATOR;
|
$ds = DIRECTORY_SEPARATOR;
|
||||||
|
$this->install_dir = $pear_install_dir;
|
||||||
$this->statedir = $pear_install_dir.$ds.'.registry';
|
$this->statedir = $pear_install_dir.$ds.'.registry';
|
||||||
$this->filemap = $pear_install_dir.$ds.'.filemap';
|
$this->filemap = $pear_install_dir.$ds.'.filemap';
|
||||||
$this->lockfile = $pear_install_dir.$ds.'.lock';
|
$this->lockfile = $pear_install_dir.$ds.'.lock';
|
||||||
if (!file_exists($this->filemap)) {
|
if (!file_exists($this->filemap)) {
|
||||||
$this->_rebuildFileMap();
|
$this->rebuildFileMap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,9 +181,9 @@ class PEAR_Registry extends PEAR
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
// {{{ _rebuildFileMap()
|
// {{{ rebuildFileMap()
|
||||||
|
|
||||||
function _rebuildFileMap()
|
function rebuildFileMap()
|
||||||
{
|
{
|
||||||
$packages = $this->listPackages();
|
$packages = $this->listPackages();
|
||||||
$files = array();
|
$files = array();
|
||||||
|
@ -194,11 +211,32 @@ class PEAR_Registry extends PEAR
|
||||||
if (!$fp) {
|
if (!$fp) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$this->filemap_cache = $files;
|
||||||
fwrite($fp, serialize($files));
|
fwrite($fp, serialize($files));
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
// {{{ readFileMap()
|
||||||
|
|
||||||
|
function readFileMap()
|
||||||
|
{
|
||||||
|
$fp = @fopen($this->filemap, 'r');
|
||||||
|
if (!$fp) {
|
||||||
|
return $this->raiseError('PEAR_Registry: could not open filemap', PEAR_REGISTRY_ERROR_FILE, null, null, $php_errormsg);
|
||||||
|
}
|
||||||
|
$fsize = filesize($this->filemap);
|
||||||
|
$data = fread($fp, $fsize);
|
||||||
|
fclose($fp);
|
||||||
|
$tmp = unserialize($data);
|
||||||
|
if (!$tmp && $fsize > 7) {
|
||||||
|
return $this->raiseError('PEAR_Registry: invalid filemap data', PEAR_REGISTRY_ERROR_FORMAT, null, null, $data);
|
||||||
|
}
|
||||||
|
$this->filemap = $tmp;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
// {{{ _lock()
|
// {{{ _lock()
|
||||||
|
|
||||||
|
@ -216,7 +254,7 @@ class PEAR_Registry extends PEAR
|
||||||
*/
|
*/
|
||||||
function _lock($mode = LOCK_EX)
|
function _lock($mode = LOCK_EX)
|
||||||
{
|
{
|
||||||
if(!strstr(php_uname(), 'Windows 95/98')) {
|
if (!strstr(php_uname(), 'Windows 95/98')) {
|
||||||
if ($mode != LOCK_UN && is_resource($this->lock_fp)) {
|
if ($mode != LOCK_UN && is_resource($this->lock_fp)) {
|
||||||
// XXX does not check type of lock (LOCK_SH/LOCK_EX)
|
// XXX does not check type of lock (LOCK_SH/LOCK_EX)
|
||||||
return true;
|
return true;
|
||||||
|
@ -389,7 +427,7 @@ class PEAR_Registry extends PEAR
|
||||||
}
|
}
|
||||||
$file = $this->_packageFileName($package);
|
$file = $this->_packageFileName($package);
|
||||||
$ret = @unlink($file);
|
$ret = @unlink($file);
|
||||||
$this->_rebuildFileMap();
|
$this->rebuildFileMap();
|
||||||
$this->_unlock();
|
$this->_unlock();
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
@ -406,9 +444,6 @@ class PEAR_Registry extends PEAR
|
||||||
if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
|
if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
|
||||||
return $e;
|
return $e;
|
||||||
}
|
}
|
||||||
if (!file_exists($this->filemap)) {
|
|
||||||
$this->_rebuildFileMap();
|
|
||||||
}
|
|
||||||
$fp = $this->_openPackageFile($package, 'w');
|
$fp = $this->_openPackageFile($package, 'w');
|
||||||
if ($fp === null) {
|
if ($fp === null) {
|
||||||
$this->_unlock();
|
$this->_unlock();
|
||||||
|
@ -422,13 +457,61 @@ class PEAR_Registry extends PEAR
|
||||||
}
|
}
|
||||||
$this->_closePackageFile($fp);
|
$this->_closePackageFile($fp);
|
||||||
if (isset($info['filelist'])) {
|
if (isset($info['filelist'])) {
|
||||||
$this->_rebuildFileMap();
|
$this->rebuildFileMap();
|
||||||
}
|
}
|
||||||
$this->_unlock();
|
$this->_unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
// {{{ checkFileMap()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test whether a file belongs to a package.
|
||||||
|
*
|
||||||
|
* @param string $path file path, absolute or relative to the pear
|
||||||
|
* install dir
|
||||||
|
*
|
||||||
|
* @return string which package the file belongs to, or an empty
|
||||||
|
* string if the file does not belong to an installed package
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function checkFileMap($path)
|
||||||
|
{
|
||||||
|
if (is_array($path)) {
|
||||||
|
static $notempty;
|
||||||
|
if (empty($notempty)) {
|
||||||
|
$notempty = create_function('$a','return !empty($a);');
|
||||||
|
}
|
||||||
|
$pkgs = array();
|
||||||
|
foreach ($path as $name => $attrs) {
|
||||||
|
if (isset($attrs['baseinstalldir'])) {
|
||||||
|
$name = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
|
||||||
|
}
|
||||||
|
$pkgs[$name] = $this->checkFileMap($name);
|
||||||
|
}
|
||||||
|
return array_filter($pkgs, $notempty);
|
||||||
|
}
|
||||||
|
if (empty($this->filemap_cache) && PEAR::isError($this->readFileMap())) {
|
||||||
|
return $err;
|
||||||
|
}
|
||||||
|
if (isset($this->filemap_cache[$path])) {
|
||||||
|
return $this->filemap_cache[$path];
|
||||||
|
}
|
||||||
|
$l = strlen($this->install_dir);
|
||||||
|
if (substr($path, 0, $l) == $this->install_dir) {
|
||||||
|
$path = preg_replace('!^'.DIRECTORY_SEPARATOR.'+!', '', substr($path, $l));
|
||||||
|
}
|
||||||
|
if (isset($this->filemap_cache[$path])) {
|
||||||
|
return $this->filemap_cache[$path];
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
// {{{ rebuildDepsFile()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Experimental dependencies database handling functions (not yet in production)
|
Experimental dependencies database handling functions (not yet in production)
|
||||||
|
@ -601,6 +684,8 @@ class PEAR_Registry extends PEAR
|
||||||
}
|
}
|
||||||
return $this->_depWriteDepDB($data);
|
return $this->_depWriteDepDB($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
|
@ -8,24 +8,72 @@ include dirname(__FILE__)."/../PEAR/Registry.php";
|
||||||
PEAR::setErrorHandling(PEAR_ERROR_DIE, "%s\n");
|
PEAR::setErrorHandling(PEAR_ERROR_DIE, "%s\n");
|
||||||
cleanall();
|
cleanall();
|
||||||
|
|
||||||
|
$files1 = array(
|
||||||
|
"pkg1-1.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
),
|
||||||
|
"pkg1-2.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
"baseinstalldir" => "pkg1",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$files2 = array(
|
||||||
|
"pkg2-1.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
),
|
||||||
|
"pkg2-2.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
"baseinstalldir" => "pkg2",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$files3 = array(
|
||||||
|
"pkg3-1.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
),
|
||||||
|
"pkg3-2.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
"baseinstalldir" => "pkg3",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$files3_new = array(
|
||||||
|
"pkg3-3.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
"baseinstalldir" => "pkg3",
|
||||||
|
),
|
||||||
|
"pkg3-4.php" => array(
|
||||||
|
"role" => "php",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
print "creating registry object\n";
|
print "creating registry object\n";
|
||||||
$reg = new PEAR_Registry;
|
$reg = new PEAR_Registry;
|
||||||
$reg->statedir = getcwd();
|
$reg->statedir = getcwd();
|
||||||
dumpall($reg);
|
dumpall($reg);
|
||||||
$reg->addPackage("pkg1", array("name" => "pkg1", "version" => "1.0"));
|
|
||||||
|
$reg->addPackage("pkg1", array("name" => "pkg1", "version" => "1.0", "filelist" => $files1));
|
||||||
dumpall($reg);
|
dumpall($reg);
|
||||||
$reg->addPackage("pkg2", array("name" => "pkg2", "version" => "2.0"));
|
|
||||||
$reg->addPackage("pkg3", array("name" => "pkg3", "version" => "3.0"));
|
$reg->addPackage("pkg2", array("name" => "pkg2", "version" => "2.0", "filelist" => $files2));
|
||||||
|
$reg->addPackage("pkg3", array("name" => "pkg3", "version" => "3.0", "filelist" => $files3));
|
||||||
dumpall($reg);
|
dumpall($reg);
|
||||||
|
|
||||||
$reg->updatePackage("pkg2", array("version" => "2.1"));
|
$reg->updatePackage("pkg2", array("version" => "2.1"));
|
||||||
dumpall($reg);
|
dumpall($reg);
|
||||||
|
|
||||||
var_dump($reg->deletePackage("pkg2"));
|
var_dump($reg->deletePackage("pkg2"));
|
||||||
dumpall($reg);
|
dumpall($reg);
|
||||||
|
|
||||||
var_dump($reg->deletePackage("pkg2"));
|
var_dump($reg->deletePackage("pkg2"));
|
||||||
dumpall($reg);
|
dumpall($reg);
|
||||||
|
|
||||||
$reg->updatePackage("pkg3", array("version" => "3.1b1", "status" => "beta"));
|
$reg->updatePackage("pkg3", array("version" => "3.1b1", "status" => "beta"));
|
||||||
dumpall($reg);
|
dumpall($reg);
|
||||||
|
|
||||||
|
print_r($reg->checkFilemap(array_merge($files3, $files2)));
|
||||||
|
|
||||||
|
$reg->updatePackage("pkg3", array("filelist" => $files3_new));
|
||||||
|
dumpall($reg);
|
||||||
|
|
||||||
print "tests done\n";
|
print "tests done\n";
|
||||||
|
|
||||||
cleanall();
|
cleanall();
|
||||||
|
@ -51,8 +99,24 @@ function dumpall(&$reg)
|
||||||
unset($pkg["name"]);
|
unset($pkg["name"]);
|
||||||
foreach ($pkg as $k => $v) {
|
foreach ($pkg as $k => $v) {
|
||||||
if ($k == '_lastmodified') continue;
|
if ($k == '_lastmodified') continue;
|
||||||
|
if (is_array($v) && $k == 'filelist') {
|
||||||
|
print " $k=array(";
|
||||||
|
$i = 0;
|
||||||
|
foreach ($v as $k2 => $v2) {
|
||||||
|
if ($i++ > 0) print ",";
|
||||||
|
print "{$k2}[";
|
||||||
|
$j = 0;
|
||||||
|
foreach ($v2 as $k3 => $v3) {
|
||||||
|
if ($j++ > 0) print ",";
|
||||||
|
print "$k3=$v3";
|
||||||
|
}
|
||||||
|
print "]";
|
||||||
|
}
|
||||||
|
print ")";
|
||||||
|
} else {
|
||||||
print " $k=\"$v\"";
|
print " $k=\"$v\"";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
print "\n";
|
print "\n";
|
||||||
}
|
}
|
||||||
print "dump done\n";
|
print "dump done\n";
|
||||||
|
@ -64,30 +128,39 @@ creating registry object
|
||||||
dumping registry...
|
dumping registry...
|
||||||
dump done
|
dump done
|
||||||
dumping registry...
|
dumping registry...
|
||||||
pkg1: version="1.0"
|
pkg1: version="1.0" filelist=array(pkg1-1.php[role=php],pkg1-2.php[role=php,baseinstalldir=pkg1])
|
||||||
dump done
|
dump done
|
||||||
dumping registry...
|
dumping registry...
|
||||||
pkg1: version="1.0"
|
pkg1: version="1.0" filelist=array(pkg1-1.php[role=php],pkg1-2.php[role=php,baseinstalldir=pkg1])
|
||||||
pkg2: version="2.0"
|
pkg2: version="2.0" filelist=array(pkg2-1.php[role=php],pkg2-2.php[role=php,baseinstalldir=pkg2])
|
||||||
pkg3: version="3.0"
|
pkg3: version="3.0" filelist=array(pkg3-1.php[role=php],pkg3-2.php[role=php,baseinstalldir=pkg3])
|
||||||
dump done
|
dump done
|
||||||
dumping registry...
|
dumping registry...
|
||||||
pkg1: version="1.0"
|
pkg1: version="1.0" filelist=array(pkg1-1.php[role=php],pkg1-2.php[role=php,baseinstalldir=pkg1])
|
||||||
pkg2: version="2.1"
|
pkg2: version="2.1" filelist=array(pkg2-1.php[role=php],pkg2-2.php[role=php,baseinstalldir=pkg2])
|
||||||
pkg3: version="3.0"
|
pkg3: version="3.0" filelist=array(pkg3-1.php[role=php],pkg3-2.php[role=php,baseinstalldir=pkg3])
|
||||||
dump done
|
dump done
|
||||||
bool(true)
|
bool(true)
|
||||||
dumping registry...
|
dumping registry...
|
||||||
pkg1: version="1.0"
|
pkg1: version="1.0" filelist=array(pkg1-1.php[role=php],pkg1-2.php[role=php,baseinstalldir=pkg1])
|
||||||
pkg3: version="3.0"
|
pkg3: version="3.0" filelist=array(pkg3-1.php[role=php],pkg3-2.php[role=php,baseinstalldir=pkg3])
|
||||||
dump done
|
dump done
|
||||||
bool(false)
|
bool(false)
|
||||||
dumping registry...
|
dumping registry...
|
||||||
pkg1: version="1.0"
|
pkg1: version="1.0" filelist=array(pkg1-1.php[role=php],pkg1-2.php[role=php,baseinstalldir=pkg1])
|
||||||
pkg3: version="3.0"
|
pkg3: version="3.0" filelist=array(pkg3-1.php[role=php],pkg3-2.php[role=php,baseinstalldir=pkg3])
|
||||||
dump done
|
dump done
|
||||||
dumping registry...
|
dumping registry...
|
||||||
pkg1: version="1.0"
|
pkg1: version="1.0" filelist=array(pkg1-1.php[role=php],pkg1-2.php[role=php,baseinstalldir=pkg1])
|
||||||
pkg3: version="3.1b1" status="beta"
|
pkg3: version="3.1b1" filelist=array(pkg3-1.php[role=php],pkg3-2.php[role=php,baseinstalldir=pkg3]) status="beta"
|
||||||
|
dump done
|
||||||
|
Array
|
||||||
|
(
|
||||||
|
[pkg3-1.php] => pkg3
|
||||||
|
[pkg3/pkg3-2.php] => pkg3
|
||||||
|
)
|
||||||
|
dumping registry...
|
||||||
|
pkg1: version="1.0" filelist=array(pkg1-1.php[role=php],pkg1-2.php[role=php,baseinstalldir=pkg1])
|
||||||
|
pkg3: version="3.1b1" filelist=array(pkg3-3.php[role=php,baseinstalldir=pkg3],pkg3-4.php[role=php]) status="beta"
|
||||||
dump done
|
dump done
|
||||||
tests done
|
tests done
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue