You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Move.java 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.BuildException;
  56. import org.apache.tools.ant.Project;
  57. import org.apache.tools.ant.types.FilterSetCollection;
  58. import org.apache.tools.ant.types.FilterSet;
  59. import org.apache.tools.ant.types.FileSet;
  60. import java.io.File;
  61. import java.io.IOException;
  62. import java.util.Enumeration;
  63. /**
  64. * Moves a file or directory to a new file or directory.
  65. * By default, the
  66. * destination file is overwritten if it already exists.
  67. * When <i>overwrite</i> is
  68. * turned off, then files are only moved if the source file is
  69. * newer than the destination file, or when the destination file does
  70. * not exist.
  71. *
  72. * <p>Source files and directories are only deleted when the file or
  73. * directory has been copied to the destination successfully. Filtering
  74. * also works.</p>
  75. *
  76. * <p>This implementation is based on Arnout Kuiper's initial design
  77. * document, the following mailing list discussions, and the
  78. * copyfile/copydir tasks.</p>
  79. *
  80. * @author Glenn McAllister
  81. * <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
  82. * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  83. * @version $Revision$
  84. *
  85. * @since Ant 1.2
  86. *
  87. * @ant.task category="filesystem"
  88. */
  89. public class Move extends Copy {
  90. public Move() {
  91. super();
  92. forceOverwrite = true;
  93. }
  94. //************************************************************************
  95. // protected and private methods
  96. //************************************************************************
  97. protected void doFileOperations() {
  98. //Attempt complete directory renames, if any, first.
  99. if (completeDirMap.size() > 0) {
  100. Enumeration e = completeDirMap.keys();
  101. while (e.hasMoreElements()) {
  102. File fromDir = (File) e.nextElement();
  103. File toDir = (File) completeDirMap.get(fromDir);
  104. try {
  105. log("Attempting to rename dir: " + fromDir +
  106. " to " + toDir, verbosity);
  107. renameFile(fromDir, toDir, filtering, forceOverwrite);
  108. } catch (IOException ioe) {
  109. String msg = "Failed to rename dir " + fromDir
  110. + " to " + toDir
  111. + " due to " + ioe.getMessage();
  112. throw new BuildException(msg, ioe, getLocation());
  113. }
  114. }
  115. }
  116. if (fileCopyMap.size() > 0) { // files to move
  117. log("Moving " + fileCopyMap.size() + " files to " +
  118. destDir.getAbsolutePath());
  119. Enumeration e = fileCopyMap.keys();
  120. while (e.hasMoreElements()) {
  121. String fromFile = (String) e.nextElement();
  122. String toFile = (String) fileCopyMap.get(fromFile);
  123. if (fromFile.equals(toFile)) {
  124. log("Skipping self-move of " + fromFile, verbosity);
  125. continue;
  126. }
  127. boolean moved = false;
  128. File f = new File(fromFile);
  129. if (f.exists()) { //Is this file still available to be moved?
  130. File d = new File(toFile);
  131. try {
  132. log("Attempting to rename: " + fromFile +
  133. " to " + toFile, verbosity);
  134. moved = renameFile(f, d, filtering, forceOverwrite);
  135. } catch (IOException ioe) {
  136. String msg = "Failed to rename " + fromFile
  137. + " to " + toFile
  138. + " due to " + ioe.getMessage();
  139. throw new BuildException(msg, ioe, getLocation());
  140. }
  141. if (!moved) {
  142. try {
  143. log("Moving " + fromFile + " to " + toFile,
  144. verbosity);
  145. FilterSetCollection executionFilters =
  146. new FilterSetCollection();
  147. if (filtering) {
  148. executionFilters
  149. .addFilterSet(getProject().getGlobalFilterSet());
  150. }
  151. for (Enumeration filterEnum =
  152. getFilterSets().elements();
  153. filterEnum.hasMoreElements();) {
  154. executionFilters
  155. .addFilterSet((FilterSet) filterEnum
  156. .nextElement());
  157. }
  158. getFileUtils().copyFile(f, d, executionFilters,
  159. getFilterChains(),
  160. forceOverwrite,
  161. getPreserveLastModified(),
  162. getEncoding(), getProject());
  163. f = new File(fromFile);
  164. if (!f.delete()) {
  165. throw new BuildException("Unable to delete "
  166. + "file "
  167. + f.getAbsolutePath());
  168. }
  169. } catch (IOException ioe) {
  170. String msg = "Failed to copy " + fromFile + " to "
  171. + toFile
  172. + " due to " + ioe.getMessage();
  173. throw new BuildException(msg, ioe, getLocation());
  174. }
  175. }
  176. }
  177. }
  178. }
  179. if (includeEmpty) {
  180. Enumeration e = dirCopyMap.elements();
  181. int count = 0;
  182. while (e.hasMoreElements()) {
  183. File d = new File((String) e.nextElement());
  184. if (!d.exists()) {
  185. if (!d.mkdirs()) {
  186. log("Unable to create directory "
  187. + d.getAbsolutePath(), Project.MSG_ERR);
  188. } else {
  189. count++;
  190. }
  191. }
  192. }
  193. if (count > 0) {
  194. log("Moved " + count + " empty directories to "
  195. + destDir.getAbsolutePath());
  196. }
  197. }
  198. if (filesets.size() > 0) {
  199. Enumeration e = filesets.elements();
  200. while (e.hasMoreElements()) {
  201. FileSet fs = (FileSet) e.nextElement();
  202. File dir = fs.getDir(getProject());
  203. if (okToDelete(dir)) {
  204. deleteDir(dir);
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * Its only ok to delete a directory tree if there are
  211. * no files in it.
  212. * @return true if a deletion can go ahead
  213. */
  214. protected boolean okToDelete(File d) {
  215. String[] list = d.list();
  216. if (list == null) {
  217. return false;
  218. } // maybe io error?
  219. for (int i = 0; i < list.length; i++) {
  220. String s = list[i];
  221. File f = new File(d, s);
  222. if (f.isDirectory()) {
  223. if (!okToDelete(f)) {
  224. return false;
  225. }
  226. } else {
  227. return false; // found a file
  228. }
  229. }
  230. return true;
  231. }
  232. /**
  233. * Go and delete the directory tree.
  234. */
  235. protected void deleteDir(File d) {
  236. String[] list = d.list();
  237. if (list == null) {
  238. return;
  239. } // on an io error list() can return null
  240. for (int i = 0; i < list.length; i++) {
  241. String s = list[i];
  242. File f = new File(d, s);
  243. if (f.isDirectory()) {
  244. deleteDir(f);
  245. } else {
  246. throw new BuildException("UNEXPECTED ERROR - The file "
  247. + f.getAbsolutePath()
  248. + " should not exist!");
  249. }
  250. }
  251. log("Deleting directory " + d.getAbsolutePath(), verbosity);
  252. if (!d.delete()) {
  253. throw new BuildException("Unable to delete directory "
  254. + d.getAbsolutePath());
  255. }
  256. }
  257. /**
  258. * Attempts to rename a file from a source to a destination.
  259. * If overwrite is set to true, this method overwrites existing file
  260. * even if the destination file is newer. Otherwise, the source file is
  261. * renamed only if the destination file is older than it.
  262. * Method then checks if token filtering is used. If it is, this method
  263. * returns false assuming it is the responsibility to the copyFile method.
  264. *
  265. * @throws IOException
  266. */
  267. protected boolean renameFile(File sourceFile, File destFile,
  268. boolean filtering, boolean overwrite)
  269. throws IOException, BuildException {
  270. boolean renamed = true;
  271. if ((getFilterSets() != null && getFilterSets().size() > 0) ||
  272. (getFilterChains() != null && getFilterChains().size() > 0)) {
  273. renamed = false;
  274. } else {
  275. if (!filtering) {
  276. // ensure that parent dir of dest file exists!
  277. // not using getParentFile method to stay 1.1 compat
  278. String parentPath = destFile.getParent();
  279. if (parentPath != null) {
  280. File parent = new File(parentPath);
  281. if (!parent.exists()) {
  282. parent.mkdirs();
  283. }
  284. }
  285. if (destFile.exists() && destFile.isFile()) {
  286. if (!destFile.delete()) {
  287. throw new BuildException("Unable to remove existing "
  288. + "file " + destFile);
  289. }
  290. }
  291. renamed = sourceFile.renameTo(destFile);
  292. } else {
  293. renamed = false;
  294. }
  295. }
  296. return renamed;
  297. }
  298. }