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 {
save(outputStream);
}
} else if (outputName.endsWith(".jar")) {
Path temp = Files.createTempFile("jadx", ".zip");
Path temp = FileUtils.createTempFile(".zip");
Files.copy(path, temp, StandardCopyOption.REPLACE_EXISTING);
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(path));
......@@ -185,8 +185,6 @@ public class ClsSet {
entry = in.getNextEntry();
}
}
Files.delete(temp);
} else {
throw new JadxRuntimeException("Unknown file format: " + outputName);
}
......
......@@ -65,24 +65,13 @@ public class FileUtils {
}
}
public static File createTempFile(String suffix) {
File temp;
public static Path createTempFile(String suffix) {
try {
temp = File.createTempFile("jadx-tmp-", System.nanoTime() + '-' + 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 path = Files.createTempFile("jadx-tmp-", suffix);
path.toFile().deleteOnExit();
return path.toFile();
return path;
} 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;
import static jadx.core.utils.files.FileUtils.isZipDexFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
......@@ -58,7 +58,7 @@ public class InputFile {
return;
}
if (fileName.endsWith(".smali")) {
Path output = Files.createTempFile("jadx", ".dex");
Path output = FileUtils.createTempFile(".dex");
SmaliOptions options = new SmaliOptions();
options.outputDexFile = output.toAbsolutePath().toString();
Smali.assemble(options, file.getAbsolutePath());
......@@ -134,7 +134,7 @@ public class InputFile {
case ".jar":
index++;
Path jarFile = Files.createTempFile(entryName, ".jar");
Path jarFile = FileUtils.createTempFile(entryName);
Files.copy(inputStream, jarFile, StandardCopyOption.REPLACE_EXISTING);
for (Dex dex : loadFromJar(jarFile)) {
addDexFile(entryName, dex);
......@@ -145,11 +145,11 @@ public class InputFile {
throw new JadxRuntimeException("Unexpected extension in zip: " + ext);
}
} else if (entryName.equals("instant-run.zip") && ext.equals(".dex")) {
File jarFile = FileUtils.createTempFile("instant-run.zip");
try (FileOutputStream fos = new FileOutputStream(jarFile)) {
Path jarFile = FileUtils.createTempFile("instant-run.zip");
try (OutputStream fos = Files.newOutputStream(jarFile)) {
IOUtils.copy(inputStream, fos);
}
InputFile tempFile = new InputFile(jarFile);
InputFile tempFile = new InputFile(jarFile.toFile());
tempFile.loadFromZip(ext);
List<DexFile> dexFiles = tempFile.getDexFiles();
if (!dexFiles.isEmpty()) {
......@@ -196,7 +196,7 @@ public class InputFile {
}
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))) {
String clsName = AsmUtils.getNameFromClassFile(file);
if (clsName == null || !ZipSecurity.isValidZipEntryName(clsName)) {
......
......@@ -66,7 +66,7 @@ public class JadxArgsValidatorOutDirsTest {
private JadxArgs makeArgs() {
JadxArgs args = new JadxArgs();
args.getInputFiles().add(FileUtils.createTempFile("some.apk"));
args.getInputFiles().add(FileUtils.createTempFile("some.apk").toFile());
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