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>
This commit is contained in:
Thomas Wolf 2019-04-03 17:32:53 +02:00
parent 440296873b
commit 965aacc7c9
1 changed files with 31 additions and 11 deletions

View File

@ -48,6 +48,7 @@
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 static void delete(File f, int options) throws IOException {
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);
}
}
}