-
-
Notifications
You must be signed in to change notification settings - Fork 11
Don't create cache directory in constructor + Fix nested directory permissions #88
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,6 @@ | |
| use function array_keys; | ||
| use function array_map; | ||
| use function closedir; | ||
| use function dirname; | ||
| use function error_get_last; | ||
| use function filemtime; | ||
| use function fileowner; | ||
|
|
@@ -89,11 +88,8 @@ | |
| */ | ||
| public function __construct( | ||
| private string $cachePath, | ||
| private int $directoryMode = 0775, | ||
|
Check warning on line 91 in src/FileCache.php
|
||
| ) { | ||
| if (!$this->createDirectoryIfNotExists($cachePath)) { | ||
| throw new CacheException("Failed to create cache directory \"$cachePath\"."); | ||
| } | ||
| } | ||
|
|
||
| public function get(string $key, mixed $default = null): mixed | ||
|
|
@@ -107,7 +103,7 @@ | |
|
|
||
| flock($filePointer, LOCK_SH); | ||
| $value = stream_get_contents($filePointer); | ||
| flock($filePointer, LOCK_UN); | ||
|
Check warning on line 106 in src/FileCache.php
|
||
| fclose($filePointer); | ||
|
|
||
| return unserialize($value); | ||
|
|
@@ -119,23 +115,16 @@ | |
| $this->gc(); | ||
| $expiration = $this->ttlToExpiration($ttl); | ||
|
|
||
| if ($expiration <= self::EXPIRATION_EXPIRED) { | ||
|
Check warning on line 118 in src/FileCache.php
|
||
| return $this->delete($key); | ||
| } | ||
|
|
||
| $file = $this->getCacheFile($key); | ||
| $cacheDirectory = dirname($file); | ||
|
|
||
| if (!is_dir($this->cachePath) | ||
| || $this->directoryLevel > 0 && !$this->createDirectoryIfNotExists($cacheDirectory) | ||
| ) { | ||
| throw new CacheException("Failed to create cache directory \"$cacheDirectory\"."); | ||
| } | ||
| $file = $this->getCacheFile($key, ensureDirectory: true); | ||
|
|
||
| // If ownership differs, the touch call will fail, so we try to | ||
| // rebuild the file from scratch by deleting it first | ||
| // https://github.com/yiisoft/yii2/pull/16120 | ||
| if (function_exists('posix_geteuid') && is_file($file) && fileowner($file) !== posix_geteuid()) { | ||
|
Check warning on line 127 in src/FileCache.php
|
||
| @unlink($file); | ||
| } | ||
|
|
||
|
|
@@ -325,25 +314,27 @@ | |
| } | ||
|
|
||
| /** | ||
| * Ensures that the directory is created. | ||
| * Ensures that the directory is existing. | ||
| * | ||
| * @param string $path The path to the directory. | ||
| * | ||
| * @return bool Whether the directory was created. | ||
| */ | ||
| private function createDirectoryIfNotExists(string $path): bool | ||
| private function ensureDirectory(string $path): void | ||
| { | ||
| if (is_dir($path)) { | ||
| return true; | ||
| return; | ||
| } | ||
|
|
||
| if (is_file($path)) { | ||
| throw new CacheException("Failed to create cache directory \"$path\"."); | ||
vjik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| $result = !is_file($path) && mkdir(directory: $path, recursive: true) && is_dir($path); | ||
| mkdir($path, recursive: true); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it fail in case of race condition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will. Race condition don't fixed in this PR. I make it next PR. |
||
|
|
||
| if ($result) { | ||
| chmod($path, $this->directoryMode); | ||
| if (!is_dir($path)) { | ||
| throw new CacheException("Failed to create cache directory \"$path\"."); | ||
| } | ||
|
|
||
| return $result; | ||
| chmod($path, $this->directoryMode); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -353,8 +344,12 @@ | |
| * | ||
| * @return string The cache file path. | ||
| */ | ||
| private function getCacheFile(string $key): string | ||
| private function getCacheFile(string $key, bool $ensureDirectory = false): string | ||
| { | ||
| if ($ensureDirectory) { | ||
| $this->ensureDirectory($this->cachePath); | ||
| } | ||
|
|
||
| if ($this->directoryLevel < 1) { | ||
| return $this->cachePath . DIRECTORY_SEPARATOR . $key . $this->fileSuffix; | ||
| } | ||
|
|
@@ -364,6 +359,9 @@ | |
| for ($i = 0; $i < $this->directoryLevel; ++$i) { | ||
| if (($prefix = substr($key, $i + $i, 2)) !== '') { | ||
| $base .= DIRECTORY_SEPARATOR . $prefix; | ||
| if ($ensureDirectory) { | ||
| $this->ensureDirectory($base); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.