Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions src/main/java/org/apache/commons/io/FilenameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,24 +529,48 @@ public static String concat(final String basePath, final String fullFilenameToAd
* @since 2.2
* @see FileUtils#directoryContains(File, File)
*/
public static boolean directoryContains(final String canonicalParent, final String canonicalChild)
throws IOException {
public static boolean directoryContains(final String canonicalParent, final String canonicalChild)
throws IOException {

// Fail fast against NullPointerException
if (canonicalParent == null) {
throw new IllegalArgumentException("Directory must not be null");
}
// Fail fast against NullPointerException
if (canonicalParent == null) {
throw new IllegalArgumentException("Directory must not be null");
}

if (canonicalChild == null) {
return false;
}
if (canonicalChild == null) {
return false;
}

if (IOCase.SYSTEM.checkEquals(canonicalParent, canonicalChild)) {
return false;
}
if (IOCase.SYSTEM.checkEquals(canonicalParent, canonicalChild)) {
return false;
}

return IOCase.SYSTEM.checkStartsWith(canonicalChild, canonicalParent);
}
return FilenameUtils.checkStartsWithDir(canonicalParent, canonicalChild);

}

/**
* Checks if the parent directory contains the child directory/file
*
* @param parent the file path to consider as parent
* @param child the file path to consider as child
* @return true if the parent file path contains the child file path
*/
public static boolean checkStartsWithDir(final String parent, final String child){
final int length = parent.length();

if(length > child.length()){
return false;
}

final int splitIndex = child.indexOf('/', length);

if(splitIndex == -1){
return false;
}

return parent.equals(child.substring(0,splitIndex));
}

//-----------------------------------------------------------------------
/**
Expand Down Expand Up @@ -715,7 +739,7 @@ public static int indexOfLastSeparator(final String filename) {
* <p>
* The output will be the same irrespective of the machine that the code is running on.
* </p>
*
*
* @param filename
* the filename to find the last extension separator in, null returns -1
* @return the index of the last extension separator character, or -1 if there is no such character
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -30,7 +30,7 @@

/**
* This class ensure the correctness of {@link FileUtils#directoryContains(File,File)}.
*
*
* @see FileUtils#directoryContains(File, File)
* @since 2.2
* @version $Id$
Expand All @@ -40,10 +40,10 @@ public class FileUtilsDirectoryContainsTestCase extends FileBasedTestCase {
private File directory1;
private File directory2;
private File directory3;
private File file1;
private File file1ByRelativeDirectory2;
private File file2;
private File file2ByRelativeDirectory1;
private File file1;
private File file1ByRelativeDirectory2;
private File file2;
private File file2ByRelativeDirectory1;
private File file3;
final File top = getTestDirectory();

Expand Down Expand Up @@ -178,4 +178,24 @@ public void testUnrealizedContainment() throws IOException {
// expected
}
}

@Test
public void testSimilarStartingCanonicalPath() throws Exception{
final File top2 = getTestDirectory();
top2.mkdirs();

File foo = new File(top2, "foo");
File foo2 = new File(top2, "foo2");

foo.mkdir();
foo2.mkdir();

File a = new File(foo, "a.txt");
File b = new File(foo2, "b.txt");

FileUtils.touch(a);
FileUtils.touch(b);

assertFalse(FileUtils.directoryContains(foo, b));
}
}