Skip to content

Commit

Permalink
bug #52105 [Cache] Remove temporary cache item file on rename() fai…
Browse files Browse the repository at this point in the history
…lure (cedric-anne)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[Cache] Remove temporary cache item file on `rename()` failure

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #52092
| License       | MIT

The filesystem cache adapter creates a temporary file to store cache item data, then moves it to its target path using `rename()` function. If rename fails, for instance target path is not writable, the temporary file will remains.

To prevent filesystem saturation, the temporary files should be deleted if this operation is not done by the `rename()` function itself.

Commits
-------

af15423 [Cache] Remove temporary cache item file on `rename()` failure
  • Loading branch information
nicolas-grekas committed Oct 17, 2023
2 parents dc331c6 + af15423 commit 8c02677
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ protected function doUnlink(string $file)

private function write(string $file, string $data, int $expiresAt = null)
{
$unlink = false;
set_error_handler(__CLASS__.'::throwError');
try {
if (null === $this->tmp) {
Expand All @@ -107,14 +108,22 @@ private function write(string $file, string $data, int $expiresAt = null)
}
fwrite($h, $data);
fclose($h);
$unlink = true;

if (null !== $expiresAt) {
touch($this->tmp, $expiresAt ?: time() + 31556952); // 1 year in seconds
}

return rename($this->tmp, $file);
$success = rename($this->tmp, $file);
$unlink = !$success;

return $success;
} finally {
restore_error_handler();

if ($unlink) {
@unlink($this->tmp);
}
}
}

Expand Down

0 comments on commit 8c02677

Please sign in to comment.