MDL-36963 Improve mdeploy worker::move_directory() method
authorDavid Mudrák <david@moodle.com>
Thu, 6 Dec 2012 01:31:36 +0000 (02:31 +0100)
committerDan Poltawski <dan@moodle.com>
Thu, 6 Dec 2012 03:03:35 +0000 (11:03 +0800)
The additional parameter allows to use this method without actual
removing the root of the source location. That is, it is now possible to
move the content of a folder only.

Also, a small refactoring happened here as we will need a variant of
this method that does not throw exception if the target already exists.

mdeploy.php

index 8381a90..9e986b0 100644 (file)
@@ -1136,23 +1136,45 @@ class worker extends singleton_pattern {
     /**
      * Moves the given source into a new location recursively
      *
     /**
      * 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 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
      */
      * @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');
         }
 
 
         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');
         }
 
         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;
 
         while ($filename = readdir($handle)) {
             $sourcepath = $source.'/'.$filename;
@@ -1163,15 +1185,22 @@ class worker extends singleton_pattern {
             }
 
             if (is_dir($sourcepath)) {
             }
 
             if (is_dir($sourcepath)) {
-                $this->move_directory($sourcepath, $targetpath);
+                $result = $result && $this->move_directory($sourcepath, $targetpath, false);
 
             } else {
 
             } else {
-                rename($sourcepath, $targetpath);
+                $result = $result && rename($sourcepath, $targetpath);
             }
         }
 
         closedir($handle);
             }
         }
 
         closedir($handle);
-        return rmdir($source);
+
+        if (!$keepsourceroot) {
+            $result = $result && rmdir($source);
+        }
+
+        clearstatcache();
+
+        return $result;
     }
 
     /**
     }
 
     /**