/build/ext/phing/classes/phing/tasks/system/MoveTask.php

http://github.com/alexgorbatchev/SyntaxHighlighter · PHP · 247 lines · 162 code · 29 blank · 56 comment · 32 complexity · 4b0fae0b26a93b6fe18979c4362c92ad MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: MoveTask.php 325 2007-12-20 15:44:58Z hans $
  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: 1.8 $
  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->renameFile($f, $d, $this->forceOverwrite);
  82. $moved = true;
  83. } catch (IOException $ioe) {
  84. $moved = false;
  85. $this->log("Failed to rename $from to $to: " . $ioe->getMessage(), $this->verbosity);
  86. }
  87. }
  88. }
  89. $copyMapSize = count($this->fileCopyMap);
  90. if ($copyMapSize > 0) {
  91. // files to move
  92. $this->log("Moving $copyMapSize files to " . $this->destDir->getAbsolutePath());
  93. foreach($this->fileCopyMap as $from => $to) {
  94. if ($from == $to) {
  95. $this->log("Skipping self-move of $from", $this->verbosity);
  96. continue;
  97. }
  98. $moved = false;
  99. $f = new PhingFile($from);
  100. $d = new PhingFile($to);
  101. $moved = false;
  102. try { // try to rename
  103. $this->log("Attempting to rename $from to $to", $this->verbosity);
  104. $this->renameFile($f, $d, $this->forceOverwrite);
  105. $moved = true;
  106. } catch (IOException $ioe) {
  107. $moved = false;
  108. $this->log("Failed to rename $from to $to: " . $ioe->getMessage(), $this->verbosity);
  109. }
  110. if (!$moved) {
  111. try { // try to move
  112. $this->log("Moving $from to $to", $this->verbosity);
  113. $this->fileUtils->copyFile($f, $d, $this->forceOverwrite, $this->preserveLMT, $this->filterChains, $this->getProject());
  114. $f = new PhingFile($fromFile);
  115. $f->delete();
  116. } catch (IOException $ioe) {
  117. $msg = "Failed to move $from to $to: " . $ioe->getMessage();
  118. throw new BuildException($msg, $this->location);
  119. }
  120. } // if !moved
  121. } // foreach fileCopyMap
  122. } // if copyMapSize
  123. // handle empty dirs if appropriate
  124. if ($this->includeEmpty) {
  125. $e = array_keys($this->dirCopyMap);
  126. $count = 0;
  127. foreach ($e as $dir) {
  128. $d = new PhingFile((string) $dir);
  129. if (!$d->exists()) {
  130. if (!$d->mkdirs()) {
  131. $this->log("Unable to create directory " . $d->getAbsolutePath(), Project::MSG_ERR);
  132. } else {
  133. $count++;
  134. }
  135. }
  136. }
  137. if ($count > 0) {
  138. $this->log("moved $count empty director" . ($count == 1 ? "y" : "ies") . " to " . $this->destDir->getAbsolutePath());
  139. }
  140. }
  141. if (count($this->filesets) > 0) {
  142. // process filesets
  143. foreach($this->filesets as $fs) {
  144. $dir = $fs->getDir($this->project);
  145. if ($this->okToDelete($dir)) {
  146. $this->deleteDir($dir);
  147. }
  148. }
  149. }
  150. }
  151. /** Its only ok to delete a dir tree if there are no files in it. */
  152. private function okToDelete($d) {
  153. $list = $d->listDir();
  154. if ($list === null) {
  155. return false; // maybe io error?
  156. }
  157. foreach($list as $s) {
  158. $f = new PhingFile($d, $s);
  159. if ($f->isDirectory()) {
  160. if (!$this->okToDelete($f)) {
  161. return false;
  162. }
  163. } else {
  164. // found a file
  165. return false;
  166. }
  167. }
  168. return true;
  169. }
  170. /** Go and delete the directory tree. */
  171. private function deleteDir($d) {
  172. $list = $d->listDir();
  173. if ($list === null) {
  174. return; // on an io error list() can return null
  175. }
  176. foreach($list as $fname) {
  177. $f = new PhingFile($d, $fname);
  178. if ($f->isDirectory()) {
  179. $this->deleteDir($f);
  180. } else {
  181. throw new BuildException("UNEXPECTED ERROR - The file " . $f->getAbsolutePath() . " should not exist!");
  182. }
  183. }
  184. $this->log("Deleting directory " . $d->getPath(), $this->verbosity);
  185. try {
  186. $d->delete();
  187. } catch (Exception $e) {
  188. throw new BuildException("Unable to delete directory " . $d->__toString() . ": " . $e->getMessage());
  189. }
  190. }
  191. /**
  192. * Attempts to rename a file from a source to a destination.
  193. * If overwrite is set to true, this method overwrites existing file
  194. * even if the destination file is newer.
  195. * Otherwise, the source f
  196. * ile is renamed only if the destination file #
  197. * is older than it.
  198. */
  199. private function renameFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite) {
  200. $renamed = true;
  201. // ensure that parent dir of dest file exists!
  202. $parent = $destFile->getParentFile();
  203. if ($parent !== null) {
  204. if (!$parent->exists()) {
  205. $parent->mkdirs();
  206. }
  207. }
  208. if ($destFile->exists()) {
  209. try {
  210. $destFile->delete();
  211. } catch (Exception $e) {
  212. throw new BuildException("Unable to remove existing file " . $destFile->__toString() . ": " . $e->getMessage());
  213. }
  214. }
  215. $renamed = $sourceFile->renameTo($destFile);
  216. return $renamed;
  217. }
  218. }