Obtain a directory suitable for writing JNA-specific temporary files. Override with jna.tmpdir
()
| 1386 | Override with <code>jna.tmpdir</code> |
| 1387 | */ |
| 1388 | static File getTempDir() throws IOException { |
| 1389 | File jnatmp; |
| 1390 | String prop = System.getProperty("jna.tmpdir"); |
| 1391 | if (prop != null) { |
| 1392 | jnatmp = new File(prop); |
| 1393 | jnatmp.mkdirs(); |
| 1394 | } |
| 1395 | else { |
| 1396 | File tmp = new File(System.getProperty("java.io.tmpdir")); |
| 1397 | if(Platform.isMac()) { |
| 1398 | // https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html |
| 1399 | jnatmp = new File(System.getProperty("user.home"), "Library/Caches/JNA/temp"); |
| 1400 | } else if (Platform.isLinux() || Platform.isSolaris() || Platform.isAIX() || Platform.isDragonFlyBSD() || Platform.isFreeBSD() || Platform.isNetBSD() || Platform.isOpenBSD() || Platform.iskFreeBSD()) { |
| 1401 | // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 1402 | // The XDG_CACHE_DIR is expected to be per user |
| 1403 | String xdgCacheEnvironment = System.getenv("XDG_CACHE_HOME"); |
| 1404 | File xdgCacheFile; |
| 1405 | if(xdgCacheEnvironment == null || xdgCacheEnvironment.trim().isEmpty()) { |
| 1406 | xdgCacheFile = new File(System.getProperty("user.home"), ".cache"); |
| 1407 | } else { |
| 1408 | xdgCacheFile = new File(xdgCacheEnvironment); |
| 1409 | } |
| 1410 | jnatmp = new File(xdgCacheFile, "JNA/temp"); |
| 1411 | } else { |
| 1412 | // Loading DLLs via System.load() under a directory with a unicode |
| 1413 | // name will fail on windows, so use a hash code of the user's |
| 1414 | // name in case the user's name contains non-ASCII characters |
| 1415 | jnatmp = new File(tmp, "jna-" + System.getProperty("user.name").hashCode()); |
| 1416 | } |
| 1417 | |
| 1418 | jnatmp.mkdirs(); |
| 1419 | if (!jnatmp.exists() || !jnatmp.canWrite()) { |
| 1420 | jnatmp = tmp; |
| 1421 | } |
| 1422 | } |
| 1423 | if (!jnatmp.exists()) { |
| 1424 | throw new IOException("JNA temporary directory '" + jnatmp + "' does not exist"); |
| 1425 | } |
| 1426 | if (!jnatmp.canWrite()) { |
| 1427 | throw new IOException("JNA temporary directory '" + jnatmp + "' is not writable"); |
| 1428 | } |
| 1429 | return jnatmp; |
| 1430 | } |
| 1431 | |
| 1432 | /** Remove all marked temporary files in the given directory. */ |
| 1433 | static void removeTemporaryFiles() throws IOException { |