Skip to content

Commit af15423

Browse files
cedric-annenicolas-grekas
authored andcommitted
[Cache] Remove temporary cache item file on rename() failure
1 parent 3d4a4e1 commit af15423

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected function doUnlink(string $file)
9090

9191
private function write(string $file, string $data, int $expiresAt = null)
9292
{
93+
$unlink = false;
9394
set_error_handler(__CLASS__.'::throwError');
9495
try {
9596
if (null === $this->tmp) {
@@ -107,14 +108,22 @@ private function write(string $file, string $data, int $expiresAt = null)
107108
}
108109
fwrite($h, $data);
109110
fclose($h);
111+
$unlink = true;
110112

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

115-
return rename($this->tmp, $file);
117+
$success = rename($this->tmp, $file);
118+
$unlink = !$success;
119+
120+
return $success;
116121
} finally {
117122
restore_error_handler();
123+
124+
if ($unlink) {
125+
@unlink($this->tmp);
126+
}
118127
}
119128
}
120129

0 commit comments

Comments
 (0)