1 /*
2 * Copyright (c) 2008, 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 <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 /* Definitions for GIO */
43
44 #define G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "standard::content-type"
45
46 typedef void* gpointer;
47 typedef struct _GFile GFile;
48 typedef struct _GFileInfo GFileInfo;
49 typedef struct _GCancellable GCancellable;
50 typedef struct _GError GError;
51
52 typedef enum {
53 G_FILE_QUERY_INFO_NONE = 0
54 } GFileQueryInfoFlags;
55
56 typedef void (*g_type_init_func)(void);
57 typedef void (*g_object_unref_func)(gpointer object);
58 typedef GFile* (*g_file_new_for_path_func)(const char* path);
59 typedef GFileInfo* (*g_file_query_info_func)(GFile *file,
60 const char *attributes, GFileQueryInfoFlags flags,
61 GCancellable *cancellable, GError **error);
62 typedef char* (*g_file_info_get_content_type_func)(GFileInfo *info);
63
64 static g_type_init_func g_type_init;
65 static g_object_unref_func g_object_unref;
66 static g_file_new_for_path_func g_file_new_for_path;
67 static g_file_query_info_func g_file_query_info;
68 static g_file_info_get_content_type_func g_file_info_get_content_type;
69
70
71 /* Definitions for GNOME VFS */
72
73 typedef int gboolean;
74
75 typedef gboolean (*gnome_vfs_init_function)(void);
76 typedef const char* (*gnome_vfs_mime_type_from_name_function)
77 (const char* filename);
78
79 static gnome_vfs_init_function gnome_vfs_init;
80 static gnome_vfs_mime_type_from_name_function gnome_vfs_mime_type_from_name;
81
82
83 #include "sun_nio_fs_GnomeFileTypeDetector.h"
84
85
86 JNIEXPORT jboolean JNICALL
Java_sun_nio_fs_GnomeFileTypeDetector_initializeGio(JNIEnv * env,jclass this)87 Java_sun_nio_fs_GnomeFileTypeDetector_initializeGio
88 (JNIEnv* env, jclass this)
89 {
90 void* gio_handle;
91
92 gio_handle = dlopen("libgio-2.0.so", RTLD_LAZY);
93 if (gio_handle == NULL) {
94 gio_handle = dlopen("libgio-2.0.so.0", RTLD_LAZY);
95 if (gio_handle == NULL) {
96 return JNI_FALSE;
97 }
98 }
99
100 g_type_init = (g_type_init_func)dlsym(gio_handle, "g_type_init");
101 (*g_type_init)();
102
103 g_object_unref = (g_object_unref_func)dlsym(gio_handle, "g_object_unref");
104
105 g_file_new_for_path =
106 (g_file_new_for_path_func)dlsym(gio_handle, "g_file_new_for_path");
107
108 g_file_query_info =
109 (g_file_query_info_func)dlsym(gio_handle, "g_file_query_info");
110
111 g_file_info_get_content_type = (g_file_info_get_content_type_func)
112 dlsym(gio_handle, "g_file_info_get_content_type");
113
114
115 if (g_type_init == NULL ||
116 g_object_unref == NULL ||
117 g_file_new_for_path == NULL ||
118 g_file_query_info == NULL ||
119 g_file_info_get_content_type == NULL)
120 {
121 dlclose(gio_handle);
122 return JNI_FALSE;
123 }
124
125 (*g_type_init)();
126 return JNI_TRUE;
127 }
128
129 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio(JNIEnv * env,jclass this,jlong pathAddress)130 Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio
131 (JNIEnv* env, jclass this, jlong pathAddress)
132 {
133 char* path = (char*)jlong_to_ptr(pathAddress);
134 GFile* gfile;
135 GFileInfo* gfileinfo;
136 jbyteArray result = NULL;
137
138 gfile = (*g_file_new_for_path)(path);
139 gfileinfo = (*g_file_query_info)(gfile, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
140 G_FILE_QUERY_INFO_NONE, NULL, NULL);
141 if (gfileinfo != NULL) {
142 const char* mime = (*g_file_info_get_content_type)(gfileinfo);
143 if (mime != NULL) {
144 jsize len = strlen(mime);
145 result = (*env)->NewByteArray(env, len);
146 if (result != NULL) {
147 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
148 }
149 }
150 (*g_object_unref)(gfileinfo);
151 }
152 (*g_object_unref)(gfile);
153
154 return result;
155 }
156
157 JNIEXPORT jboolean JNICALL
Java_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs(JNIEnv * env,jclass this)158 Java_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs
159 (JNIEnv* env, jclass this)
160 {
161 void* vfs_handle;
162
163 vfs_handle = dlopen("libgnomevfs-2.so", RTLD_LAZY);
164 if (vfs_handle == NULL) {
165 vfs_handle = dlopen("libgnomevfs-2.so.0", RTLD_LAZY);
166 }
167 if (vfs_handle == NULL) {
168 return JNI_FALSE;
169 }
170
171 gnome_vfs_init = (gnome_vfs_init_function)dlsym(vfs_handle, "gnome_vfs_init");
172 gnome_vfs_mime_type_from_name = (gnome_vfs_mime_type_from_name_function)
173 dlsym(vfs_handle, "gnome_vfs_mime_type_from_name");
174
175 if (gnome_vfs_init == NULL ||
176 gnome_vfs_mime_type_from_name == NULL)
177 {
178 dlclose(vfs_handle);
179 return JNI_FALSE;
180 }
181
182 (*gnome_vfs_init)();
183 return JNI_TRUE;
184 }
185
186 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs(JNIEnv * env,jclass this,jlong pathAddress)187 Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs
188 (JNIEnv* env, jclass this, jlong pathAddress)
189 {
190 char* path = (char*)jlong_to_ptr(pathAddress);
191 const char* mime = (*gnome_vfs_mime_type_from_name)(path);
192
193 if (mime == NULL) {
194 return NULL;
195 } else {
196 jbyteArray result;
197 jsize len = strlen(mime);
198 result = (*env)->NewByteArray(env, len);
199 if (result != NULL) {
200 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
201 }
202 return result;
203 }
204 }
205