1 /* |
|
2 * Copyright (c) 2008, 2015, 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 <stdlib.h> |
|
32 #include <dlfcn.h> |
|
33 |
|
34 #ifdef __solaris__ |
|
35 #include <strings.h> |
|
36 #endif |
|
37 |
|
38 #if defined(__linux__) |
|
39 #include <string.h> |
|
40 #endif |
|
41 |
|
42 /* |
|
43 * For reference see for example the GFileInfo section at |
|
44 * https://developer.gnome.org/gio/unstable/. |
|
45 */ |
|
46 |
|
47 /* Definitions for GIO */ |
|
48 |
|
49 #define G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "standard::content-type" |
|
50 |
|
51 typedef void* gpointer; |
|
52 typedef struct _GFile GFile; |
|
53 typedef struct _GFileInfo GFileInfo; |
|
54 typedef struct _GCancellable GCancellable; |
|
55 typedef struct _GError GError; |
|
56 |
|
57 typedef enum { |
|
58 G_FILE_QUERY_INFO_NONE = 0 |
|
59 } GFileQueryInfoFlags; |
|
60 |
|
61 typedef void (*g_type_init_func)(void); |
|
62 typedef void (*g_object_unref_func)(gpointer object); |
|
63 typedef GFile* (*g_file_new_for_path_func)(const char* path); |
|
64 typedef GFileInfo* (*g_file_query_info_func)(GFile *file, |
|
65 const char *attributes, GFileQueryInfoFlags flags, |
|
66 GCancellable *cancellable, GError **error); |
|
67 typedef char* (*g_file_info_get_content_type_func)(GFileInfo *info); |
|
68 |
|
69 static g_type_init_func g_type_init; |
|
70 static g_object_unref_func g_object_unref; |
|
71 static g_file_new_for_path_func g_file_new_for_path; |
|
72 static g_file_query_info_func g_file_query_info; |
|
73 static g_file_info_get_content_type_func g_file_info_get_content_type; |
|
74 |
|
75 |
|
76 #include "sun_nio_fs_GnomeFileTypeDetector.h" |
|
77 |
|
78 |
|
79 JNIEXPORT jboolean JNICALL |
|
80 Java_sun_nio_fs_GnomeFileTypeDetector_initializeGio |
|
81 (JNIEnv* env, jclass this) |
|
82 { |
|
83 void* gio_handle; |
|
84 |
|
85 gio_handle = dlopen("libgio-2.0.so", RTLD_LAZY); |
|
86 if (gio_handle == NULL) { |
|
87 gio_handle = dlopen("libgio-2.0.so.0", RTLD_LAZY); |
|
88 if (gio_handle == NULL) { |
|
89 return JNI_FALSE; |
|
90 } |
|
91 } |
|
92 |
|
93 g_type_init = (g_type_init_func)dlsym(gio_handle, "g_type_init"); |
|
94 (*g_type_init)(); |
|
95 |
|
96 g_object_unref = (g_object_unref_func)dlsym(gio_handle, "g_object_unref"); |
|
97 |
|
98 g_file_new_for_path = |
|
99 (g_file_new_for_path_func)dlsym(gio_handle, "g_file_new_for_path"); |
|
100 |
|
101 g_file_query_info = |
|
102 (g_file_query_info_func)dlsym(gio_handle, "g_file_query_info"); |
|
103 |
|
104 g_file_info_get_content_type = (g_file_info_get_content_type_func) |
|
105 dlsym(gio_handle, "g_file_info_get_content_type"); |
|
106 |
|
107 |
|
108 if (g_type_init == NULL || |
|
109 g_object_unref == NULL || |
|
110 g_file_new_for_path == NULL || |
|
111 g_file_query_info == NULL || |
|
112 g_file_info_get_content_type == NULL) |
|
113 { |
|
114 dlclose(gio_handle); |
|
115 return JNI_FALSE; |
|
116 } |
|
117 |
|
118 (*g_type_init)(); |
|
119 return JNI_TRUE; |
|
120 } |
|
121 |
|
122 JNIEXPORT jbyteArray JNICALL |
|
123 Java_sun_nio_fs_GnomeFileTypeDetector_probeGio |
|
124 (JNIEnv* env, jclass this, jlong pathAddress) |
|
125 { |
|
126 char* path = (char*)jlong_to_ptr(pathAddress); |
|
127 GFile* gfile; |
|
128 GFileInfo* gfileinfo; |
|
129 jbyteArray result = NULL; |
|
130 |
|
131 gfile = (*g_file_new_for_path)(path); |
|
132 gfileinfo = (*g_file_query_info)(gfile, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, |
|
133 G_FILE_QUERY_INFO_NONE, NULL, NULL); |
|
134 if (gfileinfo != NULL) { |
|
135 const char* mime = (*g_file_info_get_content_type)(gfileinfo); |
|
136 if (mime != NULL) { |
|
137 jsize len = strlen(mime); |
|
138 result = (*env)->NewByteArray(env, len); |
|
139 if (result != NULL) { |
|
140 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime); |
|
141 } |
|
142 } |
|
143 (*g_object_unref)(gfileinfo); |
|
144 } |
|
145 (*g_object_unref)(gfile); |
|
146 |
|
147 return result; |
|
148 } |
|