From ac92e4e6bd1e87f2d689a1588b469dca22316ecd Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Tue, 20 Jun 2006 15:09:20 +0000 Subject: [PATCH] add tempfile deleteOnExit. PR# 39842. Submitted by Patrick Martin (then slightly modified). git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@415695 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + WHATSNEW | 2 ++ docs/manual/CoreTasks/tempfile.html | 16 ++++++++-- .../apache/tools/ant/taskdefs/TempFile.java | 22 ++++++++++++- .../org/apache/tools/ant/util/FileUtils.java | 31 +++++++++++++++++++ 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index e5af82d2b..fbf1d0eca 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -178,6 +178,7 @@ Oystein Gisnas Patrick C. Beard Patrick Chanezon Patrick G. Heck (Gus Heck) +Patrick Martin Paul Austin Paul Christmann Paul Galbraith diff --git a/WHATSNEW b/WHATSNEW index daad993f0..efaca6099 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -434,6 +434,8 @@ Other changes: * Minor performance updates. Bugzilla report 39565. +* New deleteonexit attribute for the task. Bugzilla report 39842. + Changes from Ant 1.6.4 to Ant 1.6.5 =================================== diff --git a/docs/manual/CoreTasks/tempfile.html b/docs/manual/CoreTasks/tempfile.html index 4e2ce843e..bd512609b 100644 --- a/docs/manual/CoreTasks/tempfile.html +++ b/docs/manual/CoreTasks/tempfile.html @@ -128,7 +128,7 @@ File - + Optional @@ -156,6 +156,18 @@ String + + + + deleteonexit + + + 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 + + @@ -197,7 +209,7 @@
- Copyright © 2000-2004, The Apache Software Foundation. All Rights Reserved. + Copyright © 2000-2004, 2006, The Apache Software Foundation. All Rights Reserved.
diff --git a/src/main/org/apache/tools/ant/taskdefs/TempFile.java b/src/main/org/apache/tools/ant/taskdefs/TempFile.java index 14fbb11c1..80337df1f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/TempFile.java +++ b/src/main/org/apache/tools/ant/taskdefs/TempFile.java @@ -62,6 +62,8 @@ public class TempFile extends Task { */ private String suffix = ""; + /** deleteOnExit flag */ + private boolean deleteOnExit; /** * Sets the property you wish to assign the temporary file to. @@ -104,6 +106,22 @@ public class TempFile extends Task { this.suffix = suffix; } + /** + * Set whether the tempfile created by this task should be set + * for deletion on normal VM exit. + * @param deleteOnExit boolean flag. + */ + public void setDeleteOnExit(boolean deleteOnExit) { + this.deleteOnExit = deleteOnExit; + } + + /** + * Learn whether deleteOnExit is set for this tempfile task. + * @return boolean deleteOnExit flag. + */ + public boolean isDeleteOnExit() { + return deleteOnExit; + } /** * Creates the temporary file. @@ -117,7 +135,9 @@ public class TempFile extends Task { if (destDir == null) { destDir = FILE_UTILS.resolveFile(getProject().getBaseDir(),"."); } - File tfile = FILE_UTILS.createTempFile(prefix, suffix, destDir); + File tfile = FILE_UTILS.createTempFile( + prefix, suffix, destDir, deleteOnExit); + getProject().setNewProperty(property, tfile.toString()); } } diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index ccb5b44a5..f7813efe6 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -817,6 +817,34 @@ public class FileUtils { * @since Ant 1.5 */ public File createTempFile(String prefix, String suffix, File parentDir) { + return createTempFile(prefix, suffix, parentDir, false); + } + + /** + * Create a temporary file in a given directory. + * + *

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.

+ * + * @param prefix prefix before the random number. + * @param suffix file extension; include the '.'. + * @param parentDir Directory to create the temporary file in; + * @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 + */ + public File createTempFile(String prefix, String suffix, File parentDir, + boolean deleteOnExit) { File result = null; String parent = (parentDir == null) ? System.getProperty("java.io.tmpdir") @@ -830,6 +858,9 @@ public class FileUtils { + suffix); } while (result.exists()); } + if (deleteOnExit) { + result.deleteOnExit(); + } return result; }