-
-
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -91,9 +90,6 @@ | |
private string $cachePath, | ||
private int $directoryMode = 0775, | ||
) { | ||
if (!$this->createDirectoryIfNotExists($cachePath)) { | ||
throw new CacheException("Failed to create cache directory \"$cachePath\"."); | ||
} | ||
} | ||
|
||
public function get(string $key, mixed $default = null): mixed | ||
|
@@ -105,7 +101,7 @@ | |
return $default; | ||
} | ||
|
||
flock($filePointer, LOCK_SH); | ||
Check warning on line 104 in src/FileCache.php
|
||
$value = stream_get_contents($filePointer); | ||
flock($filePointer, LOCK_UN); | ||
fclose($filePointer); | ||
|
@@ -123,14 +119,7 @@ | |
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 | ||
|
@@ -196,7 +185,7 @@ | |
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool | ||
{ | ||
$values = $this->iterableToArray($values); | ||
$this->validateKeys(array_map('\strval', array_keys($values))); | ||
Check warning on line 188 in src/FileCache.php
|
||
|
||
foreach ($values as $key => $value) { | ||
$this->set((string) $key, $value, $ttl); | ||
|
@@ -325,25 +314,27 @@ | |
} | ||
|
||
/** | ||
* Ensures that the directory is created. | ||
* Ensures that the directory exists. | ||
* | ||
* @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, file with the same name exists: \"$path\"."); | ||
} | ||
|
||
$result = !is_file($path) && mkdir(directory: $path, recursive: true) && is_dir($path); | ||
mkdir($path, recursive: true); | ||
|
||
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); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it fail in case of race condition?
is_dir
+mkdir
is not an atomic operation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.