From 8a38a51c1e1879feb08f1323b44cb29100b6bd89 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 12 Aug 2010 21:56:30 +0200 Subject: [PATCH] fixed race condition when writing template cache on disk (closes #97) --- lib/Twig/Environment.php | 17 ++++++++++++++++- 1 files changed, 16 insertions(+), 1 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 5d58245..08b295c 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -200,7 +200,7 @@ class Twig_Environment if (!file_exists($cache) || ($this->isAutoReload() && !$this->loader->isFresh($name, filemtime($cache)))) { $content = $this->compileSource($this->loader->getSource($name), $name); - if (false === file_put_contents($cache, $content, LOCK_EX)) { + if (false === $this->writeCacheFile($cache, $content)) { eval('?>'.$content); } else { require_once $cache; @@ -403,4 +403,19 @@ class Twig_Environment return $this->filters; } + + protected function writeCacheFile($file, $content) + { + $tmpFile = tempnam(dirname($file), basename($file)); + if (false !== @file_put_contents($tmpFile, $content)) { + // rename does not work on Win32 before 5.2.6 + if (@rename($tmpFile, $file) || (@copy($tmpFile, $file) && unlink($tmpFile))) { + chmod($file, 0644); + + return; + } + } + + throw new RuntimeException(sprintf('Failed to write cache file "%s".', $file)); + } } -- 1.7.2.5