annotate src/solaris/native/sun/nio/fs/GnomeFileTypeDetector.c @ 5299:37377dcd43d0

7191556: (fs) UnixNativeDispatcher.getextmntent should be moved into platform specific code Reviewed-by: andrew
author alanb
date Thu, 16 Aug 2012 11:14:42 +0100
parents 257dee3bea4e
children
rev   line source
alanb@893 1 /*
ohair@2362 2 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
alanb@893 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
alanb@893 4 *
alanb@893 5 * This code is free software; you can redistribute it and/or modify it
alanb@893 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
alanb@893 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
alanb@893 10 *
alanb@893 11 * This code is distributed in the hope that it will be useful, but WITHOUT
alanb@893 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
alanb@893 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
alanb@893 14 * version 2 for more details (a copy is included in the LICENSE file that
alanb@893 15 * accompanied this code).
alanb@893 16 *
alanb@893 17 * You should have received a copy of the GNU General Public License version
alanb@893 18 * 2 along with this work; if not, write to the Free Software Foundation,
alanb@893 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
alanb@893 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
alanb@893 24 */
alanb@893 25
alanb@893 26 #include "jni.h"
alanb@893 27 #include "jni_util.h"
alanb@893 28 #include "jvm.h"
alanb@893 29 #include "jlong.h"
alanb@893 30
alanb@893 31 #include <stdlib.h>
alanb@893 32 #include <dlfcn.h>
alanb@893 33
alanb@893 34 #ifdef __solaris__
alanb@893 35 #include <strings.h>
alanb@893 36 #endif
alanb@893 37
alanb@5299 38 #if defined(__linux__)
alanb@893 39 #include <string.h>
alanb@893 40 #endif
alanb@893 41
alanb@893 42 /* Definitions for GIO */
alanb@893 43
alanb@893 44 #define G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "standard::content-type"
alanb@893 45
alanb@893 46 typedef void* gpointer;
alanb@893 47 typedef struct _GFile GFile;
alanb@893 48 typedef struct _GFileInfo GFileInfo;
alanb@893 49 typedef struct _GCancellable GCancellable;
alanb@893 50 typedef struct _GError GError;
alanb@893 51
alanb@893 52 typedef enum {
alanb@893 53 G_FILE_QUERY_INFO_NONE = 0
alanb@893 54 } GFileQueryInfoFlags;
alanb@893 55
alanb@893 56 typedef void (*g_type_init_func)(void);
alanb@893 57 typedef void (*g_object_unref_func)(gpointer object);
alanb@893 58 typedef GFile* (*g_file_new_for_path_func)(const char* path);
alanb@893 59 typedef GFileInfo* (*g_file_query_info_func)(GFile *file,
alanb@893 60 const char *attributes, GFileQueryInfoFlags flags,
alanb@893 61 GCancellable *cancellable, GError **error);
alanb@893 62 typedef char* (*g_file_info_get_content_type_func)(GFileInfo *info);
alanb@893 63
alanb@893 64 static g_type_init_func g_type_init;
alanb@893 65 static g_object_unref_func g_object_unref;
alanb@893 66 static g_file_new_for_path_func g_file_new_for_path;
alanb@893 67 static g_file_query_info_func g_file_query_info;
alanb@893 68 static g_file_info_get_content_type_func g_file_info_get_content_type;
alanb@893 69
alanb@893 70
alanb@893 71 /* Definitions for GNOME VFS */
alanb@893 72
alanb@893 73 typedef int gboolean;
alanb@893 74
alanb@893 75 typedef gboolean (*gnome_vfs_init_function)(void);
alanb@893 76 typedef const char* (*gnome_vfs_mime_type_from_name_function)
alanb@893 77 (const char* filename);
alanb@893 78
alanb@893 79 static gnome_vfs_init_function gnome_vfs_init;
alanb@893 80 static gnome_vfs_mime_type_from_name_function gnome_vfs_mime_type_from_name;
alanb@893 81
alanb@893 82
alanb@893 83 #include "sun_nio_fs_GnomeFileTypeDetector.h"
alanb@893 84
alanb@893 85
alanb@893 86 JNIEXPORT jboolean JNICALL
alanb@893 87 Java_sun_nio_fs_GnomeFileTypeDetector_initializeGio
alanb@893 88 (JNIEnv* env, jclass this)
alanb@893 89 {
alanb@893 90 void* gio_handle;
alanb@893 91
alanb@893 92 gio_handle = dlopen("libgio-2.0.so", RTLD_LAZY);
alanb@893 93 if (gio_handle == NULL) {
alanb@893 94 gio_handle = dlopen("libgio-2.0.so.0", RTLD_LAZY);
alanb@893 95 if (gio_handle == NULL) {
alanb@893 96 return JNI_FALSE;
alanb@893 97 }
alanb@893 98 }
alanb@893 99
alanb@893 100 g_type_init = (g_type_init_func)dlsym(gio_handle, "g_type_init");
alanb@893 101 (*g_type_init)();
alanb@893 102
alanb@893 103 g_object_unref = (g_object_unref_func)dlsym(gio_handle, "g_object_unref");
alanb@893 104
alanb@893 105 g_file_new_for_path =
alanb@893 106 (g_file_new_for_path_func)dlsym(gio_handle, "g_file_new_for_path");
alanb@893 107
alanb@893 108 g_file_query_info =
alanb@893 109 (g_file_query_info_func)dlsym(gio_handle, "g_file_query_info");
alanb@893 110
alanb@893 111 g_file_info_get_content_type = (g_file_info_get_content_type_func)
alanb@893 112 dlsym(gio_handle, "g_file_info_get_content_type");
alanb@893 113
alanb@893 114
alanb@893 115 if (g_type_init == NULL ||
alanb@893 116 g_object_unref == NULL ||
alanb@893 117 g_file_new_for_path == NULL ||
alanb@893 118 g_file_query_info == NULL ||
alanb@893 119 g_file_info_get_content_type == NULL)
alanb@893 120 {
alanb@893 121 dlclose(gio_handle);
alanb@893 122 return JNI_FALSE;
alanb@893 123 }
alanb@893 124
alanb@893 125 (*g_type_init)();
alanb@893 126 return JNI_TRUE;
alanb@893 127 }
alanb@893 128
alanb@893 129 JNIEXPORT jbyteArray JNICALL
alanb@893 130 Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio
alanb@893 131 (JNIEnv* env, jclass this, jlong pathAddress)
alanb@893 132 {
alanb@893 133 char* path = (char*)jlong_to_ptr(pathAddress);
alanb@893 134 GFile* gfile;
alanb@893 135 GFileInfo* gfileinfo;
alanb@893 136 jbyteArray result = NULL;
alanb@893 137
alanb@893 138 gfile = (*g_file_new_for_path)(path);
alanb@893 139 gfileinfo = (*g_file_query_info)(gfile, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
alanb@893 140 G_FILE_QUERY_INFO_NONE, NULL, NULL);
alanb@893 141 if (gfileinfo != NULL) {
alanb@893 142 const char* mime = (*g_file_info_get_content_type)(gfileinfo);
alanb@893 143 if (mime != NULL) {
alanb@893 144 jsize len = strlen(mime);
alanb@893 145 result = (*env)->NewByteArray(env, len);
alanb@893 146 if (result != NULL) {
alanb@893 147 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
alanb@893 148 }
alanb@893 149 }
alanb@893 150 (*g_object_unref)(gfileinfo);
alanb@893 151 }
alanb@893 152 (*g_object_unref)(gfile);
alanb@893 153
alanb@893 154 return result;
alanb@893 155 }
alanb@893 156
alanb@893 157 JNIEXPORT jboolean JNICALL
alanb@893 158 Java_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs
alanb@893 159 (JNIEnv* env, jclass this)
alanb@893 160 {
alanb@893 161 void* vfs_handle;
alanb@893 162
alanb@893 163 vfs_handle = dlopen("libgnomevfs-2.so", RTLD_LAZY);
alanb@893 164 if (vfs_handle == NULL) {
alanb@893 165 vfs_handle = dlopen("libgnomevfs-2.so.0", RTLD_LAZY);
alanb@893 166 }
alanb@893 167 if (vfs_handle == NULL) {
alanb@893 168 return JNI_FALSE;
alanb@893 169 }
alanb@893 170
alanb@893 171 gnome_vfs_init = (gnome_vfs_init_function)dlsym(vfs_handle, "gnome_vfs_init");
alanb@893 172 gnome_vfs_mime_type_from_name = (gnome_vfs_mime_type_from_name_function)
alanb@893 173 dlsym(vfs_handle, "gnome_vfs_mime_type_from_name");
alanb@893 174
alanb@893 175 if (gnome_vfs_init == NULL ||
alanb@893 176 gnome_vfs_mime_type_from_name == NULL)
alanb@893 177 {
alanb@893 178 dlclose(vfs_handle);
alanb@893 179 return JNI_FALSE;
alanb@893 180 }
alanb@893 181
alanb@893 182 (*gnome_vfs_init)();
alanb@893 183 return JNI_TRUE;
alanb@893 184 }
alanb@893 185
alanb@893 186 JNIEXPORT jbyteArray JNICALL
alanb@893 187 Java_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs
alanb@893 188 (JNIEnv* env, jclass this, jlong pathAddress)
alanb@893 189 {
alanb@893 190 char* path = (char*)jlong_to_ptr(pathAddress);
alanb@893 191 const char* mime = (*gnome_vfs_mime_type_from_name)(path);
alanb@893 192
alanb@893 193 if (mime == NULL) {
alanb@893 194 return NULL;
alanb@893 195 } else {
alanb@893 196 jbyteArray result;
alanb@893 197 jsize len = strlen(mime);
alanb@893 198 result = (*env)->NewByteArray(env, len);
alanb@893 199 if (result != NULL) {
alanb@893 200 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
alanb@893 201 }
alanb@893 202 return result;
alanb@893 203 }
alanb@893 204 }