Skip to content

Commit

Permalink
[java-native-access#985] Improve handling of dynamicaly extracted nat…
Browse files Browse the repository at this point in the history
…ive library.

On Mac OS X systems `~/Library/Application Support/JNA/temp` and on
other Unix like systems `$XDG_CACHE_DIR/JNA/temp` (Default value is:
`~/.cache/JNA/temp`) is used.

Behaviour on windows is unchanged, as since Vista the tempdir is by
default inside the user profile directory.

Closes: java-native-access#985
  • Loading branch information
matthiasblaesing committed Sep 15, 2018
1 parent 62388df commit c60ff92
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Features
* [#1007](https://github.com/java-native-access/jna/pull/1007): Added OSGi export of Solaris package - [@swimmesberger](https://github.com/swimmesberger).
* [#1003](https://github.com/java-native-access/jna/pull/1003): Allow `NativeMapped` to be used with enums - [@koraktor](https://github.com/koraktor).
* [#994](https://github.com/java-native-access/jna/issues/994): Added `CoInitializeSecurity` and `CoSetProxyBlanket` to `c.s.j.platform.win32.Ole32`, added new `c.s.j.platform.win32.Wbemcli` classes needed to query WMI, and added a `WbemcliUtil` class implementing WMI queries. - [@dbwiddis](https://github.com/dbwiddis).
* [#985](https://github.com/java-native-access/jna/issues/985): Improve handling of dynamicaly extracted native library. On Mac OS X systems `~/Library/Application Support/JNA/temp` and on other Unix like systems `$XDG_CACHE_DIR/JNA/temp` (Default value is: `~/.cache/JNA/temp`) is used - [@matthiasblaesing](https://github.com/matthiasblaesing).

Bug Fixes
---------
Expand Down
25 changes: 21 additions & 4 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,9 @@ else if (!Boolean.getBoolean("jna.nounpack")) {
if (!Boolean.getBoolean("jnidispatch.preserve")) {
lib.deleteOnExit();
}
if (DEBUG) {
System.out.println("Extracting library to " + lib.getAbsolutePath());
}
fos = new FileOutputStream(lib);
int count;
byte[] buf = new byte[1024];
Expand Down Expand Up @@ -1288,10 +1291,24 @@ static File getTempDir() throws IOException {
}
else {
File tmp = new File(System.getProperty("java.io.tmpdir"));
// Loading DLLs via System.load() under a directory with a unicode
// name will fail on windows, so use a hash code of the user's
// name in case the user's name contains non-ASCII characters
jnatmp = new File(tmp, "jna-" + System.getProperty("user.name").hashCode());
if(Platform.isMac()) {
jnatmp = new File(System.getProperty("user.home"), "Library/Application Support/JNA/temp");
} else if (Platform.isLinux() || Platform.isSolaris() || Platform.isAIX() || Platform.isFreeBSD() || Platform.isNetBSD() || Platform.isOpenBSD() || Platform.iskFreeBSD()) {
String xdgCacheEnvironment = System.getenv("XDG_CACHE_HOME");
File xdgCacheFile;
if(xdgCacheEnvironment == null) {
xdgCacheFile = new File(System.getProperty("user.home"), ".cache");
} else {
xdgCacheFile = new File(xdgCacheEnvironment);
}
jnatmp = new File(xdgCacheFile, "JNA/temp");
} else {
// Loading DLLs via System.load() under a directory with a unicode
// name will fail on windows, so use a hash code of the user's
// name in case the user's name contains non-ASCII characters
jnatmp = new File(tmp, "jna-" + System.getProperty("user.name").hashCode());
}

jnatmp.mkdirs();
if (!jnatmp.exists() || !jnatmp.canWrite()) {
jnatmp = tmp;
Expand Down
2 changes: 1 addition & 1 deletion test/com/sun/jna/JNALoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testLoadAndUnloadFromJar() throws Exception {
String path = (String)field.get(null);
assertNotNull("Native library path unavailable", path);
assertTrue("Native library not unpacked from jar: " + path,
path.startsWith(System.getProperty("java.io.tmpdir")));
path.startsWith(Native.getTempDir().getAbsolutePath()));

Reference<Class<?>> ref = new WeakReference<Class<?>>(cls);
Reference<ClassLoader> clref = new WeakReference<ClassLoader>(loader);
Expand Down

0 comments on commit c60ff92

Please sign in to comment.