jdk/src/java.base/linux/native/libnio/fs/MagicFileTypeDetector.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) 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 <dlfcn.h>
       
    32 #include <string.h>
       
    33 
       
    34 #define MAGIC_MIME_TYPE 0x000010 /* Return the MIME type */
       
    35 
       
    36 typedef struct magic_set magic_t;
       
    37 
       
    38 typedef magic_t* (*magic_open_func)(int flags);
       
    39 typedef int (*magic_load_func)(magic_t* cookie, const char* filename);
       
    40 typedef const char* (*magic_file_func)(magic_t* cookie, const char* filename);
       
    41 typedef void (*magic_close_func)(magic_t* cookie);
       
    42 
       
    43 static void* magic_handle;
       
    44 static magic_open_func magic_open;
       
    45 static magic_load_func magic_load;
       
    46 static magic_file_func magic_file;
       
    47 static magic_close_func magic_close;
       
    48 
       
    49 #include "sun_nio_fs_MagicFileTypeDetector.h"
       
    50 
       
    51 JNIEXPORT jboolean JNICALL
       
    52 Java_sun_nio_fs_MagicFileTypeDetector_initialize0
       
    53     (JNIEnv* env, jclass this)
       
    54 {
       
    55     magic_handle = dlopen("libmagic.so", RTLD_LAZY);
       
    56     if (magic_handle == NULL) {
       
    57         magic_handle = dlopen("libmagic.so.1", RTLD_LAZY);
       
    58         if (magic_handle == NULL) {
       
    59             return JNI_FALSE;
       
    60         }
       
    61     }
       
    62 
       
    63     magic_open = (magic_open_func)dlsym(magic_handle, "magic_open");
       
    64 
       
    65     magic_load = (magic_load_func)dlsym(magic_handle, "magic_load");
       
    66 
       
    67     magic_file = (magic_file_func)dlsym(magic_handle, "magic_file");
       
    68 
       
    69     magic_close = (magic_close_func)dlsym(magic_handle, "magic_close");
       
    70 
       
    71     if (magic_open == NULL ||
       
    72         magic_load == NULL ||
       
    73         magic_file == NULL ||
       
    74         magic_close == NULL)
       
    75     {
       
    76         dlclose(magic_handle);
       
    77         return JNI_FALSE;
       
    78     }
       
    79 
       
    80     return JNI_TRUE;
       
    81 }
       
    82 
       
    83 JNIEXPORT jbyteArray JNICALL
       
    84 Java_sun_nio_fs_MagicFileTypeDetector_probe0
       
    85     (JNIEnv* env, jclass this, jlong pathAddress)
       
    86 {
       
    87     char* path = (char*)jlong_to_ptr(pathAddress);
       
    88     magic_t* cookie;
       
    89     jbyteArray result = NULL;
       
    90 
       
    91     cookie = (*magic_open)(MAGIC_MIME_TYPE);
       
    92 
       
    93     if (cookie != NULL) {
       
    94         if ((*magic_load)(cookie, NULL) != -1) {
       
    95             const char* type = (*magic_file)(cookie, path);
       
    96             if (type != NULL) {
       
    97                 jsize len = strlen(type);
       
    98                 result = (*env)->NewByteArray(env, len);
       
    99                 if (result != NULL) {
       
   100                     (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)type);
       
   101                 }
       
   102             }
       
   103         }
       
   104         (*magic_close)(cookie);
       
   105     }
       
   106 
       
   107     return result;
       
   108 }