Skip to content

Commit 8c02677

Browse files
bug symfony#52105 [Cache] Remove temporary cache item file on rename() failure (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 symfony#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
2 parents dc331c6 + af15423 commit 8c02677

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)