Skip to content

Commit

Permalink
bug #33930 [Cache] clean tags folder on invalidation (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3 branch.

Discussion
----------

[Cache] clean tags folder on invalidation

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

/cc @andrerom for review :)

Commits
-------

b377e41 [Cache] clean tags folder on invalidation
  • Loading branch information
nicolas-grekas committed Oct 9, 2019
2 parents 332d9e9 + b377e41 commit 49dfa8a
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/Symfony/Component/Cache/Adapter/FilesystemTagAwareAdapter.php
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Cache\Adapter;

use Symfony\Component\Cache\Exception\LogicException;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
use Symfony\Component\Cache\PruneableInterface;
Expand Down Expand Up @@ -115,22 +114,39 @@ protected function doDelete(array $ids, array $tagData = []): bool
protected function doInvalidate(array $tagIds): bool
{
foreach ($tagIds as $tagId) {
$tagsFolder = $this->getTagFolder($tagId);
if (!file_exists($tagsFolder)) {
if (!file_exists($tagsFolder = $this->getTagFolder($tagId))) {
continue;
}

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($tagsFolder, \FilesystemIterator::SKIP_DOTS)) as $itemLink) {
if (!$itemLink->isLink()) {
throw new LogicException('Expected a (sym)link when iterating over tag folder, non link found: '.$itemLink);
set_error_handler(static function () {});

try {
if (rename($tagsFolder, $renamed = substr_replace($tagsFolder, bin2hex(random_bytes(4)), -1))) {
$tagsFolder = $renamed.\DIRECTORY_SEPARATOR;
} else {
$renamed = null;
}

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($tagsFolder, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME)) as $itemLink) {
unlink(realpath($itemLink) ?: $itemLink);
unlink($itemLink);
}

$valueFile = $itemLink->getRealPath();
if ($valueFile && file_exists($valueFile)) {
@unlink($valueFile);
if (null === $renamed) {
continue;
}

@unlink((string) $itemLink);
$chars = '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

for ($i = 0; $i < 38; ++$i) {
for ($j = 0; $j < 38; ++$j) {
rmdir($tagsFolder.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j]);
}
rmdir($tagsFolder.$chars[$i]);
}
rmdir($renamed);
} finally {
restore_error_handler();
}
}

Expand Down

0 comments on commit 49dfa8a

Please sign in to comment.