author | alanb |
Thu, 16 Aug 2012 11:14:42 +0100 | |
changeset 13571 | 737b7b4bd73a |
parent 12047 | 320a714614e9 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
2 |
* Copyright (c) 2008, 2009, 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 <sys/param.h> |
|
32 |
#include <sys/mount.h> |
|
33 |
#ifdef ST_RDONLY |
|
34 |
#define statfs statvfs |
|
35 |
#define getfsstat getvfsstat |
|
36 |
#define f_flags f_flag |
|
37 |
#define ISREADONLY ST_RDONLY |
|
38 |
#else |
|
39 |
#define ISREADONLY MNT_RDONLY |
|
40 |
#endif |
|
41 |
||
42 |
#include <stdlib.h> |
|
43 |
#include <string.h> |
|
44 |
||
45 |
static jfieldID entry_name; |
|
46 |
static jfieldID entry_dir; |
|
47 |
static jfieldID entry_fstype; |
|
48 |
static jfieldID entry_options; |
|
49 |
||
50 |
struct fsstat_iter { |
|
51 |
struct statfs *buf; |
|
52 |
int pos; |
|
53 |
int nentries; |
|
54 |
}; |
|
55 |
||
56 |
#include "sun_nio_fs_BsdNativeDispatcher.h" |
|
57 |
||
58 |
static void throwUnixException(JNIEnv* env, int errnum) { |
|
59 |
jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException", |
|
60 |
"(I)V", errnum); |
|
61 |
if (x != NULL) { |
|
62 |
(*env)->Throw(env, x); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Initialize jfieldIDs |
|
68 |
*/ |
|
69 |
JNIEXPORT void JNICALL |
|
70 |
Java_sun_nio_fs_BsdNativeDispatcher_initIDs(JNIEnv* env, jclass this) |
|
71 |
{ |
|
72 |
jclass clazz; |
|
73 |
||
74 |
clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry"); |
|
75 |
if (clazz == NULL) { |
|
76 |
return; |
|
77 |
} |
|
78 |
entry_name = (*env)->GetFieldID(env, clazz, "name", "[B"); |
|
79 |
entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B"); |
|
80 |
entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B"); |
|
81 |
entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B"); |
|
82 |
} |
|
83 |
||
84 |
JNIEXPORT jlong JNICALL |
|
85 |
Java_sun_nio_fs_BsdNativeDispatcher_getfsstat(JNIEnv* env, jclass this) |
|
86 |
{ |
|
87 |
int nentries; |
|
88 |
size_t bufsize; |
|
89 |
struct fsstat_iter *iter = malloc(sizeof(*iter)); |
|
90 |
||
91 |
if (iter == NULL) { |
|
92 |
JNU_ThrowOutOfMemoryError(env, "native heap"); |
|
93 |
return 0; |
|
94 |
} |
|
95 |
||
96 |
iter->pos = 0; |
|
97 |
iter->nentries = 0; |
|
98 |
iter->buf = NULL; |
|
99 |
||
100 |
nentries = getfsstat(NULL, 0, MNT_NOWAIT); |
|
101 |
||
102 |
if (nentries <= 0) { |
|
103 |
free(iter); |
|
104 |
throwUnixException(env, errno); |
|
105 |
return 0; |
|
106 |
} |
|
107 |
||
108 |
// It's possible that a new filesystem gets mounted between |
|
109 |
// the first getfsstat and the second so loop until consistant |
|
110 |
||
111 |
while (nentries != iter->nentries) { |
|
112 |
if (iter->buf != NULL) |
|
113 |
free(iter->buf); |
|
114 |
||
115 |
bufsize = nentries * sizeof(struct statfs); |
|
116 |
iter->nentries = nentries; |
|
117 |
||
118 |
iter->buf = malloc(bufsize); |
|
119 |
if (iter->buf == NULL) { |
|
120 |
free(iter); |
|
121 |
JNU_ThrowOutOfMemoryError(env, "native heap"); |
|
122 |
return 0; |
|
123 |
} |
|
124 |
||
125 |
nentries = getfsstat(iter->buf, bufsize, MNT_WAIT); |
|
126 |
if (nentries <= 0) { |
|
127 |
free(iter->buf); |
|
128 |
free(iter); |
|
129 |
throwUnixException(env, errno); |
|
130 |
return 0; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
return (jlong)iter; |
|
135 |
} |
|
136 |
||
137 |
JNIEXPORT jint JNICALL |
|
138 |
Java_sun_nio_fs_BsdNativeDispatcher_fsstatEntry(JNIEnv* env, jclass this, |
|
139 |
jlong value, jobject entry) |
|
140 |
{ |
|
141 |
struct fsstat_iter *iter = jlong_to_ptr(value); |
|
142 |
jsize len; |
|
143 |
jbyteArray bytes; |
|
144 |
char* name; |
|
145 |
char* dir; |
|
146 |
char* fstype; |
|
147 |
char* options; |
|
148 |
dev_t dev; |
|
149 |
||
150 |
if (iter == NULL || iter->pos >= iter->nentries) |
|
151 |
return -1; |
|
152 |
||
153 |
name = iter->buf[iter->pos].f_mntfromname; |
|
154 |
dir = iter->buf[iter->pos].f_mntonname; |
|
155 |
fstype = iter->buf[iter->pos].f_fstypename; |
|
156 |
if (iter->buf[iter->pos].f_flags & ISREADONLY) |
|
157 |
options="ro"; |
|
158 |
else |
|
159 |
options=""; |
|
160 |
||
161 |
iter->pos++; |
|
162 |
||
163 |
len = strlen(name); |
|
164 |
bytes = (*env)->NewByteArray(env, len); |
|
165 |
if (bytes == NULL) |
|
166 |
return -1; |
|
167 |
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)name); |
|
168 |
(*env)->SetObjectField(env, entry, entry_name, bytes); |
|
169 |
||
170 |
len = strlen(dir); |
|
171 |
bytes = (*env)->NewByteArray(env, len); |
|
172 |
if (bytes == NULL) |
|
173 |
return -1; |
|
174 |
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)dir); |
|
175 |
(*env)->SetObjectField(env, entry, entry_dir, bytes); |
|
176 |
||
177 |
len = strlen(fstype); |
|
178 |
bytes = (*env)->NewByteArray(env, len); |
|
179 |
if (bytes == NULL) |
|
180 |
return -1; |
|
181 |
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)fstype); |
|
182 |
(*env)->SetObjectField(env, entry, entry_fstype, bytes); |
|
183 |
||
184 |
len = strlen(options); |
|
185 |
bytes = (*env)->NewByteArray(env, len); |
|
186 |
if (bytes == NULL) |
|
187 |
return -1; |
|
188 |
(*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)options); |
|
189 |
(*env)->SetObjectField(env, entry, entry_options, bytes); |
|
190 |
||
191 |
return 0; |
|
192 |
} |
|
193 |
||
194 |
JNIEXPORT void JNICALL |
|
195 |
Java_sun_nio_fs_BsdNativeDispatcher_endfsstat(JNIEnv* env, jclass this, jlong value) |
|
196 |
{ |
|
197 |
struct fsstat_iter *iter = jlong_to_ptr(value); |
|
198 |
||
199 |
if (iter != NULL) { |
|
200 |
free(iter->buf); |
|
201 |
free(iter); |
|
202 |
} |
|
203 |
} |
|
13571
737b7b4bd73a
7191556: (fs) UnixNativeDispatcher.getextmntent should be moved into platform specific code
alanb
parents:
12047
diff
changeset
|
204 |