2121
2222import java .io .IOException ;
2323import java .io .InputStream ;
24- import java .io .OutputStream ;
25- import java .nio .file .FileAlreadyExistsException ;
2624import java .nio .file .Files ;
2725import java .nio .file .Path ;
28- import java .nio .file .Paths ;
29- import java .nio .file .StandardCopyOption ;
30- import java .security .DigestInputStream ;
31- import java .security .MessageDigest ;
32- import java .security .NoSuchAlgorithmException ;
3326
3427/**
3528 * Loads the {@code datafusion_jni} native library on demand.
3629 *
37- * <p>The loader first tries {@link System#loadLibrary(String)} so that
38- * operators can override the bundled library by placing a build on
39- * {@code java.library.path} (for example via
40- * {@code -Djava.library.path=...} or {@code LD_LIBRARY_PATH}). If that
41- * fails the loader extracts the platform-specific library from the JAR
42- * resource tree and loads it via {@link System#load(String)}.
30+ * <p>The loader first tries {@link System#loadLibrary(String)} so that operators can override the
31+ * bundled library by placing a build on {@code java.library.path} (for example via {@code
32+ * -Djava.library.path=...} or {@code LD_LIBRARY_PATH}). If that fails the loader extracts the
33+ * platform-specific library from the JAR resource tree and loads it via {@link
34+ * System#load(String)}.
4335 *
44- * <p>The bundled libraries live at the conventional path
45- * {@code org/apache/datafusion/<os>/<arch>/<libfile>}.
46- * Extracted files are written under
47- * {@code $TMPDIR/datafusion- java/<sha256>/} so that concurrent JVMs
48- * sharing a temp directory converge on the same file rather than each
49- * extracting their own copy .
36+ * <p>The bundled libraries live at the conventional path {@code
37+ * org/apache/datafusion/<os>/<arch>/<libfile>}. Each JVM extracts into its own
38+ * private temporary directory created with {@link Files#createTempDirectory(String,
39+ * java.nio.file.attribute.FileAttribute[])}, which is mode 0700 on POSIX and user-private under the
40+ * default Windows {@code TMP}. Keeping the directory private prevents another user on the host from
41+ * planting a same-named library for the JVM to pick up .
5042 */
5143public final class NativeLibraryLoader {
5244
53- private static final String TMP_DIR_NAME = "datafusion-java" ;
45+ private static final String TMP_DIR_PREFIX = "datafusion-java- " ;
5446
5547 private static volatile boolean loaded ;
5648
@@ -83,8 +75,11 @@ private static void loadFromClasspath() {
8375 try (InputStream check = NativeLibraryLoader .class .getResourceAsStream (resource )) {
8476 if (check == null ) {
8577 throw new UnsatisfiedLinkError (
86- "No bundled datafusion_jni library for " + platform
87- + " (expected classpath:" + resource + ")."
78+ "No bundled datafusion_jni library for "
79+ + platform
80+ + " (expected classpath:"
81+ + resource
82+ + ")."
8883 + " Build the native crate and add it to java.library.path,"
8984 + " or depend on a JAR built for this platform." );
9085 }
@@ -93,73 +88,24 @@ private static void loadFromClasspath() {
9388 }
9489
9590 try {
96- Path extracted = extractToTempDir (resource , platform .libFileName ());
91+ Path extracted = extractToPrivateTempDir (resource , platform .libFileName ());
9792 System .load (extracted .toAbsolutePath ().toString ());
9893 } catch (IOException e ) {
9994 throw linkError ("Failed to extract " + resource , e );
10095 }
10196 }
10297
103- private static Path extractToTempDir (String resource , String fileName ) throws IOException {
104- Path tmpRoot = Files .createDirectories (
105- Paths .get (System .getProperty ("java.io.tmpdir" ), TMP_DIR_NAME ));
106- Path staging = Files .createTempFile (tmpRoot , fileName + "." , ".part" );
107-
108- String hash ;
109- try (InputStream raw = NativeLibraryLoader .class .getResourceAsStream (resource );
110- DigestInputStream in = new DigestInputStream (raw , sha256 ());
111- OutputStream out = Files .newOutputStream (staging )) {
112- in .transferTo (out );
113- hash = toHex (in .getMessageDigest ().digest ());
114- } catch (IOException e ) {
115- Files .deleteIfExists (staging );
116- throw e ;
117- }
118-
119- Path versionedDir = Files .createDirectories (tmpRoot .resolve (hash ));
120- Path target = versionedDir .resolve (fileName );
121-
122- if (Files .exists (target ) && Files .size (target ) == Files .size (staging )) {
123- Files .deleteIfExists (staging );
124- return target ;
125- }
126-
127- try {
128- Files .move (staging , target , StandardCopyOption .ATOMIC_MOVE );
129- } catch (FileAlreadyExistsException e ) {
130- // Another JVM extracted the same content while we were writing.
131- // Their copy is identical (same SHA-256), so discard ours.
132- Files .deleteIfExists (staging );
133- } catch (IOException e ) {
134- // Atomic move not supported on this filesystem. Fall back to a
135- // replacement move; the hash directory guarantees content equality.
136- try {
137- Files .move (staging , target , StandardCopyOption .REPLACE_EXISTING );
138- } catch (IOException retry ) {
139- Files .deleteIfExists (staging );
140- throw retry ;
141- }
98+ private static Path extractToPrivateTempDir (String resource , String fileName ) throws IOException {
99+ Path tmpDir = Files .createTempDirectory (TMP_DIR_PREFIX );
100+ tmpDir .toFile ().deleteOnExit ();
101+ Path target = tmpDir .resolve (fileName );
102+ target .toFile ().deleteOnExit ();
103+ try (InputStream in = NativeLibraryLoader .class .getResourceAsStream (resource )) {
104+ Files .copy (in , target );
142105 }
143106 return target ;
144107 }
145108
146- private static MessageDigest sha256 () {
147- try {
148- return MessageDigest .getInstance ("SHA-256" );
149- } catch (NoSuchAlgorithmException e ) {
150- throw new IllegalStateException ("SHA-256 not available" , e );
151- }
152- }
153-
154- private static String toHex (byte [] bytes ) {
155- StringBuilder sb = new StringBuilder (bytes .length * 2 );
156- for (byte b : bytes ) {
157- sb .append (Character .forDigit ((b >> 4 ) & 0xf , 16 ));
158- sb .append (Character .forDigit (b & 0xf , 16 ));
159- }
160- return sb .toString ();
161- }
162-
163109 private static UnsatisfiedLinkError linkError (String message , Throwable cause ) {
164110 UnsatisfiedLinkError err = new UnsatisfiedLinkError (message + ": " + cause .getMessage ());
165111 err .initCause (cause );
0 commit comments