Skip to content

Commit

Permalink
Fix UseScanManager.isValidArchive() and UseReportConverter
Browse files Browse the repository at this point in the history
In UseScanManager.isValidArchive() the jar/zip-file was closed too early
making it impossible to read from it.
  • Loading branch information
HannesWell committed Mar 16, 2024
1 parent 0d546e9 commit fb74b56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
Expand Up @@ -1680,7 +1680,7 @@ private File getXML(String type) {
}
// try looking in the default 'xml' directory as a raw report root
// might have been specified
return new File(getReportsRoot() + File.separator + "xml", "meta" + XML_EXTENSION); //$NON-NLS-1$ //$NON-NLS-2$
return new File(getReportsRoot() + File.separator + "xml", type + XML_EXTENSION); //$NON-NLS-1$
}

/**
Expand Down
Expand Up @@ -17,8 +17,8 @@
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -294,38 +294,35 @@ public static boolean isValidDirectory(File file) {
* <code>false</code> otherwise
*/
public static boolean isValidArchive(File file) {
String fname = file.getName().toLowerCase();
String fname = file.getName().toLowerCase(Locale.ENGLISH);
if (file.exists() && Util.isArchive(fname)) {
Enumeration<? extends ZipEntry> entries = null;
if (fname.endsWith(Util.DOT_JAR)) {
try (JarFile jfile = new JarFile(file)) {
entries = jfile.entries();
return containsUseScans(jfile);
} catch (IOException ioe) {
return false;
}
} else if (fname.endsWith(Util.DOT_ZIP)) {
try (ZipFile zfile = new ZipFile(file)) {
entries = zfile.entries();
return containsUseScans(zfile);
} catch (IOException e) {
return false;
}
}
if (entries != null) {
while (entries.hasMoreElements()) {
ZipEntry o = entries.nextElement();
if (o.isDirectory()) {
IPath path = IPath.fromOSString(o.getName());
int count = path.segmentCount();
if (count > 2) {
return NAME_REGEX.matcher(path.segment(0)).matches() || NAME_REGEX.matcher(path.segment(1)).matches();
}
}
}
}
}
return false;
}

private static boolean containsUseScans(ZipFile zfile) {
return zfile.stream().filter(ZipEntry::isDirectory).anyMatch(o -> {
IPath path = IPath.fromOSString(o.getName());
if (path.segmentCount() > 2) {
return NAME_REGEX.matcher(path.segment(0)).matches() || NAME_REGEX.matcher(path.segment(1)).matches();
}
return false;
});
}

/**
* Returns if the scan if a valid API use scan
*
Expand Down

0 comments on commit fb74b56

Please sign in to comment.