upgrade to hwi 2.0.2 and relocate hwi to sparrow home folder on osx to avoid partial installation deletions in tmpdir

This commit is contained in:
Craig Raw 2021-06-10 14:06:14 +02:00
parent a59d5d3086
commit e99b1d4171
4 changed files with 61 additions and 39 deletions

View file

@ -30,7 +30,10 @@ import java.util.zip.ZipInputStream;
public class Hwi { public class Hwi {
private static final Logger log = LoggerFactory.getLogger(Hwi.class); private static final Logger log = LoggerFactory.getLogger(Hwi.class);
private static final String VERSION_PREFIX = "hwi-2.0.1"; private static final String HWI_HOME_DIR = "hwi";
private static final String HWI_VERSION_PREFIX = "hwi-";
private static final String HWI_VERSION = "2.0.2";
private static final String HWI_VERSION_DIR = HWI_VERSION_PREFIX + HWI_VERSION;
private static boolean isPromptActive = false; private static boolean isPromptActive = false;
@ -51,7 +54,7 @@ public class Hwi {
} }
return Arrays.stream(devices).filter(device -> device != null && device.getModel() != null).collect(Collectors.toList()); return Arrays.stream(devices).filter(device -> device != null && device.getModel() != null).collect(Collectors.toList());
} catch(IOException e) { } catch(IOException e) {
log.error("Error executing " + VERSION_PREFIX, e); log.error("Error executing " + HWI_VERSION_DIR, e);
throw new ImportException(e); throw new ImportException(e);
} finally { } finally {
isPromptActive = false; isPromptActive = false;
@ -222,9 +225,10 @@ public class Hwi {
private synchronized File getHwiExecutable(Command command) { private synchronized File getHwiExecutable(Command command) {
File hwiExecutable = Config.get().getHwi(); File hwiExecutable = Config.get().getHwi();
if(hwiExecutable != null && hwiExecutable.exists()) { if(hwiExecutable != null && hwiExecutable.exists()) {
File homeDir = getHwiHomeDir();
String tmpDir = System.getProperty("java.io.tmpdir"); String tmpDir = System.getProperty("java.io.tmpdir");
String hwiPath = hwiExecutable.getAbsolutePath(); String hwiPath = hwiExecutable.getAbsolutePath();
if(command.isTestFirst() && hwiPath.startsWith(tmpDir) && (!hwiPath.contains(VERSION_PREFIX) || !testHwi(hwiExecutable))) { if(command.isTestFirst() && (hwiPath.contains(tmpDir) || hwiPath.startsWith(homeDir.getAbsolutePath())) && (!hwiPath.contains(HWI_VERSION_DIR) || !testHwi(hwiExecutable))) {
if(Platform.getCurrent() == Platform.OSX) { if(Platform.getCurrent() == Platform.OSX) {
deleteDirectory(hwiExecutable.getParentFile()); deleteDirectory(hwiExecutable.getParentFile());
} else { } else {
@ -243,55 +247,65 @@ public class Hwi {
//The check will still happen on first invocation, but will not thereafter //The check will still happen on first invocation, but will not thereafter
//See https://github.com/bitcoin-core/HWI/issues/327 for details //See https://github.com/bitcoin-core/HWI/issues/327 for details
if(platform == Platform.OSX) { if(platform == Platform.OSX) {
InputStream inputStream = Hwi.class.getResourceAsStream("/native/osx/x64/" + VERSION_PREFIX + "-mac-amd64-signed.zip"); InputStream inputStream = Hwi.class.getResourceAsStream("/native/osx/x64/" + HWI_VERSION_DIR + "-mac-amd64-signed.zip");
Path tempHwiDirPath = Files.createTempDirectory(VERSION_PREFIX, PosixFilePermissions.asFileAttribute(ownerExecutableWritable)); if(inputStream == null) {
File tempHwiDir = tempHwiDirPath.toFile(); throw new IllegalStateException("Cannot load " + HWI_VERSION_DIR + " from classpath");
//tempHwiDir.deleteOnExit(); }
log.debug("Using temp HWI path: " + tempHwiDir.getAbsolutePath());
File tempExec = null; File hwiHomeDir = getHwiHomeDir();
ZipInputStream zis = new ZipInputStream(inputStream); File hwiVersionDir = new File(hwiHomeDir, HWI_VERSION_DIR);
if(!hwiVersionDir.exists()) {
Files.createDirectories(hwiVersionDir.toPath(), PosixFilePermissions.asFileAttribute(ownerExecutableWritable));
}
log.debug("Using HWI path: " + hwiVersionDir.getAbsolutePath());
File hwiExec = null;
try(ZipInputStream zis = new ZipInputStream(inputStream)) {
ZipEntry zipEntry = zis.getNextEntry(); ZipEntry zipEntry = zis.getNextEntry();
while(zipEntry != null) { while(zipEntry != null) {
if(zipEntry.isDirectory()) { if(zipEntry.isDirectory()) {
newDirectory(tempHwiDir, zipEntry, ownerExecutableWritable); newDirectory(hwiVersionDir, zipEntry, ownerExecutableWritable);
} else { } else {
File newFile = newFile(tempHwiDir, zipEntry, ownerExecutableWritable); File newFile = newFile(hwiVersionDir, zipEntry, ownerExecutableWritable);
//newFile.deleteOnExit(); try(FileOutputStream fos = new FileOutputStream(newFile)) {
FileOutputStream fos = new FileOutputStream(newFile);
ByteStreams.copy(zis, new FileOutputStream(newFile)); ByteStreams.copy(zis, new FileOutputStream(newFile));
fos.flush(); fos.flush();
fos.close(); };
if(zipEntry.getName().equals("hwi")) { if(zipEntry.getName().equals("hwi")) {
tempExec = newFile; hwiExec = newFile;
} }
} }
zipEntry = zis.getNextEntry(); zipEntry = zis.getNextEntry();
} }
zis.closeEntry(); zis.closeEntry();
zis.close(); }
hwiExecutable = tempExec; hwiExecutable = hwiExec;
} else { } else {
InputStream inputStream; InputStream inputStream;
Path tempExecPath; Path tempExecPath;
if(platform == Platform.WINDOWS) { if(platform == Platform.WINDOWS) {
inputStream = Hwi.class.getResourceAsStream("/native/windows/x64/hwi.exe"); inputStream = Hwi.class.getResourceAsStream("/native/windows/x64/hwi.exe");
tempExecPath = Files.createTempFile(VERSION_PREFIX, null); tempExecPath = Files.createTempFile(HWI_VERSION_DIR, null);
} else { } else {
inputStream = Hwi.class.getResourceAsStream("/native/linux/x64/hwi"); inputStream = Hwi.class.getResourceAsStream("/native/linux/x64/hwi");
tempExecPath = Files.createTempFile(VERSION_PREFIX, null, PosixFilePermissions.asFileAttribute(ownerExecutableWritable)); tempExecPath = Files.createTempFile(HWI_VERSION_DIR, null, PosixFilePermissions.asFileAttribute(ownerExecutableWritable));
}
if(inputStream == null) {
throw new IllegalStateException("Cannot load " + HWI_VERSION_DIR + " from classpath");
} }
File tempExec = tempExecPath.toFile(); File tempExec = tempExecPath.toFile();
tempExec.deleteOnExit(); tempExec.deleteOnExit();
OutputStream tempExecStream = new BufferedOutputStream(new FileOutputStream(tempExec)); try(OutputStream tempExecStream = new BufferedOutputStream(new FileOutputStream(tempExec))) {
ByteStreams.copy(inputStream, tempExecStream); ByteStreams.copy(inputStream, tempExecStream);
inputStream.close(); inputStream.close();
tempExecStream.flush(); tempExecStream.flush();
tempExecStream.close(); };
hwiExecutable = tempExec; hwiExecutable = tempExec;
} }
@ -305,6 +319,14 @@ public class Hwi {
return hwiExecutable; return hwiExecutable;
} }
private File getHwiHomeDir() {
if(Platform.getCurrent() == Platform.OSX) {
return new File(Storage.getSparrowHome(), HWI_HOME_DIR);
}
return new File(System.getProperty("java.io.tmpdir"));
}
private boolean testHwi(File hwiExecutable) { private boolean testHwi(File hwiExecutable) {
try { try {
List<String> command = List.of(hwiExecutable.getAbsolutePath(), "--version"); List<String> command = List.of(hwiExecutable.getAbsolutePath(), "--version");