Browse Source

Make <replace> more robust by:

(1) making sure that files will be closed
(2) deleting temporary files

even if something goes wrong.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269961 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
150b0c8bba
2 changed files with 49 additions and 17 deletions
  1. +31
    -13
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  2. +18
    -4
      src/main/org/apache/tools/ant/util/FileUtils.java

+ 31
- 13
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -57,6 +57,7 @@ package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.FileUtils;

import java.io.File;
import java.io.FileInputStream;
@@ -101,6 +102,8 @@ public class Replace extends MatchingTask {
/** The encoding used to read and write files - if null, uses default */
private String encoding = null;
private FileUtils fileUtils = FileUtils.newFileUtils();

//Inner class
public class NestedString {

@@ -302,20 +305,19 @@ public class Replace extends MatchingTask {
throw new BuildException("Replace: source file " + src.getPath() + " doesn't exist", location);
}

File temp = new File(src.getPath() + ".temp");

if (temp.exists()) {
throw new BuildException("Replace: temporary file " + temp.getPath() + " already exists", location);
}
File temp = fileUtils.createTempFile("rep", ".tmp",
fileUtils.getParentFile(src));

Reader reader = null;
Writer writer = null;
try {
Reader fileReader = encoding == null ? new FileReader(src)
: new InputStreamReader(new FileInputStream(src), encoding);
Writer fileWriter = encoding == null ? new FileWriter(temp)
: new OutputStreamWriter(new FileOutputStream(temp), encoding);
reader = encoding == null ? new FileReader(src)
: new InputStreamReader(new FileInputStream(src), encoding);
writer = encoding == null ? new FileWriter(temp)
: new OutputStreamWriter(new FileOutputStream(temp), encoding);
BufferedReader br = new BufferedReader(fileReader);
BufferedWriter bw = new BufferedWriter(fileWriter);
BufferedReader br = new BufferedReader(reader);
BufferedWriter bw = new BufferedWriter(writer);

// read the entire file into a StringBuffer
// size of work buffer may be bigger than needed
@@ -365,7 +367,9 @@ public class Replace extends MatchingTask {

// cleanup
bw.close();
writer = null;
br.close();
reader = null;

// If there were changes, move the new one to the old one;
// otherwise, delete the new one
@@ -373,13 +377,27 @@ public class Replace extends MatchingTask {
++fileCount;
src.delete();
temp.renameTo(src);
} else {
temp.delete();
temp = null;
}
} catch (IOException ioe) {
throw new BuildException("IOException in " + src + " - " +
ioe.getClass().getName() + ":" + ioe.getMessage(), ioe, location);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {}
}
if (temp != null) {
temp.delete();
}
}
}

private String processReplacefilters(String buffer, String filename) {


+ 18
- 4
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -211,7 +211,7 @@ public class FileUtils {

// ensure that parent dir of dest file exists!
// not using getParentFile method to stay 1.1 compat
File parent = new File(destFile.getParent());
File parent = getParentFile(destFile);
if (!parent.exists()) {
parent.mkdirs();
}
@@ -347,14 +347,13 @@ public class FileUtils {
while (tok.hasMoreTokens()) {
String part = tok.nextToken();
if (part.equals("..")) {
String parentFile = helpFile.getParent();
if (parentFile == null) {
helpFile = getParentFile(helpFile);
if (helpFile == null) {
String msg = "The file or path you specified ("
+ filename + ") is invalid relative to "
+ file.getPath();
throw new BuildException(msg);
}
helpFile = new File(parentFile);
} else if (part.equals(".")) {
// Do nothing here
} else {
@@ -558,5 +557,20 @@ public class FileUtils {
}
}
}

/**
* Emulation of File.getParentFile for JDK 1.1
*
* @since 1.10
*/
public File getParentFile(File f) {
if (f != null) {
String p = f.getParent();
if (p != null) {
return new File(p);
}
}
return null;
}
}


Loading…
Cancel
Save