|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any 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 <dlfcn.h> |
|
33 #include <errno.h> |
|
34 #include <mntent.h> |
|
35 |
|
36 #include "sun_nio_fs_LinuxNativeDispatcher.h" |
|
37 |
|
38 typedef size_t fgetxattr_func(int fd, const char* name, void* value, size_t size); |
|
39 typedef int fsetxattr_func(int fd, const char* name, void* value, size_t size, int flags); |
|
40 typedef int fremovexattr_func(int fd, const char* name); |
|
41 typedef int flistxattr_func(int fd, char* list, size_t size); |
|
42 |
|
43 fgetxattr_func* my_fgetxattr_func = NULL; |
|
44 fsetxattr_func* my_fsetxattr_func = NULL; |
|
45 fremovexattr_func* my_fremovexattr_func = NULL; |
|
46 flistxattr_func* my_flistxattr_func = NULL; |
|
47 |
|
48 static void throwUnixException(JNIEnv* env, int errnum) { |
|
49 jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException", |
|
50 "(I)V", errnum); |
|
51 if (x != NULL) { |
|
52 (*env)->Throw(env, x); |
|
53 } |
|
54 } |
|
55 |
|
56 JNIEXPORT void JNICALL |
|
57 Java_sun_nio_fs_LinuxNativeDispatcher_init(JNIEnv *env, jclass clazz) |
|
58 { |
|
59 my_fgetxattr_func = (fgetxattr_func*)dlsym(RTLD_DEFAULT, "fgetxattr"); |
|
60 my_fsetxattr_func = (fsetxattr_func*)dlsym(RTLD_DEFAULT, "fsetxattr"); |
|
61 my_fremovexattr_func = (fremovexattr_func*)dlsym(RTLD_DEFAULT, "fremovexattr"); |
|
62 my_flistxattr_func = (flistxattr_func*)dlsym(RTLD_DEFAULT, "flistxattr"); |
|
63 } |
|
64 |
|
65 JNIEXPORT jint JNICALL |
|
66 Java_sun_nio_fs_LinuxNativeDispatcher_fgetxattr0(JNIEnv* env, jclass clazz, |
|
67 jint fd, jlong nameAddress, jlong valueAddress, jint valueLen) |
|
68 { |
|
69 size_t res = -1; |
|
70 const char* name = jlong_to_ptr(nameAddress); |
|
71 void* value = jlong_to_ptr(valueAddress); |
|
72 |
|
73 if (my_fgetxattr_func == NULL) { |
|
74 errno = ENOTSUP; |
|
75 } else { |
|
76 /* EINTR not documented */ |
|
77 res = (*my_fgetxattr_func)(fd, name, value, valueLen); |
|
78 } |
|
79 if (res == (size_t)-1) |
|
80 throwUnixException(env, errno); |
|
81 return (jint)res; |
|
82 } |
|
83 |
|
84 JNIEXPORT void JNICALL |
|
85 Java_sun_nio_fs_LinuxNativeDispatcher_fsetxattr0(JNIEnv* env, jclass clazz, |
|
86 jint fd, jlong nameAddress, jlong valueAddress, jint valueLen) |
|
87 { |
|
88 int res = -1; |
|
89 const char* name = jlong_to_ptr(nameAddress); |
|
90 void* value = jlong_to_ptr(valueAddress); |
|
91 |
|
92 if (my_fsetxattr_func == NULL) { |
|
93 errno = ENOTSUP; |
|
94 } else { |
|
95 /* EINTR not documented */ |
|
96 res = (*my_fsetxattr_func)(fd, name, value, valueLen, 0); |
|
97 } |
|
98 if (res == -1) |
|
99 throwUnixException(env, errno); |
|
100 } |
|
101 |
|
102 JNIEXPORT void JNICALL |
|
103 Java_sun_nio_fs_LinuxNativeDispatcher_fremovexattr0(JNIEnv* env, jclass clazz, |
|
104 jint fd, jlong nameAddress) |
|
105 { |
|
106 int res = -1; |
|
107 const char* name = jlong_to_ptr(nameAddress); |
|
108 |
|
109 if (my_fremovexattr_func == NULL) { |
|
110 errno = ENOTSUP; |
|
111 } else { |
|
112 /* EINTR not documented */ |
|
113 res = (*my_fremovexattr_func)(fd, name); |
|
114 } |
|
115 if (res == -1) |
|
116 throwUnixException(env, errno); |
|
117 } |
|
118 |
|
119 JNIEXPORT jint JNICALL |
|
120 Java_sun_nio_fs_LinuxNativeDispatcher_flistxattr(JNIEnv* env, jclass clazz, |
|
121 jint fd, jlong listAddress, jint size) |
|
122 { |
|
123 size_t res = -1; |
|
124 char* list = jlong_to_ptr(listAddress); |
|
125 |
|
126 if (my_flistxattr_func == NULL) { |
|
127 errno = ENOTSUP; |
|
128 } else { |
|
129 /* EINTR not documented */ |
|
130 res = (*my_flistxattr_func)(fd, list, (size_t)size); |
|
131 } |
|
132 if (res == (size_t)-1) |
|
133 throwUnixException(env, errno); |
|
134 return (jint)res; |
|
135 } |
|
136 |
|
137 JNIEXPORT jlong JNICALL |
|
138 Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0(JNIEnv* env, jclass this, jlong pathAddress, |
|
139 jlong modeAddress) |
|
140 { |
|
141 FILE* fp = NULL; |
|
142 const char* path = (const char*)jlong_to_ptr(pathAddress); |
|
143 const char* mode = (const char*)jlong_to_ptr(modeAddress); |
|
144 |
|
145 do { |
|
146 fp = setmntent(path, mode); |
|
147 } while (fp == NULL && errno == EINTR); |
|
148 if (fp == NULL) { |
|
149 throwUnixException(env, errno); |
|
150 } |
|
151 return ptr_to_jlong(fp); |
|
152 } |
|
153 |
|
154 JNIEXPORT void JNICALL |
|
155 Java_sun_nio_fs_LinuxNativeDispatcher_endmntent(JNIEnv* env, jclass this, jlong stream) |
|
156 { |
|
157 FILE* fp = jlong_to_ptr(stream); |
|
158 /* FIXME - man page doesn't explain how errors are returned */ |
|
159 endmntent(fp); |
|
160 } |