eae195b6 by Amin Bandali

Fix a few exceptions uncovered while runnning on the large corpus

1 parent 0bf0effa
......@@ -53,7 +53,7 @@
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.0.0-alpha-3</version>
<version>4.0.0-beta-1b</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
......
package org.aminb.alloy.catalyst;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
......@@ -102,10 +103,13 @@ protected final CorpusResult analyzeCorpus(String corpus) {
corpus, cr.elapsed_time));
long ssrs = 0, ssrc = 0;
for (FileResult fr : cr.file_results) {
ssrs += fr.ssr_result.sig_count;
ssrc += fr.ssr_result.ssr_candidate_count;
}
for (FileResult fr : cr.file_results)
if (fr.ssr_result != null) { // happens when there are
// errors in analyzeFile
// and it exits early
ssrs += fr.ssr_result.sig_count;
ssrc += fr.ssr_result.ssr_candidate_count;
}
pln(String.format("SSR candidates: %d/%d (%.2f%%)",
ssrc, ssrs, 100. * ssrc / ssrs));
cr.ssr_result = new SSRResult();
......@@ -124,11 +128,30 @@ protected final FileResult analyzeFile(String corpus, Path p, int index, int tot
fr.mutation_results = new ArrayList<MutationResult>();
fr.errors = new ArrayList<ParseOrTypeError>();
Path outFile = Paths.get(outDir, corpus, fr.file_name.replace("/", outSep));
Writer w = setupWriter(outFile, true);
String progress = String.format("[%d/%d]: ", index, total);
Path prefix = Paths.get(outDir, corpus);
Path fName;
try {
int diff = outSep.length() - 1 /* length of separatorChar */;
int wiggleRoom =
prefix.toString().length() + 1 /* one joining separatorChar */ +
Utils.countMatches(fr.file_name, File.separatorChar) * diff;
fName = Utils.shortenPathIfNeeded(Paths.get(fr.file_name), 255, wiggleRoom);
}
catch (PathTooLongException e) {
String
msg = "Error in creating output json file:",
err = Utils.toString(e);
pln(progress + "error: " + fr.file_name);
fr.errors.add(new ParseOrTypeError(String.format("%s%n%s", msg, err), true));
errorCount.getAndIncrement();
return fr;
}
Path outFile = Paths.get(prefix.toString(),
fName.toString().replace(File.separator, outSep));
Writer w = setupWriter(outFile, true);
A4Reporter rep = new A4Reporter() {
@Override
public void warning(ErrorWarning msg) {
......
package org.aminb.alloy.catalyst;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_256;
import org.apache.commons.codec.digest.DigestUtils;
......@@ -10,8 +14,12 @@ public static String sha256(String s) {
return new org.apache.commons.codec.binary.Base32().encodeAsString(new DigestUtils(SHA_256).digest(s));
}
public static String sha256_n(String s, int n) {
return sha256(s).substring(0, n).toLowerCase();
}
public static String sha256_32(String s) {
return sha256(s).substring(0, 32).toLowerCase();
return sha256_n(s, 32);
}
public static RuntimeException systemExit(int status) {
......@@ -36,4 +44,43 @@ public static String toString(Throwable ex) {
}
return sb.toString().trim();
}
public static int countMatches(String s, char c) {
if (s.isEmpty())
return 0;
int count = 0;
for (int i = 0; i < s.length(); i++)
if (c == s.charAt(i))
count++;
return count;
}
public static Path shortenPathIfNeeded(Path p, int max, int wiggleRoom)
throws PathTooLongException {
if (p.toString().length() > max - wiggleRoom) {
Path parent = p;
for (int i = 0; i < countMatches(p.toString(), File.separatorChar); i++) {
parent = parent.getParent();
String parentName = parent.getFileName().toString();
if (parentName.length() > 16)
p = Paths.get(p.toString()
.replace(parentName, sha256_n(parentName, 16)));
if (p.toString().length() <= max - wiggleRoom)
return p;
}
throw new PathTooLongException("Path too long and/or too deeply nested");
}
else
return p;
}
}
class PathTooLongException extends Exception {
public PathTooLongException(String msg) {
super(msg);
}
public PathTooLongException(String msg, Throwable err) {
super(msg, err);
}
}
......