Skip to content
Open
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ limitations under the License.
</parent>

<artifactId>plexus-utils</artifactId>
<version>4.2.1-SNAPSHOT</version>
<version>4.3.0-SNAPSHOT</version>

<name>Plexus Common Utilities</name>
<description>A collection of various utility classes to ease working with strings, files, command lines and
Expand Down
66 changes: 64 additions & 2 deletions src/main/java/org/codehaus/plexus/util/AbstractScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ protected static boolean match(String pattern, String str, boolean isCaseSensiti
*
* <p>When a pattern ends with a '/' or '\', "**" is appended.</p>
*
* <p>Absolute patterns below the base directory are treated as relative to it; absolute patterns outside of the
* base directory are ignored. Dot segments in the base directory and in the literal prefix of an absolute pattern
* are normalized without resolving symbolic links. Regex- and ant-prefixed patterns are unchanged.
* Support for absolute patterns has been added in 4.3.0.</p>
*
* @param includes A list of include patterns. May be <code>null</code>, indicating that all files should be
* included. If a non-<code>null</code> list is given, all elements must be non-<code>null</code>.
*/
Expand All @@ -263,6 +268,11 @@ public void setIncludes(String[] includes) {
*
* <p>When a pattern ends with a '/' or '\', "**" is appended.</p>
*
* <p>Absolute patterns below the base directory are treated as relative to it; absolute patterns outside of the
* base directory are ignored. Dot segments in the base directory and in the literal prefix of an absolute pattern
* are normalized without resolving symbolic links. Regex- and ant-prefixed patterns are unchanged.
* Support for absolute patterns has been added in 4.3.0.</p>
*
* @param excludes A list of exclude patterns. May be <code>null</code>, indicating that no files should be
* excluded. If a non-<code>null</code> list is given, all elements must be non-<code>null</code>.
*/
Expand Down Expand Up @@ -385,8 +395,60 @@ protected void setupDefaultFilters() {
}

protected void setupMatchPatterns() {
includesPatterns = MatchPatterns.from(includes);
excludesPatterns = MatchPatterns.from(excludes);
includesPatterns = MatchPatterns.from(relativizePatterns(includes));
excludesPatterns = MatchPatterns.from(relativizePatterns(excludes));
}

/**
* Converts absolute patterns below the base directory to relative ones and drops absolute patterns which are
* not below the base directory. Relative and regex/ant-prefixed patterns are kept as is.
*/
private String[] relativizePatterns(String[] patterns) {
File basedir = getBasedir();
if (basedir == null) {
return patterns;
}
String root = basedir.getAbsoluteFile().toPath().normalize().toString();
if (!root.endsWith(File.separator)) {
root += File.separator;
}
List<String> result = new ArrayList<>(patterns.length);
for (String pattern : patterns) {
if (pattern.startsWith(SelectorUtils.REGEX_HANDLER_PREFIX)
|| pattern.startsWith(SelectorUtils.ANT_HANDLER_PREFIX)
|| !new File(pattern).isAbsolute()) {
result.add(pattern);
} else {
String normalized = normalizeAbsolutePattern(pattern);
if (normalized.length() > root.length()
&& normalized.regionMatches(!isCaseSensitive, 0, root, 0, root.length())) {
result.add(normalized.substring(root.length()));
}
}
}
return result.toArray(new String[0]);
}

private String normalizeAbsolutePattern(String pattern) {
int wildcard = pattern.length();
int star = pattern.indexOf('*');
int question = pattern.indexOf('?');
if (star >= 0) {
wildcard = Math.min(wildcard, star);
}
if (question >= 0) {
wildcard = Math.min(wildcard, question);
}
if (wildcard == pattern.length()) {
return new File(pattern).toPath().normalize().toString();
}
int prefixEnd = pattern.lastIndexOf(File.separatorChar, wildcard) + 1;
String prefix =
new File(pattern.substring(0, prefixEnd)).toPath().normalize().toString();
if (!prefix.endsWith(File.separator)) {
prefix += File.separator;
}
return prefix + pattern.substring(prefixEnd);
}

@Override
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/codehaus/plexus/util/DirectoryScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,68 @@ void crossPlatformExcludesString() throws Exception {
assertEquals(0, files.length);
}

@Test
void absolutePatterns() throws Exception {
File basedir = new File(getTestResourcesDir() + File.separator + "directory-scanner").getCanonicalFile();
String outside = new File(basedir.getParentFile(), "other").getAbsolutePath();

DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(basedir);
ds.setIncludes(new String[] {basedir.getAbsolutePath() + File.separator + "foo" + File.separator, outside});
ds.scan();
assertEquals(1, ds.getIncludedFiles().length);

ds = new DirectoryScanner();
ds.setBasedir(basedir);
ds.setIncludes(new String[] {"**"});
ds.setExcludes(new String[] {basedir.getAbsolutePath() + File.separator + "foo" + File.separator, outside});
ds.scan();
assertEquals(0, ds.getIncludedFiles().length);
}

@Test
void absolutePatternsWithDotSegments() throws Exception {
File basedir = new File(getTestResourcesDir(), "directory-scanner").getCanonicalFile();
File dottedBasedir = new File(basedir, "foo/..");
String pattern = new File(basedir, "./foo/../foo").getAbsolutePath() + File.separator + "b?r/**";

DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(dottedBasedir);
ds.setIncludes(new String[] {basedir + File.separator + "foo" + File.separator + "**"});
ds.scan();
assertEquals(1, ds.getIncludedFiles().length);

ds = new DirectoryScanner();
ds.setBasedir(new File(basedir, "."));
ds.setExcludes(new String[] {pattern});
ds.scan();
assertEquals(0, ds.getIncludedFiles().length);

ds = new DirectoryScanner();
ds.setBasedir(new File(basedir, "."));
ds.setIncludes(new String[] {new File(basedir, "foo/../foo/bar/munchy.txt").getAbsolutePath()});
ds.scan();
assertEquals(1, ds.getIncludedFiles().length);
}

@Test
void absolutePatternNormalizationPreservesWildcardsAndPrefixes() throws Exception {
File basedir = new File(getTestResourcesDir(), "directory-scanner").getCanonicalFile();
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(new File(basedir, "."));
ds.setIncludes(new String[] {basedir + File.separator + "*" + File.separator + ".." + File.separator + "**"});
ds.scan();
assertEquals(0, ds.getIncludedFiles().length);

for (String pattern : new String[] {"%ant[foo/**]", "%regex[foo.*]"}) {
ds = new DirectoryScanner();
ds.setBasedir(new File(basedir, "."));
ds.setIncludes(new String[] {pattern});
ds.scan();
assertEquals(1, ds.getIncludedFiles().length);
}
}

private String getTestResourcesDir() throws URISyntaxException {
ClassLoader cloader = Thread.currentThread().getContextClassLoader();
URL resource = cloader.getResource("test.txt");
Expand Down
Loading