Skip to content

Commit 9453252

Browse files
committed
[GR-47631] Using XDG_CACHE_HOME on Linux.
PullRequest: graal/15116
2 parents ae07ef6 + b5fd03c commit 9453252

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InternalResourceCache.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@
5050

5151
import java.io.IOError;
5252
import java.io.IOException;
53+
import java.io.PrintStream;
5354
import java.lang.reflect.Constructor;
5455
import java.nio.file.DirectoryStream;
5556
import java.nio.file.FileAlreadyExistsException;
5657
import java.nio.file.Files;
58+
import java.nio.file.InvalidPathException;
5759
import java.nio.file.Path;
5860
import java.nio.file.Paths;
5961
import java.nio.file.StandardCopyOption;
@@ -246,7 +248,28 @@ private static Path findCacheRootOnHotSpot() throws IOException {
246248
Path userHome = Paths.get(userHomeValue);
247249
Path container = switch (InternalResource.OS.getCurrent()) {
248250
case DARWIN -> userHome.resolve(Path.of("Library", "Caches"));
249-
case LINUX -> userHome.resolve(".cache");
251+
case LINUX -> {
252+
Path userCacheDir = null;
253+
String xdgCacheValue = System.getenv("XDG_CACHE_HOME");
254+
if (xdgCacheValue != null) {
255+
try {
256+
Path xdgCacheDir = Path.of(xdgCacheValue);
257+
// Do not fail when XDG_CACHE_HOME value is invalid. Fall back to
258+
// $HOME/.cache.
259+
if (xdgCacheDir.isAbsolute()) {
260+
userCacheDir = xdgCacheDir;
261+
} else {
262+
emitWarning("The value of the environment variable 'XDG_CACHE_HOME' is not an absolute path. Using the default cache folder '%s'.", userHome.resolve(".cache"));
263+
}
264+
} catch (InvalidPathException notPath) {
265+
emitWarning("The value of the environment variable 'XDG_CACHE_HOME' is not a valid path. Using the default cache folder '%s'.", userHome.resolve(".cache"));
266+
}
267+
}
268+
if (userCacheDir == null) {
269+
userCacheDir = userHome.resolve(".cache");
270+
}
271+
yield userCacheDir;
272+
}
250273
case WINDOWS -> userHome.resolve(Path.of("AppData", "Local"));
251274
};
252275
Path cache = container.resolve("org.graalvm.polyglot");
@@ -257,6 +280,11 @@ private static Path findCacheRootOnHotSpot() throws IOException {
257280
return res.getLeft();
258281
}
259282

283+
private static void emitWarning(String message, Object... args) {
284+
PrintStream out = System.err;
285+
out.printf(message + "%n", args);
286+
}
287+
260288
private static Path findCacheRootOnNativeImage() {
261289
Pair<Path, Boolean> res = cacheRoot;
262290
if (res == null) {

0 commit comments

Comments
 (0)