diff --git a/config-dist.php b/config-dist.php index 912b92e2fd8..083f2174a24 100644 --- a/config-dist.php +++ b/config-dist.php @@ -476,6 +476,7 @@ $CFG->admin = 'admin'; // $CFG->tempdir = '/var/www/moodle/temp'; // Directory MUST BE SHARED by all cluster nodes. // $CFG->cachedir = '/var/www/moodle/cache'; // Directory MUST BE SHARED by all cluster nodes, locking required. // $CFG->localcachedir = '/var/local/cache'; // Intended for local node caching. +// $CFG->localrequestdir = '/tmp'; // Intended for local only temporary files. The defaults uses sys_get_temp_dir(). // // It is possible to specify a different backup temp directory, use local fast filesystem // for normal web servers. Server clusters MUST use shared filesystem for backuptempdir! diff --git a/lib/setup.php b/lib/setup.php index cd31d059fb9..1326f1327c2 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -40,6 +40,7 @@ * - $CFG->tempdir - Path to moodle's temp file directory on server's filesystem. * - $CFG->cachedir - Path to moodle's cache directory on server's filesystem (shared by cluster nodes). * - $CFG->localcachedir - Path to moodle's local cache directory (not shared by cluster nodes). + * - $CFG->localrequestdir - Path to moodle's local temp request directory (not shared by cluster nodes). * * @global object $CFG * @name $CFG @@ -208,6 +209,11 @@ if (!isset($CFG->localcachedir)) { $CFG->localcachedir = "$CFG->dataroot/localcache"; } +// Allow overriding of localrequestdir. +if (!isset($CFG->localrequestdir)) { + $CFG->localrequestdir = sys_get_temp_dir() . '/requestdir'; +} + // Location of all languages except core English pack. if (!isset($CFG->langotherroot)) { $CFG->langotherroot = $CFG->dataroot.'/lang'; diff --git a/lib/setuplib.php b/lib/setuplib.php index 2133479e41f..b28c58a6326 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -1635,14 +1635,11 @@ function get_request_storage_directory($exceptiononerror = true, bool $forcecrea $createnewdirectory = $forcecreate || !$writabledirectoryexists; if ($createnewdirectory) { - if ($CFG->localcachedir !== "$CFG->dataroot/localcache") { - check_dir_exists($CFG->localcachedir, true, true); - protect_directory($CFG->localcachedir); - } else { - protect_directory($CFG->dataroot); - } + $basedir = "{$CFG->localrequestdir}/{$CFG->siteidentifier}"; + make_writable_directory($basedir); + protect_directory($basedir); - if ($dir = make_unique_writable_directory($CFG->localcachedir, $exceptiononerror)) { + if ($dir = make_unique_writable_directory($basedir, $exceptiononerror)) { // Register a shutdown handler to remove the directory. \core_shutdown_manager::register_function('remove_dir', [$dir]); } diff --git a/lib/tests/setuplib_test.php b/lib/tests/setuplib_test.php index bed8fc7588a..469f6cab4cb 100644 --- a/lib/tests/setuplib_test.php +++ b/lib/tests/setuplib_test.php @@ -204,6 +204,8 @@ class core_setuplib_testcase extends advanced_testcase { } public function test_get_request_storage_directory() { + $this->resetAfterTest(true); + // Making a call to get_request_storage_directory should always give the same result. $firstdir = get_request_storage_directory(); $seconddir = get_request_storage_directory(); @@ -230,6 +232,11 @@ class core_setuplib_testcase extends advanced_testcase { $fourthdir = get_request_storage_directory(); $this->assertTrue(is_dir($fourthdir)); $this->assertNotEquals($thirddir, $fourthdir); + + $now = $this->setCurrentTimeStart(); + set_config('localcachedirpurged', $now - 2); + purge_all_caches(); + $this->assertTrue(is_dir($fourthdir)); } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index c5fe41f75e1..294062a0422 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -52,6 +52,7 @@ information provided here is intended especially for developers. renamed to `is_listed()` and `get_not_listed()` respectively. * Method `mustache_helper_collection::strip_blacklisted_helpers()` has been deprecated and renamed to `strip_disallowed_helpers()`. +* New setting $CFG->localtempdir overrides which defaults to sys_get_temp_dir() === 3.9 === * Following function has been deprecated, please use \core\task\manager::run_from_cli().