Browse Source

pr33969: tempfile task / FileUtils.createTempFile now actually creates the file.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@574339 13f79535-47bb-0310-9956-ffa450edef68
master
Jacobus Martinus Kruithof 18 years ago
parent
commit
7e05b3d981
5 changed files with 96 additions and 21 deletions
  1. +5
    -0
      WHATSNEW
  2. +14
    -3
      docs/manual/CoreTasks/tempfile.html
  3. +27
    -2
      src/main/org/apache/tools/ant/taskdefs/TempFile.java
  4. +2
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java
  5. +48
    -14
      src/main/org/apache/tools/ant/util/FileUtils.java

+ 5
- 0
WHATSNEW View File

@@ -28,6 +28,11 @@ Changes that could break older environments:
* Remove fall-back mechanism for references that are not resolved
during normal runtime execution.

* FileUtils.createTempFile now actually creates the file.
The TempFile task still does not create the file by default, can be instructed
to do so however using a new parameter.
Bugzilla report 33969.
Fixed bugs:
-----------



+ 14
- 3
docs/manual/CoreTasks/tempfile.html View File

@@ -144,7 +144,7 @@
<td bgcolor="#eeeeee" valign="top" align="left">
<font color="#000000" size="-1" face="arial,helvetica,sanserif">File</font>
</td>
<td bgcolor="#eeeeee" valign="top" align="left" rowspan="4">
<td bgcolor="#eeeeee" valign="top" align="left" rowspan="5">
<font color="#000000" size="-1" face="arial,helvetica,sanserif">Optional</font>
</td>
</tr>
@@ -181,10 +181,21 @@
<font color="#000000" size="-1" face="arial,helvetica,sanserif">Whether the temp file will be marked for deletion on normal exit of the Java Virtual Machine (even though the file may never be created); default <em>false</em>. <strong>Since Ant 1.7</strong></font>
</td>
<td bgcolor="#eeeeee" valign="top" align="left">
<font color="#000000" size="-1" face="arial,helvetica,sanserif">String</font>
<font color="#000000" size="-1" face="arial,helvetica,sanserif">boolean</font>
</td>
</tr>
<!-- Attribute -->
<tr>
<td bgcolor="#eeeeee" valign="top" align="left">
<font color="#000000" size="-1" face="arial,helvetica,sanserif">createfile</font>
</td>
<td bgcolor="#eeeeee" valign="top" align="left">
<font color="#000000" size="-1" face="arial,helvetica,sanserif">Whether the temp file should be created by this task.<strong>Since Ant 1.8</strong></font>
</td>
<td bgcolor="#eeeeee" valign="top" align="left">
<font color="#000000" size="-1" face="arial,helvetica,sanserif">boolean</font>
</td>
</tr>


</table>
</blockquote></td></tr>


+ 27
- 2
src/main/org/apache/tools/ant/taskdefs/TempFile.java View File

