MDL-55020 admin: Fix renaming of the plugin package root folder

There was a problem with core\update\code_manager::unzip_plugin_file()
if it was used to extract a plugin package into a non-empty target
directory and the plugin package root folder was being renamed at the
same time.

The problem was caused by the underlying helper method
rename_extracted_rootdir() that worked only for ZIPs extracted to an
empty temporary location. When the plugin was extracted to the actual
dirroot with other existing plugin folders present, the method failed
badly.

The solution in the patch is to always extract the ZIP into a temporary
empty location, perform the eventual root renaming there, and only then
move the extracted contents to the final destination. Additionally we
are changing the behaviour of the rename_extracted_rootdir() method so
that now it throws exception if the plugin package contains multiple
root folders (it should not happen in normal situations as such a plugin
would not pass the pre-install validation).

Unit tests did not catch this bug before because in the tests, the
target directory had been empty. Now we are adding a new directory
"aaa_another" to the target location to test in more realistic
environment. Tests for the new behaviour of the renaming method are
added, too.

p.s. I noticed that moodle_exception class was not imported into the
namespace and this is fixed now too (and covered with unit tests).
This commit is contained in:
David Mudrák
2016-07-01 01:13:21 +02:00
parent b8474fe0c7
commit 2785fd193d
3 changed files with 60 additions and 9 deletions
+47 -9
View File
@@ -26,6 +26,7 @@ namespace core\update;
use core_component;
use coding_exception;
use moodle_exception;
use SplFileInfo;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
@@ -159,15 +160,18 @@ class code_manager {
*/
public function unzip_plugin_file($zipfilepath, $targetdir, $rootdir = '') {
// Extract the package into a temporary location.
$fp = get_file_packer('application/zip');
$files = $fp->extract_to_pathname($zipfilepath, $targetdir);
$tempdir = make_request_directory();
$files = $fp->extract_to_pathname($zipfilepath, $tempdir);
if (!$files) {
return array();
}
// If requested, rename the root directory of the plugin.
if (!empty($rootdir)) {
$files = $this->rename_extracted_rootdir($targetdir, $rootdir, $files);
$files = $this->rename_extracted_rootdir($tempdir, $rootdir, $files);
}
// Sometimes zip may not contain all parent directories, add them to make it consistent.
@@ -187,6 +191,9 @@ class code_manager {
}
}
// Move the extracted files into the target location.
$this->move_extracted_plugin_files($tempdir, $targetdir, $files);
// Set the permissions of extracted subdirs and files.
$this->set_plugin_files_permissions($targetdir, $files);
@@ -443,12 +450,10 @@ class code_manager {
/**
* Renames the root directory of the extracted ZIP package.
*
* This method does not validate the presence of the single root directory
* (it is the validator's duty). It just searches for the first directory
* under the given location and renames it.
*
* The method will not rename the root if the requested location already
* exists.
* This internal helper method assumes that the plugin ZIP package has been
* extracted into a temporary empty directory so the plugin folder is the
* only folder there. The ZIP package is supposed to be validated so that
* it contains just a single root folder.
*
* @param string $dirname fullpath location of the extracted ZIP package
* @param string $rootdir the requested name of the root directory
@@ -473,8 +478,11 @@ class code_manager {
continue;
}
if (is_dir($dirname.'/'.$item)) {
if ($found !== null and $found !== $item) {
// Multiple directories found.
throw new moodle_exception('unexpected_archive_structure', 'core_plugin');
}
$found = $item;
break;
}
}
@@ -520,4 +528,34 @@ class code_manager {
}
}
}
/**
* Moves the extracted contents of the plugin ZIP into the target location.
*
* @param string $sourcedir full path to the directory the ZIP file was extracted to
* @param mixed $targetdir full path to the directory where the files should be moved to
* @param array $files list of extracted files
*/
protected function move_extracted_plugin_files($sourcedir, $targetdir, array $files) {
global $CFG;
foreach ($files as $file => $status) {
if ($status !== true) {
throw new moodle_exception('corrupted_archive_structure', 'core_plugin', '', $file, $status);
}
$source = $sourcedir.'/'.$file;
$target = $targetdir.'/'.$file;
if (is_dir($source)) {
continue;
} else {
if (!is_dir(dirname($target))) {
mkdir(dirname($target), $CFG->directorypermissions, true);
}
rename($source, $target);
}
}
}
}