From b6d376a1775fd4efeb7742452c56e7629cbed885 Mon Sep 17 00:00:00 2001 From: Christian Halstrick Date: Tue, 21 Feb 2012 14:23:43 +0100 Subject: [PATCH] Add IGNORE_ERRORS to FileUtils.delete() There are a few situations where you want to delete files or folders but where you are not interested in getting exceptions if this doesn't succeed. E.g. if you delete garbage in the GC class you want that if certain files can't be deleted the command succeeds. Maybe the next garbage collector run has more luck not to interfere with a virus scanner run on Windows. Therefore an option is added to FileUtils.delete() not to report errors in such cases. Change-Id: I58994d8c481e591dcbb0f2be7dfa562e125f0f08 Signed-off-by: Christian Halstrick --- .../src/org/eclipse/jgit/util/FileUtils.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java index 56d20d4ff..0d97510c5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java @@ -77,6 +77,11 @@ public class FileUtils { */ public static final int SKIP_MISSING = 4; + /** + * Option not to throw exceptions when a deletion finally doesn't succeed. + */ + public static final int IGNORE_ERRORS = 8; + /** * Delete file or empty folder * @@ -106,7 +111,8 @@ public class FileUtils { * if deletion of {@code f} fails. This may occur if {@code f} * didn't exist when the method was called. This can therefore * cause IOExceptions during race conditions when multiple - * concurrent threads all try to delete the same file. + * concurrent threads all try to delete the same file. This + * exception is not thrown when IGNORE_ERRORS is set. */ public static void delete(final File f, int options) throws IOException { if ((options & SKIP_MISSING) != 0 && !f.exists()) @@ -131,8 +137,9 @@ public class FileUtils { return; } } - throw new IOException(MessageFormat.format( - JGitText.get().deleteFileFailed, f.getAbsolutePath())); + if ((options & IGNORE_ERRORS) == 0) + throw new IOException(MessageFormat.format( + JGitText.get().deleteFileFailed, f.getAbsolutePath())); } }