9: Clean tmp dir after performing APK audit and add checking sources

This commit is contained in:
Bartłomiej Pluta
2019-04-04 10:11:48 +02:00
parent f255ad2be8
commit 8b565ad972
4 changed files with 44 additions and 4 deletions

View File

@@ -4,6 +4,9 @@ import com.bartek.esa.analyser.core.Analyser;
import com.bartek.esa.core.archetype.Plugin;
import com.bartek.esa.core.executor.PluginExecutor;
import com.bartek.esa.decompiler.decompiler.Decompiler;
import com.bartek.esa.error.EsaException;
import com.bartek.esa.file.cleaner.FileCleaner;
import com.bartek.esa.file.matcher.GlobMatcher;
import com.bartek.esa.file.provider.FileProvider;
import java.io.File;
@@ -15,17 +18,29 @@ public class ApkAnalyser extends Analyser {
private static final String LAYOUT_GLOB = "**/" + Decompiler.XML_FILES_DIR + "/**/layout*/*.xml";
private final Decompiler decompiler;
private final FileCleaner fileCleaner;
private final GlobMatcher globMatcher;
public ApkAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider, Decompiler decompiler) {
public ApkAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider, Decompiler decompiler, FileCleaner fileCleaner, GlobMatcher globMatcher) {
super(pluginExecutor, plugins, fileProvider);
this.decompiler = decompiler;
this.fileCleaner = fileCleaner;
this.globMatcher = globMatcher;
}
@Override
protected String prepareSources(String source) {
checkIfSourceIsApkFile(source);
System.out.println("Decompiling APK...");
return decompiler.decompile(new File(source)).getAbsolutePath();
}
private void checkIfSourceIsApkFile(String source) {
if (!globMatcher.fileMatchesGlobPattern(new File(source), "**/*.apk")) {
throw new EsaException("Provided source is not *.apk file. Interrupting...");
}
}
@Override
protected String getAndroidManifestGlob() {
return ANDROID_MANIFEST_GLOB;
@@ -40,4 +55,9 @@ public class ApkAnalyser extends Analyser {
protected String getLayoutGlob() {
return LAYOUT_GLOB;
}
@Override
protected void performCleaning(String source) {
fileCleaner.deleteRecursively(new File(source));
}
}

View File

@@ -31,7 +31,9 @@ public abstract class Analyser {
Set<File> files = getFiles(newSource);
Set<Plugin> selectedPlugins = getPlugins(pluginCodes, excludeCodes);
return pluginExecutor.executeForFiles(manifest, files, selectedPlugins);
List<Issue> issues = pluginExecutor.executeForFiles(manifest, files, selectedPlugins);
performCleaning(newSource);
return issues;
}
protected abstract String prepareSources(String source);
@@ -42,6 +44,8 @@ public abstract class Analyser {
protected abstract String getLayoutGlob();
protected abstract void performCleaning(String source);
private File getManifest(String source) {
Set<File> manifests = fileProvider.getGlobMatchedFiles(source, getAndroidManifestGlob());

View File

@@ -5,6 +5,8 @@ import com.bartek.esa.analyser.source.SourceAnalyser;
import com.bartek.esa.core.archetype.Plugin;
import com.bartek.esa.core.executor.PluginExecutor;
import com.bartek.esa.decompiler.decompiler.Decompiler;
import com.bartek.esa.file.cleaner.FileCleaner;
import com.bartek.esa.file.matcher.GlobMatcher;
import com.bartek.esa.file.provider.FileProvider;
import dagger.Module;
import dagger.Provides;
@@ -20,7 +22,7 @@ public class AnalyserModule {
}
@Provides
public ApkAnalyser apkAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider, Decompiler decompiler) {
return new ApkAnalyser(pluginExecutor, plugins, fileProvider, decompiler);
public ApkAnalyser apkAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider, Decompiler decompiler, FileCleaner fileCleaner, GlobMatcher globMatcher) {
return new ApkAnalyser(pluginExecutor, plugins, fileProvider, decompiler, fileCleaner, globMatcher);
}
}

View File

@@ -3,8 +3,10 @@ package com.bartek.esa.analyser.source;
import com.bartek.esa.analyser.core.Analyser;
import com.bartek.esa.core.archetype.Plugin;
import com.bartek.esa.core.executor.PluginExecutor;
import com.bartek.esa.error.EsaException;
import com.bartek.esa.file.provider.FileProvider;
import java.io.File;
import java.util.Set;
public class SourceAnalyser extends Analyser {
@@ -18,9 +20,16 @@ public class SourceAnalyser extends Analyser {
@Override
protected String prepareSources(String source) {
checkIfSourceIsDirectory(source);
return source;
}
private void checkIfSourceIsDirectory(String source) {
if (!new File(source).isDirectory()) {
throw new EsaException("Provided source is not a directory. Interrupting...");
}
}
@Override
protected String getAndroidManifestGlob() {
return ANDROID_MANIFEST_GLOB;
@@ -35,4 +44,9 @@ public class SourceAnalyser extends Analyser {
protected String getLayoutGlob() {
return LAYOUT_GLOB;
}
@Override
protected void performCleaning(String source) {
// do nothing
}
}