|
| 1 | +/* |
| 2 | + * Copyright 2026 Code Intelligence GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.code_intelligence.jazzer.mutation.support; |
| 18 | + |
| 19 | +import static java.util.Collections.EMPTY_LIST; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.FileSystems; |
| 23 | +import java.nio.file.FileVisitResult; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.PathMatcher; |
| 27 | +import java.nio.file.Paths; |
| 28 | +import java.nio.file.SimpleFileVisitor; |
| 29 | +import java.nio.file.attribute.BasicFileAttributes; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +public class GlobUtils { |
| 35 | + protected static List<Path> collectPathsForGlob(Path baseDir, String glob) { |
| 36 | + if (ON_WINDOWS) { |
| 37 | + glob = glob.replace("\\\\", "/"); |
| 38 | + } |
| 39 | + int firstGlobChar = indexOfFirstGlobChar(glob); |
| 40 | + if (firstGlobChar == -1) { |
| 41 | + Path target = baseDir.resolve(unescapeGlobChars(glob)).toAbsolutePath().normalize(); |
| 42 | + return Files.isRegularFile(target) ? Arrays.asList(target) : EMPTY_LIST; |
| 43 | + } |
| 44 | + |
| 45 | + String prefix = glob.substring(0, firstGlobChar); |
| 46 | + int lastSeparator = |
| 47 | + ON_WINDOWS |
| 48 | + ? Math.max(prefix.lastIndexOf('/'), prefix.lastIndexOf("\\\\")) |
| 49 | + : prefix.lastIndexOf('/'); |
| 50 | + |
| 51 | + // The 'start' path is always absolute |
| 52 | + Path start; |
| 53 | + String remainingPattern; |
| 54 | + if (lastSeparator == -1) { |
| 55 | + start = baseDir.toAbsolutePath().normalize(); |
| 56 | + remainingPattern = glob; |
| 57 | + } else { |
| 58 | + String basePrefix = prefix.substring(0, lastSeparator); |
| 59 | + start = baseDir.resolve(unescapeGlobChars(basePrefix)).toAbsolutePath().normalize(); |
| 60 | + remainingPattern = glob.substring(lastSeparator + 1); |
| 61 | + } |
| 62 | + if (!Files.exists(start)) { |
| 63 | + return EMPTY_LIST; |
| 64 | + } |
| 65 | + |
| 66 | + // The matcher is always relative to start path |
| 67 | + PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + remainingPattern); |
| 68 | + |
| 69 | + List<Path> matches = new ArrayList<>(); |
| 70 | + try { |
| 71 | + Files.walkFileTree( |
| 72 | + start, |
| 73 | + new SimpleFileVisitor<Path>() { |
| 74 | + @Override |
| 75 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 76 | + if (!Files.isRegularFile(file)) { |
| 77 | + return FileVisitResult.CONTINUE; |
| 78 | + } |
| 79 | + Path relativePath = start.relativize(file); |
| 80 | + if (matcher.matches(relativePath)) { |
| 81 | + matches.add(file); |
| 82 | + } |
| 83 | + return FileVisitResult.CONTINUE; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public FileVisitResult visitFileFailed(Path file, IOException exc) { |
| 88 | + return FileVisitResult.CONTINUE; |
| 89 | + } |
| 90 | + }); |
| 91 | + } catch (IOException ignored) { |
| 92 | + // Best effort - return what we found so far |
| 93 | + } |
| 94 | + return matches; |
| 95 | + } |
| 96 | + |
| 97 | + private static final String SPECIAL_CHARS = "*{}[]-?\\"; |
| 98 | + |
| 99 | + private static boolean isSpecialChar(char c) { |
| 100 | + return SPECIAL_CHARS.indexOf(c) != -1; |
| 101 | + } |
| 102 | + |
| 103 | + protected static Path unescapeGlobChars(String glob) { |
| 104 | + StringBuilder sb = new StringBuilder(); |
| 105 | + char[] chars = glob.toCharArray(); |
| 106 | + boolean escaped = false; |
| 107 | + |
| 108 | + for (char c : chars) { |
| 109 | + if (escaped) { |
| 110 | + if (!isSpecialChar(c)) { |
| 111 | + sb.append('\\'); |
| 112 | + } |
| 113 | + sb.append(c); |
| 114 | + escaped = false; |
| 115 | + } else if (c == '\\') { |
| 116 | + escaped = true; |
| 117 | + } else { |
| 118 | + sb.append(c); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return Paths.get(sb.toString()); |
| 123 | + } |
| 124 | + |
| 125 | + private static final String GLOB_CHARS = "*?[{"; |
| 126 | + |
| 127 | + private static boolean isGlobChar(char c) { |
| 128 | + return GLOB_CHARS.indexOf(c) != -1; |
| 129 | + } |
| 130 | + |
| 131 | + protected static final boolean ON_WINDOWS = FileSystems.getDefault().getSeparator().equals("\\"); |
| 132 | + |
| 133 | + private static int indexOfFirstGlobChar(String glob) { |
| 134 | + char[] chars = glob.toCharArray(); |
| 135 | + boolean escaped = false; |
| 136 | + for (int i = 0; i < chars.length; i++) { |
| 137 | + char c = chars[i]; |
| 138 | + if (escaped) { |
| 139 | + escaped = false; |
| 140 | + continue; |
| 141 | + } else if (c == '\\') { |
| 142 | + escaped = true; |
| 143 | + } |
| 144 | + if (isGlobChar(c)) { |
| 145 | + return i; |
| 146 | + } |
| 147 | + } |
| 148 | + return -1; |
| 149 | + } |
| 150 | +} |
0 commit comments