Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
[Cache] Remove temporary cache item file on rename() failure
  • Loading branch information
cedric-anne authored and nicolas-grekas committed Oct 17, 2023
commit af15423674deba32628828e473cbc8b7b4f3d083
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