Skip to content

Commit

Permalink
fix: always use FileUtils.createTempFile (PR #634)
Browse files Browse the repository at this point in the history
  • Loading branch information
asashour authored and skylot committed Apr 25, 2019
1 parent 77cee15 commit cab3f5d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
4 changes: 1 addition & 3 deletions jadx-core/src/main/java/jadx/core/clsp/ClsSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ void save(Path path) throws IOException {
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));
Expand All @@ -185,8 +185,6 @@ void save(Path path) throws IOException {
entry = in.getNextEntry();
}
}
Files.delete(temp);

} else {
throw new JadxRuntimeException("Unknown file format: " + outputName);
}
Expand Down
19 changes: 4 additions & 15 deletions jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,13 @@ public static void makeDirs(@Nullable File dir) {
}
}

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);
}
}

Expand Down
14 changes: 7 additions & 7 deletions jadx-core/src/main/java/jadx/core/utils/files/InputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
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;
Expand Down Expand Up @@ -58,7 +58,7 @@ private void searchDexFiles(boolean skipSources) throws IOException, DecodeExcep
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());
Expand Down Expand Up @@ -134,7 +134,7 @@ private boolean loadFromZip(String ext) throws IOException, DecodeException {

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);
Expand All @@ -145,11 +145,11 @@ private boolean loadFromZip(String ext) throws IOException, DecodeException {
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()) {
Expand Down Expand Up @@ -196,7 +196,7 @@ private static List<Dex> loadFromJar(Path jar) throws 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))) {
String clsName = AsmUtils.getNameFromClassFile(file);
if (clsName == null || !ZipSecurity.isValidZipEntryName(clsName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void checkOutDirs(String outDir, String srcDir, String resDir) {

private JadxArgs makeArgs() {
JadxArgs args = new JadxArgs();
args.getInputFiles().add(FileUtils.createTempFile("some.apk"));
args.getInputFiles().add(FileUtils.createTempFile("some.apk").toFile());
return args;
}
}

0 comments on commit cab3f5d

Please sign in to comment.