~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to backup/util/helper/backup_helper.class.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
 
42
42
    /**
43
43
     * Given one backupid, ensure its temp dir is completely empty
 
44
     *
 
45
     * If supplied, progress object should be ready to receive indeterminate
 
46
     * progress reports.
 
47
     *
 
48
     * @param string $backupid Backup id
 
49
     * @param core_backup_progress $progress Optional progress reporting object
44
50
     */
45
 
    static public function clear_backup_dir($backupid) {
 
51
    static public function clear_backup_dir($backupid, core_backup_progress $progress = null) {
46
52
        global $CFG;
47
 
        if (!self::delete_dir_contents($CFG->tempdir . '/backup/' . $backupid)) {
 
53
        if (!self::delete_dir_contents($CFG->tempdir . '/backup/' . $backupid, '', $progress)) {
48
54
            throw new backup_helper_exception('cannot_empty_backup_temp_dir');
49
55
        }
50
56
        return true;
52
58
 
53
59
    /**
54
60
     * Given one backupid, delete completely its temp dir
 
61
     *
 
62
     * If supplied, progress object should be ready to receive indeterminate
 
63
     * progress reports.
 
64
     *
 
65
     * @param string $backupid Backup id
 
66
     * @param core_backup_progress $progress Optional progress reporting object
55
67
     */
56
 
     static public function delete_backup_dir($backupid) {
 
68
     static public function delete_backup_dir($backupid, core_backup_progress $progress = null) {
57
69
         global $CFG;
58
 
         self::clear_backup_dir($backupid);
 
70
         self::clear_backup_dir($backupid, $progress);
59
71
         return rmdir($CFG->tempdir . '/backup/' . $backupid);
60
72
     }
61
73
 
63
75
     * Given one fullpath to directory, delete its contents recursively
64
76
     * Copied originally from somewhere in the net.
65
77
     * TODO: Modernise this
 
78
     *
 
79
     * If supplied, progress object should be ready to receive indeterminate
 
80
     * progress reports.
 
81
     *
 
82
     * @param string $dir Directory to delete
 
83
     * @param string $excludedir Exclude this directory
 
84
     * @param core_backup_progress $progress Optional progress reporting object
66
85
     */
67
 
    static public function delete_dir_contents($dir, $excludeddir='') {
 
86
    static public function delete_dir_contents($dir, $excludeddir='', core_backup_progress $progress = null) {
68
87
        global $CFG;
69
88
 
 
89
        if ($progress) {
 
90
            $progress->progress();
 
91
        }
 
92
 
70
93
        if (!is_dir($dir)) {
71
94
            // if we've been given a directory that doesn't exist yet, return true.
72
95
            // this happens when we're trying to clear out a course that has only just
108
131
        // Empty sub directories and then remove the directory
109
132
        for ($i=0; $i<count($dir_subdirs); $i++) {
110
133
            chmod($dir_subdirs[$i], $CFG->directorypermissions);
111
 
            if (self::delete_dir_contents($dir_subdirs[$i]) == false) {
 
134
            if (self::delete_dir_contents($dir_subdirs[$i], '', $progress) == false) {
112
135
                return false;
113
136
            } else {
114
137
                if (remove_dir($dir_subdirs[$i]) == false) {
125
148
    }
126
149
 
127
150
    /**
128
 
     * Delete all the temp dirs older than the time specified
 
151
     * Delete all the temp dirs older than the time specified.
 
152
     *
 
153
     * If supplied, progress object should be ready to receive indeterminate
 
154
     * progress reports.
 
155
     *
 
156
     * @param int $deletefrom Time to delete from
 
157
     * @param core_backup_progress $progress Optional progress reporting object
129
158
     */
130
 
    static public function delete_old_backup_dirs($deletefrom) {
 
159
    static public function delete_old_backup_dirs($deletefrom, core_backup_progress $progress = null) {
131
160
        global $CFG;
132
161
 
133
162
        $status = true;
140
169
                //If directory, recurse
141
170
                if (is_dir($file_path)) {
142
171
                    // $file is really the backupid
143
 
                    $status = self::delete_backup_dir($file);
 
172
                    $status = self::delete_backup_dir($file, $progress);
144
173
                //If file
145
174
                } else {
146
175
                    unlink($file_path);
179
208
     *
180
209
     * Note: the $filepath is deleted if the backup file is created successfully
181
210
     *
 
211
     * If you specify the progress monitor, this will start a new progress section
 
212
     * to track progress in processing (in case this task takes a long time).
 
213
     *
182
214
     * @param int $backupid
183
215
     * @param string $filepath zip file containing the backup
 
216
     * @param core_backup_progress $progress Optional progress monitor
184
217
     * @return stored_file if created, null otherwise
185
218
     *
186
219
     * @throws moodle_exception in case of any problems
187
220
     */
188
 
    static public function store_backup_file($backupid, $filepath) {
 
221
    static public function store_backup_file($backupid, $filepath, core_backup_progress $progress = null) {
189
222
        global $CFG;
190
223
 
191
224
        // First of all, get some information from the backup_controller to help us decide
192
 
        list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($backupid);
 
225
        list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information(
 
226
                $backupid, $progress);
193
227
 
194
228
        // Extract useful information to decide
195
229
        $hasusers  = (bool)$sinfo['users']->value;     // Backup has users
259
293
                if (@rename($filepath, $filedest)) {
260
294
                    return null;
261
295
                }
262
 
                umask(0000);
 
296
                umask($CFG->umaskpermissions);
263
297
                if (copy($filepath, $filedest)) {
264
298
                    @chmod($filedest, $CFG->filepermissions); // may fail because the permissions may not make sense outside of dataroot
265
299
                    unlink($filepath);