/usr/share/php/Horde/Cache/Storage/File.php is in php-horde-cache 2.5.2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | <?php
/**
* Copyright 1999-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 1999-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
/**
* Cache storage in the filesystem.
*
* @author Anil Madhavapeddy <anil@recoil.org>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 1999-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Cache
*/
class Horde_Cache_Storage_File extends Horde_Cache_Storage_Base
{
/* Location of the garbage collection data file. */
const GC_FILE = 'horde_cache_gc';
/**
* List of key to filename mappings.
*
* @var array
*/
protected $_file = array();
/**
* Constructor.
*
* @param array $params Optional parameters:
* <pre>
* - dir: (string) The base directory to store the cache files in.
* DEFAULT: System default
* - no_gc: (boolean) If true, don't perform garbage collection.
* DEFAULT: false
* - prefix: (string) The filename prefix to use for the cache files.
* DEFAULT: 'cache_'
* - sub: (integer) If non-zero, the number of subdirectories to create
* to store the file (i.e. PHP's session.save_path).
* DEFAULT: 0
* </pre>
*/
public function __construct(array $params = array())
{
$params = array_merge(array(
'prefix' => 'cache_',
'sub' => 0
), $params);
if (!isset($params['dir']) || !@is_dir($params['dir'])) {
$params['dir'] = sys_get_temp_dir();
}
parent::__construct($params);
}
/**
* Destructor.
*/
public function __destruct()
{
$c_time = time();
/* Only do garbage collection 0.1% of the time we create an object. */
if (!empty($this->_params['no_gc']) ||
(intval(substr($c_time, -3)) !== 0)) {
return;
}
$this->_gc();
}
/**
*/
public function get($key, $lifetime = 0)
{
if (!$this->exists($key, $lifetime)) {
/* Nothing cached, return failure. */
return false;
}
$filename = $this->_keyToFile($key);
$size = filesize($filename);
return $size
? @file_get_contents($filename)
: '';
}
/**
*/
public function set($key, $data, $lifetime = 0)
{
$filename = $this->_keyToFile($key, true);
$tmp_file = Horde_Util::getTempFile('HordeCache', true, $this->_params['dir']);
if (isset($this->_params['umask'])) {
chmod($tmp_file, 0666 & ~$this->_params['umask']);
}
if (file_put_contents($tmp_file, $data) === false) {
throw new Horde_Cache_Exception('Cannot write to cache directory ' . $this->_params['dir']);
}
@rename($tmp_file, $filename);
if ($lifetime &&
($fp = @fopen(dirname($filename) . '/' . self::GC_FILE, 'a'))) {
// This may result in duplicate entries in GC_FILE, but we
// will take care of these whenever we do GC and this is quicker
// than having to check every time we access the file.
fwrite($fp, $filename . "\t" . (time() + $lifetime) . "\n");
fclose($fp);
}
}
/**
*/
public function exists($key, $lifetime = 0)
{
$filename = $this->_keyToFile($key);
/* Key exists in the cache */
if (file_exists($filename)) {
/* 0 means no expire.
* Also, If the file was been created after the supplied value,
* the data is valid (fresh). */
if (($lifetime == 0) ||
(time() - $lifetime <= filemtime($filename))) {
return true;
}
@unlink($filename);
}
return false;
}
/**
*/
public function expire($key)
{
return @unlink($this->_keyToFile($key));
}
/**
*/
public function clear()
{
foreach ($this->_getCacheFiles() as $val) {
@unlink($val);
}
foreach ($this->_getGCFiles() as $val) {
@unlink($val);
}
}
/**
* Return a list of cache files.
*
* @param string $start The directory to start searching.
*
* @return array Pathnames to cache files.
*/
protected function _getCacheFiles($start = null)
{
$paths = array();
try {
$it = empty($this->_params['sub'])
? new DirectoryIterator($this->_params['dir'])
: new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start ?: $this->_params['dir']), RecursiveIteratorIterator::CHILD_FIRST);
} catch (UnexpectedValueException $e) {
return $paths;
}
foreach ($it as $val) {
if (!$val->isDir() &&
($fname = $val->getFilename()) &&
(strpos($fname, $this->_params['prefix']) === 0)) {
$paths[$fname] = $val->getPathname();
}
}
return $paths;
}
/**
* Return a list of GC indexes.
*
* @return array Pathnames to GC indexes.
*/
protected function _getGCFiles()
{
$glob = $this->_params['dir'];
if (!empty($this->_params['sub'])) {
$glob .= '/'
. implode('/', array_fill(0, $this->_params['sub'], '*'));
}
$glob .= '/' . self::GC_FILE;
return glob($glob);
}
/**
* Map a cache key to a unique filename.
*
* @param string $key Cache key.
* @param string $create Create path if it doesn't exist?
*
* @return string Fully qualified filename.
*/
protected function _keyToFile($key, $create = false)
{
if ($create || !isset($this->_file[$key])) {
$dir = $this->_params['dir'] . '/';
$md5 = hash('md5', $key);
$sub = '';
if (!empty($this->_params['sub'])) {
$max = min($this->_params['sub'], strlen($md5));
for ($i = 0; $i < $max; $i++) {
$sub .= $md5[$i];
if ($create && !is_dir($dir . $sub)) {
if (!mkdir($dir . $sub)) {
$sub = '';
break;
}
}
$sub .= '/';
}
}
$this->_file[$key] = $dir . $sub . $this->_params['prefix'] . $md5;
}
return $this->_file[$key];
}
/**
* Garbage collector.
*/
protected function _gc()
{
$c_time = time();
$excepts = array();
if (!empty($this->_params['sub']) &&
file_exists($this->_params['dir'] . '/' . self::GC_FILE)) {
// If we cannot migrate, we cannot GC either, because we expect the
// new format.
try {
$this->_migrateGc();
} catch (Horde_Cache_Exception $e) {
return;
}
}
foreach ($this->_getGCFiles() as $filename) {
if (is_readable($filename)) {
$fp = fopen($filename, 'r');
while (!feof($fp) && ($data = fgets($fp))) {
$parts = explode("\t", trim($data), 2);
$excepts[$parts[0]] = $parts[1];
}
}
foreach ($this->_getCacheFiles(dirname($filename)) as $pname) {
if (!empty($excepts[$pname]) &&
($c_time > $excepts[$pname])) {
@unlink($pname);
unset($excepts[$pname]);
}
}
if ($fp = @fopen($filename, 'w')) {
foreach ($excepts as $key => $val) {
fwrite($fp, $key . "\t" . $val . "\n");
}
fclose($fp);
}
}
}
/**
* Migrates single GC indexes to per-directory indexes.
*/
protected function _migrateGc()
{
// Read the old GC index.
$filename = $this->_params['dir'] . '/' . self::GC_FILE;
if (!is_readable($filename)) {
return;
}
$fhs = array();
$fp = fopen($filename, 'r');
if (!flock($fp, LOCK_EX)) {
throw new Horde_Cache_Exception('Cannot acquire lock for old garbage collection index');
}
// Loops through all cached files from the old index and write their GC
// information to the new GC indexes.
while (!feof($fp) && ($data = fgets($fp))) {
list($path, $time) = explode("\t", trim($data), 2);
$dir = dirname($path);
if ($dir == $this->_params['dir']) {
continue;
}
if (!isset($fhs[$dir])) {
$fhs[$dir] = @fopen($dir . '/' . self::GC_FILE, 'a');
// Maybe too many open file handles?
if (!$fhs[$dir] && count($fhs)) {
unset($fhs[$dir]);
foreach ($fhs as $fh) {
fclose($fh);
}
$fhs = array();
$fhs[$dir] = @fopen($dir . '/' . self::GC_FILE, 'a');
}
if (!$fhs[$dir]) {
throw new Horde_Cache_Exception('Cannot migrate to new garbage collection index format');
}
}
fwrite($fhs[$dir], $path . "\t" . $time . "\n");
}
// Clean up.
foreach ($fhs as $fh) {
fclose($fh);
}
fclose($fp);
unlink($filename);
}
}
|