ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r703151 - in /ant/core/trunk: WHATSNEW src/main/org/apache/tools/ant/taskdefs/Delete.java src/main/org/apache/tools/ant/util/FileUtils.java
Date Thu, 09 Oct 2008 12:23:20 GMT
Author: bodewig
Date: Thu Oct  9 05:23:19 2008
New Revision: 703151

URL: http://svn.apache.org/viewvc?rev=703151&view=rev
Log:
use the same logic as <delete> if deletion of a recently closed file fails on Windows
during rename.  PR 45960.

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Delete.java
    ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=703151&r1=703150&r2=703151&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Thu Oct  9 05:23:19 2008
@@ -404,6 +404,12 @@
  * <ftp> now supports selectors for remote directories as well.
    Bugzilla Report 44726.
 
+ * In some cases Ant fails to rename files if the source or target
+   file has just recently been closed on Windows.  It will now try to
+   delete the offending file once again after giving the Java VM time
+   to really close the file.
+   Bugzilla Report 45960.
+
 Changes from Ant 1.7.0 TO Ant 1.7.1
 =============================================
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Delete.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Delete.java?rev=703151&r1=703150&r2=703151&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Delete.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Delete.java Thu Oct  9 05:23:19
2008
@@ -57,6 +57,7 @@
 import org.apache.tools.ant.types.selectors.MajoritySelector;
 import org.apache.tools.ant.types.selectors.ContainsRegexpSelector;
 import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector;
+import org.apache.tools.ant.util.FileUtils;
 import org.apache.tools.ant.util.SymbolicLinkUtils;
 
 /**
@@ -74,7 +75,6 @@
  * @ant.task category="filesystem"
  */
 public class Delete extends MatchingTask {
-    private static final int DELETE_RETRY_SLEEP_MILLIS = 10;
     private static final ResourceComparator REVERSE_FILESYSTEM = new Reverse(new FileSystem());
     private static final ResourceSelector EXISTS = new Exists();
 
@@ -114,6 +114,7 @@
     private boolean failonerror = true;
     private boolean deleteOnExit = false;
     private Resources rcs = null;
+    private static FileUtils FILE_UTILS = FileUtils.getFileUtils();
     private static SymbolicLinkUtils SYMLINK_UTILS =
         SymbolicLinkUtils.getSymbolicLinkUtils();
 
@@ -659,16 +660,7 @@
      * wait a little and try again.
      */
     private boolean delete(File f) {
-        if (!f.delete()) {
-            if (Os.isFamily("windows")) {
-                System.gc();
-            }
-            try {
-                Thread.sleep(DELETE_RETRY_SLEEP_MILLIS);
-            } catch (InterruptedException ex) {
-                // Ignore Exception
-            }
-            if (!f.delete()) {
+        if (!FILE_UTILS.tryHardToDelete(f)) {
                 if (deleteOnExit) {
                     int level = quiet ? Project.MSG_VERBOSE : Project.MSG_INFO;
                     log("Failed to delete " + f + ", calling deleteOnExit."
@@ -678,7 +670,6 @@
                     return true;
                 }
                 return false;
-            }
         }
         return true;
     }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java?rev=703151&r1=703150&r2=703151&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java Thu Oct  9 05:23:19 2008
@@ -54,6 +54,7 @@
  *
  */
 public class FileUtils {
+    private static final int DELETE_RETRY_SLEEP_MILLIS = 10;
     private static final int EXPAND_SPACE = 50;
     private static final FileUtils PRIMARY_INSTANCE = new FileUtils();
 
@@ -1229,7 +1230,7 @@
             System.err.println("Rename of " + from + " to " + to + " is a no-op.");
             return;
         }
-        if (to.exists() && !(from.equals(to.getCanonicalFile()) || to.delete()))
{
+        if (to.exists() && !(from.equals(to.getCanonicalFile()) || tryHardToDelete(to)))
{
             throw new IOException("Failed to delete " + to + " while trying to rename " +
from);
         }
         File parent = to.getParentFile();
@@ -1239,7 +1240,7 @@
         }
         if (!from.renameTo(to)) {
             copyFile(from, to);
-            if (!from.delete()) {
+            if (!tryHardToDelete(from)) {
                 throw new IOException("Failed to delete " + from + " while trying to rename
it.");
             }
         }
@@ -1438,6 +1439,30 @@
     }
 
     /**
+     * Accommodate Windows bug encountered in both Sun and IBM JDKs.
+     * Others possible. If the delete does not work, call System.gc(),
+     * wait a little and try again.
+     *
+     * @return whether deletion was successful
+     * @since Ant 1.8.0
+     */
+    public boolean tryHardToDelete(File f) {
+        if (!f.delete()) {
+            if (ON_WINDOWS) {
+                System.gc();
+            }
+            try {
+                Thread.sleep(DELETE_RETRY_SLEEP_MILLIS);
+            } catch (InterruptedException ex) {
+                // Ignore Exception
+            }
+            return f.delete();
+        }
+        return true;
+    }
+
+
+    /**
      * Calculates the relative path between two files.
      * <p>
      * Implementation note:<br/> This function may throw an IOException if an I/O error
occurs



Mime
View raw message