From 1fed67296c940fa7f0cdd98c5f60a4ce397f7ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Fri, 26 Jul 2013 20:27:36 +0200 Subject: [PATCH 1/2] MDL-40891 work around make_writable_directory() race condition You will still get a "PHP Warning: mkdir(): File exists" but that should not cause any problems on production sites. --- lib/setuplib.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/setuplib.php b/lib/setuplib.php index 7495c246032..b48aba25c0e 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -1236,10 +1236,15 @@ function make_writable_directory($dir, $exceptiononerror = true) { if (!file_exists($dir)) { if (!mkdir($dir, $CFG->directorypermissions, true)) { - if ($exceptiononerror) { - throw new invalid_dataroot_permissions($dir.' can not be created, check permissions.'); - } else { - return false; + clearstatcache(); + // There might be a race condition when creating directory. + if (!is_dir($dir)) { + if ($exceptiononerror) { + throw new invalid_dataroot_permissions($dir.' can not be created, check permissions.'); + } else { + debugging('Can not create directory: '.$dir, DEBUG_DEVELOPER); + return false; + } } } } From bde942541edae94c73296e56a9282aec245f43ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Fri, 26 Jul 2013 20:28:57 +0200 Subject: [PATCH 2/2] MDL-40891 prevent exceptions when creating directories in file store Even if cache creation fails moodle should continue working. --- cache/stores/file/lib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index 455665817eb..c8e2116b1e2 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -266,7 +266,7 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i $this->definition = $definition; $hash = preg_replace('#[^a-zA-Z0-9]+#', '_', $this->definition->get_id()); $this->path = $this->filestorepath.'/'.$hash; - make_writable_directory($this->path); + make_writable_directory($this->path, false); if ($this->prescan && $definition->get_mode() !== self::MODE_REQUEST) { $this->prescan = false; } @@ -318,7 +318,7 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i $dir = $this->path . '/' . $subdir; if ($create) { // Create the directory. This function does it recursivily! - make_writable_directory($dir); + make_writable_directory($dir, false); } return $dir . '/' . $key . '.cache'; }