Skip to content

FileStorage: always creates directories #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions src/Bridges/CacheDI/CacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function __construct($tempDir)

public function loadConfiguration()
{
@mkdir($this->tempDir . '/cache'); // @ - directory may exists
Copy link
Contributor

@fprochazka fprochazka Oct 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho there should be a check for the return value and exception, no?

if (!@mkdir(...) && !is_dir(...)) throw

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileStorage will throw exception.


$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('journal'))
Expand All @@ -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;
}

}
10 changes: 3 additions & 7 deletions src/Caching/Storages/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down