Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only process a path as a JAR if it exists and is a regular file #338

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,34 @@ public static void consumeAsPath(URL url, Consumer<Path> consumer) {
public static <R> R processAsPath(URL url, Function<Path, R> function) {
if (JAR.equals(url.getProtocol())) {
final String file = url.getFile();
final int exclam = file.indexOf("!/");
try {
URL fileUrl;
String subPath;
if (exclam == -1) {
// assume the first element is a JAR file, not a plain file, since it was a `jar:` URL
fileUrl = new URL(file);
subPath = "/";
} else {
fileUrl = new URL(file.substring(0, exclam));
subPath = file.substring(exclam + 1);
}
if (!fileUrl.getProtocol().equals("file")) {
throw new IllegalArgumentException("Sub-URL of JAR URL is expected to have a scheme of `file`");
}
int exclam = file.indexOf("!/");
for (;;) {
try {
URL fileUrl;
String subPath;
if (exclam == -1) {
// assume the first element is a JAR file, not a plain file, since it was a `jar:` URL
fileUrl = new URL(file);
subPath = "/";
} else {
fileUrl = new URL(file.substring(0, exclam));
subPath = file.substring(exclam + 1);
}
if (!fileUrl.getProtocol().equals("file")) {
throw new IllegalArgumentException("Sub-URL of JAR URL is expected to have a scheme of `file`");
}

return processAsJarPath(toLocalPath(fileUrl), subPath, function);
} catch (MalformedURLException e) {
throw new RuntimeException("Failed to create a URL for '" + file.substring(0, exclam) + "'", e);
Path proposedPath = toLocalPath(fileUrl);
if (Files.isRegularFile(proposedPath)) {
return processAsJarPath(proposedPath, subPath, function);
}
if (exclam == -1) {
throw new IllegalArgumentException("File not found: " + fileUrl);
}
exclam = file.indexOf("!/", exclam + 1);
} catch (MalformedURLException e) {
throw new RuntimeException("Failed to create a URL for '" + file.substring(0, exclam) + "'", e);
}
}
}

Expand Down