From f6ae46f69b453b7688459a46fc6380a54e18cd11 Mon Sep 17 00:00:00 2001 From: Jerome Hollon Date: Thu, 10 Oct 2019 15:47:01 -0400 Subject: [PATCH 1/3] IO-629: Providing more meaningful exceptions on file delete --- .../java/org/apache/commons/io/FileUtils.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index def8e93a04b..78fe8980bb9 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -34,6 +34,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; @@ -1335,13 +1336,14 @@ public static void forceDelete(final File file) throws IOException { deleteDirectory(file); } else { final boolean filePresent = file.exists(); - if (!file.delete()) { - if (!filePresent) { - throw new FileNotFoundException("File does not exist: " + file); - } - final String message = - "Unable to delete file: " + file; - throw new IOException(message); + try { + Files.delete(file.toPath()); + } catch (NoSuchFileException e) { + // Throw FileNotFoundException for backwards compatibility. + throw new FileNotFoundException("File does not exist: " + file); + } catch (IOException e) { + final String message = "Unable to delete file: " + file; + throw new IOException(message, e); } } } From 8f85432518d11da5b3f0ed84ae529307ce2554e3 Mon Sep 17 00:00:00 2001 From: Jerome Hollon Date: Thu, 10 Oct 2019 15:47:01 -0400 Subject: [PATCH 2/3] IO-629: Providing more meaningful exceptions on file delete --- .../java/org/apache/commons/io/FileUtils.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index c3eaeb570c5..c70c1b438e6 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -34,6 +34,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; @@ -1335,13 +1336,14 @@ public static void forceDelete(final File file) throws IOException { deleteDirectory(file); } else { final boolean filePresent = file.exists(); - if (!file.delete()) { - if (!filePresent) { - throw new FileNotFoundException("File does not exist: " + file); - } - final String message = - "Unable to delete file: " + file; - throw new IOException(message); + try { + Files.delete(file.toPath()); + } catch (NoSuchFileException e) { + // Throw FileNotFoundException for backwards compatibility. + throw new FileNotFoundException("File does not exist: " + file); + } catch (IOException e) { + final String message = "Unable to delete file: " + file; + throw new IOException(message, e); } } } From 49007274949db73984c21724bcae7a67193a376c Mon Sep 17 00:00:00 2001 From: Jerome Hollon Date: Mon, 21 Oct 2019 11:42:33 -0400 Subject: [PATCH 3/3] IS-629: Refactoring FileUtils#forceDelete to use PathUtils delete api --- .../java/org/apache/commons/io/FileUtils.java | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index c70c1b438e6..4e460217ad0 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -34,7 +34,6 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; @@ -46,6 +45,8 @@ import java.util.zip.CheckedInputStream; import java.util.zip.Checksum; +import org.apache.commons.io.file.Counters; +import org.apache.commons.io.file.PathUtils; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.FalseFileFilter; import org.apache.commons.io.filefilter.FileFilterUtils; @@ -1332,19 +1333,17 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo * @throws IOException in case deletion is unsuccessful */ public static void forceDelete(final File file) throws IOException { - if (file.isDirectory()) { - deleteDirectory(file); - } else { - final boolean filePresent = file.exists(); - try { - Files.delete(file.toPath()); - } catch (NoSuchFileException e) { - // Throw FileNotFoundException for backwards compatibility. - throw new FileNotFoundException("File does not exist: " + file); - } catch (IOException e) { - final String message = "Unable to delete file: " + file; - throw new IOException(message, e); - } + Counters.PathCounters deleteCounter; + try { + deleteCounter = PathUtils.delete(file.toPath()); + } catch (IOException e) { + final String message = "Unable to delete file: " + file; + throw new IOException(message, e); + } + + if(deleteCounter.getFileCounter().get() < 1 && deleteCounter.getDirectoryCounter().get() < 1) { + // didn't find a file to delete. + throw new FileNotFoundException("File does not exist: " + file); } }