From c1fc8712d544ca7057340615dce346531fd008ea Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 28 Mar 2024 09:12:44 +0200 Subject: [PATCH] use default sparrow home location in user dir for instance lock file pointer --- .../sparrowwallet/sparrow/SparrowWallet.java | 2 +- .../sparrow/instance/Instance.java | 28 ++++++++++++------- .../com/sparrowwallet/sparrow/io/Storage.java | 14 ++++++++-- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/SparrowWallet.java b/src/main/java/com/sparrowwallet/sparrow/SparrowWallet.java index b030de27..ff409f08 100644 --- a/src/main/java/com/sparrowwallet/sparrow/SparrowWallet.java +++ b/src/main/java/com/sparrowwallet/sparrow/SparrowWallet.java @@ -130,7 +130,7 @@ public class SparrowWallet { private final List fileUriArguments; public Instance(List fileUriArguments) { - super(SparrowWallet.APP_ID + "." + Network.get(), true); + super(SparrowWallet.APP_ID, true); this.fileUriArguments = fileUriArguments; } diff --git a/src/main/java/com/sparrowwallet/sparrow/instance/Instance.java b/src/main/java/com/sparrowwallet/sparrow/instance/Instance.java index 75da7d68..9247df6b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/instance/Instance.java +++ b/src/main/java/com/sparrowwallet/sparrow/instance/Instance.java @@ -134,17 +134,20 @@ public abstract class Instance { private Path getLockFile(boolean findExisting) { if(findExisting) { - Path pointer = getSystemLockFilePointer(); + Path pointer = getUserLockFilePointer(); try { - if(Files.exists(pointer)) { + if(pointer != null && Files.exists(pointer)) { if(Files.isSymbolicLink(pointer)) { return Files.readSymbolicLink(pointer); } else { - return Path.of(Files.readString(pointer, StandardCharsets.UTF_8)); + Path lockFile = Path.of(Files.readString(pointer, StandardCharsets.UTF_8)); + if(Files.exists(lockFile)) { + return lockFile; + } } } } catch(IOException e) { - log.warn("Could not follow symbolic link at " + pointer.toAbsolutePath()); + log.warn("Could not find lock file at " + pointer.toAbsolutePath()); } catch(Exception e) { //ignore } @@ -154,9 +157,9 @@ public abstract class Instance { } private void createSymlink(Path lockFile) { - Path pointer = getSystemLockFilePointer(); + Path pointer = getUserLockFilePointer(); try { - if(!Files.exists(pointer, LinkOption.NOFOLLOW_LINKS)) { + if(pointer != null && !Files.exists(pointer, LinkOption.NOFOLLOW_LINKS)) { Files.createSymbolicLink(pointer, lockFile); pointer.toFile().deleteOnExit(); } @@ -174,8 +177,12 @@ public abstract class Instance { } } - private Path getSystemLockFilePointer() { - return Path.of(System.getProperty("java.io.tmpdir")).resolve(applicationId + ".link"); + private Path getUserLockFilePointer() { + try { + return Storage.getSparrowDir(true).toPath().resolve(applicationId + ".default"); + } catch(Exception e) { + return null; + } } /** @@ -188,8 +195,9 @@ public abstract class Instance { if(serverChannel != null && serverChannel.isOpen()) { serverChannel.close(); } - - Files.deleteIfExists(getSystemLockFilePointer()); + if(getUserLockFilePointer() != null) { + Files.deleteIfExists(getUserLockFilePointer()); + } Files.deleteIfExists(getLockFile(false)); } catch(Exception e) { throw new InstanceException(e); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 41e4f481..223434e8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -536,11 +536,15 @@ public class Storage { } public static File getSparrowDir() { + return getSparrowDir(false); + } + + public static File getSparrowDir(boolean useDefault) { File sparrowDir; if(Network.get() != Network.MAINNET) { - sparrowDir = new File(getSparrowHome(), Network.get().getName()); + sparrowDir = new File(getSparrowHome(useDefault), Network.get().getName()); } else { - sparrowDir = getSparrowHome(); + sparrowDir = getSparrowHome(useDefault); } if(!sparrowDir.exists()) { @@ -551,7 +555,11 @@ public class Storage { } public static File getSparrowHome() { - if(System.getProperty(SparrowWallet.APP_HOME_PROPERTY) != null) { + return getSparrowHome(false); + } + + public static File getSparrowHome(boolean useDefault) { + if(!useDefault && System.getProperty(SparrowWallet.APP_HOME_PROPERTY) != null) { return new File(System.getProperty(SparrowWallet.APP_HOME_PROPERTY)); }