Skip to content

Commit

Permalink
Merge pull request #617 from HenrikJannsen/fix_wallet_init_for_osx
Browse files Browse the repository at this point in the history
Fix wallet init for osx
  • Loading branch information
alvasw authored Dec 30, 2022
2 parents e14c4fa + 996178d commit e5c16e6
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ public DefaultApplicationService(String[] args) {
public CompletableFuture<Boolean> initialize() {
return securityService.initialize()
.thenCompose(result -> walletService.initialize())
.whenComplete((r, t) -> setState(State.START_NETWORK))
.whenComplete((result, throwable) -> {
if (throwable == null) {
setState(State.START_NETWORK);
} else {
log.error("Error at walletService.initialize", throwable);
}
})
.thenCompose(result -> networkService.initialize())
.whenComplete((r, t) -> setState(State.NETWORK_STARTED))
.thenCompose(result -> identityService.initialize())
Expand Down
2 changes: 1 addition & 1 deletion application/src/main/resources/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ application {
}

wallet = {
enabled = false
enabled = true
}
}
29 changes: 16 additions & 13 deletions common/src/main/java/bisq/common/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,36 @@ public static void deleteOnExit(File file) {
/**
* @param file The file to delete and to get removed from the `DeleteOnExitHook`.
*/
public static void releaseTempFile(File file) {
public static void releaseTempFile(File file) throws IOException {
if (!DeleteOnExitHook.isShutdownInProgress()) {
DeleteOnExitHook.remove(file.getPath());
}
deleteFile(file);
}

public static void deleteDirectory(String dirPath) {
deleteDirectory(new File(dirPath));
public static void deleteFileOrDirectory(String dirPath) throws IOException {
deleteFileOrDirectory(new File(dirPath));
}

public static void deleteDirectory(File dir) {
public static void deleteFileOrDirectory(Path path) throws IOException {
deleteFileOrDirectory(path.toFile());
}

public static void deleteFileOrDirectory(File dir) throws IOException {
File[] files = dir.listFiles();
if (files != null) {
for (final File file : files) {
deleteDirectory(file);
for (File file : files) {
deleteFileOrDirectory(file);
}
}
//noinspection ResultOfMethodCallIgnored
dir.delete();
if (dir.exists()) {
Files.delete(dir.toPath());
}
}

public static void deleteFile(File file) {
if (file != null && file.exists()) {
if (!file.delete()) {
log.error("Cannot delete file {}", file);
}
public static void deleteFile(File file) throws IOException {
if (file.exists()) {
Files.delete(file.toPath());
}
}

Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/bisq/common/util/OsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -96,6 +97,15 @@ public static String getOSName() {
return System.getProperty("os.name").toLowerCase(Locale.US);
}

public static void makeBinaryExecutable(Path binaryPath) {
boolean isSuccess = binaryPath.toFile().setExecutable(true);
if (!isSuccess) {
throw new IllegalStateException(
String.format("Couldn't make `%s` executable.", binaryPath)
);
}
}

public static boolean open(File file) {
return open(file.getPath());
}
Expand Down
8 changes: 7 additions & 1 deletion network/tor/src/test/java/bisq/TorIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -39,7 +40,12 @@ public class TorIntegrationTest {
public void testShutdownDuringStartup() {
String torDirPath = OsUtils.getUserDataDir() + "/TorifyIntegrationTest";
File versionFile = new File(torDirPath + "/" + Constants.VERSION);
FileUtils.deleteDirectory(new File(torDirPath));
try {
FileUtils.deleteFileOrDirectory(torDirPath);
} catch (IOException e) {
log.error("Could not delete " + torDirPath, e);
throw new RuntimeException(e);
}
assertFalse(versionFile.exists());
Tor tor = Tor.getTor(torDirPath);
new Thread(() -> {
Expand Down
7 changes: 6 additions & 1 deletion network/tor/src/test/java/bisq/tor/AbstractTorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public static void cleanTorInstallDir(String torDirPathSpec) {
File torDir = new File(torDirPathSpec);
if (torDir.exists()) {
log.info("Cleaning tor install dir {}", torDirPathSpec);
FileUtils.deleteDirectory(torDir);
try {
FileUtils.deleteFileOrDirectory(torDir);
} catch (IOException e) {
log.error("Could not delete " + torDir, e);
throw new RuntimeException(e);
}
}
File versionFile = new File(torDir, VERSION);
assertFalse(versionFile.exists());
Expand Down
6 changes: 5 additions & 1 deletion persistence/src/main/java/bisq/persistence/Persistence.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ public boolean persist(T persistableStore) {
log.error("Error closing stream " + ioe.getMessage(), ioe); // swallow
}
if (tempFile != null) {
FileUtils.releaseTempFile(tempFile);
try {
FileUtils.releaseTempFile(tempFile);
} catch (IOException e) {
log.error("Could not delete " + tempFile, e);
}
}
}
return success;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import bisq.common.util.FileUtils;
import bisq.common.util.OsUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -31,6 +32,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@Slf4j
public class ElectrumBinaryExtractor {

public static final String LINUX_BINARY_SUFFIX = "AppImage";
Expand Down Expand Up @@ -113,9 +115,11 @@ private File extractFileWithSuffixFromStream(InputStream inputStream, String fil
private void deleteElectrumAppFileIfExisting() {
File electrumAppInDataDir = new File(destDir, "Electrum.app");
if (electrumAppInDataDir.exists()) {
boolean isSuccess = electrumAppInDataDir.delete();
if (!isSuccess) {
throw new IllegalStateException("Couldn't delete old Electrum.app");
try {
FileUtils.deleteFileOrDirectory(electrumAppInDataDir);
} catch (IOException e) {
log.error("Could not delete " + electrumAppInDataDir, e);
throw new IllegalStateException("Couldn't delete old Electrum.app", e);
}
}
}
Expand All @@ -126,19 +130,19 @@ private void copyZipFromResourcesToDataDir(InputStream inputStream) {
Files.copy(inputStream, zipFileInDataDir.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new ElectrumExtractionFailedException(
"Couldn't copy Electrum binaries zip file from resources to the data directory."
"Couldn't copy Electrum binaries zip file from resources to the data directory.", e
);
}
}

private void unpackZipFileWithUnzipCommand() {
try {
Process extractProcess = new ProcessBuilder("unzip", ARCHIVE_FILENAME)
Process extractProcess = new ProcessBuilder("unzip", "-o", ARCHIVE_FILENAME)
.directory(destDir)
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectError(ProcessBuilder.Redirect.DISCARD)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start();
boolean isSuccess = extractProcess.waitFor(1, TimeUnit.MINUTES);
boolean isSuccess = extractProcess.waitFor(30, TimeUnit.SECONDS);
if (!isSuccess) {
throw new ElectrumExtractionFailedException("Could not copy Electrum.app to data directory.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public ElectrumProcess(Path electrumRootDataDir, ElectrumProcessConfig processCo
@Override
public void start() {
unpackArchive();
if (isRunningOnLinux()) {
makeBinaryExecutable();
if (OsUtils.isLinux()) {
OsUtils.makeBinaryExecutable(binaryPath.orElseThrow());
}
createAndStartProcess();
}
Expand All @@ -70,21 +70,6 @@ private void unpackArchive() {
binaryPath = Optional.of(extractedFilePath);
}

private boolean isRunningOnLinux() {
String osName = System.getProperty("os.name");
return osName.equals("Linux");
}

private void makeBinaryExecutable() {
Path binaryPath = this.binaryPath.orElseThrow();
boolean isSuccess = binaryPath.toFile().setExecutable(true);
if (!isSuccess) {
throw new IllegalStateException(
String.format("Couldn't make `%s` executable.", binaryPath)
);
}
}

private void createAndStartProcess() {
Path path = binaryPath.orElseThrow();
var process = new ElectrumRegtestProcess(path, processConfig);
Expand Down

0 comments on commit e5c16e6

Please sign in to comment.