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();
} }
if(torService.getState() != Worker.State.RUNNING) {
torService.start(); 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,19 +241,28 @@ public class Hwi {
} }
private String execute(List<String> command) throws IOException { private String execute(List<String> command) throws IOException {
long start = System.currentTimeMillis();
Process process = null;
try {
ProcessBuilder processBuilder = new ProcessBuilder(command); ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start(); process = processBuilder.start();
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);
}
} }
private String execute(List<String> arguments, Command command, String... commandArguments) throws IOException { private String execute(List<String> arguments, Command command, String... commandArguments) throws IOException {
long start = System.currentTimeMillis();
Process process = null;
try {
List<String> processArguments = new ArrayList<>(arguments); List<String> processArguments = new ArrayList<>(arguments);
processArguments.add("--stdin"); 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());
@ -267,6 +277,9 @@ public class Hwi {
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);
}
} }
private synchronized File getHwiExecutable(Command command) { private synchronized File getHwiExecutable(Command command) {
@ -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);