use default sparrow home location in user dir for instance lock file pointer

This commit is contained in:
Craig Raw 2024-03-28 09:12:44 +02:00
parent 5d674b7e91
commit c1fc8712d5
3 changed files with 30 additions and 14 deletions

View file

@ -130,7 +130,7 @@ public class SparrowWallet {
private final List<String> fileUriArguments;
public Instance(List<String> fileUriArguments) {
super(SparrowWallet.APP_ID + "." + Network.get(), true);
super(SparrowWallet.APP_ID, true);
this.fileUriArguments = fileUriArguments;
}

View file

@ -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);

View file

@ -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));
}