893N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A#include "jni.h"
893N/A#include "jni_util.h"
893N/A#include "jvm.h"
893N/A#include "jlong.h"
893N/A
893N/A#include <stdlib.h>
893N/A#include <dlfcn.h>
893N/A
893N/A#ifdef __solaris__
893N/A#include <strings.h>
893N/A#endif
893N/A
5316N/A#if defined(__linux__)
893N/A#include <string.h>
893N/A#endif
893N/A
893N/A/* Definitions for GIO */
893N/A
893N/A#define G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "standard::content-type"
893N/A
893N/Atypedef void* gpointer;
893N/Atypedef struct _GFile GFile;
893N/Atypedef struct _GFileInfo GFileInfo;
893N/Atypedef struct _GCancellable GCancellable;
893N/Atypedef struct _GError GError;
893N/A
893N/Atypedef enum {
893N/A G_FILE_QUERY_INFO_NONE = 0
893N/A} GFileQueryInfoFlags;
893N/A
893N/Atypedef void (*g_type_init_func)(void);
893N/Atypedef void (*g_object_unref_func)(gpointer object);
893N/Atypedef GFile* (*g_file_new_for_path_func)(const char* path);
893N/Atypedef GFileInfo* (*g_file_query_info_func)(GFile *file,
893N/A const char *attributes, GFileQueryInfoFlags flags,
893N/A GCancellable *cancellable, GError **error);
893N/Atypedef char* (*g_file_info_get_content_type_func)(GFileInfo *info);
893N/A
893N/Astatic g_type_init_func g_type_init;
893N/Astatic g_object_unref_func g_object_unref;
893N/Astatic g_file_new_for_path_func g_file_new_for_path;
893N/Astatic g_file_query_info_func g_file_query_info;
893N/Astatic g_file_info_get_content_type_func g_file_info_get_content_type;
893N/A
893N/A
893N/A/* Definitions for GNOME VFS */
893N/A
893N/Atypedef int gboolean;
893N/A
893N/Atypedef gboolean (*gnome_vfs_init_function)(void);
893N/Atypedef const char* (*gnome_vfs_mime_type_from_name_function)
893N/A (const char* filename);
893N/A
893N/Astatic gnome_vfs_init_function gnome_vfs_init;
893N/Astatic gnome_vfs_mime_type_from_name_function gnome_vfs_mime_type_from_name;
893N/A
893N/A
893N/A#include "sun_nio_fs_GnomeFileTypeDetector.h"
893N/A
893N/A
893N/AJNIEXPORT jboolean JNICALL
893N/AJava_sun_nio_fs_GnomeFileTypeDetector_initializeGio
893N/A (JNIEnv* env, jclass this)
893N/A{
893N/A void* gio_handle;
893N/A
893N/A gio_handle = dlopen("libgio-2.0.so", RTLD_LAZY);
893N/A if (gio_handle == NULL) {
893N/A gio_handle = dlopen("libgio-2.0.so.0", RTLD_LAZY);
893N/A if (gio_handle == NULL) {
893N/A return JNI_FALSE;
893N/A }
893N/A }
893N/A
893N/A g_type_init = (g_type_init_func)dlsym(gio_handle, "g_type_init");
893N/A (*g_type_init)();
893N/A
893N/A g_object_unref = (g_object_unref_func)dlsym(gio_handle, "g_object_unref");
893N/A
893N/A g_file_new_for_path =
893N/A (g_file_new_for_path_func)dlsym(gio_handle, "g_file_new_for_path");
893N/A
893N/A g_file_query_info =
893N/A (g_file_query_info_func)dlsym(gio_handle, "g_file_query_info");
893N/A
893N/A g_file_info_get_content_type = (g_file_info_get_content_type_func)
893N/A dlsym(gio_handle, "g_file_info_get_content_type");
893N/A
893N/A
893N/A if (g_type_init == NULL ||
893N/A g_object_unref == NULL ||
893N/A g_file_new_for_path == NULL ||
893N/A g_file_query_info == NULL ||
893N/A g_file_info_get_content_type == NULL)
893N/A {
893N/A dlclose(gio_handle);
893N/A return JNI_FALSE;
893N/A }
893N/A
893N/A (*g_type_init)();
893N/A return JNI_TRUE;
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_GnomeFileTypeDetector_probeUsingGio
893N/A (JNIEnv* env, jclass this, jlong pathAddress)
893N/A{
893N/A char* path = (char*)jlong_to_ptr(pathAddress);
893N/A GFile* gfile;
893N/A GFileInfo* gfileinfo;
893N/A jbyteArray result = NULL;
893N/A
893N/A gfile = (*g_file_new_for_path)(path);
893N/A gfileinfo = (*g_file_query_info)(gfile, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
893N/A G_FILE_QUERY_INFO_NONE, NULL, NULL);
893N/A if (gfileinfo != NULL) {
893N/A const char* mime = (*g_file_info_get_content_type)(gfileinfo);
893N/A if (mime != NULL) {
893N/A jsize len = strlen(mime);
893N/A result = (*env)->NewByteArray(env, len);
893N/A if (result != NULL) {
893N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
893N/A }
893N/A }
893N/A (*g_object_unref)(gfileinfo);
893N/A }
893N/A (*g_object_unref)(gfile);
893N/A
893N/A return result;
893N/A}
893N/A
893N/AJNIEXPORT jboolean JNICALL
893N/AJava_sun_nio_fs_GnomeFileTypeDetector_initializeGnomeVfs
893N/A (JNIEnv* env, jclass this)
893N/A{
893N/A void* vfs_handle;
893N/A
893N/A vfs_handle = dlopen("libgnomevfs-2.so", RTLD_LAZY);
893N/A if (vfs_handle == NULL) {
893N/A vfs_handle = dlopen("libgnomevfs-2.so.0", RTLD_LAZY);
893N/A }
893N/A if (vfs_handle == NULL) {
893N/A return JNI_FALSE;
893N/A }
893N/A
893N/A gnome_vfs_init = (gnome_vfs_init_function)dlsym(vfs_handle, "gnome_vfs_init");
893N/A gnome_vfs_mime_type_from_name = (gnome_vfs_mime_type_from_name_function)
893N/A dlsym(vfs_handle, "gnome_vfs_mime_type_from_name");
893N/A
893N/A if (gnome_vfs_init == NULL ||
893N/A gnome_vfs_mime_type_from_name == NULL)
893N/A {
893N/A dlclose(vfs_handle);
893N/A return JNI_FALSE;
893N/A }
893N/A
893N/A (*gnome_vfs_init)();
893N/A return JNI_TRUE;
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_GnomeFileTypeDetector_probeUsingGnomeVfs
893N/A (JNIEnv* env, jclass this, jlong pathAddress)
893N/A{
893N/A char* path = (char*)jlong_to_ptr(pathAddress);
893N/A const char* mime = (*gnome_vfs_mime_type_from_name)(path);
893N/A
893N/A if (mime == NULL) {
893N/A return NULL;
893N/A } else {
893N/A jbyteArray result;
893N/A jsize len = strlen(mime);
893N/A result = (*env)->NewByteArray(env, len);
893N/A if (result != NULL) {
893N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)mime);
893N/A }
893N/A return result;
893N/A }
893N/A}