fix: use temp dir env var only in apps
Some checks failed
Build Artifacts / build (push) Waiting to run
Build Artifacts / build-win-bundle (push) Waiting to run
Build Test / tests (ubuntu-latest) (push) Waiting to run
Build Test / tests (windows-latest) (push) Waiting to run
Validate Gradle Wrapper / Validation (push) Waiting to run
CodeQL / Analyze (java) (push) Has been cancelled

This commit is contained in:
Skylot 2024-10-10 19:31:06 +01:00
parent 063af8cd62
commit c21cabcba7
No known key found for this signature in database
GPG Key ID: 47A4975761262B6A
15 changed files with 172 additions and 112 deletions

View File

@ -16,7 +16,6 @@ import jadx.cli.LogHelper.LogLevelEnum;
import jadx.cli.plugins.JadxFilesGetter;
import jadx.commons.app.JadxCommonEnv;
import jadx.core.utils.exceptions.JadxArgsValidateException;
import jadx.core.utils.files.FileUtils;
import jadx.plugins.tools.JadxExternalPluginsLoader;
public class JadxCLI {
@ -33,7 +32,6 @@ public class JadxCLI {
LOG.error("Process error:", e);
result = 1;
} finally {
FileUtils.deleteTempRootDir();
System.exit(result);
}
}

View File

