Skip to content
Browse files

Merge branch 'MDL-36963-deployment-backup_24' of git://github.com/mud…

…rd8mz/moodle into MOODLE_24_STABLE
  • Loading branch information...
2 parents c6e0d38 + 81c89dd commit 0c767bf6cc75b598551495b4886b55cd784bcf97 @danpoltawski danpoltawski committed
Showing with 99 additions and 13 deletions.
  1. +53 −13 mdeploy.php
  2. +46 −0 mdeploytest.php
View
66 mdeploy.php
@@ -754,7 +754,7 @@ public function execute() {
}
// Looking good, let's try it.
- if (!$this->move_directory($sourcelocation, $backuplocation)) {
+ if (!$this->move_directory($sourcelocation, $backuplocation, true)) {
throw new backup_folder_exception('Unable to backup the current version of the plugin (moving failed)');
}
@@ -1136,23 +1136,45 @@ protected function move_directory_target_precheck($target) {
/**
* Moves the given source into a new location recursively
*
+ * The target location can not exist.
+ *
* @param string $source full path to the existing directory
* @param string $destination full path to the new location of the folder
+ * @param bool $keepsourceroot should the root of the $source be kept or removed at the end
* @return bool
*/
- protected function move_directory($source, $target) {
+ protected function move_directory($source, $target, $keepsourceroot = false) {
if (file_exists($target)) {
throw new filesystem_exception('Unable to move the directory - target location already exists');
}
+ return $this->move_directory_into($source, $target, $keepsourceroot);
+ }
+
+ /**
+ * Moves the given source into a new location recursively
+ *
+ * If the target already exists, files are moved into it. The target is created otherwise.
+ *
+ * @param string $source full path to the existing directory
+ * @param string $destination full path to the new location of the folder
+ * @param bool $keepsourceroot should the root of the $source be kept or removed at the end
+ * @return bool
+ */
+ protected function move_directory_into($source, $target, $keepsourceroot = false) {
+
if (is_dir($source)) {
$handle = opendir($source);
} else {
throw new filesystem_exception('Source location is not a directory');
}
- mkdir($target, 02777);
+ if (is_dir($target)) {
+ $result = true;
+ } else {
+ $result = mkdir($target, 02777);
+ }
while ($filename = readdir($handle)) {
$sourcepath = $source.'/'.$filename;
@@ -1163,26 +1185,37 @@ protected function move_directory($source, $target) {
}
if (is_dir($sourcepath)) {
- $this->move_directory($sourcepath, $targetpath);
+ $result = $result && $this->move_directory($sourcepath, $targetpath, false);
} else {
- rename($sourcepath, $targetpath);
+ $result = $result && rename($sourcepath, $targetpath);
}
}
closedir($handle);
- return rmdir($source);
+
+ if (!$keepsourceroot) {
+ $result = $result && rmdir($source);
+ }
+
+ clearstatcache();
+
+ return $result;
}
/**
* Deletes the given directory recursively
*
* @param string $path full path to the directory
+ * @param bool $keeppathroot should the root of the $path be kept (i.e. remove the content only) or removed too
+ * @return bool
*/
- protected function remove_directory($path) {
+ protected function remove_directory($path, $keeppathroot = false) {
+
+ $result = true;
if (!file_exists($path)) {
- return;
+ return $result;
}
if (is_dir($path)) {
@@ -1199,15 +1232,22 @@ protected function remove_directory($path) {
}
if (is_dir($filepath)) {
- $this->remove_directory($filepath);
+ $result = $result && $this->remove_directory($filepath, false);
} else {
- unlink($filepath);
+ $result = $result && unlink($filepath);
}
}
closedir($handle);
- return rmdir($path);
+
+ if (!$keeppathroot) {
+ $result = $result && rmdir($path);
+ }
+
+ clearstatcache();
+
+ return $result;
}
/**
@@ -1242,8 +1282,8 @@ protected function unzip_plugin($ziplocation, $plugintyperoot, $expectedlocation
if (!$zip->extractTo($plugintyperoot)) {
$zip->close();
- $this->remove_directory($expectedlocation); // just in case something was created
- $this->move_directory($backuplocation, $expectedlocation);
+ $this->remove_directory($expectedlocation, true); // just in case something was created
+ $this->move_directory_into($backuplocation, $expectedlocation);
throw new zip_exception('Unable to extract the zip package');
}
View
46 mdeploytest.php
@@ -86,6 +86,30 @@ protected function initialize() {
/**
+ * Testable subclass
+ *
+ * @copyright 2012 David Mudrak <david@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class testable_worker extends worker {
+
+ /**
+ * Provides access to the protected method.
+ */
+ public function move_directory($source, $target, $keepsourceroot = false) {
+ return parent::move_directory($source, $target, $keepsourceroot);
+ }
+
+ /**
+ * Provides access to the protected method.
+ */
+ public function remove_directory($path, $keeppathroot = false) {
+ return parent::remove_directory($path, $keeppathroot);
+ }
+}
+
+
+/**
* Test cases for the mdeploy utility
*
* @copyright 2012 David Mudrak <david@moodle.com>
@@ -212,4 +236,26 @@ public function test_get_option() {
$this->assertTrue(true);
}
}
+
+ public function test_moving_and_removing_directories() {
+ $worker = testable_worker::instance();
+
+ $root = sys_get_temp_dir().'/'.uniqid('mdeploytest', true);
+ mkdir($root.'/a', 0777, true);
+ touch($root.'/a/a.txt');
+
+ $this->assertTrue(file_exists($root.'/a/a.txt'));
+ $this->assertFalse(file_exists($root.'/b/a.txt'));
+ $this->assertTrue($worker->move_directory($root.'/a', $root.'/b'));
+ $this->assertFalse(is_dir($root.'/a'));
+ $this->assertTrue(file_exists($root.'/b/a.txt'));
+ $this->assertTrue($worker->move_directory($root.'/b', $root.'/c', true));
+ $this->assertTrue(file_exists($root.'/c/a.txt'));
+ $this->assertFalse(file_exists($root.'/b/a.txt'));
+ $this->assertTrue(is_dir($root.'/b'));
+ $this->assertTrue($worker->remove_directory($root.'/c', true));
+ $this->assertFalse(file_exists($root.'/c/a.txt'));
+ $this->assertTrue($worker->remove_directory($root.'/c'));
+ $this->assertFalse(is_dir($root.'/c'));
+ }
}

0 comments on commit 0c767bf

Please sign in to comment.
Something went wrong with that request. Please try again.