Browse Source

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 <thomas.wolf@paranor.ch>
stable-5.4
Thomas Wolf 6 years ago
parent
commit
965aacc7c9
  1. 40
      org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java

40
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 static java.nio.charset.StandardCharsets.UTF_8;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.CopyOption; import java.nio.file.CopyOption;
@ -193,31 +194,50 @@ public class FileUtils {
if ((options & EMPTY_DIRECTORIES_ONLY) != 0) { if ((options & EMPTY_DIRECTORIES_ONLY) != 0) {
if (f.isDirectory()) { if (f.isDirectory()) {
delete = true; delete = true;
} else { } else if ((options & IGNORE_ERRORS) == 0) {
if ((options & IGNORE_ERRORS) == 0)
throw new IOException(MessageFormat.format( throw new IOException(MessageFormat.format(
JGitText.get().deleteFileFailed, JGitText.get().deleteFileFailed, f.getAbsolutePath()));
f.getAbsolutePath()));
} }
} else { } else {
delete = true; delete = true;
} }
if (delete && !f.delete()) { if (delete) {
if ((options & RETRY) != 0 && fs.exists(f)) { 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++) { for (int i = 1; i < 10; i++) {
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException e) { } catch (InterruptedException ex) {
// ignore // ignore
} }
if (f.delete()) try {
Files.deleteIfExists(p);
return; return;
} catch (IOException e) {
t = e;
}
} }
} }
if ((options & IGNORE_ERRORS) == 0) if ((options & IGNORE_ERRORS) == 0) {
throw new IOException(MessageFormat.format( throw new IOException(MessageFormat.format(
JGitText.get().deleteFileFailed, f.getAbsolutePath())); JGitText.get().deleteFileFailed, f.getAbsolutePath()),
t);
}
} }
} }

Loading…
Cancel
Save