@ -3,8 +3,8 @@ package jadx.cli.plugins;
import java.nio.file.Path;
import jadx.commons.app.JadxCommonFiles;
import jadx.commons.app.JadxTempFiles;
import jadx.core.plugins.files.IJadxFilesGetter;
import jadx.core.utils.files.FileUtils;
public class JadxFilesGetter implements IJadxFilesGetter {
@ -22,7 +22,7 @@ public class JadxFilesGetter implements IJadxFilesGetter {
@Override
public Path getTempDir() {
return FileUtils.getTempRootDir();
return JadxTempFiles.getTempRootDir();
}
private JadxFilesGetter() {

View File

@ -12,13 +12,11 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.core.utils.files.FileUtils;
import static org.assertj.core.api.Assertions.assertThat;
public class TestInput {
@ -29,6 +27,9 @@ public class TestInput {
return true;
};
@TempDir
Path testDir;
@Test
public void testHelp() {
int result = JadxCLI.execute(new String[] { "--help" });
@ -37,83 +38,68 @@ public class TestInput {
@Test
public void testDexInput() throws Exception {
decompile("dex", "samples/hello.dex");
decompile("samples/hello.dex");
}
@Test
public void testSmaliInput() throws Exception {
decompile("smali", "samples/HelloWorld.smali");
decompile("samples/HelloWorld.smali");
}
@Test
public void testClassInput() throws Exception {
decompile("class", "samples/HelloWorld.class");
decompile("samples/HelloWorld.class");
}
@Test
public void testMultipleInput() throws Exception {
decompile("multi", "samples/hello.dex", "samples/HelloWorld.smali");
decompile("samples/hello.dex", "samples/HelloWorld.smali");
}
@Test
public void testFallbackMode() throws Exception {
Path tempDir = FileUtils.createTempDir("fallback");
List<String> args = buildArgs(tempDir, "samples/hello.dex");
args.add(0, "-f");
int result = JadxCLI.execute(args.toArray(new String[0]));
int result = JadxCLI.execute(buildArgs(List.of("-f"), "samples/hello.dex"));
assertThat(result).isEqualTo(0);
List<Path> files = collectJavaFilesInDir(tempDir);
List<Path> files = collectJavaFilesInDir(testDir);
assertThat(files).hasSize(1);
}
@Test
public void testSimpleMode() throws Exception {
Path tempDir = FileUtils.createTempDir("simple");
List<String> args = buildArgs(tempDir, "samples/hello.dex");
args.add(0, "--decompilation-mode");
args.add(1, "simple");
int result = JadxCLI.execute(args.toArray(new String[0]));
int result = JadxCLI.execute(buildArgs(List.of("--decompilation-mode", "simple"), "samples/hello.dex"));
assertThat(result).isEqualTo(0);
List<Path> files = collectJavaFilesInDir(tempDir);
List<Path> files = collectJavaFilesInDir(testDir);
assertThat(files).hasSize(1);
}
@Test
public void testResourceOnly() throws Exception {
Path tempDir = FileUtils.createTempDir("resourceOnly");
List<String> args = buildArgs(tempDir, "samples/resources-only.apk");
int result = JadxCLI.execute(args.toArray(new String[0]));
int result = JadxCLI.execute(buildArgs(List.of(), "samples/resources-only.apk"));
assertThat(result).isEqualTo(0);
List<Path> files = collectFilesInDir(tempDir,
List<Path> files = collectFilesInDir(testDir,
path -> path.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml"));
assertThat(files).isNotEmpty();
}
private void decompile(String tmpDirName, String... inputSamples) throws URISyntaxException, IOException {
Path tempDir = FileUtils.createTempDir(tmpDirName);
List<String> args = buildArgs(tempDir, inputSamples);
int result = JadxCLI.execute(args.toArray(new String[0]));
private void decompile(String... inputSamples) throws URISyntaxException, IOException {
int result = JadxCLI.execute(buildArgs(List.of(), inputSamples));
assertThat(result).isEqualTo(0);
List<Path> resultJavaFiles = collectJavaFilesInDir(tempDir);
List<Path> resultJavaFiles = collectJavaFilesInDir(testDir);
assertThat(resultJavaFiles).isNotEmpty();
// do not copy input files as resources
for (Path path : collectFilesInDir(tempDir, LOG_ALL_FILES)) {
for (Path path : collectFilesInDir(testDir, LOG_ALL_FILES)) {
for (String inputSample : inputSamples) {
assertThat(path.toAbsolutePath().toString()).doesNotContain(inputSample);
}
}
}
private List<String> buildArgs(Path tempDir, String... inputSamples) throws URISyntaxException {
List<String> args = new ArrayList<>();
private String[] buildArgs(List<String> options, String... inputSamples) throws URISyntaxException {
List<String> args = new ArrayList<>(options);
args.add("-v");
args.add("-d");
args.add(tempDir.toAbsolutePath().toString());
args.add(testDir.toAbsolutePath().toString());
for (String inputSample : inputSamples) {
URL resource = getClass().getClassLoader().getResource(inputSample);
@ -121,7 +107,7 @@ public class TestInput {
String sampleFile = resource.toURI().getRawPath();
args.add(sampleFile);
}
return args;
return args.toArray(new String[0]);
}
private static List<Path> collectJavaFilesInDir(Path dir) throws IOException {
@ -137,9 +123,4 @@ public class TestInput {
.collect(Collectors.toList());
}
}
@AfterAll
public static void cleanup() {
FileUtils.clearTempRootDir();
}
}

View File

@ -0,0 +1,31 @@
package jadx.commons.app;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JadxTempFiles {
private static final String JADX_TMP_INSTANCE_PREFIX = "jadx-instance-";
private static final Path TEMP_ROOT_DIR = createTempRootDir();
public static Path getTempRootDir() {
return TEMP_ROOT_DIR;
}
private static Path createTempRootDir() {
try {
String jadxTmpDir = System.getenv("JADX_TMP_DIR");
Path dir;
if (jadxTmpDir != null) {
dir = Files.createTempDirectory(Paths.get(jadxTmpDir), JADX_TMP_INSTANCE_PREFIX);
} else {
dir = Files.createTempDirectory(JADX_TMP_INSTANCE_PREFIX);
}
dir.toFile().deleteOnExit();
return dir;
} catch (Exception e) {
throw new RuntimeException("Failed to create temp root directory", e);
}
}
}

View File

@ -111,6 +111,7 @@ public final class JadxDecompiler implements Closeable {
reset();
JadxArgsValidator.validate(this);
LOG.info("loading ...");
FileUtils.updateTempRootDir(args.getFilesGetter().getTempDir());
loadPlugins();
loadInputFiles();
@ -174,6 +175,7 @@ public final class JadxDecompiler implements Closeable {
closeInputs();
closeLoaders();
args.close();
FileUtils.deleteTempRootDir();
}
private void closeInputs() {

View File

@ -1,5 +1,6 @@
package jadx.core.plugins.files;
import java.nio.file.Files;
import java.nio.file.Path;
import jadx.core.utils.files.FileUtils;
@ -8,22 +9,35 @@ public class TempFilesGetter implements IJadxFilesGetter {
public static final TempFilesGetter INSTANCE = new TempFilesGetter();
private final Path tempRootDir;
private TempFilesGetter() {
try {
tempRootDir = Files.createTempDirectory("jadx-temp-");
tempRootDir.toFile().deleteOnExit();
} catch (Exception e) {
throw new RuntimeException("Failed to create temp directory", e);
}
}
@Override
public Path getConfigDir() {
return FileUtils.getTempRootDir().resolve("config");
return makeSubDir("config");
}
@Override
public Path getCacheDir() {
return FileUtils.getTempRootDir().resolve("cache");
return makeSubDir("cache");
}
@Override
public Path getTempDir() {
return FileUtils.getTempRootDir().resolve("temp");
return tempRootDir;
}
private TempFilesGetter() {
// singleton
private Path makeSubDir(String subDir) {
Path dir = tempRootDir.resolve(subDir);
FileUtils.makeDirs(dir);
return dir;
}
}

View File

@ -45,7 +45,31 @@ public class FileUtils {
public static final String JADX_TMP_INSTANCE_PREFIX = "jadx-instance-";
public static final String JADX_TMP_PREFIX = "jadx-tmp-";
private static Path tempRootDir = createTempRootDir();
private FileUtils() {
// utility class
}
public static synchronized Path updateTempRootDir(Path newTempRootDir) {
try {
Path dir = Files.createTempDirectory(newTempRootDir, JADX_TMP_INSTANCE_PREFIX);
tempRootDir = dir;
dir.toFile().deleteOnExit();
return dir;
} catch (Exception e) {
throw new JadxRuntimeException("Failed to update temp root directory", e);
}
}
private static Path createTempRootDir() {
try {
Path dir = Files.createTempDirectory(JADX_TMP_INSTANCE_PREFIX);
dir.toFile().deleteOnExit();
return dir;
} catch (Exception e) {
throw new JadxRuntimeException("Failed to create temp root directory", e);
}
}
public static List<Path> expandDirs(List<Path> paths) {
@ -150,40 +174,13 @@ public class FileUtils {
}
}
private static final Path TEMP_ROOT_DIR = createTempRootDir();
private static Path createTempRootDir() {
try {
String jadxTmpDir = System.getenv("JADX_TMP_DIR");
Path dir;
if (jadxTmpDir != null) {
dir = Files.createTempDirectory(Paths.get(jadxTmpDir), "jadx-instance-");
} else {
dir = Files.createTempDirectory(JADX_TMP_INSTANCE_PREFIX);
}
dir.toFile().deleteOnExit();
return dir;
} catch (Exception e) {
throw new JadxRuntimeException("Failed to create temp root directory", e);
}
}
public static Path getTempRootDir() {
return TEMP_ROOT_DIR;
}
public static void deleteTempRootDir() {
deleteDirIfExists(TEMP_ROOT_DIR);
}
public static void clearTempRootDir() {
deleteDirIfExists(TEMP_ROOT_DIR);
makeDirs(TEMP_ROOT_DIR);
deleteDirIfExists(tempRootDir);
}
public static Path createTempDir(String prefix) {
try {
Path dir = Files.createTempDirectory(TEMP_ROOT_DIR, prefix);
Path dir = Files.createTempDirectory(tempRootDir, prefix);
dir.toFile().deleteOnExit();
return dir;
} catch (Exception e) {
@ -193,7 +190,7 @@ public class FileUtils {
public static Path createTempFile(String suffix) {
try {
Path path = Files.createTempFile(TEMP_ROOT_DIR, JADX_TMP_PREFIX, suffix);
Path path = Files.createTempFile(tempRootDir, JADX_TMP_PREFIX, suffix);
path.toFile().deleteOnExit();
return path;
} catch (Exception e) {

View File

@ -8,7 +8,6 @@ import java.io.RandomAccessFile;
import java.lang.reflect.UndeclaredThrowableException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@ -126,14 +125,12 @@ public class ZipFile extends java.util.zip.ZipFile {
}
private static File copyFile(File file) throws IOException {
var newFile = Files.createTempFile(file.getName(), ".apk").toFile();
var newFile = FileUtils.createTempFile(file.getName()).toFile();
try (var in = new FileInputStream(file)) {
try (var out = new FileOutputStream(newFile)) {
in.transferTo(out);
}
}
return newFile;
}

View File

@ -1,19 +1,24 @@
package jadx.api;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.core.utils.files.FileUtils;
import static jadx.core.utils.files.FileUtils.toFile;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class JadxArgsValidatorOutDirsTest {
private static final Logger LOG = LoggerFactory.getLogger(JadxArgsValidatorOutDirsTest.class);
public JadxArgs args;
@TempDir
Path testDir;
@Test
public void checkAllSet() {
setOutDirs("r", "s", "r");
@ -64,8 +69,12 @@ public class JadxArgsValidatorOutDirsTest {
}
private JadxArgs makeArgs() {
JadxArgs args = new JadxArgs();
args.getInputFiles().add(FileUtils.createTempFile("some.apk").toFile());
return args;
try {
JadxArgs args = new JadxArgs();
args.getInputFiles().add(Files.createTempFile(testDir, "test-", ".apk").toFile());
return args;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -8,8 +8,8 @@ import java.net.URL;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import jadx.core.utils.files.FileUtils;
import jadx.core.xmlgen.ResContainer;
import jadx.plugins.input.dex.DexInputPlugin;
@ -17,15 +17,17 @@ import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class JadxDecompilerTest {
@TempDir
File testDir;
@Test
public void testExampleUsage() {
File sampleApk = getFileFromSampleDir("app-with-fake-dex.apk");
File outDir = FileUtils.createTempDir("jadx-usage-example").toFile();
// test simple apk loading
JadxArgs args = new JadxArgs();
args.getInputFiles().add(sampleApk);
args.setOutDir(outDir);
args.setOutDir(testDir);
try (JadxDecompiler jadx = new JadxDecompiler(args)) {
jadx.load();
@ -59,11 +61,10 @@ public class JadxDecompilerTest {
@Test
public void testResourcesLoad() {
File sampleApk = getFileFromSampleDir("app-with-fake-dex.apk");
File outDir = FileUtils.createTempDir("jadx-usage-example-2").toFile();
JadxArgs args = new JadxArgs();
args.getInputFiles().add(sampleApk);
args.setOutDir(outDir);
args.setOutDir(testDir);
args.setSkipSources(true);
try (JadxDecompiler jadx = new JadxDecompiler(args)) {
jadx.load();

View File

@ -30,6 +30,7 @@ import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -53,12 +54,12 @@ import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.core.utils.files.FileUtils;
import jadx.core.xmlgen.ResourceStorage;
import jadx.core.xmlgen.entry.ResourceEntry;
import jadx.tests.api.compiler.CompilerOptions;
import jadx.tests.api.compiler.JavaUtils;
import jadx.tests.api.compiler.TestCompiler;
import jadx.tests.api.utils.TestFilesGetter;
import jadx.tests.api.utils.TestUtils;
import static org.apache.commons.lang3.StringUtils.leftPad;
@ -115,6 +116,9 @@ public abstract class IntegrationTest extends TestUtils {
protected JadxDecompiler jadxDecompiler;
@TempDir
Path testDir;
@BeforeEach
public void init() {
this.compile = true;
@ -124,7 +128,7 @@ public abstract class IntegrationTest extends TestUtils {
this.useJavaInput = null;
args = new JadxArgs();
args.setOutDir(new File("test-out-tmp"));
args.setOutDir(testDir.toFile());
args.setShowInconsistentCode(true);
args.setThreadsCount(1);
args.setSkipResources(true);
@ -132,6 +136,7 @@ public abstract class IntegrationTest extends TestUtils {
args.setDeobfuscationOn(false);
args.setGeneratedRenamesMappingFileMode(GeneratedRenamesMappingFileMode.IGNORE);
args.setRunDebugChecks(true);
args.setFilesGetter(new TestFilesGetter(testDir));
// use the same values on all systems
args.setFsCaseSensitive(false);
@ -144,7 +149,6 @@ public abstract class IntegrationTest extends TestUtils {
close(jadxDecompiler);
close(sourceCompiler);
close(decompiledCompiler);
FileUtils.clearTempRootDir();
}
private void close(Closeable closeable) throws IOException {
@ -154,7 +158,7 @@ public abstract class IntegrationTest extends TestUtils {
}
public void setOutDirSuffix(String suffix) {
args.setOutDir(new File("test-out-" + suffix + "-tmp"));
args.setOutDir(new File(testDir.toFile(), suffix));
}
public String getTestName() {
@ -499,7 +503,7 @@ public abstract class IntegrationTest extends TestUtils {
}
private List<File> compileSourceFiles(List<File> compileFileList) throws IOException {
Path outTmp = FileUtils.createTempDir("jadx-tmp-classes");
Path outTmp = Files.createTempDirectory(testDir, "jadx-tmp-classes");
sourceCompiler = new TestCompiler(compilerOptions);
List<File> files = sourceCompiler.compileFiles(compileFileList, outTmp);
if (saveTestJar) {
@ -509,7 +513,7 @@ public abstract class IntegrationTest extends TestUtils {
}
private void saveToJar(List<File> files, Path baseDir) throws IOException {
Path jarFile = Files.createTempFile("tests-" + getTestName() + '-', ".jar");
Path jarFile = Files.createTempFile(testDir, "tests-" + getTestName() + '-', ".jar");
try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(jarFile))) {
for (File file : files) {
Path fullPath = file.toPath();

View File

@ -0,0 +1,35 @@
package jadx.tests.api.utils;
import java.nio.file.Path;
import jadx.core.plugins.files.IJadxFilesGetter;
import jadx.core.utils.files.FileUtils;
public class TestFilesGetter implements IJadxFilesGetter {
private final Path testDir;
public TestFilesGetter(Path testDir) {
this.testDir = testDir;
}
@Override
public Path getConfigDir() {
return makeSubDir("config");
}
@Override
public Path getCacheDir() {
return makeSubDir("cache");
}
@Override
public Path getTempDir() {
return testDir;
}
private Path makeSubDir(String config) {
Path dir = testDir.resolve(config);
FileUtils.makeDirs(dir);
return dir;
}
}

View File

@ -13,16 +13,16 @@ import org.slf4j.LoggerFactory;
import jadx.api.ICodeCache;
import jadx.api.JavaClass;
import jadx.api.utils.tasks.ITaskExecutor;
import jadx.commons.app.JadxCommonEnv;
import jadx.core.utils.tasks.TaskExecutor;
import jadx.gui.JadxWrapper;
import jadx.gui.ui.MainWindow;
import jadx.gui.utils.NLS;
import jadx.gui.utils.UiUtils;
public class DecompileTask extends CancelableBackgroundTask {
private static final Logger LOG = LoggerFactory.getLogger(DecompileTask.class);
private static final int CLS_LIMIT = Integer.parseInt(UiUtils.getEnvVar("JADX_CLS_PROCESS_LIMIT", "50"));
private static final int CLS_LIMIT = JadxCommonEnv.getInt("JADX_CLS_PROCESS_LIMIT", 50);
public static int calcDecompileTimeLimit(int classCount) {
return classCount * CLS_LIMIT + 5000;

View File

@ -1549,7 +1549,6 @@ public class MainWindow extends JFrame {
heapUsageBar.reset();
closeAll();
FileUtils.deleteTempRootDir();
dispose();
System.exit(0);
}

View File

@ -337,14 +337,6 @@ public class UiUtils {
return null;
}
public static String getEnvVar(String varName, String defValue) {
String envVal = System.getenv(varName);
if (envVal == null) {
return defValue;
}
return envVal;
}
public static void showMessageBox(Component parent, String msg) {
JOptionPane.showMessageDialog(parent, msg);
}