You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/09/03 18:18:50 UTC

[commons-io] branch master updated (a8184d6 -> d42b928)

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

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


    from a8184d6  Fix usage of assertNotNull #269.
     new 3418d09  Javadoc.
     new a873e46  Minor cleanup.
     new d42b928  [IO-727] FilenameUtils directoryContains() should handle files with the same prefix #217.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  3 ++
 .../java/org/apache/commons/io/FileSystem.java     |  4 +--
 .../java/org/apache/commons/io/FilenameUtils.java  | 32 +++++++++++++++-------
 .../io/FileUtilsDirectoryContainsTestCase.java     | 13 +++++++++
 .../apache/commons/io/FilenameUtilsTestCase.java   | 17 ++++++++++++
 5 files changed, 57 insertions(+), 12 deletions(-)

[commons-io] 01/03: Javadoc.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3418d095f20a856f9d4de5bb1c976a8104acde45
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Sep 3 12:43:50 2021 -0400

    Javadoc.
---
 src/main/java/org/apache/commons/io/FilenameUtils.java | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/FilenameUtils.java b/src/main/java/org/apache/commons/io/FilenameUtils.java
index 7ef31cc..b577748 100644
--- a/src/main/java/org/apache/commons/io/FilenameUtils.java
+++ b/src/main/java/org/apache/commons/io/FilenameUtils.java
@@ -453,35 +453,43 @@ public class FilenameUtils {
      * The effect is equivalent to resultant directory after changing
      * directory to the first argument, followed by changing directory to
      * the second argument.
+     * </p>
      * <p>
      * The first argument is the base path, the second is the path to concatenate.
      * The returned path is always normalized via {@link #normalize(String)},
      * thus {@code ..} is handled.
+     * </p>
      * <p>
      * If {@code pathToAdd} is absolute (has an absolute prefix), then
      * it will be normalized and returned.
      * Otherwise, the paths will be joined, normalized and returned.
+     * </p>
      * <p>
      * The output will be the same on both Unix and Windows except
      * for the separator character.
+     * </p>
      * <pre>
      * /foo/      + bar        --&gt;  /foo/bar
      * /foo       + bar        --&gt;  /foo/bar
      * /foo       + /bar       --&gt;  /bar
      * /foo       + C:/bar     --&gt;  C:/bar
-     * /foo       + C:bar      --&gt;  C:bar (*)
+     * /foo       + C:bar      --&gt;  C:bar [1]
      * /foo/a/    + ../bar     --&gt;  /foo/bar
      * /foo/      + ../../bar  --&gt;  null
      * /foo/      + /bar       --&gt;  /bar
      * /foo/..    + /bar       --&gt;  /bar
      * /foo       + bar/c.txt  --&gt;  /foo/bar/c.txt
-     * /foo/c.txt + bar        --&gt;  /foo/c.txt/bar (!)
+     * /foo/c.txt + bar        --&gt;  /foo/c.txt/bar [2]
      * </pre>
-     * (*) Note that the Windows relative drive prefix is unreliable when
+     * <p>
+     * [1] Note that the Windows relative drive prefix is unreliable when
      * used with this method.
-     * (!) Note that the first parameter must be a path. If it ends with a name, then
+     * </p>
+     * <p>
+     * [2] Note that the first parameter must be a path. If it ends with a name, then
      * the name will be built into the concatenated path. If this might be a problem,
      * use {@link #getFullPath(String)} on the base path argument.
+     * </p>
      *
      * @param basePath  the base path to attach to, always treated as a path
      * @param fullFileNameToAdd  the fileName (or path) to attach to the base

[commons-io] 02/03: Minor cleanup.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit a873e46f90891bc7963133be8eac94107e5cfbb6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Sep 3 14:06:13 2021 -0400

    Minor cleanup.
---
 src/main/java/org/apache/commons/io/FileSystem.java | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/FileSystem.java b/src/main/java/org/apache/commons/io/FileSystem.java
index dadf719..a4c386c 100644
--- a/src/main/java/org/apache/commons/io/FileSystem.java
+++ b/src/main/java/org/apache/commons/io/FileSystem.java
@@ -134,10 +134,10 @@ public enum FileSystem {
             return LINUX;
         }
         if (IS_OS_MAC) {
-            return FileSystem.MAC_OSX;
+            return MAC_OSX;
         }
         if (IS_OS_WINDOWS) {
-            return FileSystem.WINDOWS;
+            return WINDOWS;
         }
         return GENERIC;
     }

[commons-io] 03/03: [IO-727] FilenameUtils directoryContains() should handle files with the same prefix #217.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit d42b9286eb7a534f1b2b6243858ff7ef6fcac14c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Sep 3 14:18:47 2021 -0400

    [IO-727] FilenameUtils directoryContains() should handle files with the
    same prefix #217.
    
    Apply a modified PR #217 from trungPa to resolve conflicts.
---
 src/changes/changes.xml                                 |  3 +++
 src/main/java/org/apache/commons/io/FilenameUtils.java  | 16 ++++++++++------
 .../commons/io/FileUtilsDirectoryContainsTestCase.java  | 13 +++++++++++++
 .../org/apache/commons/io/FilenameUtilsTestCase.java    | 17 +++++++++++++++++
 4 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 850dfdd..0ed1e69 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,6 +59,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix" due-to="Davide Angelocola">
         Fix usage of assertNotNull #269.
       </action>
+      <action issue="IO-727" dev="ggregory" type="fix" due-to="trungPa, Gary Gregory">
+        FilenameUtils directoryContains() should handle files with the same prefix #217.
+      </action>
       <!-- ADD -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add BrokenReader.INSTANCE.
diff --git a/src/main/java/org/apache/commons/io/FilenameUtils.java b/src/main/java/org/apache/commons/io/FilenameUtils.java
index b577748..6edb92c 100644
--- a/src/main/java/org/apache/commons/io/FilenameUtils.java
+++ b/src/main/java/org/apache/commons/io/FilenameUtils.java
@@ -23,7 +23,6 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Deque;
 import java.util.List;
-import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -539,9 +538,7 @@ public class FilenameUtils {
      * @see FileUtils#directoryContains(File, File)
      */
     public static boolean directoryContains(final String canonicalParent, final String canonicalChild) {
-        Objects.requireNonNull(canonicalParent, "canonicalParent");
-
-        if (canonicalChild == null) {
+        if (isEmpty(canonicalParent) || isEmpty(canonicalChild)) {
             return false;
         }
 
@@ -549,7 +546,14 @@ public class FilenameUtils {
             return false;
         }
 
-        return IOCase.SYSTEM.checkStartsWith(canonicalChild, canonicalParent);
+        final char separator = canonicalParent.charAt(0) == UNIX_SEPARATOR ? UNIX_SEPARATOR : WINDOWS_SEPARATOR;
+        final String parentWithEndSeparator = canonicalParent.charAt(canonicalParent.length() - 1) == separator ? canonicalParent : canonicalParent + separator;
+
+        return IOCase.SYSTEM.checkStartsWith(canonicalChild, parentWithEndSeparator);
+    }
+
+    private static boolean isEmpty(final String string) {
+        return string == null || string.isEmpty();
     }
 
     /**
@@ -1235,7 +1239,7 @@ public class FilenameUtils {
         }
         requireNonNullChars(fileName);
 
-        if (extension == null || extension.isEmpty()) {
+        if (isEmpty(extension)) {
             return indexOfExtension(fileName) == NOT_FOUND;
         }
         final String fileExt = getExtension(fileName);
diff --git a/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
index 6ac3f51..89a320e 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsDirectoryContainsTestCase.java
@@ -140,6 +140,19 @@ public class FileUtilsDirectoryContainsTestCase {
     }
 
     @Test
+    public void testFileHavingSamePrefixBug() throws IOException {
+        final File foo = new File(top, "foo");
+        final File foobar = new File(top, "foobar");
+        final File fooTxt = new File(top, "foo.txt");
+        foo.mkdir();
+        foobar.mkdir();
+        FileUtils.touch(fooTxt);
+
+        assertFalse(FileUtils.directoryContains(foo, foobar));
+        assertFalse(FileUtils.directoryContains(foo, fooTxt));
+    }
+
+    @Test
     public void testFileDoesNotExist() throws IOException {
         assertFalse(FileUtils.directoryContains(top, null));
         final File file = new File("DOESNOTEXIST");
diff --git a/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java b/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
index c0790b2..fc39d66 100644
--- a/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java
@@ -1184,4 +1184,21 @@ public class FilenameUtilsTestCase {
         assertFalse(FilenameUtils.isExtension("a.b\\file.txt", new ArrayList<>(Arrays.asList("TXT"))));
         assertFalse(FilenameUtils.isExtension("a.b\\file.txt", new ArrayList<>(Arrays.asList("TXT", "RTF"))));
     }
+
+    //-----------------------------------------------------------------------
+    @Test
+    public void testDirectoryContains() throws IOException {
+        assertTrue(FilenameUtils.directoryContains("/foo", "/foo/bar"));
+        assertTrue(FilenameUtils.directoryContains("/foo/", "/foo/bar"));
+        assertTrue(FilenameUtils.directoryContains("C:\\foo", "C:\\foo\\bar"));
+        assertTrue(FilenameUtils.directoryContains("C:\\foo\\", "C:\\foo\\bar"));
+
+        assertFalse(FilenameUtils.directoryContains("/foo", "/foo"));
+        assertFalse(FilenameUtils.directoryContains("/foo", "/foobar"));
+        assertFalse(FilenameUtils.directoryContains("C:\\foo", "C:\\foobar"));
+        assertFalse(FilenameUtils.directoryContains("/foo", null));
+        assertFalse(FilenameUtils.directoryContains("", ""));
+        assertFalse(FilenameUtils.directoryContains("", "/foo"));
+        assertFalse(FilenameUtils.directoryContains("/foo", ""));
+    }
 }