From 7e05b3d981ceab1b87536eed26f67d82470f1a0c Mon Sep 17 00:00:00 2001 From: Jacobus Martinus Kruithof Date: Mon, 10 Sep 2007 19:15:27 +0000 Subject: [PATCH] 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 --- WHATSNEW | 5 ++ docs/manual/CoreTasks/tempfile.html | 17 ++++- .../apache/tools/ant/taskdefs/TempFile.java | 29 ++++++++- .../tools/ant/taskdefs/optional/net/FTP.java | 4 +- .../org/apache/tools/ant/util/FileUtils.java | 62 ++++++++++++++----- 5 files changed, 96 insertions(+), 21 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index d0f4e6e90..1de258964 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -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: ----------- diff --git a/docs/manual/CoreTasks/tempfile.html b/docs/manual/CoreTasks/tempfile.html index 696b4e297..54cefb680 100644 --- a/docs/manual/CoreTasks/tempfile.html +++ b/docs/manual/CoreTasks/tempfile.html @@ -144,7 +144,7 @@ File - + Optional @@ -181,10 +181,21 @@ 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 false. Since Ant 1.7 - String + boolean + + + + + + createfile + + + Whether the temp file should be created by this task.Since Ant 1.8 + + + boolean - diff --git a/src/main/org/apache/tools/ant/taskdefs/TempFile.java b/src/main/org/apache/tools/ant/taskdefs/TempFile.java index 7e9166df7..d61fb2b46 100644 --- a/src/main/org/apache/tools/ant/taskdefs/TempFile.java +++ b/src/main/org/apache/tools/ant/taskdefs/TempFile.java @@ -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()); } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java b/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java index 6a421d720..34dd9c98b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java @@ -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 { diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 184427ed2..8cc97d1c5 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -786,12 +786,8 @@ public class FileUtils { *

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.

- *

- * The filename is prefixNNNNNsuffix where NNNN is a random number. - *

- *

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.

+ * + *

As of ant 1.8 the file is actually created.

* * @param prefix prefix before the random number. * @param suffix file extension; include the '.'. @@ -811,19 +807,15 @@ public class FileUtils { *

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.

- *

- * The filename is prefixNNNNNsuffix where NNNN is a random number. - *

- *

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.

+ * + *

As of ant 1.8 the file is actually created.

* * @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. + * + *

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.

+ *

+ * The filename is prefixNNNNNsuffix where NNNN is a random number. + *

+ * + * @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.