Exit with non-zero status if any error occurred while running baksmali

This commit is contained in:
Ben Gruver 2013-05-12 12:16:32 -07:00
parent dd77ba20f4
commit dea5f8d544
2 changed files with 24 additions and 18 deletions

View File

@ -45,7 +45,7 @@ import java.util.concurrent.*;
public class baksmali { public class baksmali {
public static void disassembleDexFile(DexFile dexFile, final baksmaliOptions options) { public static boolean disassembleDexFile(DexFile dexFile, final baksmaliOptions options) {
if (options.registerInfo != 0 || options.deodex) { if (options.registerInfo != 0 || options.deodex) {
try { try {
Iterable<String> extraClassPathEntries; Iterable<String> extraClassPathEntries;
@ -92,21 +92,23 @@ public class baksmali {
final ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDirectoryFile, ".smali"); final ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDirectoryFile, ".smali");
ExecutorService executor = Executors.newFixedThreadPool(options.jobs); ExecutorService executor = Executors.newFixedThreadPool(options.jobs);
List<Future<Void>> tasks = Lists.newArrayList(); List<Future<Boolean>> tasks = Lists.newArrayList();
for (final ClassDef classDef: classDefs) { for (final ClassDef classDef: classDefs) {
tasks.add(executor.submit(new Callable<Void>() { tasks.add(executor.submit(new Callable<Boolean>() {
@Override public Void call() throws Exception { @Override public Boolean call() throws Exception {
disassembleClass(classDef, fileNameHandler, options); return disassembleClass(classDef, fileNameHandler, options);
return null;
} }
})); }));
} }
for (Future<Void> task: tasks) { boolean errorOccurred = false;
for (Future<Boolean> task: tasks) {
while(true) { while(true) {
try { try {
task.get(); if (!task.get()) {
errorOccurred = true;
}
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
continue; continue;
} catch (ExecutionException ex) { } catch (ExecutionException ex) {
@ -117,10 +119,11 @@ public class baksmali {
} }
executor.shutdown(); executor.shutdown();
return !errorOccurred;
} }
private static void disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, private static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler,
baksmaliOptions options) { baksmaliOptions options) {
/** /**
* The path for the disassembly file is based on the package name * The path for the disassembly file is based on the package name
* The class descriptor will look something like: * The class descriptor will look something like:
@ -134,7 +137,7 @@ public class baksmali {
if (classDescriptor.charAt(0) != 'L' || if (classDescriptor.charAt(0) != 'L' ||
classDescriptor.charAt(classDescriptor.length()-1) != ';') { classDescriptor.charAt(classDescriptor.length()-1) != ';') {
System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class"); System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
return; return false;
} }
File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor); File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
@ -152,7 +155,7 @@ public class baksmali {
// check again, it's likely it was created in a different thread // check again, it's likely it was created in a different thread
if (!smaliParent.exists()) { if (!smaliParent.exists()) {
System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class"); System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
return; return false;
} }
} }
} }
@ -160,7 +163,7 @@ public class baksmali {
if (!smaliFile.exists()){ if (!smaliFile.exists()){
if (!smaliFile.createNewFile()) { if (!smaliFile.createNewFile()) {
System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class"); System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
return; return false;
} }
} }
@ -174,6 +177,7 @@ public class baksmali {
ex.printStackTrace(); ex.printStackTrace();
// noinspection ResultOfMethodCallIgnored // noinspection ResultOfMethodCallIgnored
smaliFile.delete(); smaliFile.delete();
return false;
} }
finally finally
{ {
@ -186,9 +190,6 @@ public class baksmali {
} }
} }
} }
return true;
if (!options.ignoreErrors && classDefinition.hadValidationErrors()) {
System.exit(1);
}
} }
} }

View File

@ -259,8 +259,9 @@ public class main {
} }
} }
boolean errorOccurred = false;
if (disassemble) { if (disassemble) {
baksmali.disassembleDexFile(dexFile, options); errorOccurred = !baksmali.disassembleDexFile(dexFile, options);
} }
if (doDump) { if (doDump) {
@ -269,6 +270,10 @@ public class main {
} }
dump.dump(dexFile, dumpFileName, options.apiLevel); dump.dump(dexFile, dumpFileName, options.apiLevel);
} }
if (errorOccurred) {
System.exit(1);
}
} }
/** /**