|
@ -19,6 +19,8 @@ import java.nio.file.attribute.PosixFilePermissions; |
|
|
import java.util.Arrays; |
|
|
import java.util.Arrays; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Set; |
|
|
import java.util.Set; |
|
|
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
|
|
import java.util.zip.ZipInputStream; |
|
|
|
|
|
|
|
|
public class Hwi { |
|
|
public class Hwi { |
|
|
private static File hwiExecutable; |
|
|
private static File hwiExecutable; |
|
@ -85,27 +87,79 @@ public class Hwi { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private synchronized File getHwiExecutable() throws IOException { |
|
|
private synchronized File getHwiExecutable() throws IOException { |
|
|
if(hwiExecutable == null) { |
|
|
try { |
|
|
Platform platform = Platform.getCurrent(); |
|
|
if (hwiExecutable == null) { |
|
|
System.out.println("/external/" + platform.getPlatformId().toLowerCase() + "/hwi"); |
|
|
Platform platform = Platform.getCurrent(); |
|
|
InputStream inputStream = Hwi.class.getResourceAsStream("/external/" + platform.getPlatformId().toLowerCase() + "/hwi"); |
|
|
Set<PosixFilePermission> ownerExecutableWritable = PosixFilePermissions.fromString("rwxr--r--"); |
|
|
Set<PosixFilePermission> ownerExecutableWritable = PosixFilePermissions.fromString("rwxr--r--"); |
|
|
|
|
|
Path tempExecPath = Files.createTempFile("hwi", null, PosixFilePermissions.asFileAttribute(ownerExecutableWritable)); |
|
|
//A PyInstaller --onefile expands into a new directory on every run triggering OSX Gatekeeper checks.
|
|
|
File tempExec = tempExecPath.toFile(); |
|
|
//To avoid doing these with every invocation, use a --onedir packaging and expand into a temp folder on OSX
|
|
|
System.out.println(tempExec.getAbsolutePath()); |
|
|
//The check will still happen on first invocation, but will not thereafter
|
|
|
tempExec.deleteOnExit(); |
|
|
//See https://github.com/bitcoin-core/HWI/issues/327 for details
|
|
|
OutputStream tempExecStream = new BufferedOutputStream(new FileOutputStream(tempExec)); |
|
|
if(platform.getPlatformId().toLowerCase().equals("mac")) { |
|
|
ByteStreams.copy(new FramedLZ4CompressorInputStream(inputStream), tempExecStream); |
|
|
InputStream inputStream = Hwi.class.getResourceAsStream("/external/" + platform.getPlatformId().toLowerCase() + "/hwi-1.1.0-mac-amd64-signed.zip"); |
|
|
inputStream.close(); |
|
|
Path tempHwiDirPath = Files.createTempDirectory("hwi", PosixFilePermissions.asFileAttribute(ownerExecutableWritable)); |
|
|
tempExecStream.flush(); |
|
|
File tempHwiDir = tempHwiDirPath.toFile(); |
|
|
tempExecStream.close(); |
|
|
tempHwiDir.deleteOnExit(); |
|
|
|
|
|
|
|
|
hwiExecutable = tempExec; |
|
|
System.out.println(tempHwiDir.getAbsolutePath()); |
|
|
|
|
|
|
|
|
|
|
|
File tempExec = null; |
|
|
|
|
|
ZipInputStream zis = new ZipInputStream(inputStream); |
|
|
|
|
|
ZipEntry zipEntry = zis.getNextEntry(); |
|
|
|
|
|
while (zipEntry != null) { |
|
|
|
|
|
File newFile = newFile(tempHwiDir, zipEntry, ownerExecutableWritable); |
|
|
|
|
|
newFile.deleteOnExit(); |
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(newFile); |
|
|
|
|
|
ByteStreams.copy(zis, new FileOutputStream(newFile)); |
|
|
|
|
|
fos.flush(); |
|
|
|
|
|
fos.close(); |
|
|
|
|
|
|
|
|
|
|
|
if (zipEntry.getName().equals("hwi")) { |
|
|
|
|
|
tempExec = newFile; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
zipEntry = zis.getNextEntry(); |
|
|
|
|
|
} |
|
|
|
|
|
zis.closeEntry(); |
|
|
|
|
|
zis.close(); |
|
|
|
|
|
|
|
|
|
|
|
hwiExecutable = tempExec; |
|
|
|
|
|
} else { |
|
|
|
|
|
InputStream inputStream = Hwi.class.getResourceAsStream("/external/" + platform.getPlatformId().toLowerCase() + "/hwi"); |
|
|
|
|
|
Path tempExecPath = Files.createTempFile("hwi", null, PosixFilePermissions.asFileAttribute(ownerExecutableWritable)); |
|
|
|
|
|
File tempExec = tempExecPath.toFile(); |
|
|
|
|
|
tempExec.deleteOnExit(); |
|
|
|
|
|
OutputStream tempExecStream = new BufferedOutputStream(new FileOutputStream(tempExec)); |
|
|
|
|
|
ByteStreams.copy(new FramedLZ4CompressorInputStream(inputStream), tempExecStream); |
|
|
|
|
|
inputStream.close(); |
|
|
|
|
|
tempExecStream.flush(); |
|
|
|
|
|
tempExecStream.close(); |
|
|
|
|
|
|
|
|
|
|
|
hwiExecutable = tempExec; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} catch(Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return hwiExecutable; |
|
|
return hwiExecutable; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static File newFile(File destinationDir, ZipEntry zipEntry, Set<PosixFilePermission> setFilePermissions) throws IOException { |
|
|
|
|
|
String destDirPath = destinationDir.getCanonicalPath(); |
|
|
|
|
|
|
|
|
|
|
|
Path path = Path.of(destDirPath, zipEntry.getName()); |
|
|
|
|
|
File destFile = Files.createFile(path, PosixFilePermissions.asFileAttribute(setFilePermissions)).toFile(); |
|
|
|
|
|
|
|
|
|
|
|
String destFilePath = destFile.getCanonicalPath(); |
|
|
|
|
|
if (!destFilePath.startsWith(destDirPath + File.separator)) { |
|
|
|
|
|
throw new IOException("Entry is outside of the target dir: " + zipEntry.getName()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return destFile; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private boolean wasSuccessful(String output) throws ImportException { |
|
|
private boolean wasSuccessful(String output) throws ImportException { |
|
|
JsonObject result = JsonParser.parseString(output).getAsJsonObject(); |
|
|
JsonObject result = JsonParser.parseString(output).getAsJsonObject(); |
|
|
if(result.get("error") != null) { |
|
|
if(result.get("error") != null) { |
|
|