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

@ -442,6 +442,7 @@ if ($interactive) {
} }
} }
$CFG->tempdir = $CFG->dataroot.'/temp'; $CFG->tempdir = $CFG->dataroot.'/temp';
$CFG->backuptempdir = $CFG->tempdir.'/backup';
$CFG->cachedir = $CFG->dataroot.'/cache'; $CFG->cachedir = $CFG->dataroot.'/cache';
$CFG->localcachedir = $CFG->dataroot.'/localcache'; $CFG->localcachedir = $CFG->dataroot.'/localcache';

View file

@ -207,9 +207,9 @@ class category_bin extends base_bin {
// Get the backup file. // Get the backup file.
$file = reset($files); $file = reset($files);
// Get a temp directory name and create it. // Get a backup temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id); $tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_temp_directory('/backup/' . $tempdir); $fulltempdir = make_backup_temp_directory($tempdir);
// Extract the backup to tmpdir. // Extract the backup to tmpdir.
$fb = get_file_packer('application/vnd.moodle.backup'); $fb = get_file_packer('application/vnd.moodle.backup');

View file

@ -211,9 +211,9 @@ class course_bin extends base_bin {
// Get the backup file. // Get the backup file.
$file = reset($files); $file = reset($files);
// Get a temp directory name and create it. // Get a backup temp directory name and create it.
$tempdir = \restore_controller::get_tempdir_name($context->id, $user->id); $tempdir = \restore_controller::get_tempdir_name($context->id, $user->id);
$fulltempdir = make_temp_directory('/backup/' . $tempdir); $fulltempdir = make_backup_temp_directory($tempdir);
// Extract the backup to tempdir. // Extract the backup to tempdir.
$fb = get_file_packer('application/vnd.moodle.backup'); $fb = get_file_packer('application/vnd.moodle.backup');

View file

@ -347,7 +347,7 @@ class tool_uploadcourse_course {
/** /**
* Get the directory of the object to restore. * Get the directory of the object to restore.
* *
* @return string|false|null subdirectory in $CFG->tempdir/backup/..., false when an error occured * @return string|false|null subdirectory in $CFG->backuptempdir/..., false when an error occured
* and null when there is simply nothing. * and null when there is simply nothing.
*/ */
protected function get_restore_content_dir() { protected function get_restore_content_dir() {

View file

@ -227,7 +227,7 @@ class tool_uploadcourse_helper {
} }
// If we don't use the cache, or if we do and not set, or the directory doesn't exist any more. // If we don't use the cache, or if we do and not set, or the directory doesn't exist any more.
if (!$usecache || (($backupid = $cache->get($cachekey)) === false || !is_dir("$CFG->tempdir/backup/$backupid"))) { if (!$usecache || (($backupid = $cache->get($cachekey)) === false || !is_dir(get_backup_temp_directory($backupid)))) {
// Use null instead of false because it would consider that the cache key has not been set. // Use null instead of false because it would consider that the cache key has not been set.
$backupid = null; $backupid = null;
@ -236,7 +236,7 @@ class tool_uploadcourse_helper {
// Extracting the backup file. // Extracting the backup file.
$packer = get_file_packer('application/vnd.moodle.backup'); $packer = get_file_packer('application/vnd.moodle.backup');
$backupid = restore_controller::get_tempdir_name(SITEID, $USER->id); $backupid = restore_controller::get_tempdir_name(SITEID, $USER->id);
$path = "$CFG->tempdir/backup/$backupid/"; $path = make_backup_temp_directory($backupid, false);
$result = $packer->extract_to_pathname($backupfile, $path); $result = $packer->extract_to_pathname($backupfile, $path);
if (!$result) { if (!$result) {
$errors['invalidbackupfile'] = new lang_string('invalidbackupfile', 'tool_uploadcourse'); $errors['invalidbackupfile'] = new lang_string('invalidbackupfile', 'tool_uploadcourse');

View file

@ -259,7 +259,7 @@ class tool_uploadcourse_processor {
/** /**
* Get the directory of the object to restore. * Get the directory of the object to restore.
* *
* @return string subdirectory in $CFG->tempdir/backup/... * @return string subdirectory in $CFG->backuptempdir/...
*/ */
protected function get_restore_content_dir() { protected function get_restore_content_dir() {
$backupfile = null; $backupfile = null;

View file

@ -32,7 +32,7 @@
*/ */
class restore_controller extends base_controller { class restore_controller extends base_controller {
protected $tempdir; // Directory under tempdir/backup awaiting restore protected $tempdir; // Directory under $CFG->backuptempdir awaiting restore
protected $restoreid; // Unique identificator for this restore protected $restoreid; // Unique identificator for this restore
protected $courseid; // courseid where restore is going to happen protected $courseid; // courseid where restore is going to happen
@ -68,7 +68,7 @@ class restore_controller extends base_controller {
* while loading the plan, as well as for future use. (You can change it * while loading the plan, as well as for future use. (You can change it
* for a different one later using set_progress.) * for a different one later using set_progress.)
* *
* @param string $tempdir Directory under tempdir/backup awaiting restore * @param string $tempdir Directory under $CFG->backuptempdir awaiting restore
* @param int $courseid Course id where restore is going to happen * @param int $courseid Course id where restore is going to happen
* @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false] * @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false]
* @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ] * @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ]

View file

@ -110,7 +110,7 @@ class core_backup_controller_testcase extends advanced_testcase {
global $CFG; global $CFG;
// Make a backup. // Make a backup.
check_dir_exists($CFG->tempdir . '/backup'); make_backup_temp_directory('');
$bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid); backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$backupid = $bc->get_backupid(); $backupid = $bc->get_backupid();
@ -159,7 +159,7 @@ class core_backup_controller_testcase extends advanced_testcase {
$foldername = 'deadlock'; $foldername = 'deadlock';
$fp = get_file_packer('application/vnd.moodle.backup'); $fp = get_file_packer('application/vnd.moodle.backup');
$tempdir = $CFG->dataroot . '/temp/backup/' . $foldername; $tempdir = make_backup_temp_directory($foldername);
$files = $fp->extract_to_pathname($CFG->dirroot . '/backup/controller/tests/fixtures/deadlock.mbz', $tempdir); $files = $fp->extract_to_pathname($CFG->dirroot . '/backup/controller/tests/fixtures/deadlock.mbz', $tempdir);
$this->setAdminUser(); $this->setAdminUser();

View file

@ -148,7 +148,7 @@ abstract class base_converter implements loggable {
public function get_workdir_path() { public function get_workdir_path() {
global $CFG; global $CFG;
return "$CFG->tempdir/backup/$this->workdir"; return make_backup_temp_directory($this->workdir);
} }
/** /**
@ -157,7 +157,7 @@ abstract class base_converter implements loggable {
public function get_tempdir_path() { public function get_tempdir_path() {
global $CFG; global $CFG;
return "$CFG->tempdir/backup/$this->tempdir"; return make_backup_temp_directory($this->tempdir);
} }
/// public static methods ////////////////////////////////////////////////// /// public static methods //////////////////////////////////////////////////

View file

@ -139,7 +139,7 @@ class imscc11_backup_convert extends backup_execution_step {
require_once($CFG->dirroot . '/backup/cc/cc_includes.php'); require_once($CFG->dirroot . '/backup/cc/cc_includes.php');
$tempdir = $CFG->tempdir . '/backup/' . uniqid('', true); $tempdir = $CFG->backuptempdir . '/' . uniqid('', true);
if (mkdir($tempdir, $CFG->directorypermissions, true)) { if (mkdir($tempdir, $CFG->directorypermissions, true)) {

View file

@ -89,7 +89,8 @@ class moodle1_converter extends base_converter {
public static function detect_format($tempdir) { public static function detect_format($tempdir) {
global $CFG; global $CFG;
$filepath = $CFG->tempdir . '/backup/' . $tempdir . '/moodle.xml'; $tempdirpath = make_backup_temp_directory($tempdir, false);
$filepath = $tempdirpath . '/moodle.xml';
if (file_exists($filepath)) { if (file_exists($filepath)) {
// looks promising, lets load some information // looks promising, lets load some information
$handle = fopen($filepath, 'r'); $handle = fopen($filepath, 'r');

View file

@ -35,6 +35,9 @@ class core_backup_moodle1_converter_testcase extends advanced_testcase {
/** @var string the name of the directory containing the unpacked Moodle 1.9 backup */ /** @var string the name of the directory containing the unpacked Moodle 1.9 backup */
protected $tempdir; protected $tempdir;
/** @var string the full name of the directory containing the unpacked Moodle 1.9 backup */
protected $tempdirpath;
/** @var string saved hash of an icon file used during testing */ /** @var string saved hash of an icon file used during testing */
protected $iconhash; protected $iconhash;
@ -42,39 +45,40 @@ class core_backup_moodle1_converter_testcase extends advanced_testcase {
global $CFG; global $CFG;
$this->tempdir = convert_helper::generate_id('unittest'); $this->tempdir = convert_helper::generate_id('unittest');
check_dir_exists("$CFG->tempdir/backup/$this->tempdir/course_files/sub1"); $this->tempdirpath = make_backup_temp_directory($this->tempdir);
check_dir_exists("$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7"); check_dir_exists("$this->tempdirpath/course_files/sub1");
check_dir_exists("$this->tempdirpath/moddata/unittest/4/7");
copy( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/moodle.xml", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/moodle.xml",
"$CFG->tempdir/backup/$this->tempdir/moodle.xml" "$this->tempdirpath/moodle.xml"
); );
copy( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/course_files/file1.gif" "$this->tempdirpath/course_files/file1.gif"
); );
copy( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/course_files/sub1/file2.gif" "$this->tempdirpath/course_files/sub1/file2.gif"
); );
copy( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/file1.gif" "$this->tempdirpath/moddata/unittest/4/file1.gif"
); );
copy( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif" "$this->tempdirpath/moddata/unittest/4/icon.gif"
); );
$this->iconhash = file_storage::hash_from_path($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif'); $this->iconhash = file_storage::hash_from_path($this->tempdirpath.'/moddata/unittest/4/icon.gif');
copy( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif" "$this->tempdirpath/moddata/unittest/4/7/icon.gif"
); );
} }
protected function tearDown() { protected function tearDown() {
global $CFG; global $CFG;
if (empty($CFG->keeptempdirectoriesonbackup)) { if (empty($CFG->keeptempdirectoriesonbackup)) {
fulldelete("$CFG->tempdir/backup/$this->tempdir"); fulldelete($this->tempdirpath);
} }
} }
@ -563,7 +567,7 @@ as it is parsed from the backup file. <br /><br /><img border="0" width="110" vs
copy( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/questions.xml", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/questions.xml",
"$CFG->tempdir/backup/$this->tempdir/moodle.xml" "$this->tempdirpath/moodle.xml"
); );
$converter = convert_factory::get_converter('moodle1', $this->tempdir); $converter = convert_factory::get_converter('moodle1', $this->tempdir);
$converter->convert(); $converter->convert();

View file

@ -140,7 +140,7 @@ if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
// Check whether the backup directory still exists. If missing, something // Check whether the backup directory still exists. If missing, something
// went really wrong in backup, throw error. Note that backup::MODE_IMPORT // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
// backups don't store resulting files ever // backups don't store resulting files ever
$tempdestination = $CFG->tempdir . '/backup/' . $backupid; $tempdestination = make_backup_temp_directory($backupid, false);
if (!file_exists($tempdestination) || !is_dir($tempdestination)) { if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
print_error('unknownbackupexporterror'); // shouldn't happen ever print_error('unknownbackupexporterror'); // shouldn't happen ever
} }

View file

@ -127,8 +127,7 @@ class core_backup_moodle2_testcase extends advanced_testcase {
// Extract backup file. // Extract backup file.
$backupid = 'abc'; $backupid = 'abc';
$backuppath = $CFG->tempdir . '/backup/' . $backupid; $backuppath = make_backup_temp_directory($backupid);
check_dir_exists($backuppath);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname( get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . '/fixtures/availability_26_format.mbz', $backuppath); __DIR__ . '/fixtures/availability_26_format.mbz', $backuppath);
@ -972,8 +971,7 @@ class core_backup_moodle2_testcase extends advanced_testcase {
foreach ($backupfiles as $backupfile) { foreach ($backupfiles as $backupfile) {
// Extract backup file. // Extract backup file.
$backupid = $backupfile; $backupid = $backupfile;
$backuppath = $CFG->tempdir . '/backup/' . $backupid; $backuppath = make_backup_temp_directory($backupid);
check_dir_exists($backuppath);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname( get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . "/fixtures/$backupfile.mbz", $backuppath); __DIR__ . "/fixtures/$backupfile.mbz", $backuppath);

View file

@ -72,7 +72,7 @@ if (is_null($course)) {
$browser = get_file_browser(); $browser = get_file_browser();
// check if tmp dir exists // check if tmp dir exists
$tmpdir = $CFG->tempdir . '/backup'; $tmpdir = make_backup_temp_directory('', false);
if (!check_dir_exists($tmpdir, true, true)) { if (!check_dir_exists($tmpdir, true, true)) {
throw new restore_controller_exception('cannot_create_backup_temp_dir'); throw new restore_controller_exception('cannot_create_backup_temp_dir');
} }

View file

@ -60,9 +60,9 @@ abstract class backup_factory {
// Create file_logger, observing $CFG->backup_file_logger_level // Create file_logger, observing $CFG->backup_file_logger_level
// defaulting to $dfltloglevel // defaulting to $dfltloglevel
check_dir_exists($CFG->tempdir . '/backup', true, true); // need to ensure that temp/backup already exists $backuptempdir = make_backup_temp_directory(''); // Need to ensure that $CFG->backuptempdir already exists.
$fllevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : $dfltloglevel; $fllevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : $dfltloglevel;
$enabledloggers[] = new file_logger($fllevel, true, true, $CFG->tempdir . '/backup/' . $backupid . '.log'); $enabledloggers[] = new file_logger($fllevel, true, true, $backuptempdir . '/' . $backupid . '.log');
// Create database_logger, observing $CFG->backup_database_logger_level and defaulting to LOG_WARNING // Create database_logger, observing $CFG->backup_database_logger_level and defaulting to LOG_WARNING
// and pointing to the backup_logs table // and pointing to the backup_logs table

View file

@ -43,7 +43,8 @@ class backup_file_manager {
public static function get_backup_storage_base_dir($backupid) { public static function get_backup_storage_base_dir($backupid) {
global $CFG; global $CFG;
return $CFG->tempdir . '/backup/' . $backupid . '/files'; $backupiddir = make_backup_temp_directory($backupid);
return $backupiddir . '/files';
} }
/** /**

View file

@ -121,7 +121,8 @@ abstract class backup_general_helper extends backup_helper {
$info = new stdclass(); // Final information goes here $info = new stdclass(); // Final information goes here
$moodlefile = $CFG->tempdir . '/backup/' . $tempdir . '/moodle_backup.xml'; $backuptempdir = make_backup_temp_directory('', false);
$moodlefile = $backuptempdir . '/' . $tempdir . '/moodle_backup.xml';
if (!file_exists($moodlefile)) { // Shouldn't happen ever, but... if (!file_exists($moodlefile)) { // Shouldn't happen ever, but...
throw new backup_helper_exception('missing_moodle_backup_xml_file', $moodlefile); throw new backup_helper_exception('missing_moodle_backup_xml_file', $moodlefile);
} }
@ -267,7 +268,7 @@ abstract class backup_general_helper extends backup_helper {
// Extract moodle_backup.xml. // Extract moodle_backup.xml.
$tmpname = 'info_from_mbz_' . time() . '_' . random_string(4); $tmpname = 'info_from_mbz_' . time() . '_' . random_string(4);
$tmpdir = $CFG->tempdir . '/backup/' . $tmpname; $tmpdir = make_backup_temp_directory($tmpname);
$fp = get_file_packer('application/vnd.moodle.backup'); $fp = get_file_packer('application/vnd.moodle.backup');
$extracted = $fp->extract_to_pathname($filepath, $tmpdir, array('moodle_backup.xml'), $progress); $extracted = $fp->extract_to_pathname($filepath, $tmpdir, array('moodle_backup.xml'), $progress);

View file

@ -33,8 +33,8 @@ abstract class backup_helper {
* Given one backupid, create all the needed dirs to have one backup temp dir available * Given one backupid, create all the needed dirs to have one backup temp dir available
*/ */
static public function check_and_create_backup_dir($backupid) { static public function check_and_create_backup_dir($backupid) {
global $CFG; $backupiddir = make_backup_temp_directory($backupid, false);
if (!check_dir_exists($CFG->tempdir . '/backup/' . $backupid, true, true)) { if (empty($backupiddir)) {
throw new backup_helper_exception('cannot_create_backup_temp_dir'); throw new backup_helper_exception('cannot_create_backup_temp_dir');
} }
} }
@ -49,8 +49,8 @@ abstract class backup_helper {
* @param \core\progress\base $progress Optional progress reporting object * @param \core\progress\base $progress Optional progress reporting object
*/ */
static public function clear_backup_dir($backupid, \core\progress\base $progress = null) { static public function clear_backup_dir($backupid, \core\progress\base $progress = null) {
global $CFG; $backupiddir = make_backup_temp_directory($backupid, false);
if (!self::delete_dir_contents($CFG->tempdir . '/backup/' . $backupid, '', $progress)) { if (!self::delete_dir_contents($backupiddir, '', $progress)) {
throw new backup_helper_exception('cannot_empty_backup_temp_dir'); throw new backup_helper_exception('cannot_empty_backup_temp_dir');
} }
return true; return true;
@ -66,9 +66,9 @@ abstract class backup_helper {
* @param \core\progress\base $progress Optional progress reporting object * @param \core\progress\base $progress Optional progress reporting object
*/ */
static public function delete_backup_dir($backupid, \core\progress\base $progress = null) { static public function delete_backup_dir($backupid, \core\progress\base $progress = null) {
global $CFG; $backupiddir = make_backup_temp_directory($backupid, false);
self::clear_backup_dir($backupid, $progress); self::clear_backup_dir($backupid, $progress);
return rmdir($CFG->tempdir . '/backup/' . $backupid); return rmdir($backupiddir);
} }
/** /**
@ -157,13 +157,12 @@ abstract class backup_helper {
* @param \core\progress\base $progress Optional progress reporting object * @param \core\progress\base $progress Optional progress reporting object
*/ */
static public function delete_old_backup_dirs($deletefrom, \core\progress\base $progress = null) { static public function delete_old_backup_dirs($deletefrom, \core\progress\base $progress = null) {
global $CFG;
$status = true; $status = true;
// Get files and directories in the temp backup dir witout descend // Get files and directories in the backup temp dir without descend.
$list = get_directory_list($CFG->tempdir . '/backup', '', false, true, true); $backuptempdir = make_backup_temp_directory('');
$list = get_directory_list($backuptempdir, '', false, true, true);
foreach ($list as $file) { foreach ($list as $file) {
$file_path = $CFG->tempdir . '/backup/' . $file; $file_path = $backuptempdir . '/' . $file;
$moddate = filemtime($file_path); $moddate = filemtime($file_path);
if ($status && $moddate < $deletefrom) { if ($status && $moddate < $deletefrom) {
//If directory, recurse //If directory, recurse

View file

@ -131,15 +131,12 @@ abstract class convert_helper {
* @return boolean true if moodle2 format detected, false otherwise * @return boolean true if moodle2 format detected, false otherwise
*/ */
public static function detect_moodle2_format($tempdir) { public static function detect_moodle2_format($tempdir) {
global $CFG; $dirpath = make_backup_temp_directory($tempdir, false);
$dirpath = $CFG->tempdir . '/backup/' . $tempdir;
$filepath = $dirpath . '/moodle_backup.xml';
if (!is_dir($dirpath)) { if (!is_dir($dirpath)) {
throw new convert_helper_exception('tmp_backup_directory_not_found', $dirpath); throw new convert_helper_exception('tmp_backup_directory_not_found', $dirpath);
} }
$filepath = $dirpath . '/moodle_backup.xml';
if (!file_exists($filepath)) { if (!file_exists($filepath)) {
return false; return false;
} }

View file

@ -37,13 +37,12 @@ class backup_plan extends base_plan implements loggable {
* Constructor - instantiates one object of this class * Constructor - instantiates one object of this class
*/ */
public function __construct($controller) { public function __construct($controller) {
global $CFG;
if (! $controller instanceof backup_controller) { if (! $controller instanceof backup_controller) {
throw new backup_plan_exception('wrong_backup_controller_specified'); throw new backup_plan_exception('wrong_backup_controller_specified');
} }
$backuptempdir = make_backup_temp_directory('');
$this->controller = $controller; $this->controller = $controller;
$this->basepath = $CFG->tempdir . '/backup/' . $controller->get_backupid(); $this->basepath = $backuptempdir . '/' . $controller->get_backupid();
$this->excludingdactivities = false; $this->excludingdactivities = false;
parent::__construct('backup_plan'); parent::__construct('backup_plan');
} }

View file

@ -49,8 +49,9 @@ class restore_plan extends base_plan implements loggable {
if (! $controller instanceof restore_controller) { if (! $controller instanceof restore_controller) {
throw new restore_plan_exception('wrong_restore_controller_specified'); throw new restore_plan_exception('wrong_restore_controller_specified');
} }
$backuptempdir = make_backup_temp_directory('');
$this->controller = $controller; $this->controller = $controller;
$this->basepath = $CFG->tempdir . '/backup/' . $controller->get_tempdir(); $this->basepath = $backuptempdir . '/' . $controller->get_tempdir();
$this->preloaded = false; $this->preloaded = false;
$this->decoder = new restore_decode_processor($this->get_restoreid(), $this->get_info()->original_wwwroot, $CFG->wwwroot); $this->decoder = new restore_decode_processor($this->get_restoreid(), $this->get_info()->original_wwwroot, $CFG->wwwroot);
$this->missingmodules = false; $this->missingmodules = false;

View file

@ -288,9 +288,9 @@ class restore_ui_stage_confirm extends restore_ui_independent_stage implements f
* @throws restore_ui_exception * @throws restore_ui_exception
*/ */
public function process() { public function process() {
global $CFG; $backuptempdir = make_backup_temp_directory('');
if ($this->filename) { if ($this->filename) {
$archivepath = $CFG->tempdir . '/backup/' . $this->filename; $archivepath = $backuptempdir . '/' . $this->filename;
if (!file_exists($archivepath)) { if (!file_exists($archivepath)) {
throw new restore_ui_exception('invalidrestorefile'); throw new restore_ui_exception('invalidrestorefile');
} }
@ -316,13 +316,14 @@ class restore_ui_stage_confirm extends restore_ui_independent_stage implements f
* @return bool * @return bool
*/ */
protected function extract_file_to_dir($source) { protected function extract_file_to_dir($source) {
global $CFG, $USER; global $USER;
$this->filepath = restore_controller::get_tempdir_name($this->contextid, $USER->id); $this->filepath = restore_controller::get_tempdir_name($this->contextid, $USER->id);
$backuptempdir = make_backup_temp_directory('', false);
$fb = get_file_packer('application/vnd.moodle.backup'); $fb = get_file_packer('application/vnd.moodle.backup');
$result = $fb->extract_to_pathname($source, $result = $fb->extract_to_pathname($source,
$CFG->tempdir . '/backup/' . $this->filepath . '/', null, $this); $backuptempdir . '/' . $this->filepath . '/', null, $this);
// If any progress happened, end it. // If any progress happened, end it.
if ($this->startedprogress) { if ($this->startedprogress) {
@ -473,8 +474,9 @@ class restore_ui_stage_destination extends restore_ui_independent_stage {
* @throws restore_ui_exception * @throws restore_ui_exception
*/ */
public function process() { public function process() {
global $CFG, $DB; global $DB;
if (!file_exists("$CFG->tempdir/backup/".$this->filepath) || !is_dir("$CFG->tempdir/backup/".$this->filepath)) { $filepathdir = make_backup_temp_directory($this->filepath, false);
if (!file_exists($filepathdir) || !is_dir($filepathdir)) {
throw new restore_ui_exception('invalidrestorepath'); throw new restore_ui_exception('invalidrestorepath');
} }
if (optional_param('searchcourses', false, PARAM_BOOL)) { if (optional_param('searchcourses', false, PARAM_BOOL)) {

View file

@ -75,7 +75,8 @@ $cancelrestore = optional_param('cancelrestore', false, PARAM_INT);
if ($usercandownload and $cancelrestore and confirm_sesskey()) { if ($usercandownload and $cancelrestore and confirm_sesskey()) {
$filename = optional_param('filename', '', PARAM_ALPHANUMEXT); $filename = optional_param('filename', '', PARAM_ALPHANUMEXT);
//delete temp file //delete temp file
unlink($CFG->tempdir . '/backup/' . $filename . ".mbz"); $backuptempdir = make_backup_temp_directory('');
unlink($backuptempdir . '/' . $filename . ".mbz");
} }
/// Download /// Download

View file

@ -434,10 +434,16 @@ $CFG->admin = 'admin';
// Localcachedir is intended for server clusters, it does not have to be shared by cluster nodes. // Localcachedir is intended for server clusters, it does not have to be shared by cluster nodes.
// The directories must not be accessible via web. // The directories must not be accessible via web.
// //
// $CFG->tempdir = '/var/www/moodle/temp'; // Directory MUST BE SHARED by all clsuter nodes. // $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->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->localcachedir = '/var/local/cache'; // Intended for local node caching.
// //
// 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!
// The directory must not be accessible via web.
//
// $CFG->backuptempdir = '/var/www/moodle/backuptemp'; // Directory MUST BE SHARED by all cluster nodes.
//
// Some filesystems such as NFS may not support file locking operations. // Some filesystems such as NFS may not support file locking operations.
// Locking resolves race conditions and is strongly recommended for production servers. // Locking resolves race conditions and is strongly recommended for production servers.
// $CFG->preventfilelocking = false; // $CFG->preventfilelocking = false;

View file

@ -45,7 +45,7 @@ class core_course_restore_testcase extends advanced_testcase {
* @return string * @return string
*/ */
protected function backup_course($courseid, $userid = 2) { protected function backup_course($courseid, $userid = 2) {
globaL $CFG; $backuptempdir = make_backup_temp_directory('');
$packer = get_file_packer('application/vnd.moodle.backup'); $packer = get_file_packer('application/vnd.moodle.backup');
$bc = new backup_controller(backup::TYPE_1COURSE, $courseid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, $bc = new backup_controller(backup::TYPE_1COURSE, $courseid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO,
@ -53,7 +53,7 @@ class core_course_restore_testcase extends advanced_testcase {
$bc->execute_plan(); $bc->execute_plan();
$results = $bc->get_results(); $results = $bc->get_results();
$results['backup_destination']->extract_to_pathname($packer, "$CFG->tempdir/backup/core_course_testcase"); $results['backup_destination']->extract_to_pathname($packer, "$backuptempdir/core_course_testcase");
$bc->destroy(); $bc->destroy();
unset($bc); unset($bc);

View file

@ -163,6 +163,7 @@ $CFG->wwwroot = install_guess_wwwroot(); // can not be changed - pp
$CFG->httpswwwroot = $CFG->wwwroot; $CFG->httpswwwroot = $CFG->wwwroot;
$CFG->dataroot = $config->dataroot; $CFG->dataroot = $config->dataroot;
$CFG->tempdir = $CFG->dataroot.'/temp'; $CFG->tempdir = $CFG->dataroot.'/temp';
$CFG->backuptempdir = $CFG->tempdir.'/backup';
$CFG->cachedir = $CFG->dataroot.'/cache'; $CFG->cachedir = $CFG->dataroot.'/cache';
$CFG->localcachedir = $CFG->dataroot.'/localcache'; $CFG->localcachedir = $CFG->dataroot.'/localcache';
$CFG->admin = $config->admin; $CFG->admin = $config->admin;

View file

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

View file

@ -38,13 +38,13 @@ class file_temp_cleanup_task extends scheduled_task {
} }
/** /**
* Do the job. * Do the job, given the target directory.
* Throw exceptions on errors (the job will be retried). *
* @param string $tmpdir The directory hosting the candidate stale temp files.
*/ */
public function execute() { protected function execute_on($tmpdir) {
global $CFG; global $CFG;
$tmpdir = $CFG->tempdir;
// Default to last weeks time. // Default to last weeks time.
$time = time() - ($CFG->tempdatafoldercleanup * 3600); $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->dataroot - Path to moodle data files directory on server's filesystem.
* - $CFG->dirroot - Path to moodle's library folder 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->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->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->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->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"; $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 // Allow overriding of cachedir but be backwards compatible
if (!isset($CFG->cachedir)) { if (!isset($CFG->cachedir)) {
$CFG->cachedir = "$CFG->dataroot/cache"; $CFG->cachedir = "$CFG->dataroot/cache";

View file

@ -1661,6 +1661,42 @@ function make_request_directory($exceptiononerror = true) {
return make_unique_writable_directory($basedir, $exceptiononerror); 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. * 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_temp_directory('');
make_backup_temp_directory('');
make_cache_directory(''); make_cache_directory('');
make_localcache_directory(''); make_localcache_directory('');
// Purge all data from the caches. This is required for consistency between tests. // 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) { public function test_cron_delete_from_temp($nodes, $expected) {
global $CFG; global $CFG;
$tmpdir = $CFG->tempdir; $tmpdir = realpath($CFG->tempdir);
foreach ($nodes as $data) { foreach ($nodes as $data) {
if ($data->isdir) { if ($data->isdir) {
@ -168,7 +168,12 @@ class cronlib_testcase extends basic_testcase {
$actual = array(); $actual = array();
for ($iter->rewind(); $iter->valid(); $iter->next()) { 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(); $actual[] = $iter->getRealPath();
} }
} }

View file

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

View file

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

View file

@ -41,8 +41,7 @@ class mod_quiz_tags_testcase extends advanced_testcase {
$this->setAdminUser(); $this->setAdminUser();
$backupid = 'abc'; $backupid = 'abc';
$backuppath = $CFG->tempdir . '/backup/' . $backupid; $backuppath = make_backup_temp_directory($backupid);
check_dir_exists($backuppath);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname( get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . "/fixtures/random_by_tag_quiz.mbz", $backuppath); __DIR__ . "/fixtures/random_by_tag_quiz.mbz", $backuppath);

View file

@ -1,6 +1,11 @@
This files describes API changes in /mod/* - activity modules, This files describes API changes in /mod/* - activity modules,
information provided here is intended especially for developers. information provided here is intended especially for developers.
=== 3.5 ===
Required changes in code:
* use new make_backup_temp_directory()
=== 3.4 === === 3.4 ===
* Navigation between activities via a previous and next link was added to Boost, Clean and Bootstrapbase. This * Navigation between activities via a previous and next link was added to Boost, Clean and Bootstrapbase. This