Skip to content

Commit 2f7306f

Browse files
Merge branch '5.4' into 6.3
* 5.4: [Cache] Remove temporary cache item file on `rename()` failure
2 parents a4cf378 + 8c02677 commit 2f7306f

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
@@ -87,6 +87,7 @@ protected function doUnlink(string $file)
8787

8888
private function write(string $file, string $data, int $expiresAt = null): bool
8989
{
90+
$unlink = false;
9091
set_error_handler(__CLASS__.'::throwError');
9192
try {
9293
$tmp = $this->directory.$this->tmpSuffix ??= str_replace('/', '-', base64_encode(random_bytes(6)));
@@ -102,14 +103,22 @@ private function write(string $file, string $data, int $expiresAt = null): bool
102103
}
103104
fwrite($h, $data);
104105
fclose($h);
106+
$unlink = true;
105107

106108
if (null !== $expiresAt) {
107109
touch($tmp, $expiresAt ?: time() + 31556952); // 1 year in seconds
108110
}
109111

110-
return rename($tmp, $file);
112+
$success = rename($tmp, $file);
113+
$unlink = !$success;
114+
115+
return $success;
111116
} finally {
112117
restore_error_handler();
118+
119+
if ($unlink) {
120+
@unlink($tmp);
121+
}
113122
}
114123
}
115124

0 commit comments

Comments
 (0)