This is an automated email from the ASF dual-hosted git repository.

ebourg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 167effd  IO-692: Fix PathUtils.deleteFile() on symbolic links pointing 
to non-existing files
167effd is described below

commit 167effda26f5fabfef36a9878f0a0ce395855d10
Author: Emmanuel Bourg <ebo...@apache.org>
AuthorDate: Mon Dec 7 00:18:49 2020 +0100

    IO-692: Fix PathUtils.deleteFile() on symbolic links pointing to 
non-existing files
---
 src/changes/changes.xml                               |  4 ++++
 .../java/org/apache/commons/io/file/PathUtils.java    |  2 +-
 .../commons/io/file/PathUtilsDeleteFileTest.java      | 19 +++++++++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b439ba7..e5b9c76 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -74,6 +74,10 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix" due-to="Gary Gregory">
         FileUtils.forceDelete(File) actually forces deletion of read-only 
files as it did in version 2.6.
       </action>
+      <action issue="IO-692" dev="ebourg" type="fix" due-to="Matthew Rooney, 
Emmanuel Bourg">
+        PathUtils.deleteFile() no longer throws a NoSuchFileException when 
applied on a symbolic link pointing
+        to a file that doesn't exist.
+      </action>
       <!-- ADD -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add FileSystemProviders class.
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java 
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 2ccd609..c850d81 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -481,7 +481,7 @@ public final class PathUtils {
         }
         final PathCounters pathCounts = Counters.longPathCounters();
         final boolean exists = Files.exists(file, linkOptions);
-        final long size = exists ? Files.size(file) : 0;
+        final long size = exists && !Files.isSymbolicLink(file) ? 
Files.size(file) : 0;
         if (overrideReadOnly(deleteOptions) && exists) {
             setReadOnly(file, false, linkOptions);
         }
diff --git 
a/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java 
b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java
index fa2375d..bb30e6a 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java
@@ -20,8 +20,11 @@ package org.apache.commons.io.file;
 import static org.apache.commons.io.file.CounterAssertions.assertCounts;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeFalse;
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.LinkOption;
 import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -142,4 +145,20 @@ public class PathUtilsDeleteFileTest {
         // This will throw if not empty.
         Files.deleteIfExists(tempDir);
     }
+
+    @Test
+    public void testDeleteBrokenLink() throws IOException {
+        assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+        Path missingFile = tempDir.resolve("missing.txt");
+        Path brokenLink = tempDir.resolve("broken.txt");
+        Files.createSymbolicLink(brokenLink, missingFile);
+
+        assertTrue(Files.exists(brokenLink, LinkOption.NOFOLLOW_LINKS));
+        assertFalse(Files.exists(missingFile, LinkOption.NOFOLLOW_LINKS));
+
+        PathUtils.deleteFile(brokenLink);
+
+        assertFalse(Files.exists(brokenLink, LinkOption.NOFOLLOW_LINKS), 
"Symbolic link not removed");
+    }
 }

Reply via email to