@@ -65,6 +65,9 @@ public class TempFile extends Task {

/** deleteOnExit flag */
private boolean deleteOnExit;
/** createFile flag */
private boolean createFile;

/**
* Sets the property you wish to assign the temporary file to.
@@ -123,6 +126,22 @@ public class TempFile extends Task {
public boolean isDeleteOnExit() {
return deleteOnExit;
}
/**
* If set the file is actually created, if not just a name is created.
* @param createFile boolean flag.
*/
public void setCreateFile(boolean createFile) {
this.createFile = deleteOnExit;
}

/**
* Learn whether createFile flag is set for this tempfile task.
* @return the createFile flag.
*/
public boolean isCreateFile() {
return createFile;
}

/**
* Creates the temporary file.
@@ -136,8 +155,14 @@ public class TempFile extends Task {
if (destDir == null) {
destDir = getProject().resolveFile(".");
}
File tfile = FILE_UTILS.createTempFile(
prefix, suffix, destDir, deleteOnExit);
File tfile;
if (createFile) {
tfile = FILE_UTILS.createTempFile(prefix, suffix, destDir,
deleteOnExit);
} else {
tfile = FILE_UTILS.createTempFileName(prefix, suffix, destDir,
deleteOnExit);
}

getProject().setNewProperty(property, tfile.toString());
}


+ 2
- 2
src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java View File

@@ -1824,8 +1824,8 @@ public class FTP
FTPFile [] theFiles = null;
final int maxIterations = 1000;
for (int counter = 1; counter < maxIterations; counter++) {
File localFile = FILE_UTILS.createTempFile("ant" + Integer.toString(counter), ".tmp",
null);
File localFile = FILE_UTILS.createTempFileName("ant" + Integer.toString(counter), ".tmp",
null, false);
String fileName = localFile.getName();
boolean found = false;
try {


+ 48
- 14
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -786,12 +786,8 @@ public class FileUtils {
* <p>The file denoted by the returned abstract pathname did not
* exist before this method was invoked, any subsequent invocation
* of this method will yield a different file name.</p>
* <p>
* The filename is prefixNNNNNsuffix where NNNN is a random number.
* </p>
* <p>This method is different from File.createTempFile() of JDK 1.2
* as it doesn't create the file itself. It uses the location pointed
* to by java.io.tmpdir when the parentDir attribute is null.</p>
*
* <p>As of ant 1.8 the file is actually created.</p>
*
* @param prefix prefix before the random number.
* @param suffix file extension; include the '.'.
@@ -811,19 +807,15 @@ public class FileUtils {
* <p>The file denoted by the returned abstract pathname did not
* exist before this method was invoked, any subsequent invocation
* of this method will yield a different file name.</p>
* <p>
* The filename is prefixNNNNNsuffix where NNNN is a random number.
* </p>
* <p>This method is different from File.createTempFile() of JDK 1.2
* as it doesn't create the file itself. It uses the location pointed
* to by java.io.tmpdir when the parentDir attribute is null.</p>
*
* <p>As of ant 1.8 the file is actually created.</p>
*
* @param prefix prefix before the random number.
* @param suffix file extension; include the '.'.
* @param parentDir Directory to create the temporary file in;
* java.io.tmpdir used if not specified.
* @param deleteOnExit whether to set the tempfile for deletion on
* normal VM exit.
* java.io.tmpdir used if not specified.
*
* @return a File reference to the new temporary file.
* @since Ant 1.7
@@ -834,11 +826,51 @@ public class FileUtils {
String parent = (parentDir == null)
? System.getProperty("java.io.tmpdir")
: parentDir.getPath();
try {
result = File.createTempFile(prefix, suffix, new File(parent));
} catch (IOException e) {
throw new BuildException("Could not create tempfile in " + parent, e);
}
if (deleteOnExit) {
result.deleteOnExit();
}
return result;
}
/**
* Create a File object for a temporary file in a given directory. Without
* actually creating the file.
*
* <p>The file denoted by the returned abstract pathname did not
* exist before this method was invoked, any subsequent invocation
* of this method will yield a different file name.</p>
* <p>
* The filename is prefixNNNNNsuffix where NNNN is a random number.
* </p>
*
* @param prefix prefix before the random number.
* @param suffix file extension; include the '.'.
* @param parentDir Directory to create the temporary file in;
* java.io.tmpdir used if not specified.
* @param deleteOnExit whether to set the tempfile for deletion on
* normal VM exit.
*
* @return a File reference to the new, nonexistent temporary file.
* @since Ant 1.8
*/
public File createTempFileName(String prefix, String suffix,
File parentDir, boolean deleteOnExit) {
File result = null;
String parent = (parentDir == null) ? System
.getProperty("java.io.tmpdir") : parentDir.getPath();

DecimalFormat fmt = new DecimalFormat("#####");
synchronized (rand) {
do {
result = new File(parent, prefix + fmt.format(Math.abs(rand.nextInt())) + suffix);
result = new File(parent, prefix
+ fmt.format(Math.abs(rand.nextInt())) + suffix);
} while (result.exists());
}
if (deleteOnExit) {
@@ -846,6 +878,8 @@ public class FileUtils {
}
return result;
}

/**
* Compares the contents of two files.


Loading…
Cancel
Save