From 00aaffb8f955a4ddca8d7ebb74882637b5e22887 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 26 Nov 2020 14:28:58 +0100 Subject: [PATCH] Make cache writes atomic --- src/Composer/Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Cache.php b/src/Composer/Cache.php index d92cb4ac4..c4bb4b2eb 100644 --- a/src/Composer/Cache.php +++ b/src/Composer/Cache.php @@ -115,7 +115,7 @@ class Cache $this->io->writeError('Writing '.$this->root . $file.' into cache', true, IOInterface::DEBUG); try { - return file_put_contents($this->root . $file, $contents); + return file_put_contents($this->root . $file.'.tmp', $contents) !== false && rename($this->root . $file . '.tmp', $this->root . $file); } catch (\ErrorException $e) { $this->io->writeError('Failed to write into cache: '.$e->getMessage().'', true, IOInterface::DEBUG); if (preg_match('{^file_put_contents\(\): Only ([0-9]+) of ([0-9]+) bytes written}', $e->getMessage(), $m)) {