From e7eae9053bc96931946ef4f578c0c6f5df28db41 Mon Sep 17 00:00:00 2001
From: Helmut Hummel <helmut.hummel@typo3.org>
Date: Fri, 21 Mar 2014 11:05:09 +0100
Subject: [PATCH] [BUGFIX] Mitigate race condition on cache flush
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When file caches are flushed, the cache directory
is also deleted.

Now when a second request comes in and creates
the caches it happens that the cache directory of
a given cache is still there for this cache when
calling setCache in the SimpleFileBackend,
but is deleted by the first request afterwards.

Now the cache directory for the second request
does not exist and setting cache entries will fail.

Mitigate this by immediately recreating
the cache directory.

Resolves: #57136
Releases: 6.2
Change-Id: I3c4c5c63c6c754447549285d9718798272f9e585
Reviewed-on: https://review.typo3.org/28594
Reviewed-by: Ernesto Baschny
Reviewed-by: Thomas Maroschik
Reviewed-by: Pascal Dürsteler
Reviewed-by: Markus Klein
Tested-by: Markus Klein
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
---
 .../sysext/core/Classes/Cache/Backend/SimpleFileBackend.php | 3 +--
 typo3/sysext/core/Classes/Utility/GeneralUtility.php        | 6 +++++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/typo3/sysext/core/Classes/Cache/Backend/SimpleFileBackend.php b/typo3/sysext/core/Classes/Cache/Backend/SimpleFileBackend.php
index 9467a660333a..cee2d3130d55 100644
--- a/typo3/sysext/core/Classes/Cache/Backend/SimpleFileBackend.php
+++ b/typo3/sysext/core/Classes/Cache/Backend/SimpleFileBackend.php
@@ -312,8 +312,7 @@ class SimpleFileBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend im
 	 * @api
 	 */
 	public function flush() {
-		\TYPO3\CMS\Core\Utility\GeneralUtility::flushDirectory($this->cacheDirectory);
-		$this->createFinalCacheDirectory($this->cacheDirectory);
+		\TYPO3\CMS\Core\Utility\GeneralUtility::flushDirectory($this->cacheDirectory, TRUE);
 	}
 
 	/**
diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php
index 922b4d1cdcdb..2285f3a1ee89 100644
--- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php
+++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php
@@ -2851,14 +2851,18 @@ Connection: close
 	 * to prevent race conditions on concurrent processes accessing the same directory.
 	 *
 	 * @param string $directory The directory to be renamed and flushed
+	 * @param bool $keepOriginalDirectory Whether to only empty the directory and not remove it
 	 * @return boolean Whether the action was successful
 	 */
-	static public function flushDirectory($directory) {
+	static public function flushDirectory($directory, $keepOriginalDirectory = FALSE) {
 		$result = FALSE;
 
 		if (is_dir($directory)) {
 			$temporaryDirectory = rtrim($directory, '/') . '.' . uniqid('remove') . '/';
 			if (rename($directory, $temporaryDirectory)) {
+				if ($keepOriginalDirectory) {
+					self::mkdir($directory);
+				}
 				clearstatcache();
 				$result = self::rmdir($temporaryDirectory, TRUE);
 			}
-- 
GitLab