Commit cab3f5da authored by Ahmed Ashour's avatar Ahmed Ashour Committed by skylot

fix: always use FileUtils.createTempFile (PR #634)

parent 77cee15d
...@@ -168,7 +168,7 @@ public class ClsSet { ...@@ -168,7 +168,7 @@ public class ClsSet {
save(outputStream); save(outputStream);
} }
} else if (outputName.endsWith(".jar")) { } else if (outputName.endsWith(".jar")) {
Path temp = Files.createTempFile("jadx", ".zip"); Path temp = FileUtils.createTempFile(".zip");
Files.copy(path, temp, StandardCopyOption.REPLACE_EXISTING); Files.copy(path, temp, StandardCopyOption.REPLACE_EXISTING);
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(path)); try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(path));
...@@ -185,8 +185,6 @@ public class ClsSet { ...@@ -185,8 +185,6 @@ public class ClsSet {
entry = in.getNextEntry(); entry = in.getNextEntry();
} }
} }
Files.delete(temp);
} else { } else {
throw new JadxRuntimeException("Unknown file format: " + outputName); throw new JadxRuntimeException("Unknown file format: " + outputName);
} }
......
...@@ -65,24 +65,13 @@ public class FileUtils { ...@@ -65,24 +65,13 @@ public class FileUtils {
} }
} }
public static File createTempFile(String suffix) { public static Path createTempFile(String suffix) {
File temp;
try { try {
temp = File.createTempFile("jadx-tmp-", System.nanoTime() + '-' + suffix); Path path = Files.createTempFile("jadx-tmp-", suffix);
temp.deleteOnExit();
} catch (IOException e) {
throw new JadxRuntimeException("Failed to create temp file with suffix: " + suffix);
}
return temp;
}
public static File createTempDir(String suffix) {
try {
Path path = Files.createTempDirectory("jadx-tmp-" + System.nanoTime() + '-' + suffix);
path.toFile().deleteOnExit(); path.toFile().deleteOnExit();
return path.toFile(); return path;
} catch (IOException e) { } catch (IOException e) {
throw new JadxRuntimeException("Failed to create temp directory with suffix: " + suffix); throw new JadxRuntimeException("Failed to create temp file with suffix: " + suffix);
} }
} }
......
...@@ -4,9 +4,9 @@ import static jadx.core.utils.files.FileUtils.isApkFile; ...@@ -4,9 +4,9 @@ import static jadx.core.utils.files.FileUtils.isApkFile;
import static jadx.core.utils.files.FileUtils.isZipDexFile; import static jadx.core.utils.files.FileUtils.isZipDexFile;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
...@@ -58,7 +58,7 @@ public class InputFile { ...@@ -58,7 +58,7 @@ public class InputFile {
return; return;
} }
if (fileName.endsWith(".smali")) { if (fileName.endsWith(".smali")) {
Path output = Files.createTempFile("jadx", ".dex"); Path output = FileUtils.createTempFile(".dex");
SmaliOptions options = new SmaliOptions(); SmaliOptions options = new SmaliOptions();
options.outputDexFile = output.toAbsolutePath().toString(); options.outputDexFile = output.toAbsolutePath().toString();
Smali.assemble(options, file.getAbsolutePath()); Smali.assemble(options, file.getAbsolutePath());
...@@ -134,7 +134,7 @@ public class InputFile { ...@@ -134,7 +134,7 @@ public class InputFile {
case ".jar": case ".jar":
index++; index++;
Path jarFile = Files.createTempFile(entryName, ".jar"); Path jarFile = FileUtils.createTempFile(entryName);
Files.copy(inputStream, jarFile, StandardCopyOption.REPLACE_EXISTING); Files.copy(inputStream, jarFile, StandardCopyOption.REPLACE_EXISTING);
for (Dex dex : loadFromJar(jarFile)) { for (Dex dex : loadFromJar(jarFile)) {
addDexFile(entryName, dex); addDexFile(entryName, dex);
...@@ -145,11 +145,11 @@ public class InputFile { ...@@ -145,11 +145,11 @@ public class InputFile {
throw new JadxRuntimeException("Unexpected extension in zip: " + ext); throw new JadxRuntimeException("Unexpected extension in zip: " + ext);
} }
} else if (entryName.equals("instant-run.zip") && ext.equals(".dex")) { } else if (entryName.equals("instant-run.zip") && ext.equals(".dex")) {
File jarFile = FileUtils.createTempFile("instant-run.zip"); Path jarFile = FileUtils.createTempFile("instant-run.zip");
try (FileOutputStream fos = new FileOutputStream(jarFile)) { try (OutputStream fos = Files.newOutputStream(jarFile)) {
IOUtils.copy(inputStream, fos); IOUtils.copy(inputStream, fos);
} }
InputFile tempFile = new InputFile(jarFile); InputFile tempFile = new InputFile(jarFile.toFile());
tempFile.loadFromZip(ext); tempFile.loadFromZip(ext);
List<DexFile> dexFiles = tempFile.getDexFiles(); List<DexFile> dexFiles = tempFile.getDexFiles();
if (!dexFiles.isEmpty()) { if (!dexFiles.isEmpty()) {
...@@ -196,7 +196,7 @@ public class InputFile { ...@@ -196,7 +196,7 @@ public class InputFile {
} }
private static List<Dex> loadFromClassFile(File file) throws IOException, DecodeException { private static List<Dex> loadFromClassFile(File file) throws IOException, DecodeException {
Path outFile = Files.createTempFile("cls", ".jar"); Path outFile = FileUtils.createTempFile(".jar");
try (JarOutputStream jo = new JarOutputStream(Files.newOutputStream(outFile))) { try (JarOutputStream jo = new JarOutputStream(Files.newOutputStream(outFile))) {
String clsName = AsmUtils.getNameFromClassFile(file); String clsName = AsmUtils.getNameFromClassFile(file);
if (clsName == null || !ZipSecurity.isValidZipEntryName(clsName)) { if (clsName == null || !ZipSecurity.isValidZipEntryName(clsName)) {
......
...@@ -66,7 +66,7 @@ public class JadxArgsValidatorOutDirsTest { ...@@ -66,7 +66,7 @@ public class JadxArgsValidatorOutDirsTest {
private JadxArgs makeArgs() { private JadxArgs makeArgs() {
JadxArgs args = new JadxArgs(); JadxArgs args = new JadxArgs();
args.getInputFiles().add(FileUtils.createTempFile("some.apk")); args.getInputFiles().add(FileUtils.createTempFile("some.apk").toFile());
return args; return args;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment