mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-09-03 03:01:13 +12:00
Session fixes
This commit is contained in:
parent
f0eccfb17b
commit
70568f7a9b
4 changed files with 67 additions and 18 deletions
|
@ -27,6 +27,10 @@ public abstract class LocalProcessControlProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ShellProcessControl create() {
|
public static ShellProcessControl create() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
throw new IllegalStateException("Not initialized");
|
||||||
|
}
|
||||||
|
|
||||||
return INSTANCE.createProcessControl();
|
return INSTANCE.createProcessControl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
core/src/main/java/io/xpipe/core/util/UuidHelper.java
Normal file
26
core/src/main/java/io/xpipe/core/util/UuidHelper.java
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package io.xpipe.core.util;
|
||||||
|
|
||||||
|
import io.xpipe.core.dialog.Dialog;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class UuidHelper {
|
||||||
|
|
||||||
|
public static Optional<UUID> parse(String s) {
|
||||||
|
try {
|
||||||
|
return Optional.of(UUID.fromString(s));
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Optional<UUID> parse(Dialog.FailableSupplier<String> supplier) {
|
||||||
|
try {
|
||||||
|
var s = supplier.get();
|
||||||
|
return Optional.of(UUID.fromString(s));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,21 +30,37 @@ public class XPipeSession {
|
||||||
|
|
||||||
private static XPipeSession INSTANCE;
|
private static XPipeSession INSTANCE;
|
||||||
|
|
||||||
public static void init(UUID buildSessionId) throws Exception {
|
public static void init(UUID buildSessionId) {
|
||||||
var sessionFile = XPipeTempDirectory.getLocal().resolve("xpipe_session");
|
if (INSTANCE != null) {
|
||||||
var isNew = !Files.exists(sessionFile);
|
return;
|
||||||
var systemSessionId = isNew ? UUID.randomUUID() : UUID.fromString(Files.readString(sessionFile));
|
}
|
||||||
|
|
||||||
|
var sessionFile = XPipeTempDirectory.getLocal().resolve("xpipe_session");
|
||||||
|
var isNewSystemSession = !Files.exists(sessionFile);
|
||||||
|
var systemSessionId = isNewSystemSession
|
||||||
|
? UUID.randomUUID()
|
||||||
|
: UuidHelper.parse(() -> Files.readString(sessionFile)).orElse(UUID.randomUUID());
|
||||||
|
|
||||||
|
try {
|
||||||
if (OsType.getLocal().equals(OsType.WINDOWS)) {
|
if (OsType.getLocal().equals(OsType.WINDOWS)) {
|
||||||
var pf = Path.of("C:\\pagefile.sys");
|
var pf = Path.of("C:\\pagefile.sys");
|
||||||
BasicFileAttributes attr = Files.readAttributes(pf, BasicFileAttributes.class);
|
BasicFileAttributes attr = Files.readAttributes(pf, BasicFileAttributes.class);
|
||||||
var timeUuid = UUID.nameUUIDFromBytes(attr.creationTime().toInstant().toString().getBytes());
|
var timeUuid = UUID.nameUUIDFromBytes(
|
||||||
isNew = isNew && timeUuid.equals(systemSessionId);
|
attr.creationTime().toInstant().toString().getBytes());
|
||||||
|
isNewSystemSession = isNewSystemSession && timeUuid.equals(systemSessionId);
|
||||||
systemSessionId = timeUuid;
|
systemSessionId = timeUuid;
|
||||||
}
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
isNewSystemSession = true;
|
||||||
|
systemSessionId = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
Files.writeString(sessionFile, systemSessionId.toString());
|
Files.writeString(sessionFile, systemSessionId.toString());
|
||||||
INSTANCE = new XPipeSession(isNew, UUID.randomUUID(), buildSessionId, systemSessionId);
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
INSTANCE = new XPipeSession(isNewSystemSession, UUID.randomUUID(), buildSessionId, systemSessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static XPipeSession get() {
|
public static XPipeSession get() {
|
||||||
|
|
|
@ -3,16 +3,19 @@ package io.xpipe.core.util;
|
||||||
import io.xpipe.core.impl.FileNames;
|
import io.xpipe.core.impl.FileNames;
|
||||||
import io.xpipe.core.process.OsType;
|
import io.xpipe.core.process.OsType;
|
||||||
import io.xpipe.core.process.ShellProcessControl;
|
import io.xpipe.core.process.ShellProcessControl;
|
||||||
import io.xpipe.core.store.ShellStore;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class XPipeTempDirectory {
|
public class XPipeTempDirectory {
|
||||||
|
|
||||||
public static Path getLocal() throws Exception {
|
public static Path getLocal() {
|
||||||
try (var pc = ShellStore.local().create().start()) {
|
if (OsType.getLocal().equals(OsType.WINDOWS)) {
|
||||||
return Path.of(get(pc));
|
return Path.of(System.getenv("TEMP")).resolve("xpipe");
|
||||||
|
} else if (OsType.getLocal().equals(OsType.LINUX)) {
|
||||||
|
return Path.of("/tmp/xpipe");
|
||||||
|
} else {
|
||||||
|
return Path.of(System.getenv("TMPDIR"), "xpipe");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +27,7 @@ public class XPipeTempDirectory {
|
||||||
proc.executeSimpleCommand(proc.getShellType().flatten(proc.getShellType().getMkdirsCommand(dir)), "Unable to access or create temporary directory " + dir);
|
proc.executeSimpleCommand(proc.getShellType().flatten(proc.getShellType().getMkdirsCommand(dir)), "Unable to access or create temporary directory " + dir);
|
||||||
|
|
||||||
if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MAC)) {
|
if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MAC)) {
|
||||||
proc.executeSimpleCommand("(chmod -f 777 \"" + dir + "\"");
|
proc.executeSimpleCommand("chmod -f 777 \"" + dir + "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue