From 965aacc7c9d4ddf7bf4d04079949b369c8dd7212 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Wed, 3 Apr 2019 17:32:53 +0200 Subject: [PATCH] Attach deletion failure reason in FileUtils.delete() Use Files.delete() instead of File.delete(), and if there is an exception thrown propagate it unless errors are to be ignored so that the actual deletion failure cause is available to the caller (and will be logged). Change-Id: I5fdb5a4052942437ab365289ad4bb1b563c29456 Signed-off-by: Thomas Wolf --- .../src/org/eclipse/jgit/util/FileUtils.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 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 97f480dd3..530bd9fb1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java @@ -48,6 +48,7 @@ package org.eclipse.jgit.util; import static java.nio.charset.StandardCharsets.UTF_8; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.CopyOption; @@ -193,31 +194,50 @@ public class FileUtils { if ((options & EMPTY_DIRECTORIES_ONLY) != 0) { if (f.isDirectory()) { delete = true; - } else { - if ((options & IGNORE_ERRORS) == 0) - throw new IOException(MessageFormat.format( - JGitText.get().deleteFileFailed, - f.getAbsolutePath())); + } else if ((options & IGNORE_ERRORS) == 0) { + throw new IOException(MessageFormat.format( + JGitText.get().deleteFileFailed, f.getAbsolutePath())); } } else { delete = true; } - if (delete && !f.delete()) { - if ((options & RETRY) != 0 && fs.exists(f)) { + if (delete) { + Throwable t = null; + Path p = f.toPath(); + try { + Files.delete(p); + return; + } catch (FileNotFoundException e) { + if ((options & (SKIP_MISSING | IGNORE_ERRORS)) == 0) { + throw new IOException(MessageFormat.format( + JGitText.get().deleteFileFailed, + f.getAbsolutePath()), e); + } + return; + } catch (IOException e) { + t = e; + } + if ((options & RETRY) != 0) { for (int i = 1; i < 10; i++) { try { Thread.sleep(100); - } catch (InterruptedException e) { + } catch (InterruptedException ex) { // ignore } - if (f.delete()) + try { + Files.deleteIfExists(p); return; + } catch (IOException e) { + t = e; + } } } - if ((options & IGNORE_ERRORS) == 0) + if ((options & IGNORE_ERRORS) == 0) { throw new IOException(MessageFormat.format( - JGitText.get().deleteFileFailed, f.getAbsolutePath())); + JGitText.get().deleteFileFailed, f.getAbsolutePath()), + t); + } } }