diff --git a/WHATSNEW b/WHATSNEW index 0c6aa8286..5a568d667 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -35,6 +35,8 @@ Changes that could break older environments: doesn't match files contained in a directory named foo - use foo/* instead. +* will not remove trailing whitespace at the end of lines anymore. + Other changes: -------------- @@ -145,6 +147,9 @@ Other changes: * The .NET tasks have been adapted to the beta2 release of the framework. +* will now try to rename() files before copying them byte by + byte - only if filtering is of, of course. + Fixed bugs: ----------- @@ -220,6 +225,8 @@ Fixed bugs: * failed for large files. +* removed files you tried to move to themselves. + Changes from Ant 1.2 to Ant 1.3 =========================================== diff --git a/src/main/org/apache/tools/ant/taskdefs/Move.java b/src/main/org/apache/tools/ant/taskdefs/Move.java index 3b559cc18..bfe07fd74 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Move.java +++ b/src/main/org/apache/tools/ant/taskdefs/Move.java @@ -63,19 +63,20 @@ import java.util.*; /** * Moves a file or directory to a new file or directory. By default, * the destination is overwriten when existing. When overwrite is - * turned off, then files are only moved if the source file is + * turned off, then files are only moved if the source file is * newer than the destination file, or when the destination file does * not exist.

* - *

Source files and directories are only deleted when the file or + *

Source files and directories are only deleted when the file or * directory has been copied to the destination successfully. Filtering * also works.

* *

This implementation is based on Arnout Kuiper's initial design - * document, the following mailing list discussions, and the + * document, the following mailing list discussions, and the * copyfile/copydir tasks.

* * @author Glenn McAllister glennm@ca.ibm.com + * @author Magesh Umasankar */ public class Move extends Copy { @@ -90,7 +91,7 @@ public class Move extends Copy { protected void doFileOperations() { if (fileCopyMap.size() > 0) { // files to move - log("Moving " + fileCopyMap.size() + " files to " + + log("Moving " + fileCopyMap.size() + " files to " + destDir.getAbsolutePath() ); Enumeration e = fileCopyMap.keys(); @@ -98,28 +99,52 @@ public class Move extends Copy { String fromFile = (String) e.nextElement(); String toFile = (String) fileCopyMap.get(fromFile); - try { - log("Moving " + fromFile + " to " + toFile, verbosity); - - FilterSet executionFilterSet = new FilterSet(); - if (filtering) { - executionFilterSet.addFilterSet(project.getGlobalFilterSet()); - } - for (Enumeration filterEnum = getFilterSets().elements(); filterEnum.hasMoreElements();) { - executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement()); - } - getFileUtils().copyFile(fromFile, toFile, executionFilterSet, - forceOverwrite); + if( fromFile.equals( toFile ) ) { + log("Skipping self-move of " + fromFile, verbosity); + continue; + } - File f = new File(fromFile); - if (!f.delete()) { - throw new BuildException("Unable to delete file " + f.getAbsolutePath()); - } + boolean moved = false; + File f = new File(fromFile); + File d = new File(toFile); + + try { + log("Attempting to rename: " + fromFile + + " to " + toFile, verbosity); + moved = renameFile(f, d, filtering, forceOverwrite); } catch (IOException ioe) { - String msg = "Failed to copy " + fromFile + " to " + toFile + String msg = "Failed to rename " + fromFile + + " to " + toFile + " due to " + ioe.getMessage(); throw new BuildException(msg, ioe, location); } + + if (!moved) { + try { + log("Moving " + fromFile + " to " + toFile, verbosity); + + FilterSet executionFilterSet = new FilterSet(); + if (filtering) { + executionFilterSet.addFilterSet(project.getGlobalFilterSet()); + } + for (Enumeration filterEnum = getFilterSets().elements(); filterEnum.hasMoreElements();) { + executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement()); + } + getFileUtils().copyFile(f, d, executionFilterSet, + forceOverwrite); + + f = new File(fromFile); + if (!f.delete()) { + throw new BuildException("Unable to delete file " + + f.getAbsolutePath()); + } + } catch (IOException ioe) { + String msg = "Failed to copy " + fromFile + " to " + + toFile + + " due to " + ioe.getMessage(); + throw new BuildException(msg, ioe, location); + } + } } } @@ -156,7 +181,7 @@ public class Move extends Copy { } /** - * Its only ok to delete a directory tree if there are + * Its only ok to delete a directory tree if there are * no files in it. */ protected boolean okToDelete(File d) { @@ -198,4 +223,42 @@ public class Move extends Copy { } } + /** + * Attempts to rename a file from a source to a destination. + * If overwrite is set to true, this method overwrites existing file + * even if the destination file is newer. Otherwise, the source file is + * renamed only if the destination file is older than it. + * Method then checks if token filtering is used. If it is, this method + * returns false assuming it is the responsibility to the copyFile method. + * + * @throws IOException + */ + protected boolean renameFile(File sourceFile, File destFile, + boolean filtering, boolean overwrite) + throws IOException, BuildException { + + boolean renamed = true; + if (!filtering) { + // ensure that parent dir of dest file exists! + // not using getParentFile method to stay 1.1 compat + String parentPath = destFile.getParent(); + if (parentPath != null) { + File parent = new File(parentPath); + if (!parent.exists()) { + parent.mkdirs(); + } + } + + if (destFile.exists()) { + if (!destFile.delete()) { + throw new BuildException("Unable to remove existing file " + + destFile); + } + } + renamed = sourceFile.renameTo(destFile); + } else { + renamed = false; + } + return renamed; + } }