1 /*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include "jni.h"
27 #include "jni_util.h"
28 #include "jvm.h"
29 #include "jlong.h"
30
31 #include <dlfcn.h>
32 #include <string.h>
33
34 #ifdef __FreeBSD__
35 #define MAGIC_MAJOR_VERSION "4"
36 #elif defined(__DragonFly__)
37 #define MAGIC_MAJOR_VERSION "2"
38 #else
39 #define MAGIC_MAJOR_VERSION "1"
40 #endif
41
42 #define MAGIC_MIME_TYPE 0x000010 /* Return the MIME type */
43
44 typedef struct magic_set magic_t;
45
46 typedef magic_t* (*magic_open_func)(int flags);
47 typedef int (*magic_load_func)(magic_t* cookie, const char* filename);
48 typedef const char* (*magic_file_func)(magic_t* cookie, const char* filename);
49 typedef void (*magic_close_func)(magic_t* cookie);
50
51 static void* magic_handle;
52 static magic_open_func magic_open;
53 static magic_load_func magic_load;
54 static magic_file_func magic_file;
55 static magic_close_func magic_close;
56
57 #include "sun_nio_fs_MagicFileTypeDetector.h"
58
59 JNIEXPORT jboolean JNICALL
Java_sun_nio_fs_MagicFileTypeDetector_initialize0(JNIEnv * env,jclass this)60 Java_sun_nio_fs_MagicFileTypeDetector_initialize0
61 (JNIEnv* env, jclass this)
62 {
63 magic_handle = dlopen("libmagic.so", RTLD_LAZY);
64 if (magic_handle == NULL) {
65 magic_handle = dlopen("libmagic.so." MAGIC_MAJOR_VERSION, RTLD_LAZY);
66 if (magic_handle == NULL) {
67 return JNI_FALSE;
68 }
69 }
70
71 magic_open = (magic_open_func)dlsym(magic_handle, "magic_open");
72
73 magic_load = (magic_load_func)dlsym(magic_handle, "magic_load");
74
75 magic_file = (magic_file_func)dlsym(magic_handle, "magic_file");
76
77 magic_close = (magic_close_func)dlsym(magic_handle, "magic_close");
78
79 if (magic_open == NULL ||
80 magic_load == NULL ||
81 magic_file == NULL ||
82 magic_close == NULL)
83 {
84 dlclose(magic_handle);
85 return JNI_FALSE;
86 }
87
88 return JNI_TRUE;
89 }
90
91 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_MagicFileTypeDetector_probe0(JNIEnv * env,jclass this,jlong pathAddress)92 Java_sun_nio_fs_MagicFileTypeDetector_probe0
93 (JNIEnv* env, jclass this, jlong pathAddress)
94 {
95 char* path = (char*)jlong_to_ptr(pathAddress);
96 magic_t* cookie;
97 jbyteArray result = NULL;
98
99 cookie = (*magic_open)(MAGIC_MIME_TYPE);
100
101 if (cookie != NULL) {
102 if ((*magic_load)(cookie, NULL) != -1) {
103 const char* type = (*magic_file)(cookie, path);
104 if (type != NULL) {
105 jsize len = strlen(type);
106 result = (*env)->NewByteArray(env, len);
107 if (result != NULL) {
108 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)type);
109 }
110 }
111 }
112 (*magic_close)(cookie);
113 }
114
115 return result;
116 }
117