MoveTask.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /*
  3. * $Id: MoveTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/tasks/system/CopyTask.php';
  22. include_once 'phing/system/io/PhingFile.php';
  23. include_once 'phing/system/io/IOException.php';
  24. /**
  25. * Moves a file or directory to a new file or directory.
  26. *
  27. * By default, the destination file is overwritten if it
  28. * already exists. When overwrite is turned off, then files
  29. * are only moved if the source file is newer than the
  30. * destination file, or when the destination file does not
  31. * exist.
  32. *
  33. * Source files and directories are only deleted when the file or
  34. * directory has been copied to the destination successfully.
  35. *
  36. * @version $Revision: 905 $
  37. * @package phing.tasks.system
  38. */
  39. class MoveTask extends CopyTask {
  40. function __construct() {
  41. parent::__construct();
  42. $this->forceOverwrite = true;
  43. }
  44. /**
  45. * Validates attributes coming in from XML
  46. *
  47. * @access private
  48. * @return void
  49. * @throws BuildException
  50. */
  51. protected function validateAttributes() {
  52. if ($this->file !== null && $this->file->isDirectory()) {
  53. if (($this->destFile !== null && $this->destDir !== null)
  54. || ($this->destFile === null && $this->destDir === null)) {
  55. throw new BuildException("One and only one of tofile and todir must be set.");
  56. }
  57. if ($this->destFile === null)
  58. {
  59. $this->destFile = new PhingFile($this->destDir, $this->file->getName());
  60. }
  61. if ($this->destDir === null)
  62. {
  63. $this->destDir = $this->destFile->getParentFile();
  64. }
  65. $this->completeDirMap[$this->file->getAbsolutePath()] = $this->destFile->getAbsolutePath();
  66. $this->file = null;
  67. } else {
  68. parent::validateAttributes();
  69. }
  70. }
  71. protected function doWork() {
  72. if (count($this->completeDirMap) > 0)
  73. {
  74. foreach ($this->completeDirMap as $from => $to)
  75. {
  76. $f = new PhingFile($from);
  77. $d = new PhingFile($to);
  78. $moved = false;
  79. try { // try to rename
  80. $this->log("Attempting to rename $from to $to", $this->verbosity);
  81. $this->fileUtils->copyFile($f, $d, $this->forceOverwrite, $this->preserveLMT, $this->filterChains, $this->getProject(), $this->mode);
  82. $f->delete(true);
  83. $moved = true;
  84. } catch (IOException $ioe) {
  85. $moved = false;
  86. $this->log("Failed to rename $from to $to: " . $ioe->getMessage(), $this->verbosity);
  87. }
  88. }
  89. }
  90. $copyMapSize = count($this->fileCopyMap);
  91. if ($copyMapSize > 0) {
  92. // files to move
  93. $this->log("Moving $copyMapSize files to " . $this->destDir->getAbsolutePath());
  94. foreach($this->fileCopyMap as $from => $to) {
  95. if ($from == $to) {
  96. $this->log("Skipping self-move of $from", $this->verbosity);
  97. continue;
  98. }
  99. $f = new PhingFile($from);
  100. $d = new PhingFile($to);
  101. try { // try to move
  102. $this->log("Moving $from to $to", $this->verbosity);
  103. $this->fileUtils->copyFile($f, $d, $this->forceOverwrite, $this->preserveLMT, $this->filterChains, $this->getProject(), $this->mode);
  104. $f->delete();
  105. } catch (IOException $ioe) {
  106. $msg = "Failed to move $from to $to: " . $ioe->getMessage();
  107. throw new BuildException($msg, $this->location);
  108. }
  109. } // foreach fileCopyMap
  110. } // if copyMapSize
  111. // handle empty dirs if appropriate
  112. if ($this->includeEmpty) {
  113. $e = array_keys($this->dirCopyMap);
  114. $count = 0;
  115. foreach ($e as $dir) {
  116. $d = new PhingFile((string) $dir);
  117. if (!$d->exists()) {
  118. if (!$d->mkdirs()) {
  119. $this->log("Unable to create directory " . $d->getAbsolutePath(), Project::MSG_ERR);
  120. } else {
  121. $count++;
  122. }
  123. }
  124. }
  125. if ($count > 0) {
  126. $this->log("moved $count empty director" . ($count == 1 ? "y" : "ies") . " to " . $this->destDir->getAbsolutePath());
  127. }
  128. }
  129. if (count($this->filesets) > 0) {
  130. // process filesets
  131. foreach($this->filesets as $fs) {
  132. $dir = $fs->getDir($this->project);
  133. if ($this->okToDelete($dir)) {
  134. $this->deleteDir($dir);
  135. }
  136. }
  137. }
  138. }
  139. /** Its only ok to delete a dir tree if there are no files in it. */
  140. private function okToDelete($d) {
  141. $list = $d->listDir();
  142. if ($list === null) {
  143. return false; // maybe io error?
  144. }
  145. foreach($list as $s) {
  146. $f = new PhingFile($d, $s);
  147. if ($f->isDirectory()) {
  148. if (!$this->okToDelete($f)) {
  149. return false;
  150. }
  151. } else {
  152. // found a file
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. /** Go and delete the directory tree. */
  159. private function deleteDir($d) {
  160. $list = $d->listDir();
  161. if ($list === null) {
  162. return; // on an io error list() can return null
  163. }
  164. foreach($list as $fname) {
  165. $f = new PhingFile($d, $fname);
  166. if ($f->isDirectory()) {
  167. $this->deleteDir($f);
  168. } else {
  169. throw new BuildException("UNEXPECTED ERROR - The file " . $f->getAbsolutePath() . " should not exist!");
  170. }
  171. }
  172. $this->log("Deleting directory " . $d->getPath(), $this->verbosity);
  173. try {
  174. $d->delete();
  175. } catch (Exception $e) {
  176. throw new BuildException("Unable to delete directory " . $d->__toString() . ": " . $e->getMessage());
  177. }
  178. }
  179. }