Rework temp file creation

This commit is contained in:
crschnick
2025-12-19 04:38:40 +00:00
parent 0214dc644d
commit 243d5582ed
6 changed files with 44 additions and 25 deletions
@@ -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"));
@@ -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);
@@ -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;
@@ -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)) {
@@ -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);
}
@@ -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);
}
}