Skip to content
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

Handle race condition when creating cache directory #89

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Changes from 1 commit
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
Next Next commit
Handle race condition when creating cache directory
vjik committed Jan 11, 2025

Verified

This commit was signed with the committer’s verified signature.
A6GibKm Maximiliano
commit e5a722dba24aa0086acc5d7a163b11b642b34d05
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
- Enh #88: Don't create cache directory on `FileCache` initialization (@vjik)
- Bug #88: Set correct permissions for nested directories (@vjik)
- Bug #85: Clear stat cache in `FileCache::set()` (@samdark)
- Bug #86: Handle race condition when creating cache directory (@vjik)

## 3.1.0 October 09, 2023

20 changes: 16 additions & 4 deletions src/FileCache.php
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
use function readdir;
vjik marked this conversation as resolved.
Show resolved Hide resolved
use function rmdir;
use function serialize;
use function sprintf;
vjik marked this conversation as resolved.
Show resolved Hide resolved
use function strpbrk;
use function substr;
use function unlink;
@@ -124,7 +125,7 @@
// 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 128 in src/FileCache.php

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ // 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()) { + if (!(function_exists('posix_geteuid') && is_file($file) && fileowner($file) !== posix_geteuid())) { @Unlink($file); } if (file_put_contents($file, serialize($value), LOCK_EX) === false) {
@unlink($file);
}

@@ -185,7 +186,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 189 in src/FileCache.php

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function setMultiple(iterable $values, null|int|DateInterval $ttl = null) : bool { $values = $this->iterableToArray($values); - $this->validateKeys(array_map('\\strval', array_keys($values))); + foreach ($values as $key => $value) { $this->set((string) $key, $value, $ttl); }

foreach ($values as $key => $value) {
$this->set((string) $key, $value, $ttl);
@@ -203,7 +204,7 @@
$this->delete($key);
}

return true;

Check warning on line 207 in src/FileCache.php

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ foreach ($keys as $key) { $this->delete($key); } - return true; + return false; } public function has(string $key) : bool {
}

public function has(string $key): bool
@@ -328,10 +329,21 @@
throw new CacheException("Failed to create cache directory, file with the same name exists: \"$path\".");
}

mkdir($path, recursive: true);

if (!is_dir($path)) {
throw new CacheException("Failed to create cache directory \"$path\".");
set_error_handler(

Check warning on line 332 in src/FileCache.php

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ if (is_file($path)) { throw new CacheException("Failed to create cache directory, file with the same name exists: \"{$path}\"."); } - set_error_handler(static function (int $errorNumber, string $errorString) use($path) : bool { - if (is_dir($path)) { - return true; - } - throw new CacheException(sprintf('Failed to create directory "%s". %s', $path, $errorString), $errorNumber); - }); + try { mkdir($path, recursive: true); } finally {
static function (int $errorNumber, string $errorString) use ($path): bool {
if (is_dir($path)) {
return true;

Check warning on line 335 in src/FileCache.php

Codecov / codecov/patch

src/FileCache.php#L334-L335

Added lines #L334 - L335 were not covered by tests
}
throw new CacheException(
sprintf('Failed to create directory "%s". %s', $path, $errorString),
$errorNumber,
);

Check warning on line 340 in src/FileCache.php

Codecov / codecov/patch

src/FileCache.php#L337-L340

Added lines #L337 - L340 were not covered by tests
}
);
try {
mkdir($path, recursive: true);
} finally {
restore_error_handler();
}

chmod($path, $this->directoryMode);