From 7f18b8f61106667917fa5bdd33b885344f2f5221 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 25 Jun 2003 10:31:45 +0000 Subject: [PATCH] Make fixcrlf work even with /tmp on a separate filesystem git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274712 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/FixCRLF.java | 40 ++++--------------- .../org/apache/tools/ant/util/FileUtils.java | 38 ++++++++++++++++++ 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java index 6bbc74861..4a7b25ad4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java +++ b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java @@ -552,50 +552,24 @@ public class FixCRLF extends MatchingTask { File destFile = new File(destD, file); + boolean destIsWrong = true; if (destFile.exists()) { // Compare the destination with the temp file log("destFile exists", Project.MSG_DEBUG); if (!fileUtils.contentEquals(destFile, tmpFile)) { log(destFile + " is being written", Project.MSG_DEBUG); - if (!destFile.delete()) { - throw new BuildException("Unable to delete " - + destFile); - } - if (!tmpFile.renameTo(destFile)) { - throw new BuildException( - "Failed to transform " + srcFile - + " to " + destFile - + ". Couldn't rename temporary file: " - + tmpFile); - } - - } else { // destination is equal to temp file + } else { log(destFile + " is not written, as the contents are identical", Project.MSG_DEBUG); - if (!tmpFile.delete()) { - throw new BuildException("Unable to delete " - + tmpFile); - } - } - } else { // destFile does not exist - write the temp file - log("destFile does not exist", Project.MSG_DEBUG); - - File parent = fileUtils.getParentFile(destFile); - if (!parent.exists()) { - parent.mkdirs(); - } - - if (!tmpFile.renameTo(destFile)) { - throw new BuildException( - "Failed to transform " + srcFile - + " to " + destFile - + ". Couldn't rename temporary file: " - + tmpFile); + destIsWrong = false; } } - tmpFile = null; + if (destIsWrong) { + fileUtils.rename(tmpFile, destFile); + tmpFile = null; + } } catch (IOException e) { throw new BuildException(e); diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index f1b6bf392..6def257d7 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -1122,5 +1122,43 @@ public class FileUtils { .equals(normalize(f2.getAbsolutePath())); } + /** + * Renames a file, even if that involves crossing file system boundaries. + * + *

This will remove to (if it exists), ensure that + * to's parent directory exists and move + * from, which involves deleting from as + * well.

+ * + * @throws IOException if anything bad happens during this + * process. Note that to may have been deleted + * already when this happens. + * + * @param from the file to move + * @param to the new file name + * + * @since Ant 1.6 + */ + public void rename(File from, File to) throws IOException { + if (to.exists() && !to.delete()) { + throw new IOException("Failed to delete " + to + + " while trying to rename " + from); + } + + File parent = getParentFile(to); + if (!parent.exists() && !parent.mkdirs()) { + throw new IOException("Failed to create directory " + parent + + " while trying to rename " + from); + } + + if (!from.renameTo(to)) { + copyFile(from, to); + if (!from.delete()) { + throw new IOException("Failed to delete " + from + + " while trying to rename it."); + } + } + } + }