Skip to content

Commit

Permalink
avoid saving lock file link for default instance if environment varia…
Browse files Browse the repository at this point in the history
…ble is set
  • Loading branch information
craigraw committed Mar 28, 2024
1 parent 0fad935 commit f0bfc44
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/sparrowwallet/sparrow/instance/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
Expand All @@ -23,6 +24,7 @@

public abstract class Instance {
private static final Logger log = LoggerFactory.getLogger(Instance.class);
private static final String LINK_ENV_PROPERTY = "SPARROW_NO_LOCK_FILE_LINK";

public final String applicationId;
private final boolean autoExit;
Expand Down Expand Up @@ -64,7 +66,7 @@ private void startServer(Path lockFile) throws InstanceException {
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
lockFile.toFile().deleteOnExit();
} catch(Exception e) {
throw new InstanceException("Could not open UNIX socket at " + lockFile.toAbsolutePath(), e);
throw new InstanceException("Could not open UNIX socket lock file for instance at " + lockFile.toAbsolutePath(), e);
}

Thread thread = new Thread(() -> {
Expand Down Expand Up @@ -121,7 +123,7 @@ private void doClient(Path lockFile) throws InstanceException {
try {
Files.deleteIfExists(lockFile);
startServer(lockFile);
} catch(IOException ex) {
} catch(Exception ex) {
throw new InstanceException("Could not delete lock file from previous instance", e);
}
} catch(Exception e) {
Expand Down Expand Up @@ -187,8 +189,17 @@ private void createSymlink(Path lockFile) {
}

private Path getUserLockFilePointer() {
if(Boolean.parseBoolean(System.getenv(LINK_ENV_PROPERTY))) {
return null;
}

try {
return Storage.getSparrowDir(true).toPath().resolve(applicationId + ".default");
File sparrowHome = Storage.getSparrowHome(true);
if(!sparrowHome.exists()) {
Storage.createOwnerOnlyDirectory(sparrowHome);
}

return sparrowHome.toPath().resolve(applicationId + ".default");
} catch(Exception e) {
return null;
}
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/sparrowwallet/sparrow/io/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,11 @@ static File getCertsDir() {
}

public static File getSparrowDir() {
return getSparrowDir(false);
}

public static File getSparrowDir(boolean useDefault) {
File sparrowDir;
if(Network.get() != Network.MAINNET) {
sparrowDir = new File(getSparrowHome(useDefault), Network.get().getName());
sparrowDir = new File(getSparrowHome(), Network.get().getName());
} else {
sparrowDir = getSparrowHome(useDefault);
sparrowDir = getSparrowHome();
}

if(!sparrowDir.exists()) {
Expand Down

0 comments on commit f0bfc44

Please sign in to comment.