From ececc5c3e332b97f962b94a475408606433ee0e6 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 29 Sep 2011 13:40:56 +0000 Subject: [PATCH] Add an option to to run the GC before retrying a failed build on non-Windows OSes as well. Might fix the NFS problem described in PR 45786 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1177305 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 +++++ manual/Tasks/delete.html | 13 +++++++++++++ .../org/apache/tools/ant/taskdefs/Delete.java | 17 ++++++++++++++++- .../org/apache/tools/ant/util/FileUtils.java | 14 ++++++++++++-- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index f2f8d21d3..ea82887d6 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -118,6 +118,11 @@ Other changes: * Provide read access to Mkdir.dir. Bugzilla Report 51684. + * has a new attribute performGCOnFailedDelete that may - + when set to true - help resolve some problems with deleting empty + directories on NFS shares. + Bugzilla Report 45807. + Changes from Ant 1.8.1 TO Ant 1.8.2 =================================== diff --git a/manual/Tasks/delete.html b/manual/Tasks/delete.html index b14edb9fc..bd0378ffc 100644 --- a/manual/Tasks/delete.html +++ b/manual/Tasks/delete.html @@ -171,6 +171,19 @@ in Directory-based Tasks, and see the Since Ant 1.8.0 No, default "false" + + performGCOnFailedDelete + + If ant fails to delete a file or directory it will retry the + operation once. If this flag is set to true it will perform a + garbage collection before retrying the delete.
+ Setting this flag to true is known to resolve some problems on + Windows (where it defaults to true) but also for directory trees + residing on an NFS share. + Since Ant 1.8.3 + No, default "true" on + Windows and "true" on any other OS. +

Examples

diff --git a/src/main/org/apache/tools/ant/taskdefs/Delete.java b/src/main/org/apache/tools/ant/taskdefs/Delete.java index 0f881fdf2..783728258 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Delete.java +++ b/src/main/org/apache/tools/ant/taskdefs/Delete.java @@ -27,6 +27,7 @@ import java.util.Comparator; import org.apache.tools.ant.Project; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.PatternSet; import org.apache.tools.ant.types.Resource; @@ -119,6 +120,7 @@ public class Delete extends MatchingTask { private static FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static SymbolicLinkUtils SYMLINK_UTILS = SymbolicLinkUtils.getSymbolicLinkUtils(); + private boolean performGc = Os.isFamily("windows"); /** * Set the name of a single file to be removed. @@ -197,6 +199,19 @@ public class Delete extends MatchingTask { this.includeEmpty = includeEmpty; } + /** + * Whether to perform a garbage collection before retrying a failed delete. + * + *

This may be required on Windows (where it is set to true by + * default) but also on other operating systems, for example when + * deleting directories from an NFS share.

+ * + * @since Ant 1.8.3 + */ + public void setPerformGcOnFailedDelete(boolean b) { + performGc = b; + } + /** * Adds a set of files to be deleted. * @param set the set of files to be deleted @@ -719,7 +734,7 @@ public class Delete extends MatchingTask { * wait a little and try again. */ private boolean delete(File f) { - if (!FILE_UTILS.tryHardToDelete(f)) { + if (!FILE_UTILS.tryHardToDelete(f, performGc)) { if (deleteOnExit) { int level = quiet ? Project.MSG_VERBOSE : Project.MSG_INFO; log("Failed to delete " + f + ", calling deleteOnExit." diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index a65875d46..f36ca8481 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -1562,8 +1562,19 @@ public class FileUtils { * @since Ant 1.8.0 */ public boolean tryHardToDelete(File f) { + return tryHardToDelete(f, ON_WINDOWS); + } + + /** + * If delete does not work, call System.gc() if asked to, wait a + * little and try again. + * + * @return whether deletion was successful + * @since Ant 1.8.3 + */ + public boolean tryHardToDelete(File f, boolean runGC) { if (!f.delete()) { - if (ON_WINDOWS) { + if (runGC) { System.gc(); } try { @@ -1576,7 +1587,6 @@ public class FileUtils { return true; } - /** * Calculates the relative path between two files. *