From 4d261dd53bfc701869f7259b0d0f4c511a30785a Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 6 Oct 2016 02:29:24 +0200 Subject: [PATCH] FileStorage: always creates directories --- src/Bridges/CacheDI/CacheExtension.php | 30 ++------------------------ src/Caching/Storages/FileStorage.php | 10 +++------ 2 files changed, 5 insertions(+), 35 deletions(-) diff --git a/src/Bridges/CacheDI/CacheExtension.php b/src/Bridges/CacheDI/CacheExtension.php index f7dff410..7a67adaa 100644 --- a/src/Bridges/CacheDI/CacheExtension.php +++ b/src/Bridges/CacheDI/CacheExtension.php @@ -29,6 +29,8 @@ public function __construct($tempDir) public function loadConfiguration() { + @mkdir($this->tempDir . '/cache'); // @ - directory may exists + $builder = $this->getContainerBuilder(); $builder->addDefinition($this->prefix('journal')) @@ -45,32 +47,4 @@ public function loadConfiguration() } } - - public function afterCompile(Nette\PhpGenerator\ClassType $class) - { - if (!$this->checkTempDir($this->tempDir . '/cache')) { - $class->getMethod('initialize')->addBody('Nette\Caching\Storages\FileStorage::$useDirectories = FALSE;'); - } - } - - - private function checkTempDir($dir): bool - { - @mkdir($dir); // @ - directory may exists - - // checks whether directory is writable - $uniq = uniqid('_', TRUE); - if (!@mkdir("$dir/$uniq")) { // @ - is escalated to exception - throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable."); - } - - // checks whether subdirectory is writable - $isWritable = @file_put_contents("$dir/$uniq/_", '') !== FALSE; // @ - error is expected - if ($isWritable) { - unlink("$dir/$uniq/_"); - } - rmdir("$dir/$uniq"); - return $isWritable; - } - } diff --git a/src/Caching/Storages/FileStorage.php b/src/Caching/Storages/FileStorage.php index 1704234f..d960289d 100644 --- a/src/Caching/Storages/FileStorage.php +++ b/src/Caching/Storages/FileStorage.php @@ -51,15 +51,12 @@ class FileStorage implements Nette\Caching\IStorage /** @var float probability that the clean() routine is started */ public static $gcProbability = 0.001; - /** @var bool */ + /** @deprecated */ public static $useDirectories = TRUE; /** @var string */ private $dir; - /** @var bool */ - private $useDirs; - /** @var IJournal */ private $journal; @@ -74,7 +71,6 @@ public function __construct($dir, IJournal $journal = NULL) } $this->dir = $dir; - $this->useDirs = (bool) static::$useDirectories; $this->journal = $journal; if (mt_rand() / mt_getrandmax() < static::$gcProbability) { @@ -143,7 +139,7 @@ private function verify(array $meta): bool public function lock(string $key): void { $cacheFile = $this->getCacheFile($key); - if ($this->useDirs && !is_dir($dir = dirname($cacheFile))) { + if (!is_dir($dir = dirname($cacheFile))) { @mkdir($dir); // @ - directory may already exist } $handle = fopen($cacheFile, 'c+b'); @@ -353,7 +349,7 @@ protected function readData(array $meta) protected function getCacheFile(string $key): string { $file = urlencode($key); - if ($this->useDirs && $a = strrpos($file, '%00')) { // %00 = urlencode(Nette\Caching\Cache::NAMESPACE_SEPARATOR) + if ($a = strrpos($file, '%00')) { // %00 = urlencode(Nette\Caching\Cache::NAMESPACE_SEPARATOR) $file = substr_replace($file, '/_', $a, 3); } return $this->dir . '/_' . $file;