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
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 19 additions & 4 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
use function is_dir;
use function is_file;
use function iterator_to_array;
use function mkdir;
use function opendir;
use function posix_geteuid;
use function random_int;
use function readdir;
vjik marked this conversation as resolved.
Show resolved Hide resolved
use function restore_error_handler;
use function rmdir;
use function serialize;
use function set_error_handler;
use function sprintf;
vjik marked this conversation as resolved.
Show resolved Hide resolved
use function strpbrk;
use function substr;
use function unlink;
Expand Down Expand Up @@ -191,7 +195,7 @@
$this->set((string) $key, $value, $ttl);
}

return true;

Check warning on line 198 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ foreach ($values as $key => $value) { $this->set((string) $key, $value, $ttl); } - return true; + return false; } public function deleteMultiple(iterable $keys) : bool {
}

public function deleteMultiple(iterable $keys): bool
Expand Down Expand Up @@ -325,13 +329,24 @@
}

if (is_file($path)) {
throw new CacheException("Failed to create cache directory, file with the same name exists: \"$path\".");

Check warning on line 332 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ return; } if (is_file($path)) { - throw new CacheException("Failed to create cache directory, file with the same name exists: \"{$path}\"."); + 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)) {
}

mkdir($path, recursive: true);

if (!is_dir($path)) {
throw new CacheException("Failed to create cache directory \"$path\".");
set_error_handler(
static function (int $errorNumber, string $errorString) use ($path): bool {
if (is_dir($path)) {
return true;

Check warning on line 338 in src/FileCache.php

View check run for this annotation

Codecov / codecov/patch

src/FileCache.php#L338

Added line #L338 was not covered by tests
}
throw new CacheException(
sprintf('Failed to create directory "%s". %s', $path, $errorString),
$errorNumber,
);
}
);
try {
mkdir($path, recursive: true);
} finally {
restore_error_handler();
}

chmod($path, $this->directoryMode);
Expand Down
9 changes: 9 additions & 0 deletions tests/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,13 @@ public function testSetClearsStatCache(): void
$this->assertTrue($this->cache->set(__FUNCTION__, 'cache2', 2));
$this->assertSame('cache2', $this->cache->get(__FUNCTION__));
}

public function testMkdirConvertingErrorToException(): void
{
$cache = new FileCache('');

$this->expectException(CacheException::class);
$this->expectExceptionMessage('Failed to create directory "". mkdir(): Invalid path');
$cache->set('test', 0);
}
}
Loading