Browse Source

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
master
Stefan Bodewig 22 years ago
parent
commit
7f18b8f611
2 changed files with 45 additions and 33 deletions
  1. +7
    -33
      src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  2. +38
    -0
      src/main/org/apache/tools/ant/util/FileUtils.java

+ 7
- 33
src/main/org/apache/tools/ant/taskdefs/FixCRLF.java View File

@@ -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);


+ 38
- 0
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -1122,5 +1122,43 @@ public class FileUtils {
.equals(normalize(f2.getAbsolutePath()));
}

/**
* Renames a file, even if that involves crossing file system boundaries.
*
* <p>This will remove <code>to</code> (if it exists), ensure that
* <code>to</code>'s parent directory exists and move
* <code>from</code>, which involves deleting <code>from</code> as
* well.</p>
*
* @throws IOException if anything bad happens during this
* process. Note that <code>to</code> 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.");
}
}
}

}


Loading…
Cancel
Save