diff --git a/lib/classes/update/code_manager.php b/lib/classes/update/code_manager.php index 4b6f432e015..d20a14f5682 100644 --- a/lib/classes/update/code_manager.php +++ b/lib/classes/update/code_manager.php @@ -144,40 +144,6 @@ class code_manager { return $distfile; } - /** - * Move a folder with the plugin code from the source to the target location - * - * This can be used to move plugin folders to and from the dirroot/dataroot - * as needed. It is assumed that the caller checked that both locations are - * writable. - * - * Permissions in the target location are set to the same values that the - * parent directory has (see MDL-42110 for details). - * - * @param string $source full path to the current plugin code folder - * @param string $target full path to the new plugin code folder - */ - public function move_plugin_directory($source, $target) { - - $targetparent = dirname($target); - - if ($targetparent === '.') { - // No directory separator in $target.. - throw new coding_exception('Invalid target path', $target); - } - - if (!is_writable($targetparent)) { - throw new coding_exception('Attempting to move into non-writable parent directory', $targetparent); - } - - // Use parent directory's permissions for us, too. - $dirpermissions = fileperms($targetparent); - // Strip execute flags and use that for files. - $filepermissions = ($dirpermissions & 0666); - - $this->move_directory($source, $target, $dirpermissions, $filepermissions); - } - /** * Extracts the saved plugin ZIP file. * @@ -471,63 +437,6 @@ class code_manager { $connecttimeout, $skipcertverify, $tofile, $calctimeout); } - /** - * Internal helper method supposed to be called by self::move_plugin_directory() only. - * - * Moves the given source into a new location recursively. - * This is cross-device safe implementation to be used instead of the native rename() function. - * See https://bugs.php.net/bug.php?id=54097 for more details. - * - * @param string $source full path to the existing directory - * @param string $target full path to the new location of the directory - * @param int $dirpermissions - * @param int $filepermissions - */ - protected function move_directory($source, $target, $dirpermissions, $filepermissions) { - - if (file_exists($target)) { - throw new coding_exception('Attempting to overwrite existing directory', $target); - } - - if (is_dir($source)) { - $handle = opendir($source); - } else { - throw new coding_exception('Attempting to move non-existing source directory', $source); - } - - if (!file_exists($target)) { - // Do not use make_writable_directory() here - it is intended for dataroot only. - mkdir($target, true); - @chmod($target, $dirpermissions); - } - - if (!is_writable($target)) { - closedir($handle); - throw new coding_exception('Created folder not writable', $target); - } - - while ($filename = readdir($handle)) { - $sourcepath = $source.'/'.$filename; - $targetpath = $target.'/'.$filename; - - if ($filename === '.' or $filename === '..') { - continue; - } - - if (is_dir($sourcepath)) { - $this->move_directory($sourcepath, $targetpath, $dirpermissions, $filepermissions); - - } else { - rename($sourcepath, $targetpath); - @chmod($targetpath, $filepermissions); - } - } - - closedir($handle); - rmdir($source); - clearstatcache(); - } - /** * Renames the root directory of the extracted ZIP package. * diff --git a/lib/tests/update_code_manager_test.php b/lib/tests/update_code_manager_test.php index 6e1091a65c6..a2ef45c39ee 100644 --- a/lib/tests/update_code_manager_test.php +++ b/lib/tests/update_code_manager_test.php @@ -69,56 +69,6 @@ class core_update_code_manager_testcase extends advanced_testcase { $this->assertEquals(file_get_contents($returned), 'http://valid/'); } - public function test_move_plugin_directory() { - $codeman = new \core\update\testable_code_manager(); - - $tmp = make_request_directory(); - $dir = make_writable_directory($tmp.'/mod/foo/lang/en'); - file_put_contents($dir.'/foo.txt', 'Hello world!'); - - $codeman->move_plugin_directory($tmp.'/mod/foo', $tmp.'/mod/.foo.2015100200'); - - $this->assertTrue(is_dir($tmp.'/mod')); - $this->assertFalse(is_dir($tmp.'/mod/foo')); - $this->assertTrue(is_file($tmp.'/mod/.foo.2015100200/lang/en/foo.txt')); - $this->assertSame('Hello world!', file_get_contents($tmp.'/mod/.foo.2015100200/lang/en/foo.txt')); - } - - /** - * @expectedException moodle_exception - */ - public function test_move_plugin_directory_invalid_target() { - $codeman = new \core\update\testable_code_manager(); - $codeman->move_plugin_directory(make_request_directory(), 'this_is_not_valid_path'); - } - - /** - * @expectedException moodle_exception - */ - public function test_move_plugin_directory_nonwritable_target() { - $codeman = new \core\update\testable_code_manager(); - // If this does not throw exception for you, please send me your IP address. - $codeman->move_plugin_directory(make_request_directory(), '/'); - } - - /** - * @expectedException moodle_exception - */ - public function test_move_plugin_directory_existing_target() { - $codeman = new \core\update\testable_code_manager(); - $dir1 = make_request_directory(); - $dir2 = make_request_directory(); - $codeman->move_plugin_directory($dir1, $dir2); - } - - /** - * @expectedException moodle_exception - */ - public function test_move_plugin_directory_nonexisting_source() { - $codeman = new \core\update\testable_code_manager(); - $codeman->move_plugin_directory(make_request_directory().'/source', make_request_directory().'/target'); - } - public function test_unzip_plugin_file() { $codeman = new \core\update\testable_code_manager(); $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';