From 243d5582ed2fa00ea771e3dd9afd7c4c4247ce45 Mon Sep 17 00:00:00 2001 From: crschnick Date: Fri, 19 Dec 2025 04:38:40 +0000 Subject: [PATCH] Rework temp file creation --- .../java/io/xpipe/app/core/AppSystemInfo.java | 32 +++++++++++++++++++ .../io/xpipe/app/process/ScriptHelper.java | 7 ++-- .../java/io/xpipe/app/process/ShellTemp.java | 7 ++-- .../java/io/xpipe/app/process/ShellView.java | 18 ++--------- .../xpipe/app/terminal/TerminalLauncher.java | 2 +- .../base/identity/ssh/InPlaceKeyStrategy.java | 3 +- 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/core/AppSystemInfo.java b/app/src/main/java/io/xpipe/app/core/AppSystemInfo.java index 312957579..51f6ca549 100644 --- a/app/src/main/java/io/xpipe/app/core/AppSystemInfo.java +++ b/app/src/main/java/io/xpipe/app/core/AppSystemInfo.java @@ -69,6 +69,8 @@ public abstract class AppSystemInfo { public abstract Path getTemp(); + public abstract String getUser(); + public static final class Windows extends AppSystemInfo { private Path userHome; @@ -114,6 +116,18 @@ public abstract class AppSystemInfo { return (temp = dir); } + @Override + public String getUser() { + var username = System.getenv("USERNAME"); + if (username == null) { + username = System.getProperty("user.name"); + } + if (username == null) { + username = "User"; + } + return username; + } + public Path getProgramFiles() { var env = AppSystemInfo.parsePath(System.getenv("ProgramFiles")); if (env != null) { @@ -262,6 +276,15 @@ public abstract class AppSystemInfo { return vm; } + @Override + public String getUser() { + var username = System.getProperty("user.name"); + if (username == null) { + username = "user"; + } + return username; + } + @Override public Path getUserHome() { return Path.of(System.getProperty("user.home")); @@ -313,6 +336,15 @@ public abstract class AppSystemInfo { public static class MacOs extends AppSystemInfo { + @Override + public String getUser() { + var username = System.getProperty("user.name"); + if (username == null) { + username = "user"; + } + return username; + } + @Override public Path getUserHome() { return Path.of(System.getProperty("user.home")); diff --git a/app/src/main/java/io/xpipe/app/process/ScriptHelper.java b/app/src/main/java/io/xpipe/app/process/ScriptHelper.java index ccd5b0618..4ad55ba90 100644 --- a/app/src/main/java/io/xpipe/app/process/ScriptHelper.java +++ b/app/src/main/java/io/xpipe/app/process/ScriptHelper.java @@ -8,12 +8,13 @@ import io.xpipe.core.SecretValue; import lombok.SneakyThrows; import java.util.List; +import java.util.Objects; import java.util.Random; public class ScriptHelper { - public static int getScriptHash(String content) { - return Math.abs(content.hashCode()); + public static int getScriptHash(ShellControl sc, String content) throws Exception { + return Math.abs(Objects.hash(content, sc.view().user())); } @SneakyThrows @@ -31,7 +32,7 @@ public class ScriptHelper { @SneakyThrows public static FilePath createExecScript(ShellDialect type, ShellControl processControl, String content) { content = type.prepareScriptContent(processControl, content); - var fileName = "xpipe-" + getScriptHash(content); + var fileName = "xpipe-" + getScriptHash(processControl, content); var temp = processControl.getSystemTemporaryDirectory(); var file = temp.join(fileName + "." + type.getScriptFileEnding()); return createExecScriptRaw(processControl, file, content); diff --git a/app/src/main/java/io/xpipe/app/process/ShellTemp.java b/app/src/main/java/io/xpipe/app/process/ShellTemp.java index cf75c0967..bb14eb1ce 100644 --- a/app/src/main/java/io/xpipe/app/process/ShellTemp.java +++ b/app/src/main/java/io/xpipe/app/process/ShellTemp.java @@ -20,16 +20,15 @@ public class ShellTemp { // On Windows and macOS, we already have user specific temp directories // Even on macOS as root we will have a unique directory (in contrast to shell controls) if (OsType.ofLocal() == OsType.LINUX) { - var user = System.getenv("USER"); - temp = temp.resolve(user != null ? user : "user"); - try { - FileUtils.forceMkdir(temp.toFile()); // We did not set this in earlier versions. If we are running as a different user, it might fail Files.setPosixFilePermissions(temp, PosixFilePermissions.fromString("rwxrwxrwx")); } catch (Exception e) { ErrorEventFactory.fromThrowable(e).omit().expected().handle(); } + + var user = System.getenv("USER"); + temp = temp.resolve(user != null ? user : "user"); } return sub != null ? temp.resolve(sub) : temp; diff --git a/app/src/main/java/io/xpipe/app/process/ShellView.java b/app/src/main/java/io/xpipe/app/process/ShellView.java index 53e9e27d8..0ada76b64 100644 --- a/app/src/main/java/io/xpipe/app/process/ShellView.java +++ b/app/src/main/java/io/xpipe/app/process/ShellView.java @@ -10,10 +10,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; public class ShellView { @@ -62,19 +59,8 @@ public class ShellView { return groupFile; } - public FilePath writeRawFileDeterministic(FilePath base, byte[] data) throws Exception { - var hash = Math.abs(Arrays.hashCode(data)); - var ext = base.getExtension(); - var target = FilePath.of(base.getBaseName().toString() + "-" + hash + (ext.isPresent() ? "." + ext.get() : "")); - if (fileExists(target)) { - return target; - } - writeRawFile(target, data); - return target; - } - public FilePath writeTextFileDeterministic(FilePath base, String text) throws Exception { - var hash = Math.abs(text.hashCode()); + var hash = Math.abs(Objects.hash(text, user())); var ext = base.getExtension(); var target = FilePath.of(base.getBaseName().toString() + "-" + hash + (ext.isPresent() ? "." + ext.get() : "")); if (fileExists(target)) { diff --git a/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java b/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java index 1652b6f0e..04ac57264 100644 --- a/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java +++ b/app/src/main/java/io/xpipe/app/terminal/TerminalLauncher.java @@ -33,7 +33,7 @@ public class TerminalLauncher { boolean exit) throws Exception { var content = constructTerminalInitScript(t, processControl, workingDirectory, preInit, postInit, config, exit); - var hash = ScriptHelper.getScriptHash(content); + var hash = ScriptHelper.getScriptHash(processControl, content); var file = t.getInitFileName(processControl, hash); return ScriptHelper.createExecScriptRaw(processControl, file, content); } diff --git a/ext/base/src/main/java/io/xpipe/ext/base/identity/ssh/InPlaceKeyStrategy.java b/ext/base/src/main/java/io/xpipe/ext/base/identity/ssh/InPlaceKeyStrategy.java index 857609a3c..a0f9e06f3 100644 --- a/ext/base/src/main/java/io/xpipe/ext/base/identity/ssh/InPlaceKeyStrategy.java +++ b/ext/base/src/main/java/io/xpipe/ext/base/identity/ssh/InPlaceKeyStrategy.java @@ -32,6 +32,7 @@ import org.kordamp.ikonli.javafx.FontIcon; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; @Value @@ -174,7 +175,7 @@ public class InPlaceKeyStrategy implements SshIdentityStrategy { } private FilePath getTargetFilePath() { - var temp = AppSystemInfo.ofCurrent().getTemp().resolve("xpipe-" + Math.abs(hashCode()) + ".key"); + var temp = AppSystemInfo.ofCurrent().getTemp().resolve("xpipe-" + Math.abs(Objects.hash(this, AppSystemInfo.ofCurrent().getUser())) + ".key"); return FilePath.of(temp); } }