Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/linux/native/libnio/fs/LinuxNativeDispatcher.c
41133 views
1
/*
2
* Copyright (c) 2008, 2019, 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 <stdio.h>
32
#include <string.h>
33
#include <dlfcn.h>
34
#include <errno.h>
35
#include <mntent.h>
36
37
#include "sun_nio_fs_LinuxNativeDispatcher.h"
38
39
static jfieldID entry_name;
40
static jfieldID entry_dir;
41
static jfieldID entry_fstype;
42
static jfieldID entry_options;
43
44
static void throwUnixException(JNIEnv* env, int errnum) {
45
jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
46
"(I)V", errnum);
47
if (x != NULL) {
48
(*env)->Throw(env, x);
49
}
50
}
51
52
JNIEXPORT void JNICALL
53
Java_sun_nio_fs_LinuxNativeDispatcher_init(JNIEnv *env, jclass clazz)
54
{
55
clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry");
56
CHECK_NULL(clazz);
57
entry_name = (*env)->GetFieldID(env, clazz, "name", "[B");
58
CHECK_NULL(entry_name);
59
entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B");
60
CHECK_NULL(entry_dir);
61
entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B");
62
CHECK_NULL(entry_fstype);
63
entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B");
64
CHECK_NULL(entry_options);
65
}
66
67
JNIEXPORT jlong JNICALL
68
Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0(JNIEnv* env, jclass this, jlong pathAddress,
69
jlong modeAddress)
70
{
71
FILE* fp = NULL;
72
const char* path = (const char*)jlong_to_ptr(pathAddress);
73
const char* mode = (const char*)jlong_to_ptr(modeAddress);
74
75
do {
76
fp = setmntent(path, mode);
77
} while (fp == NULL && errno == EINTR);
78
if (fp == NULL) {
79
throwUnixException(env, errno);
80
}
81
return ptr_to_jlong(fp);
82
}
83
84
JNIEXPORT jint JNICALL
85
Java_sun_nio_fs_LinuxNativeDispatcher_getmntent0(JNIEnv* env, jclass this,
86
jlong value, jobject entry, jlong buffer, jint bufLen)
87
{
88
struct mntent ent;
89
char * buf = (char*)jlong_to_ptr(buffer);
90
struct mntent* m;
91
FILE* fp = jlong_to_ptr(value);
92
jsize len;
93
jbyteArray bytes;
94
char* name;
95
char* dir;
96
char* fstype;
97
char* options;
98
99
m = getmntent_r(fp, &ent, buf, (int)bufLen);
100
if (m == NULL)
101
return -1;
102
name = m->mnt_fsname;
103
dir = m->mnt_dir;
104
fstype = m->mnt_type;
105
options = m->mnt_opts;
106
107
len = strlen(name);
108
bytes = (*env)->NewByteArray(env, len);
109
if (bytes == NULL)
110
return -1;
111
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)name);
112
(*env)->SetObjectField(env, entry, entry_name, bytes);
113
114
len = strlen(dir);
115
bytes = (*env)->NewByteArray(env, len);
116
if (bytes == NULL)
117
return -1;
118
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)dir);
119
(*env)->SetObjectField(env, entry, entry_dir, bytes);
120
121
len = strlen(fstype);
122
bytes = (*env)->NewByteArray(env, len);
123
if (bytes == NULL)
124
return -1;
125
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)fstype);
126
(*env)->SetObjectField(env, entry, entry_fstype, bytes);
127
128
len = strlen(options);
129
bytes = (*env)->NewByteArray(env, len);
130
if (bytes == NULL)
131
return -1;
132
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)options);
133
(*env)->SetObjectField(env, entry, entry_options, bytes);
134
135
return 0;
136
}
137
138
JNIEXPORT void JNICALL
139
Java_sun_nio_fs_LinuxNativeDispatcher_endmntent(JNIEnv* env, jclass this, jlong stream)
140
{
141
FILE* fp = jlong_to_ptr(stream);
142
/* FIXME - man page doesn't explain how errors are returned */
143
endmntent(fp);
144
}
145
146