Skip to content

Commit

Permalink
Merge pull request #851 from attilapuskas/feature/ClasspathElementDir…
Browse files Browse the repository at this point in the history
…-review-adjustments

ClasspathElementDir review adjustments
  • Loading branch information
lukehutch committed Apr 19, 2024
2 parents 9ea7871 + 3d2ebc2 commit 0bf6d6d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main/java/io/github/classgraph/ClasspathElementDir.java
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -409,11 +410,13 @@ private void scanPathRecursively(final Path path, final LogNode log) {
// Only scan files in directory if directory is not only an ancestor of an accepted path
if (parentMatchStatus != ScanSpecPathMatch.ANCESTOR_OF_ACCEPTED_PATH) {
// Do preorder traversal (files in dir, then subdirs), to reduce filesystem cache misses
for (final Path subPath : new ArrayList<>(pathsInDir)) {
final Iterator<Path> pathsIterator = pathsInDir.iterator();
while (pathsIterator.hasNext()) {
final Path subPath = pathsIterator.next();
// Process files in dir before recursing
BasicFileAttributes fileAttributes = getFileAttributes.get(subPath);
if (fileAttributes.isRegularFile()) {
pathsInDir.remove(subPath);
pathsIterator.remove();
final Path subPathRelative = classpathEltPath.relativize(subPath);
final String subPathRelativeStr = FastPathResolver.resolve(subPathRelative.toString());
// If this is a modular jar, ignore all classfiles other than "module-info.class" in the
Expand Down Expand Up @@ -452,11 +455,13 @@ private void scanPathRecursively(final Path path, final LogNode log) {
}
} else if (scanSpec.enableClassInfo && dirRelativePathStr.equals("/")) {
// Always check for module descriptor in package root, even if package root isn't in accept
for (final Path subPath : new ArrayList<>(pathsInDir)) {
final Iterator<Path> pathsIterator = pathsInDir.iterator();
while (pathsIterator.hasNext()) {
final Path subPath = pathsIterator.next();
if (subPath.getFileName().toString().equals("module-info.class")) {
BasicFileAttributes fileAttributes = getFileAttributes.get(subPath);
if (fileAttributes.isRegularFile()) {
pathsInDir.remove(subPath);
pathsIterator.remove();
final Resource resource = newResource(subPath, fileAttributes);
addAcceptedResource(resource, parentMatchStatus, /* isClassfileOnly = */ true, subLog);
try {
Expand All @@ -472,7 +477,7 @@ private void scanPathRecursively(final Path path, final LogNode log) {
// Recurse into subdirectories
for (final Path subPath : pathsInDir) {
try {
if (FileUtils.isDir(subPath)) {
if (getFileAttributes.get(subPath).isDirectory()) {
scanPathRecursively(subPath, subLog);
}
} catch (final SecurityException e) {
Expand Down

0 comments on commit 0bf6d6d

Please sign in to comment.