Skip to content

Commit

Permalink
Create temp dir unique to user
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Sep 4, 2024
1 parent ee34393 commit 3e5f77a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/java/org/scalasbt/ipcsocket/NativeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ class NativeLoader {
private static final String pid =
isWindows ? "" : ManagementFactory.getRuntimeMXBean().getName().replaceAll("@.*", "");

private static Path tmpDir() {
private static Path runtimeDir() {
String prop = System.getProperty("sbt.ipcsocket.tmpdir");
String tmp = System.getProperty("java.io.tmpdir");
if (prop != null) {
return Paths.get(prop);
} else {
return Paths.get(tmp).resolve(".sbt").resolve("ipcsocket");
String runtimeDir = System.getenv("XDG_RUNTIME_DIR");
if (runtimeDir == null) {
runtimeDir = System.getProperty("java.io.tmpdir");
}
String home = System.getProperty("user.home");
if (home == null) {
home = "unknown_home";
}
return Paths.get(runtimeDir)
.resolve(".sbt" + Integer.toString(home.hashCode()))
.resolve("ipcsocket");
}
}

Expand Down Expand Up @@ -72,7 +81,7 @@ static void load() throws UnsatisfiedLinkError {
final URL url = NativeLoader.class.getClassLoader().getResource(resource);
if (url == null) throw new UnsatisfiedLinkError(resource + " not found on classpath");
try {
final Path base = Files.createDirectories(tmpDir());
final Path base = Files.createDirectories(runtimeDir());
final Path output = Files.createTempFile(base, tempFilePrefix, extension);
try (final InputStream in = url.openStream();
final FileChannel channel = FileChannel.open(output, StandardOpenOption.WRITE)) {
Expand Down Expand Up @@ -131,7 +140,7 @@ private static class CleanupRunnable implements Runnable {
public void run() {
try {
Files.walkFileTree(
tmpDir(),
runtimeDir(),
new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(
Expand Down

0 comments on commit 3e5f77a

Please sign in to comment.