bug #39788 [Cache] fix possible collision when writing tmp file in filesystem adapter (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Cache] fix possible collision when writing tmp file in filesystem adapter

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39786
| License       | MIT
| Doc PR        | -

Commits
-------

340d15e400 [Cache] fix possible collision when writing tmp file in filesystem adapter
This commit is contained in:
Nicolas Grekas 2021-01-12 11:20:15 +01:00
commit 2852a42468

View File

@ -93,9 +93,20 @@ trait FilesystemCommonTrait
set_error_handler(__CLASS__.'::throwError');
try {
if (null === $this->tmp) {
$this->tmp = $this->directory.uniqid('', true);
$this->tmp = $this->directory.bin2hex(random_bytes(6));
}
file_put_contents($this->tmp, $data);
try {
$h = fopen($this->tmp, 'x');
} catch (\ErrorException $e) {
if (false === strpos($e->getMessage(), 'File exists')) {
throw $e;
}
$this->tmp = $this->directory.bin2hex(random_bytes(6));
$h = fopen($this->tmp, 'x');
}
fwrite($h, $data);
fclose($h);
if (null !== $expiresAt) {
touch($this->tmp, $expiresAt);