Merge branch 'm35_MDL-60923_New_BackupDir_Setting-v2' of https://github.com/scara/moodle

This commit is contained in:
Andrew Nicols 2018-04-17 09:44:44 +08:00
commit f8ad868fad
37 changed files with 170 additions and 83 deletions

View file

@ -375,9 +375,9 @@ class publication {
global $CFG, $USER;
require_once($CFG->libdir . "/filelib.php");
make_temp_directory('backup');
$backuptempdir = make_backup_temp_directory('');
$filename = md5(time() . '-' . $hubcourseid . '-'. $USER->id . '-'. random_string(20));
$path = $CFG->tempdir.'/backup/'.$filename.".mbz";
$path = $backuptempdir.'/'.$filename.".mbz";
api::download_course_backup($hubcourseid, $path);

View file

@ -38,13 +38,13 @@ class file_temp_cleanup_task extends scheduled_task {
}
/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
* Do the job, given the target directory.
*
* @param string $tmpdir The directory hosting the candidate stale temp files.
*/
public function execute() {
protected function execute_on($tmpdir) {
global $CFG;
$tmpdir = $CFG->tempdir;
// Default to last weeks time.
$time = time() - ($CFG->tempdatafoldercleanup * 3600);
@ -96,4 +96,23 @@ class file_temp_cleanup_task extends scheduled_task {
}
}
/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
global $CFG;
// The directories hosting the candidate stale temp files eventually are $CFG->tempdir and $CFG->backuptempdir.
// Do the job on each of the directories above.
// Let's start with $CFG->tempdir.
$this->execute_on($CFG->tempdir);
// Run on $CFG->backuptempdir too, if different from the default one, '$CFG->tempdir/backup'.
if (realpath(dirname($CFG->backuptempdir)) !== $CFG->tempdir) {
// The $CFG->backuptempdir setting is different from the default '$CFG->tempdir/backup'.
$this->execute_on($CFG->backuptempdir);
}
}
}

View file

@ -36,6 +36,7 @@
* - $CFG->dataroot - Path to moodle data files directory on server's filesystem.
* - $CFG->dirroot - Path to moodle's library folder on server's filesystem.
* - $CFG->libdir - Path to moodle's library folder on server's filesystem.
* - $CFG->backuptempdir - Path to moodle's backup temp file directory on server's filesystem.
* - $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).
@ -192,6 +193,11 @@ if (!isset($CFG->tempdir)) {
$CFG->tempdir = "$CFG->dataroot/temp";
}
// Allow overriding of backuptempdir but be backwards compatible
if (!isset($CFG->backuptempdir)) {
$CFG->backuptempdir = "$CFG->tempdir/backup";
}
// Allow overriding of cachedir but be backwards compatible
if (!isset($CFG->cachedir)) {
$CFG->cachedir = "$CFG->dataroot/cache";

View file

@ -1661,6 +1661,42 @@ function make_request_directory($exceptiononerror = true) {
return make_unique_writable_directory($basedir, $exceptiononerror);
}
/**
* Get the full path of a directory under $CFG->backuptempdir.
*
* @param string $directory the relative path of the directory under $CFG->backuptempdir
* @return string|false Returns full path to directory given a valid string; otherwise, false.
*/
function get_backup_temp_directory($directory) {
global $CFG;
if (($directory === null) || ($directory === false)) {
return false;
}
return "$CFG->backuptempdir/$directory";
}
/**
* Create a directory under $CFG->backuptempdir and make sure it is writable.
*
* Do not use for storing generic temp files - see make_temp_directory() instead for this purpose.
*
* Backup temporary files must be on a shared storage.
*
* @param string $directory the relative path of the directory to be created under $CFG->backuptempdir
* @param bool $exceptiononerror throw exception if error encountered
* @return string|false Returns full path to directory if successful, false if not; may throw exception
*/
function make_backup_temp_directory($directory, $exceptiononerror = true) {
global $CFG;
if ($CFG->backuptempdir !== "$CFG->tempdir/backup") {
check_dir_exists($CFG->backuptempdir, true, true);
protect_directory($CFG->backuptempdir);
} else {
protect_directory($CFG->tempdir);
}
return make_writable_directory("$CFG->backuptempdir/$directory", $exceptiononerror);
}
/**
* Create a directory under tempdir and make sure it is writable.
*

View file

@ -813,6 +813,7 @@ abstract class testing_util {
}
make_temp_directory('');
make_backup_temp_directory('');
make_cache_directory('');
make_localcache_directory('');
// Purge all data from the caches. This is required for consistency between tests.

View file

@ -147,7 +147,7 @@ class cronlib_testcase extends basic_testcase {
public function test_cron_delete_from_temp($nodes, $expected) {
global $CFG;
$tmpdir = $CFG->tempdir;
$tmpdir = realpath($CFG->tempdir);
foreach ($nodes as $data) {
if ($data->isdir) {
@ -168,7 +168,12 @@ class cronlib_testcase extends basic_testcase {
$actual = array();
for ($iter->rewind(); $iter->valid(); $iter->next()) {
if (!$iter->isDot()) {
$isvalid = true;
$isvalid = $isvalid && !$iter->isDot();
// Remove the default $CFG->tempdir/backup directory and $CFG->tempdir/.htaccess file from this comparison.
$isvalid = $isvalid && !($iter->isDir() && ($iter->getRealpath() === "{$tmpdir}/backup"));
$isvalid = $isvalid && !($iter->isFile() && ($iter->getRealpath() === "{$tmpdir}/.htaccess"));
if ($isvalid) {
$actual[] = $iter->getRealPath();
}
}

View file

@ -415,9 +415,10 @@ class core_scheduled_task_testcase extends advanced_testcase {
*/
public function test_file_temp_cleanup_task() {
global $CFG;
$backuptempdir = make_backup_temp_directory('');
// Create directories.
$dir = $CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR . 'courses';
$dir = $backuptempdir . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR . 'courses';
mkdir($dir, 0777, true);
// Create files to be checked and then deleted.
@ -440,11 +441,11 @@ class core_scheduled_task_testcase extends advanced_testcase {
// Change the time modified on modules.xml.
touch($file02, time() - (8 * 24 * 3600));
// Change the time modified on the courses directory.
touch($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR .
touch($backuptempdir . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR .
'courses', time() - (8 * 24 * 3600));
// Run the scheduled task to remove the file and directory.
$task->execute();
$filesarray = scandir($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01');
$filesarray = scandir($backuptempdir . DIRECTORY_SEPARATOR . 'backup01');
// There should only be two items in the array, '.' and '..'.
$this->assertEquals(2, count($filesarray));
@ -464,8 +465,8 @@ class core_scheduled_task_testcase extends advanced_testcase {
$task->execute();
$filesarray = scandir($CFG->tempdir);
// All of the files and directories should be deleted.
// There should only be two items in the array, '.' and '..'.
$this->assertEquals(2, count($filesarray));
// There should only be three items in the array, '.', '..' and '.htaccess'.
$this->assertEquals([ '.', '..', '.htaccess' ], $filesarray);
}
/**

View file

@ -1717,6 +1717,9 @@ function install_core($version, $verbose) {
remove_dir($CFG->tempdir.'', true);
make_temp_directory('', true);
remove_dir($CFG->backuptempdir.'', true);
make_backup_temp_directory('', true);
remove_dir($CFG->dataroot.'/muc', true);
make_writable_directory($CFG->dataroot.'/muc', true);