Path: blob/master/src/java.base/linux/native/libnio/fs/LinuxNativeDispatcher.c
41133 views
/*1* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"2930#include <stdio.h>31#include <string.h>32#include <dlfcn.h>33#include <errno.h>34#include <mntent.h>3536#include "sun_nio_fs_LinuxNativeDispatcher.h"3738static jfieldID entry_name;39static jfieldID entry_dir;40static jfieldID entry_fstype;41static jfieldID entry_options;4243static void throwUnixException(JNIEnv* env, int errnum) {44jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",45"(I)V", errnum);46if (x != NULL) {47(*env)->Throw(env, x);48}49}5051JNIEXPORT void JNICALL52Java_sun_nio_fs_LinuxNativeDispatcher_init(JNIEnv *env, jclass clazz)53{54clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry");55CHECK_NULL(clazz);56entry_name = (*env)->GetFieldID(env, clazz, "name", "[B");57CHECK_NULL(entry_name);58entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B");59CHECK_NULL(entry_dir);60entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B");61CHECK_NULL(entry_fstype);62entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B");63CHECK_NULL(entry_options);64}6566JNIEXPORT jlong JNICALL67Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0(JNIEnv* env, jclass this, jlong pathAddress,68jlong modeAddress)69{70FILE* fp = NULL;71const char* path = (const char*)jlong_to_ptr(pathAddress);72const char* mode = (const char*)jlong_to_ptr(modeAddress);7374do {75fp = setmntent(path, mode);76} while (fp == NULL && errno == EINTR);77if (fp == NULL) {78throwUnixException(env, errno);79}80return ptr_to_jlong(fp);81}8283JNIEXPORT jint JNICALL84Java_sun_nio_fs_LinuxNativeDispatcher_getmntent0(JNIEnv* env, jclass this,85jlong value, jobject entry, jlong buffer, jint bufLen)86{87struct mntent ent;88char * buf = (char*)jlong_to_ptr(buffer);89struct mntent* m;90FILE* fp = jlong_to_ptr(value);91jsize len;92jbyteArray bytes;93char* name;94char* dir;95char* fstype;96char* options;9798m = getmntent_r(fp, &ent, buf, (int)bufLen);99if (m == NULL)100return -1;101name = m->mnt_fsname;102dir = m->mnt_dir;103fstype = m->mnt_type;104options = m->mnt_opts;105106len = strlen(name);107bytes = (*env)->NewByteArray(env, len);108if (bytes == NULL)109return -1;110(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)name);111(*env)->SetObjectField(env, entry, entry_name, bytes);112113len = strlen(dir);114bytes = (*env)->NewByteArray(env, len);115if (bytes == NULL)116return -1;117(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)dir);118(*env)->SetObjectField(env, entry, entry_dir, bytes);119120len = strlen(fstype);121bytes = (*env)->NewByteArray(env, len);122if (bytes == NULL)123return -1;124(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)fstype);125(*env)->SetObjectField(env, entry, entry_fstype, bytes);126127len = strlen(options);128bytes = (*env)->NewByteArray(env, len);129if (bytes == NULL)130return -1;131(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)options);132(*env)->SetObjectField(env, entry, entry_options, bytes);133134return 0;135}136137JNIEXPORT void JNICALL138Java_sun_nio_fs_LinuxNativeDispatcher_endmntent(JNIEnv* env, jclass this, jlong stream)139{140FILE* fp = jlong_to_ptr(stream);141/* FIXME - man page doesn't explain how errors are returned */142endmntent(fp);143}144145146