delete temporary hwi pyinstaller extraction if hwi crashes

This commit is contained in:
Craig Raw 2022-07-28 10:59:44 +02:00
parent dcb261a631
commit 4e08334a3a
2 changed files with 68 additions and 19 deletions

View file

@ -146,7 +146,9 @@ public class AppServices {
torService.reset(); torService.reset();
} }
torService.start(); if(torService.getState() != Worker.State.RUNNING) {
torService.start();
}
} else { } else {
restartServices(); restartServices();
} }

View file

@ -25,6 +25,7 @@ import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions; import java.nio.file.attribute.PosixFilePermissions;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
@ -240,32 +241,44 @@ public class Hwi {
} }
private String execute(List<String> command) throws IOException { private String execute(List<String> command) throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder(command); long start = System.currentTimeMillis();
Process process = processBuilder.start(); Process process = null;
try(InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { try {
return CharStreams.toString(reader); ProcessBuilder processBuilder = new ProcessBuilder(command);
process = processBuilder.start();
try(InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) {
return CharStreams.toString(reader);
}
} finally {
deleteExtractionOnFailure(process, start);
} }
} }
private String execute(List<String> arguments, Command command, String... commandArguments) throws IOException { private String execute(List<String> arguments, Command command, String... commandArguments) throws IOException {
List<String> processArguments = new ArrayList<>(arguments); long start = System.currentTimeMillis();
processArguments.add("--stdin"); Process process = null;
try {
List<String> processArguments = new ArrayList<>(arguments);
processArguments.add("--stdin");
ProcessBuilder processBuilder = new ProcessBuilder(processArguments); ProcessBuilder processBuilder = new ProcessBuilder(processArguments);
Process process = processBuilder.start(); process = processBuilder.start();
try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8))) { try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8))) {
writer.write(command.toString()); writer.write(command.toString());
for(String commandArgument : commandArguments) { for(String commandArgument : commandArguments) {
writer.write(" \""); writer.write(" \"");
writer.write(commandArgument.replace("\\", "\\\\").replace("\"", "\\\"")); writer.write(commandArgument.replace("\\", "\\\\").replace("\"", "\\\""));
writer.write("\""); writer.write("\"");
}
writer.flush();
} }
writer.flush();
}
try(InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { try(InputStreamReader reader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) {
return CharStreams.toString(reader); return CharStreams.toString(reader);
}
} finally {
deleteExtractionOnFailure(process, start);
} }
} }
@ -432,6 +445,40 @@ public class Hwi {
return result.get("success").getAsBoolean(); return result.get("success").getAsBoolean();
} }
private void deleteExtractionOnFailure(Process process, long after) {
try {
if(Platform.getCurrent() != Platform.OSX && process != null && process.waitFor(100, TimeUnit.MILLISECONDS) && process.exitValue() != 0) {
File extraction = getTemporaryExtraction(after);
if(extraction != null) {
IOUtils.deleteDirectory(extraction);
}
}
} catch(Exception e) {
log.debug("Error deleting temporary extraction", e);
}
}
private File getTemporaryExtraction(long after) {
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
if(!tmpDir.exists()) {
return null;
}
File[] tmps = tmpDir.listFiles(file -> {
if(!file.isDirectory() || file.lastModified() < after) {
return false;
}
String name = file.getName();
if(name.length() < 9 || !name.startsWith("_MEI")) {
return false;
}
File hwilib = new File(file, "hwilib");
return hwilib.exists();
});
return tmps == null || tmps.length == 0 ? null : Arrays.stream(tmps).sorted(Comparator.comparingLong(File::lastModified)).findFirst().orElse(null);
}
private List<String> getDeviceArguments(Device device, Command command) throws IOException { private List<String> getDeviceArguments(Device device, Command command) throws IOException {
List<String> elements = new ArrayList<>(List.of(getHwiExecutable(command).getAbsolutePath(), "--device-path", device.getPath(), "--device-type", device.getType())); List<String> elements = new ArrayList<>(List.of(getHwiExecutable(command).getAbsolutePath(), "--device-path", device.getPath(), "--device-type", device.getType()));
addChainType(elements, false); addChainType(elements, false);