jdk/src/java.base/linux/native/libnio/fs/GioFileTypeDetector.c
changeset 39934 9c84ee88dd3a
parent 39933 c0dd0f198453
parent 39922 e613affb88d1
child 39935 6016bd47edc9
equal deleted inserted replaced
39933:c0dd0f198453 39934:9c84ee88dd3a
     1 /*
       
     2  * Copyright (c) 2008, 2016, 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_GioFileTypeDetector.h"
       
    77 
       
    78 
       
    79 JNIEXPORT jboolean JNICALL
       
    80 Java_sun_nio_fs_GioFileTypeDetector_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 
       
    95     g_object_unref = (g_object_unref_func)dlsym(gio_handle, "g_object_unref");
       
    96 
       
    97     g_file_new_for_path =
       
    98         (g_file_new_for_path_func)dlsym(gio_handle, "g_file_new_for_path");
       
    99 
       
   100     g_file_query_info =
       
   101         (g_file_query_info_func)dlsym(gio_handle, "g_file_query_info");
       
   102 
       
   103     g_file_info_get_content_type = (g_file_info_get_content_type_func)
       
   104         dlsym(gio_handle, "g_file_info_get_content_type");
       
   105 
       
   106     if (g_object_unref == NULL ||
       
   107         g_file_new_for_path == NULL ||
       
   108         g_file_query_info == NULL ||
       
   109         g_file_info_get_content_type == NULL)
       
   110     {
       
   111         dlclose(gio_handle);
       
   112         return JNI_FALSE;
       
   113     }
       
   114 
       
   115     if (g_type_init != NULL) {
       
   116         (*g_type_init)();
       
   117     }
       
   118 
       
   119     return JNI_TRUE;
       
   120 }
       
   121 
       
   122 JNIEXPORT jbyteArray JNICALL
       
   123 Java_sun_nio_fs_GioFileTypeDetector_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 }