Skip to content

Commit

Permalink
Backport logging-related AntClassLoader fixes to our fork (#5650)
Browse files Browse the repository at this point in the history
- commit 189ad7e59 bz-62764 Add a debug log message when we skip invalid path elements from the `AntClassLoader`'s classpath
- commit 008f1c8be Embrace `StringUtils#getStackTrace`
- commit 13c01b296 Improve skipping of non-zips
  • Loading branch information
basil committed Aug 5, 2021
1 parent 5f89d4d commit 6d80eac
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions core/src/main/java/jenkins/util/AntClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.apache.tools.ant.util.JavaEnvUtils;
import org.apache.tools.ant.util.LoaderUtils;
import org.apache.tools.ant.util.ReflectUtil;
import org.apache.tools.ant.util.StringUtils;
import org.apache.tools.ant.util.VectorSet;
import org.apache.tools.zip.ZipLong;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -403,6 +405,8 @@ public void setClassPath(final Path classpath) {
} catch (final BuildException e) {
// ignore path elements which are invalid
// relative to the project
log("Ignoring path element " + pathElement + " from " +
"classpath due to exception " + e, Project.MSG_DEBUG);
}
}
}
Expand Down Expand Up @@ -441,6 +445,8 @@ public void setParentFirst(final boolean parentFirst) {
protected void log(final String message, final int priority) {
if (project != null) {
project.log(message, priority);
} else if (priority < Project.MSG_INFO) {
System.err.println(message);
}
}

Expand Down Expand Up @@ -1043,6 +1049,12 @@ protected URL getResourceURL(final File file, final String resourceName) {
} else {
if (jarFile == null) {
if (file.exists()) {
if (!isZip(file)) {
final String msg = "CLASSPATH element " + file
+ " is not a JAR.";
log(msg, Project.MSG_WARN);
return null;
}
jarFile = newJarFile(file);
jarFiles.put(file, jarFile);
} else {
Expand All @@ -1063,8 +1075,7 @@ protected URL getResourceURL(final File file, final String resourceName) {
} catch (final Exception e) {
final String msg = "Unable to obtain resource from " + file + ": ";
log(msg + e, Project.MSG_WARN);
System.err.println(msg);
e.printStackTrace();
log(StringUtils.getStackTrace(e), Project.MSG_WARN);
}
return null;
}
Expand Down Expand Up @@ -1591,6 +1602,37 @@ public static AntClassLoader newAntClassLoader(final ClassLoader parent,
return new AntClassLoader(parent, project, path, parentFirst);
}

private static final ZipLong EOCD_SIG = new ZipLong(0X06054B50L);
private static final ZipLong SINGLE_SEGMENT_SPLIT_MARKER =
new ZipLong(0X30304B50L);

private static boolean isZip(final File file) throws IOException {
final byte[] sig = new byte[4];
if (readFully(file, sig)) {
final ZipLong start = new ZipLong(sig);
return ZipLong.LFH_SIG.equals(start) // normal file
|| EOCD_SIG.equals(start) // empty zip
|| ZipLong.DD_SIG.equals(start) // split zip
|| SINGLE_SEGMENT_SPLIT_MARKER.equals(start);
}
return false;
}

private static boolean readFully(final File f, final byte[] b) throws IOException {
try (InputStream fis = Files.newInputStream(f.toPath())) {
final int len = b.length;
int count = 0, x = 0;
while (count != len) {
x = fis.read(b, count, len - count);
if (x == -1) {
break;
}
count += x;
}
return count == len;
}
}

/**
*
* @param file The file representing the jar
Expand Down

0 comments on commit 6d80eac

Please sign in to comment.