Commit ef844148 authored by Matteo Scaramuccia's avatar Matteo Scaramuccia
Browse files

MDL-60923 backup: Added $CFG->backuptempdir

The new setting will allow to host the temporary backup files
into a specific target directory. Defaults to '$CFG->tempdir/backup'.

Calling make_backup_temp_directory() checks that the required sub-directory
will be properly created under the new target directory.
parent 20bf0c45
......@@ -442,6 +442,7 @@ if ($interactive) {
}
}
$CFG->tempdir = $CFG->dataroot.'/temp';
$CFG->backuptempdir = $CFG->tempdir.'/backup';
$CFG->cachedir = $CFG->dataroot.'/cache';
$CFG->localcachedir = $CFG->dataroot.'/localcache';
......
......@@ -207,9 +207,9 @@ class category_bin extends base_bin {
// Get the backup file.
$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);
$fulltempdir = make_temp_directory('/backup/' . $tempdir);
$fulltempdir = make_backup_temp_directory($tempdir);
// Extract the backup to tmpdir.
$fb = get_file_packer('application/vnd.moodle.backup');
......
......@@ -211,9 +211,9 @@ class course_bin extends base_bin {
// Get the backup file.
$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);
$fulltempdir = make_temp_directory('/backup/' . $tempdir);
$fulltempdir = make_backup_temp_directory($tempdir);
// Extract the backup to tempdir.
$fb = get_file_packer('application/vnd.moodle.backup');
......
......@@ -347,7 +347,7 @@ class tool_uploadcourse_course {
/**
* 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.
*/
protected function get_restore_content_dir() {
......
......@@ -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 (!$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.
$backupid = null;
......@@ -236,7 +236,7 @@ class tool_uploadcourse_helper {
// Extracting the backup file.
$packer = get_file_packer('application/vnd.moodle.backup');
$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);
if (!$result) {
$errors['invalidbackupfile'] = new lang_string('invalidbackupfile', 'tool_uploadcourse');
......
......@@ -259,7 +259,7 @@ class tool_uploadcourse_processor {
/**
* 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() {
$backupfile = null;
......
......@@ -32,7 +32,7 @@
*/
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 $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
* 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 bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false]
* @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ]
......
......@@ -110,7 +110,7 @@ class core_backup_controller_testcase extends advanced_testcase {
global $CFG;
// 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,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$backupid = $bc->get_backupid();
......@@ -159,7 +159,7 @@ class core_backup_controller_testcase extends advanced_testcase {
$foldername = 'deadlock';
$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);
$this->setAdminUser();
......
......@@ -148,7 +148,7 @@ abstract class base_converter implements loggable {
public function get_workdir_path() {
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() {
global $CFG;
return "$CFG->tempdir/backup/$this->tempdir";
return make_backup_temp_directory($this->tempdir);
}
/// public static methods //////////////////////////////////////////////////
......
......@@ -139,7 +139,7 @@ class imscc11_backup_convert extends backup_execution_step {
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)) {
......
......@@ -89,7 +89,8 @@ class moodle1_converter extends base_converter {
public static function detect_format($tempdir) {
global $CFG;
$filepath = $CFG->tempdir . '/backup/' . $tempdir . '/moodle.xml';
$tempdirpath = make_backup_temp_directory($tempdir, false);
$filepath = $tempdirpath . '/moodle.xml';
if (file_exists($filepath)) {
// looks promising, lets load some information
$handle = fopen($filepath, 'r');
......
......@@ -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 */
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 */
protected $iconhash;
......@@ -42,39 +45,40 @@ class core_backup_moodle1_converter_testcase extends advanced_testcase {
global $CFG;
$this->tempdir = convert_helper::generate_id('unittest');
check_dir_exists("$CFG->tempdir/backup/$this->tempdir/course_files/sub1");
check_dir_exists("$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7");
$this->tempdirpath = make_backup_temp_directory($this->tempdir);
check_dir_exists("$this->tempdirpath/course_files/sub1");
check_dir_exists("$this->tempdirpath/moddata/unittest/4/7");
copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/moodle.xml",
"$CFG->tempdir/backup/$this->tempdir/moodle.xml"
"$this->tempdirpath/moodle.xml"
);
copy(
"$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(
"$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(
"$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(
"$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(
"$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() {
global $CFG;
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(
"$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();
......
......@@ -140,7 +140,7 @@ if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
// Check whether the backup directory still exists. If missing, something
// went really wrong in backup, throw error. Note that backup::MODE_IMPORT
// 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)) {
print_error('unknownbackupexporterror'); // shouldn't happen ever
}
......
......@@ -127,8 +127,7 @@ class core_backup_moodle2_testcase extends advanced_testcase {
// Extract backup file.
$backupid = 'abc';
$backuppath = $CFG->tempdir . '/backup/' . $backupid;
check_dir_exists($backuppath);
$backuppath = make_backup_temp_directory($backupid);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . '/fixtures/availability_26_format.mbz', $backuppath);
......@@ -972,8 +971,7 @@ class core_backup_moodle2_testcase extends advanced_testcase {
foreach ($backupfiles as $backupfile) {
// Extract backup file.
$backupid = $backupfile;
$backuppath = $CFG->tempdir . '/backup/' . $backupid;
check_dir_exists($backuppath);
$backuppath = make_backup_temp_directory($backupid);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . "/fixtures/$backupfile.mbz", $backuppath);
......
......@@ -72,7 +72,7 @@ if (is_null($course)) {
$browser = get_file_browser();
// check if tmp dir exists
$tmpdir = $CFG->tempdir . '/backup';
$tmpdir = make_backup_temp_directory('', false);
if (!check_dir_exists($tmpdir, true, true)) {
throw new restore_controller_exception('cannot_create_backup_temp_dir');
}
......
......@@ -60,9 +60,9 @@ abstract class backup_factory {
// Create file_logger, observing $CFG->backup_file_logger_level
// 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;
$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
// and pointing to the backup_logs table
......
......@@ -43,7 +43,8 @@ class backup_file_manager {
public static function get_backup_storage_base_dir($backupid) {
global $CFG;
return $CFG->tempdir . '/backup/' . $backupid . '/files';
$backupiddir = make_backup_temp_directory($backupid);
return $backupiddir . '/files';
}
/**
......
......@@ -121,7 +121,8 @@ abstract class backup_general_helper extends backup_helper {
$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...
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.
$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');
$extracted = $fp->extract_to_pathname($filepath, $tmpdir, array('moodle_backup.xml'), $progress);
......
......@@ -33,8 +33,8 @@ abstract class backup_helper {
* Given one backupid, create all the needed dirs to have one backup temp dir available
*/
static public function check_and_create_backup_dir($backupid) {
global $CFG;
if (!check_dir_exists($CFG->tempdir . '/backup/' . $backupid, true, true)) {
$backupiddir = make_backup_temp_directory($backupid, false);
if (empty($backupiddir)) {
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
*/
static public function clear_backup_dir($backupid, \core\progress\base $progress = null) {
global $CFG;
if (!self::delete_dir_contents($CFG->tempdir . '/backup/' . $backupid, '', $progress)) {
$backupiddir = make_backup_temp_directory($backupid, false);
if (!self::delete_dir_contents($backupiddir, '', $progress)) {
throw new backup_helper_exception('cannot_empty_backup_temp_dir');
}
return true;
......@@ -66,9 +66,9 @@ abstract class backup_helper {
* @param \core\progress\base $progress Optional progress reporting object
*/
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);
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
*/
static public function delete_old_backup_dirs($deletefrom, \core\progress\base $progress = null) {
global $CFG;
$status = true;
// Get files and directories in the temp backup dir witout descend
$list = get_directory_list($CFG->tempdir . '/backup', '', false, true, true);
// Get files and directories in the backup temp dir without descend.
$backuptempdir = make_backup_temp_directory('');
$list = get_directory_list($backuptempdir, '', false, true, true);
foreach ($list as $file) {
$file_path = $CFG->tempdir . '/backup/' . $file;
$file_path = $backuptempdir . '/' . $file;
$moddate = filemtime($file_path);
if ($status && $moddate < $deletefrom) {
//If directory, recurse
......
......@@ -131,15 +131,12 @@ abstract class convert_helper {
* @return boolean true if moodle2 format detected, false otherwise
*/
public static function detect_moodle2_format($tempdir) {
global $CFG;
$dirpath = $CFG->tempdir . '/backup/' . $tempdir;
$filepath = $dirpath . '/moodle_backup.xml';
$dirpath = make_backup_temp_directory($tempdir, false);
if (!is_dir($dirpath)) {
throw new convert_helper_exception('tmp_backup_directory_not_found', $dirpath);
}
$filepath = $dirpath . '/moodle_backup.xml';
if (!file_exists($filepath)) {
return false;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment