1 /*
2 * Copyright (c) 2008, 2018, 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 <stdio.h>
27 #include <stdlib.h>
28 #include <limits.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <errno.h>
35 #include <dlfcn.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/statvfs.h>
39 #include <sys/time.h>
40
41 #ifdef __solaris__
42 #include <strings.h>
43 #endif
44
45 #if defined(__linux__) || defined(_AIX)
46 #include <string.h>
47 #endif
48
49 // Android-changed: Fuchsia: Alias *64 on Fuchsia builds. http://b/119496969
50 // #ifdef _ALLBSD_SOURCE
51 #if defined(_ALLBSD_SOURCE) || defined(__Fuchsia__)
52 #include <string.h>
53
54 #define stat64 stat
55 #define statvfs64 statvfs
56
57 #define open64 open
58 #define fstat64 fstat
59 #define lstat64 lstat
60 #define dirent64 dirent
61 // Android-changed: Integrate OpenJDK 12 commit to use readdir, not readdir_r. b/64362645
62 // Integrate UnixNativeDispatcher.c changes from http://hg.openjdk.java.net/jdk/jdk/rev/90144bc10fe6
63 // #define readdir64_r readdir_r
64 #define readdir64 readdir
65 #endif
66
67 #include "jni.h"
68 #include "jni_util.h"
69 #include "jlong.h"
70
71 #include "sun_nio_fs_UnixNativeDispatcher.h"
72
73 /**
74 * Size of password or group entry when not available via sysconf
75 */
76 #define ENT_BUF_SIZE 1024
77
78 #define RESTARTABLE(_cmd, _result) do { \
79 do { \
80 _result = _cmd; \
81 } while((_result == -1) && (errno == EINTR)); \
82 } while(0)
83
84 #define RESTARTABLE_RETURN_PTR(_cmd, _result) do { \
85 do { \
86 _result = _cmd; \
87 } while((_result == NULL) && (errno == EINTR)); \
88 } while(0)
89
90 static jfieldID attrs_st_mode;
91 static jfieldID attrs_st_ino;
92 static jfieldID attrs_st_dev;
93 static jfieldID attrs_st_rdev;
94 static jfieldID attrs_st_nlink;
95 static jfieldID attrs_st_uid;
96 static jfieldID attrs_st_gid;
97 static jfieldID attrs_st_size;
98 static jfieldID attrs_st_atime_sec;
99 static jfieldID attrs_st_atime_nsec;
100 static jfieldID attrs_st_mtime_sec;
101 static jfieldID attrs_st_mtime_nsec;
102 static jfieldID attrs_st_ctime_sec;
103 static jfieldID attrs_st_ctime_nsec;
104
105 #ifdef _DARWIN_FEATURE_64_BIT_INODE
106 static jfieldID attrs_st_birthtime_sec;
107 #endif
108
109 static jfieldID attrs_f_frsize;
110 static jfieldID attrs_f_blocks;
111 static jfieldID attrs_f_bfree;
112 static jfieldID attrs_f_bavail;
113
114 static jfieldID entry_name;
115 static jfieldID entry_dir;
116 static jfieldID entry_fstype;
117 static jfieldID entry_options;
118 static jfieldID entry_dev;
119
120 /**
121 * System calls that may not be available at run time.
122 */
123 typedef int openat64_func(int, const char *, int, ...);
124 typedef int fstatat64_func(int, const char *, struct stat64 *, int);
125 typedef int unlinkat_func(int, const char*, int);
126 typedef int renameat_func(int, const char*, int, const char*);
127 typedef int futimesat_func(int, const char *, const struct timeval *);
128 typedef DIR* fdopendir_func(int);
129
130 static openat64_func* my_openat64_func = NULL;
131 static fstatat64_func* my_fstatat64_func = NULL;
132 static unlinkat_func* my_unlinkat_func = NULL;
133 static renameat_func* my_renameat_func = NULL;
134 static futimesat_func* my_futimesat_func = NULL;
135 static fdopendir_func* my_fdopendir_func = NULL;
136
137 /**
138 * fstatat missing from glibc on Linux. Temporary workaround
139 * for x86/x64.
140 */
141 #if defined(__linux__) && defined(__i386)
142 #define FSTATAT64_SYSCALL_AVAILABLE
fstatat64_wrapper(int dfd,const char * path,struct stat64 * statbuf,int flag)143 static int fstatat64_wrapper(int dfd, const char *path,
144 struct stat64 *statbuf, int flag)
145 {
146 #ifndef __NR_fstatat64
147 #define __NR_fstatat64 300
148 #endif
149 return syscall(__NR_fstatat64, dfd, path, statbuf, flag);
150 }
151 #endif
152
153 #if defined(__linux__) && defined(__x86_64__)
154 #define FSTATAT64_SYSCALL_AVAILABLE
fstatat64_wrapper(int dfd,const char * path,struct stat64 * statbuf,int flag)155 static int fstatat64_wrapper(int dfd, const char *path,
156 struct stat64 *statbuf, int flag)
157 {
158 #ifndef __NR_newfstatat
159 #define __NR_newfstatat 262
160 #endif
161 return syscall(__NR_newfstatat, dfd, path, statbuf, flag);
162 }
163 #endif
164
165 /**
166 * Call this to throw an internal UnixException when a system/library
167 * call fails
168 */
throwUnixException(JNIEnv * env,int errnum)169 static void throwUnixException(JNIEnv* env, int errnum) {
170 jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
171 "(I)V", errnum);
172 if (x != NULL) {
173 (*env)->Throw(env, x);
174 }
175 }
176
177 /**
178 * Initialization
179 */
180 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv * env,jclass this)181 Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
182 {
183 jint capabilities = 0;
184 jclass clazz;
185
186 clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileAttributes");
187 CHECK_NULL_RETURN(clazz, 0);
188 attrs_st_mode = (*env)->GetFieldID(env, clazz, "st_mode", "I");
189 CHECK_NULL_RETURN(attrs_st_mode, 0);
190 attrs_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J");
191 CHECK_NULL_RETURN(attrs_st_ino, 0);
192 attrs_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J");
193 CHECK_NULL_RETURN(attrs_st_dev, 0);
194 attrs_st_rdev = (*env)->GetFieldID(env, clazz, "st_rdev", "J");
195 CHECK_NULL_RETURN(attrs_st_rdev, 0);
196 attrs_st_nlink = (*env)->GetFieldID(env, clazz, "st_nlink", "I");
197 CHECK_NULL_RETURN(attrs_st_nlink, 0);
198 attrs_st_uid = (*env)->GetFieldID(env, clazz, "st_uid", "I");
199 CHECK_NULL_RETURN(attrs_st_uid, 0);
200 attrs_st_gid = (*env)->GetFieldID(env, clazz, "st_gid", "I");
201 CHECK_NULL_RETURN(attrs_st_gid, 0);
202 attrs_st_size = (*env)->GetFieldID(env, clazz, "st_size", "J");
203 CHECK_NULL_RETURN(attrs_st_size, 0);
204 attrs_st_atime_sec = (*env)->GetFieldID(env, clazz, "st_atime_sec", "J");
205 CHECK_NULL_RETURN(attrs_st_atime_sec, 0);
206 attrs_st_atime_nsec = (*env)->GetFieldID(env, clazz, "st_atime_nsec", "J");
207 CHECK_NULL_RETURN(attrs_st_atime_nsec, 0);
208 attrs_st_mtime_sec = (*env)->GetFieldID(env, clazz, "st_mtime_sec", "J");
209 CHECK_NULL_RETURN(attrs_st_mtime_sec, 0);
210 attrs_st_mtime_nsec = (*env)->GetFieldID(env, clazz, "st_mtime_nsec", "J");
211 CHECK_NULL_RETURN(attrs_st_mtime_nsec, 0);
212 attrs_st_ctime_sec = (*env)->GetFieldID(env, clazz, "st_ctime_sec", "J");
213 CHECK_NULL_RETURN(attrs_st_ctime_sec, 0);
214 attrs_st_ctime_nsec = (*env)->GetFieldID(env, clazz, "st_ctime_nsec", "J");
215 CHECK_NULL_RETURN(attrs_st_ctime_nsec, 0);
216
217 #ifdef _DARWIN_FEATURE_64_BIT_INODE
218 attrs_st_birthtime_sec = (*env)->GetFieldID(env, clazz, "st_birthtime_sec", "J");
219 CHECK_NULL_RETURN(attrs_st_birthtime_sec, 0);
220 #endif
221
222 clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileStoreAttributes");
223 CHECK_NULL_RETURN(clazz, 0);
224 attrs_f_frsize = (*env)->GetFieldID(env, clazz, "f_frsize", "J");
225 CHECK_NULL_RETURN(attrs_f_frsize, 0);
226 attrs_f_blocks = (*env)->GetFieldID(env, clazz, "f_blocks", "J");
227 CHECK_NULL_RETURN(attrs_f_blocks, 0);
228 attrs_f_bfree = (*env)->GetFieldID(env, clazz, "f_bfree", "J");
229 CHECK_NULL_RETURN(attrs_f_bfree, 0);
230 attrs_f_bavail = (*env)->GetFieldID(env, clazz, "f_bavail", "J");
231 CHECK_NULL_RETURN(attrs_f_bavail, 0);
232
233 clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry");
234 CHECK_NULL_RETURN(clazz, 0);
235 entry_name = (*env)->GetFieldID(env, clazz, "name", "[B");
236 CHECK_NULL_RETURN(entry_name, 0);
237 entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B");
238 CHECK_NULL_RETURN(entry_dir, 0);
239 entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B");
240 CHECK_NULL_RETURN(entry_fstype, 0);
241 entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B");
242 CHECK_NULL_RETURN(entry_options, 0);
243 entry_dev = (*env)->GetFieldID(env, clazz, "dev", "J");
244 CHECK_NULL_RETURN(entry_dev, 0);
245
246 /* system calls that might not be available at run time */
247
248 #if (defined(__solaris__) && defined(_LP64)) || defined(_ALLBSD_SOURCE)
249 /* Solaris 64-bit does not have openat64/fstatat64 */
250 my_openat64_func = (openat64_func*)dlsym(RTLD_DEFAULT, "openat");
251 my_fstatat64_func = (fstatat64_func*)dlsym(RTLD_DEFAULT, "fstatat");
252 #else
253 my_openat64_func = (openat64_func*) dlsym(RTLD_DEFAULT, "openat64");
254 my_fstatat64_func = (fstatat64_func*) dlsym(RTLD_DEFAULT, "fstatat64");
255 #endif
256 my_unlinkat_func = (unlinkat_func*) dlsym(RTLD_DEFAULT, "unlinkat");
257 my_renameat_func = (renameat_func*) dlsym(RTLD_DEFAULT, "renameat");
258 my_futimesat_func = (futimesat_func*) dlsym(RTLD_DEFAULT, "futimesat");
259 my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir");
260
261 #if defined(FSTATAT64_SYSCALL_AVAILABLE)
262 /* fstatat64 missing from glibc */
263 if (my_fstatat64_func == NULL)
264 my_fstatat64_func = (fstatat64_func*)&fstatat64_wrapper;
265 #endif
266
267 /* supports futimes or futimesat */
268
269 #ifdef _ALLBSD_SOURCE
270 capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
271 #else
272 if (my_futimesat_func != NULL)
273 capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
274 #endif
275
276 /* supports openat, etc. */
277
278 if (my_openat64_func != NULL && my_fstatat64_func != NULL &&
279 my_unlinkat_func != NULL && my_renameat_func != NULL &&
280 my_futimesat_func != NULL && my_fdopendir_func != NULL)
281 {
282 capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_OPENAT;
283 }
284
285 /* supports file birthtime */
286
287 #ifdef _DARWIN_FEATURE_64_BIT_INODE
288 capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_BIRTHTIME;
289 #endif
290
291 return capabilities;
292 }
293
294 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_getcwd(JNIEnv * env,jclass this)295 Java_sun_nio_fs_UnixNativeDispatcher_getcwd(JNIEnv* env, jclass this) {
296 jbyteArray result = NULL;
297 char buf[PATH_MAX+1];
298
299 /* EINTR not listed as a possible error */
300 char* cwd = getcwd(buf, sizeof(buf));
301 if (cwd == NULL) {
302 throwUnixException(env, errno);
303 } else {
304 jsize len = (jsize)strlen(buf);
305 result = (*env)->NewByteArray(env, len);
306 if (result != NULL) {
307 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)buf);
308 }
309 }
310 return result;
311 }
312
313 JNIEXPORT jbyteArray
Java_sun_nio_fs_UnixNativeDispatcher_strerror(JNIEnv * env,jclass this,jint error)314 Java_sun_nio_fs_UnixNativeDispatcher_strerror(JNIEnv* env, jclass this, jint error)
315 {
316 char* msg;
317 jsize len;
318 jbyteArray bytes;
319
320 #ifdef _AIX
321 /* strerror() is not thread-safe on AIX so we have to use strerror_r() */
322 char buffer[256];
323 msg = (strerror_r((int)error, buffer, 256) == 0) ? buffer : "Error while calling strerror_r";
324 #else
325 msg = strerror((int)error);
326 #endif
327 len = strlen(msg);
328 bytes = (*env)->NewByteArray(env, len);
329 if (bytes != NULL) {
330 (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)msg);
331 }
332 return bytes;
333 }
334
335 JNIEXPORT jint
Java_sun_nio_fs_UnixNativeDispatcher_dup(JNIEnv * env,jclass this,jint fd)336 Java_sun_nio_fs_UnixNativeDispatcher_dup(JNIEnv* env, jclass this, jint fd) {
337
338 int res = -1;
339
340 RESTARTABLE(dup((int)fd), res);
341 if (res == -1) {
342 throwUnixException(env, errno);
343 }
344 return (jint)res;
345 }
346
347 JNIEXPORT jlong JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fopen0(JNIEnv * env,jclass this,jlong pathAddress,jlong modeAddress)348 Java_sun_nio_fs_UnixNativeDispatcher_fopen0(JNIEnv* env, jclass this,
349 jlong pathAddress, jlong modeAddress)
350 {
351 FILE* fp = NULL;
352 const char* path = (const char*)jlong_to_ptr(pathAddress);
353 const char* mode = (const char*)jlong_to_ptr(modeAddress);
354
355 do {
356 fp = fopen(path, mode);
357 } while (fp == NULL && errno == EINTR);
358
359 if (fp == NULL) {
360 throwUnixException(env, errno);
361 }
362
363 return ptr_to_jlong(fp);
364 }
365
366 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv * env,jclass this,jlong stream)367 Java_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv* env, jclass this, jlong stream)
368 {
369 FILE* fp = jlong_to_ptr(stream);
370
371 /* NOTE: fclose() wrapper is only used with read-only streams.
372 * If it ever is used with write streams, it might be better to add
373 * RESTARTABLE(fflush(fp)) before closing, to make sure the stream
374 * is completely written even if fclose() failed.
375 */
376 if (fclose(fp) == EOF && errno != EINTR) {
377 throwUnixException(env, errno);
378 }
379 }
380
381 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_open0(JNIEnv * env,jclass this,jlong pathAddress,jint oflags,jint mode)382 Java_sun_nio_fs_UnixNativeDispatcher_open0(JNIEnv* env, jclass this,
383 jlong pathAddress, jint oflags, jint mode)
384 {
385 jint fd;
386 const char* path = (const char*)jlong_to_ptr(pathAddress);
387
388 RESTARTABLE(open64(path, (int)oflags, (mode_t)mode), fd);
389 if (fd == -1) {
390 throwUnixException(env, errno);
391 }
392 return fd;
393 }
394
395 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_openat0(JNIEnv * env,jclass this,jint dfd,jlong pathAddress,jint oflags,jint mode)396 Java_sun_nio_fs_UnixNativeDispatcher_openat0(JNIEnv* env, jclass this, jint dfd,
397 jlong pathAddress, jint oflags, jint mode)
398 {
399 jint fd;
400 const char* path = (const char*)jlong_to_ptr(pathAddress);
401
402 if (my_openat64_func == NULL) {
403 JNU_ThrowInternalError(env, "should not reach here");
404 return -1;
405 }
406
407 RESTARTABLE((*my_openat64_func)(dfd, path, (int)oflags, (mode_t)mode), fd);
408 if (fd == -1) {
409 throwUnixException(env, errno);
410 }
411 return fd;
412 }
413
414 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_close(JNIEnv * env,jclass this,jint fd)415 Java_sun_nio_fs_UnixNativeDispatcher_close(JNIEnv* env, jclass this, jint fd) {
416 // BEGIN Android-changed: Integrate OpenJDK 12 commit to use readdir, not readdir_r. b/64362645
417 // int err;
418 // /* TDB - need to decide if EIO and other errors should cause exception */
419 // RESTARTABLE(close((int)fd), err);
420 int res;
421
422 #if defined(_AIX)
423 /* AIX allows close to be restarted after EINTR */
424 RESTARTABLE(close((int)fd), res);
425 #else
426 res = close((int)fd);
427 #endif
428 if (res == -1 && errno != EINTR) {
429 throwUnixException(env, errno);
430 }
431 // END Android-changed: Integrate OpenJDK 12 commit to use readdir, not readdir_r. b/64362645
432 }
433
434 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_read(JNIEnv * env,jclass this,jint fd,jlong address,jint nbytes)435 Java_sun_nio_fs_UnixNativeDispatcher_read(JNIEnv* env, jclass this, jint fd,
436 jlong address, jint nbytes)
437 {
438 ssize_t n;
439 void* bufp = jlong_to_ptr(address);
440 RESTARTABLE(read((int)fd, bufp, (size_t)nbytes), n);
441 if (n == -1) {
442 throwUnixException(env, errno);
443 }
444 return (jint)n;
445 }
446
447 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_write(JNIEnv * env,jclass this,jint fd,jlong address,jint nbytes)448 Java_sun_nio_fs_UnixNativeDispatcher_write(JNIEnv* env, jclass this, jint fd,
449 jlong address, jint nbytes)
450 {
451 ssize_t n;
452 void* bufp = jlong_to_ptr(address);
453 RESTARTABLE(write((int)fd, bufp, (size_t)nbytes), n);
454 if (n == -1) {
455 throwUnixException(env, errno);
456 }
457 return (jint)n;
458 }
459
460 /**
461 * Copy stat64 members into sun.nio.fs.UnixFileAttributes
462 */
prepAttributes(JNIEnv * env,struct stat64 * buf,jobject attrs)463 static void prepAttributes(JNIEnv* env, struct stat64* buf, jobject attrs) {
464 (*env)->SetIntField(env, attrs, attrs_st_mode, (jint)buf->st_mode);
465 (*env)->SetLongField(env, attrs, attrs_st_ino, (jlong)buf->st_ino);
466 (*env)->SetLongField(env, attrs, attrs_st_dev, (jlong)buf->st_dev);
467 (*env)->SetLongField(env, attrs, attrs_st_rdev, (jlong)buf->st_rdev);
468 (*env)->SetIntField(env, attrs, attrs_st_nlink, (jint)buf->st_nlink);
469 (*env)->SetIntField(env, attrs, attrs_st_uid, (jint)buf->st_uid);
470 (*env)->SetIntField(env, attrs, attrs_st_gid, (jint)buf->st_gid);
471 (*env)->SetLongField(env, attrs, attrs_st_size, (jlong)buf->st_size);
472 (*env)->SetLongField(env, attrs, attrs_st_atime_sec, (jlong)buf->st_atime);
473 (*env)->SetLongField(env, attrs, attrs_st_mtime_sec, (jlong)buf->st_mtime);
474 (*env)->SetLongField(env, attrs, attrs_st_ctime_sec, (jlong)buf->st_ctime);
475
476 #ifdef _DARWIN_FEATURE_64_BIT_INODE
477 (*env)->SetLongField(env, attrs, attrs_st_birthtime_sec, (jlong)buf->st_birthtime);
478 #endif
479
480 #if (_POSIX_C_SOURCE >= 200809L) || defined(__solaris__)
481 (*env)->SetLongField(env, attrs, attrs_st_atime_nsec, (jlong)buf->st_atim.tv_nsec);
482 (*env)->SetLongField(env, attrs, attrs_st_mtime_nsec, (jlong)buf->st_mtim.tv_nsec);
483 (*env)->SetLongField(env, attrs, attrs_st_ctime_nsec, (jlong)buf->st_ctim.tv_nsec);
484 #endif
485 }
486
487 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_stat0(JNIEnv * env,jclass this,jlong pathAddress,jobject attrs)488 Java_sun_nio_fs_UnixNativeDispatcher_stat0(JNIEnv* env, jclass this,
489 jlong pathAddress, jobject attrs)
490 {
491 int err;
492 struct stat64 buf;
493 const char* path = (const char*)jlong_to_ptr(pathAddress);
494
495 RESTARTABLE(stat64(path, &buf), err);
496 if (err == -1) {
497 throwUnixException(env, errno);
498 } else {
499 prepAttributes(env, &buf, attrs);
500 }
501 }
502
503 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_stat1(JNIEnv * env,jclass this,jlong pathAddress)504 Java_sun_nio_fs_UnixNativeDispatcher_stat1(JNIEnv* env, jclass this, jlong pathAddress) {
505 int err;
506 struct stat64 buf;
507 const char* path = (const char*)jlong_to_ptr(pathAddress);
508
509 RESTARTABLE(stat64(path, &buf), err);
510 if (err == -1) {
511 return 0;
512 } else {
513 return (jint)buf.st_mode;
514 }
515 }
516
517 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_lstat0(JNIEnv * env,jclass this,jlong pathAddress,jobject attrs)518 Java_sun_nio_fs_UnixNativeDispatcher_lstat0(JNIEnv* env, jclass this,
519 jlong pathAddress, jobject attrs)
520 {
521 int err;
522 struct stat64 buf;
523 const char* path = (const char*)jlong_to_ptr(pathAddress);
524
525 RESTARTABLE(lstat64(path, &buf), err);
526 if (err == -1) {
527 throwUnixException(env, errno);
528 } else {
529 prepAttributes(env, &buf, attrs);
530 }
531 }
532
533 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fstat(JNIEnv * env,jclass this,jint fd,jobject attrs)534 Java_sun_nio_fs_UnixNativeDispatcher_fstat(JNIEnv* env, jclass this, jint fd,
535 jobject attrs)
536 {
537 int err;
538 struct stat64 buf;
539
540 RESTARTABLE(fstat64((int)fd, &buf), err);
541 if (err == -1) {
542 throwUnixException(env, errno);
543 } else {
544 prepAttributes(env, &buf, attrs);
545 }
546 }
547
548 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv * env,jclass this,jint dfd,jlong pathAddress,jint flag,jobject attrs)549 Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd,
550 jlong pathAddress, jint flag, jobject attrs)
551 {
552 int err;
553 struct stat64 buf;
554 const char* path = (const char*)jlong_to_ptr(pathAddress);
555
556 if (my_fstatat64_func == NULL) {
557 JNU_ThrowInternalError(env, "should not reach here");
558 return;
559 }
560 RESTARTABLE((*my_fstatat64_func)((int)dfd, path, &buf, (int)flag), err);
561 if (err == -1) {
562 throwUnixException(env, errno);
563 } else {
564 prepAttributes(env, &buf, attrs);
565 }
566 }
567
568 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_chmod0(JNIEnv * env,jclass this,jlong pathAddress,jint mode)569 Java_sun_nio_fs_UnixNativeDispatcher_chmod0(JNIEnv* env, jclass this,
570 jlong pathAddress, jint mode)
571 {
572 int err;
573 const char* path = (const char*)jlong_to_ptr(pathAddress);
574
575 RESTARTABLE(chmod(path, (mode_t)mode), err);
576 if (err == -1) {
577 throwUnixException(env, errno);
578 }
579 }
580
581 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fchmod(JNIEnv * env,jclass this,jint filedes,jint mode)582 Java_sun_nio_fs_UnixNativeDispatcher_fchmod(JNIEnv* env, jclass this, jint filedes,
583 jint mode)
584 {
585 int err;
586
587 RESTARTABLE(fchmod((int)filedes, (mode_t)mode), err);
588 if (err == -1) {
589 throwUnixException(env, errno);
590 }
591 }
592
593
594 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_chown0(JNIEnv * env,jclass this,jlong pathAddress,jint uid,jint gid)595 Java_sun_nio_fs_UnixNativeDispatcher_chown0(JNIEnv* env, jclass this,
596 jlong pathAddress, jint uid, jint gid)
597 {
598 int err;
599 const char* path = (const char*)jlong_to_ptr(pathAddress);
600
601 RESTARTABLE(chown(path, (uid_t)uid, (gid_t)gid), err);
602 if (err == -1) {
603 throwUnixException(env, errno);
604 }
605 }
606
607 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_lchown0(JNIEnv * env,jclass this,jlong pathAddress,jint uid,jint gid)608 Java_sun_nio_fs_UnixNativeDispatcher_lchown0(JNIEnv* env, jclass this, jlong pathAddress, jint uid, jint gid)
609 {
610 int err;
611 const char* path = (const char*)jlong_to_ptr(pathAddress);
612
613 RESTARTABLE(lchown(path, (uid_t)uid, (gid_t)gid), err);
614 if (err == -1) {
615 throwUnixException(env, errno);
616 }
617 }
618
619 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fchown(JNIEnv * env,jclass this,jint filedes,jint uid,jint gid)620 Java_sun_nio_fs_UnixNativeDispatcher_fchown(JNIEnv* env, jclass this, jint filedes, jint uid, jint gid)
621 {
622 int err;
623
624 RESTARTABLE(fchown(filedes, (uid_t)uid, (gid_t)gid), err);
625 if (err == -1) {
626 throwUnixException(env, errno);
627 }
628 }
629
630 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_utimes0(JNIEnv * env,jclass this,jlong pathAddress,jlong accessTime,jlong modificationTime)631 Java_sun_nio_fs_UnixNativeDispatcher_utimes0(JNIEnv* env, jclass this,
632 jlong pathAddress, jlong accessTime, jlong modificationTime)
633 {
634 int err;
635 struct timeval times[2];
636 const char* path = (const char*)jlong_to_ptr(pathAddress);
637
638 times[0].tv_sec = accessTime / 1000000;
639 times[0].tv_usec = accessTime % 1000000;
640
641 times[1].tv_sec = modificationTime / 1000000;
642 times[1].tv_usec = modificationTime % 1000000;
643
644 RESTARTABLE(utimes(path, ×[0]), err);
645 if (err == -1) {
646 throwUnixException(env, errno);
647 }
648 }
649
650 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv * env,jclass this,jint filedes,jlong accessTime,jlong modificationTime)651 Java_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv* env, jclass this, jint filedes,
652 jlong accessTime, jlong modificationTime)
653 {
654 struct timeval times[2];
655 int err = 0;
656
657 times[0].tv_sec = accessTime / 1000000;
658 times[0].tv_usec = accessTime % 1000000;
659
660 times[1].tv_sec = modificationTime / 1000000;
661 times[1].tv_usec = modificationTime % 1000000;
662
663 #ifdef _ALLBSD_SOURCE
664 RESTARTABLE(futimes(filedes, ×[0]), err);
665 #else
666 if (my_futimesat_func == NULL) {
667 JNU_ThrowInternalError(env, "my_ftimesat_func is NULL");
668 return;
669 }
670 RESTARTABLE((*my_futimesat_func)(filedes, NULL, ×[0]), err);
671 #endif
672 if (err == -1) {
673 throwUnixException(env, errno);
674 }
675 }
676
677 JNIEXPORT jlong JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_opendir0(JNIEnv * env,jclass this,jlong pathAddress)678 Java_sun_nio_fs_UnixNativeDispatcher_opendir0(JNIEnv* env, jclass this,
679 jlong pathAddress)
680 {
681 DIR* dir;
682 const char* path = (const char*)jlong_to_ptr(pathAddress);
683
684 /* EINTR not listed as a possible error */
685 dir = opendir(path);
686 if (dir == NULL) {
687 throwUnixException(env, errno);
688 }
689 return ptr_to_jlong(dir);
690 }
691
692 JNIEXPORT jlong JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fdopendir(JNIEnv * env,jclass this,int dfd)693 Java_sun_nio_fs_UnixNativeDispatcher_fdopendir(JNIEnv* env, jclass this, int dfd) {
694 DIR* dir;
695
696 if (my_fdopendir_func == NULL) {
697 JNU_ThrowInternalError(env, "should not reach here");
698 return (jlong)-1;
699 }
700
701 /* EINTR not listed as a possible error */
702 dir = (*my_fdopendir_func)((int)dfd);
703 if (dir == NULL) {
704 throwUnixException(env, errno);
705 }
706 return ptr_to_jlong(dir);
707 }
708
709 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv * env,jclass this,jlong dir)710 Java_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv* env, jclass this, jlong dir) {
711 DIR* dirp = jlong_to_ptr(dir);
712
713 if (closedir(dirp) == -1 && errno != EINTR) {
714 throwUnixException(env, errno);
715 }
716 }
717
718 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_readdir(JNIEnv * env,jclass this,jlong value)719 Java_sun_nio_fs_UnixNativeDispatcher_readdir(JNIEnv* env, jclass this, jlong value) {
720 // BEGIN Android-changed: Integrate OpenJDK 12 commit to use readdir, not readdir_r. b/64362645
721 /*
722 struct dirent64* result;
723 struct {
724 struct dirent64 buf;
725 char name_extra[PATH_MAX + 1 - sizeof result->d_name];
726 } entry;
727 struct dirent64* ptr = &entry.buf;
728 int res;
729 DIR* dirp = jlong_to_ptr(value);
730
731 * EINTR not listed as a possible error *
732 * TDB: reentrant version probably not required here *
733 res = readdir64_r(dirp, ptr, &result);
734
735 #ifdef _AIX
736 * On AIX, readdir_r() returns EBADF (i.e. '9') and sets 'result' to NULL for the *
737 * directory stream end. Otherwise, 'errno' will contain the error code. *
738 if (res != 0) {
739 res = (result == NULL && res == EBADF) ? 0 : errno;
740 }
741 #endif
742
743 if (res != 0) {
744 throwUnixException(env, res);
745 return NULL;
746 } else {
747 if (result == NULL) {
748 return NULL;
749 } else {
750 jsize len = strlen(ptr->d_name);
751 jbyteArray bytes = (*env)->NewByteArray(env, len);
752 if (bytes != NULL) {
753 (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)(ptr->d_name));
754 }
755 return bytes;
756 }
757 }
758 */
759 DIR* dirp = jlong_to_ptr(value);
760 struct dirent64* ptr;
761
762 errno = 0;
763 ptr = readdir64(dirp);
764 if (ptr == NULL) {
765 if (errno != 0) {
766 throwUnixException(env, errno);
767 }
768 return NULL;
769 } else {
770 jsize len = strlen(ptr->d_name);
771 jbyteArray bytes = (*env)->NewByteArray(env, len);
772 if (bytes != NULL) {
773 (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)(ptr->d_name));
774 }
775 return bytes;
776 }
777 // END Android-changed: Integrate OpenJDK 12 commit to use readdir, not readdir_r. b/64362645
778 }
779
780 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_mkdir0(JNIEnv * env,jclass this,jlong pathAddress,jint mode)781 Java_sun_nio_fs_UnixNativeDispatcher_mkdir0(JNIEnv* env, jclass this,
782 jlong pathAddress, jint mode)
783 {
784 const char* path = (const char*)jlong_to_ptr(pathAddress);
785
786 /* EINTR not listed as a possible error */
787 if (mkdir(path, (mode_t)mode) == -1) {
788 throwUnixException(env, errno);
789 }
790 }
791
792 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_rmdir0(JNIEnv * env,jclass this,jlong pathAddress)793 Java_sun_nio_fs_UnixNativeDispatcher_rmdir0(JNIEnv* env, jclass this,
794 jlong pathAddress)
795 {
796 const char* path = (const char*)jlong_to_ptr(pathAddress);
797
798 /* EINTR not listed as a possible error */
799 if (rmdir(path) == -1) {
800 throwUnixException(env, errno);
801 }
802 }
803
804 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_link0(JNIEnv * env,jclass this,jlong existingAddress,jlong newAddress)805 Java_sun_nio_fs_UnixNativeDispatcher_link0(JNIEnv* env, jclass this,
806 jlong existingAddress, jlong newAddress)
807 {
808 int err;
809 const char* existing = (const char*)jlong_to_ptr(existingAddress);
810 const char* newname = (const char*)jlong_to_ptr(newAddress);
811
812 RESTARTABLE(link(existing, newname), err);
813 if (err == -1) {
814 throwUnixException(env, errno);
815 }
816 }
817
818
819 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_unlink0(JNIEnv * env,jclass this,jlong pathAddress)820 Java_sun_nio_fs_UnixNativeDispatcher_unlink0(JNIEnv* env, jclass this,
821 jlong pathAddress)
822 {
823 const char* path = (const char*)jlong_to_ptr(pathAddress);
824
825 /* EINTR not listed as a possible error */
826 if (unlink(path) == -1) {
827 throwUnixException(env, errno);
828 }
829 }
830
831 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_unlinkat0(JNIEnv * env,jclass this,jint dfd,jlong pathAddress,jint flags)832 Java_sun_nio_fs_UnixNativeDispatcher_unlinkat0(JNIEnv* env, jclass this, jint dfd,
833 jlong pathAddress, jint flags)
834 {
835 const char* path = (const char*)jlong_to_ptr(pathAddress);
836
837 if (my_unlinkat_func == NULL) {
838 JNU_ThrowInternalError(env, "should not reach here");
839 return;
840 }
841
842 /* EINTR not listed as a possible error */
843 if ((*my_unlinkat_func)((int)dfd, path, (int)flags) == -1) {
844 throwUnixException(env, errno);
845 }
846 }
847
848 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_rename0(JNIEnv * env,jclass this,jlong fromAddress,jlong toAddress)849 Java_sun_nio_fs_UnixNativeDispatcher_rename0(JNIEnv* env, jclass this,
850 jlong fromAddress, jlong toAddress)
851 {
852 const char* from = (const char*)jlong_to_ptr(fromAddress);
853 const char* to = (const char*)jlong_to_ptr(toAddress);
854
855 /* EINTR not listed as a possible error */
856 if (rename(from, to) == -1) {
857 throwUnixException(env, errno);
858 }
859 }
860
861 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_renameat0(JNIEnv * env,jclass this,jint fromfd,jlong fromAddress,jint tofd,jlong toAddress)862 Java_sun_nio_fs_UnixNativeDispatcher_renameat0(JNIEnv* env, jclass this,
863 jint fromfd, jlong fromAddress, jint tofd, jlong toAddress)
864 {
865 const char* from = (const char*)jlong_to_ptr(fromAddress);
866 const char* to = (const char*)jlong_to_ptr(toAddress);
867
868 if (my_renameat_func == NULL) {
869 JNU_ThrowInternalError(env, "should not reach here");
870 return;
871 }
872
873 /* EINTR not listed as a possible error */
874 if ((*my_renameat_func)((int)fromfd, from, (int)tofd, to) == -1) {
875 throwUnixException(env, errno);
876 }
877 }
878
879 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_symlink0(JNIEnv * env,jclass this,jlong targetAddress,jlong linkAddress)880 Java_sun_nio_fs_UnixNativeDispatcher_symlink0(JNIEnv* env, jclass this,
881 jlong targetAddress, jlong linkAddress)
882 {
883 const char* target = (const char*)jlong_to_ptr(targetAddress);
884 const char* link = (const char*)jlong_to_ptr(linkAddress);
885
886 /* EINTR not listed as a possible error */
887 if (symlink(target, link) == -1) {
888 throwUnixException(env, errno);
889 }
890 }
891
892 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_readlink0(JNIEnv * env,jclass this,jlong pathAddress)893 Java_sun_nio_fs_UnixNativeDispatcher_readlink0(JNIEnv* env, jclass this,
894 jlong pathAddress)
895 {
896 jbyteArray result = NULL;
897 char target[PATH_MAX+1];
898 const char* path = (const char*)jlong_to_ptr(pathAddress);
899
900 /* EINTR not listed as a possible error */
901 int n = readlink(path, target, sizeof(target));
902 if (n == -1) {
903 throwUnixException(env, errno);
904 } else {
905 jsize len;
906 if (n == sizeof(target)) {
907 n--;
908 }
909 target[n] = '\0';
910 len = (jsize)strlen(target);
911 result = (*env)->NewByteArray(env, len);
912 if (result != NULL) {
913 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)target);
914 }
915 }
916 return result;
917 }
918
919 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_realpath0(JNIEnv * env,jclass this,jlong pathAddress)920 Java_sun_nio_fs_UnixNativeDispatcher_realpath0(JNIEnv* env, jclass this,
921 jlong pathAddress)
922 {
923 jbyteArray result = NULL;
924 char resolved[PATH_MAX+1];
925 const char* path = (const char*)jlong_to_ptr(pathAddress);
926
927 /* EINTR not listed as a possible error */
928 if (realpath(path, resolved) == NULL) {
929 throwUnixException(env, errno);
930 } else {
931 jsize len = (jsize)strlen(resolved);
932 result = (*env)->NewByteArray(env, len);
933 if (result != NULL) {
934 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)resolved);
935 }
936 }
937 return result;
938 }
939
940 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_access0(JNIEnv * env,jclass this,jlong pathAddress,jint amode)941 Java_sun_nio_fs_UnixNativeDispatcher_access0(JNIEnv* env, jclass this,
942 jlong pathAddress, jint amode)
943 {
944 int err;
945 const char* path = (const char*)jlong_to_ptr(pathAddress);
946
947 RESTARTABLE(access(path, (int)amode), err);
948 if (err == -1) {
949 throwUnixException(env, errno);
950 }
951 }
952
953 JNIEXPORT jboolean JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_exists0(JNIEnv * env,jclass this,jlong pathAddress)954 Java_sun_nio_fs_UnixNativeDispatcher_exists0(JNIEnv* env, jclass this, jlong pathAddress) {
955 int err;
956 const char* path = (const char*)jlong_to_ptr(pathAddress);
957 RESTARTABLE(access(path, F_OK), err);
958 return (err == 0) ? JNI_TRUE : JNI_FALSE;
959 }
960
961 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_statvfs0(JNIEnv * env,jclass this,jlong pathAddress,jobject attrs)962 Java_sun_nio_fs_UnixNativeDispatcher_statvfs0(JNIEnv* env, jclass this,
963 jlong pathAddress, jobject attrs)
964 {
965 int err;
966 struct statvfs64 buf;
967 const char* path = (const char*)jlong_to_ptr(pathAddress);
968
969
970 RESTARTABLE(statvfs64(path, &buf), err);
971 if (err == -1) {
972 throwUnixException(env, errno);
973 } else {
974 #ifdef _AIX
975 /* AIX returns ULONG_MAX in buf.f_blocks for the /proc file system. */
976 /* This is too big for a Java signed long and fools various tests. */
977 if (buf.f_blocks == ULONG_MAX) {
978 buf.f_blocks = 0;
979 }
980 /* The number of free or available blocks can never exceed the total number of blocks */
981 if (buf.f_blocks == 0) {
982 buf.f_bfree = 0;
983 buf.f_bavail = 0;
984 }
985 #endif
986 (*env)->SetLongField(env, attrs, attrs_f_frsize, long_to_jlong(buf.f_frsize));
987 (*env)->SetLongField(env, attrs, attrs_f_blocks, long_to_jlong(buf.f_blocks));
988 (*env)->SetLongField(env, attrs, attrs_f_bfree, long_to_jlong(buf.f_bfree));
989 (*env)->SetLongField(env, attrs, attrs_f_bavail, long_to_jlong(buf.f_bavail));
990 }
991 }
992
993 JNIEXPORT jlong JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_pathconf0(JNIEnv * env,jclass this,jlong pathAddress,jint name)994 Java_sun_nio_fs_UnixNativeDispatcher_pathconf0(JNIEnv* env, jclass this,
995 jlong pathAddress, jint name)
996 {
997 long err;
998 const char* path = (const char*)jlong_to_ptr(pathAddress);
999
1000 err = pathconf(path, (int)name);
1001 if (err == -1) {
1002 throwUnixException(env, errno);
1003 }
1004 return (jlong)err;
1005 }
1006
1007 JNIEXPORT jlong JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_fpathconf(JNIEnv * env,jclass this,jint fd,jint name)1008 Java_sun_nio_fs_UnixNativeDispatcher_fpathconf(JNIEnv* env, jclass this,
1009 jint fd, jint name)
1010 {
1011 long err;
1012
1013 err = fpathconf((int)fd, (int)name);
1014 if (err == -1) {
1015 throwUnixException(env, errno);
1016 }
1017 return (jlong)err;
1018 }
1019
1020 JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_mknod0(JNIEnv * env,jclass this,jlong pathAddress,jint mode,jlong dev)1021 Java_sun_nio_fs_UnixNativeDispatcher_mknod0(JNIEnv* env, jclass this,
1022 jlong pathAddress, jint mode, jlong dev)
1023 {
1024 int err;
1025 const char* path = (const char*)jlong_to_ptr(pathAddress);
1026
1027 RESTARTABLE(mknod(path, (mode_t)mode, (dev_t)dev), err);
1028 if (err == -1) {
1029 throwUnixException(env, errno);
1030 }
1031 }
1032
1033 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_getpwuid(JNIEnv * env,jclass this,jint uid)1034 Java_sun_nio_fs_UnixNativeDispatcher_getpwuid(JNIEnv* env, jclass this, jint uid)
1035 {
1036 jbyteArray result = NULL;
1037 int buflen;
1038 char* pwbuf;
1039
1040 /* allocate buffer for password record */
1041 buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX);
1042 if (buflen == -1)
1043 buflen = ENT_BUF_SIZE;
1044 pwbuf = (char*)malloc(buflen);
1045 if (pwbuf == NULL) {
1046 JNU_ThrowOutOfMemoryError(env, "native heap");
1047 } else {
1048 struct passwd pwent;
1049 struct passwd* p = NULL;
1050 int res = 0;
1051
1052 errno = 0;
1053 #ifdef __solaris__
1054 RESTARTABLE_RETURN_PTR(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen), p);
1055 #else
1056 RESTARTABLE(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen, &p), res);
1057 #endif
1058
1059 if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
1060 /* not found or error */
1061 if (errno == 0)
1062 errno = ENOENT;
1063 throwUnixException(env, errno);
1064 } else {
1065 jsize len = strlen(p->pw_name);
1066 result = (*env)->NewByteArray(env, len);
1067 if (result != NULL) {
1068 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(p->pw_name));
1069 }
1070 }
1071 free(pwbuf);
1072 }
1073
1074 return result;
1075 }
1076
1077
1078 JNIEXPORT jbyteArray JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_getgrgid(JNIEnv * env,jclass this,jint gid)1079 Java_sun_nio_fs_UnixNativeDispatcher_getgrgid(JNIEnv* env, jclass this, jint gid)
1080 {
1081 jbyteArray result = NULL;
1082 int buflen;
1083 int retry;
1084
1085 /* initial size of buffer for group record */
1086 buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX);
1087 if (buflen == -1)
1088 buflen = ENT_BUF_SIZE;
1089
1090 do {
1091 struct group grent;
1092 struct group* g = NULL;
1093 int res = 0;
1094
1095 char* grbuf = (char*)malloc(buflen);
1096 if (grbuf == NULL) {
1097 JNU_ThrowOutOfMemoryError(env, "native heap");
1098 return NULL;
1099 }
1100
1101 errno = 0;
1102 #ifdef __solaris__
1103 RESTARTABLE_RETURN_PTR(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen), g);
1104 #else
1105 RESTARTABLE(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen, &g), res);
1106 #endif
1107
1108 retry = 0;
1109 if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
1110 /* not found or error */
1111 if (errno == ERANGE) {
1112 /* insufficient buffer size so need larger buffer */
1113 buflen += ENT_BUF_SIZE;
1114 retry = 1;
1115 } else {
1116 if (errno == 0)
1117 errno = ENOENT;
1118 throwUnixException(env, errno);
1119 }
1120 } else {
1121 jsize len = strlen(g->gr_name);
1122 result = (*env)->NewByteArray(env, len);
1123 if (result != NULL) {
1124 (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(g->gr_name));
1125 }
1126 }
1127
1128 free(grbuf);
1129
1130 } while (retry);
1131
1132 return result;
1133 }
1134
1135 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_getpwnam0(JNIEnv * env,jclass this,jlong nameAddress)1136 Java_sun_nio_fs_UnixNativeDispatcher_getpwnam0(JNIEnv* env, jclass this,
1137 jlong nameAddress)
1138 {
1139 jint uid = -1;
1140 int buflen;
1141 char* pwbuf;
1142
1143 /* allocate buffer for password record */
1144 buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX);
1145 if (buflen == -1)
1146 buflen = ENT_BUF_SIZE;
1147 pwbuf = (char*)malloc(buflen);
1148 if (pwbuf == NULL) {
1149 JNU_ThrowOutOfMemoryError(env, "native heap");
1150 } else {
1151 struct passwd pwent;
1152 struct passwd* p = NULL;
1153 int res = 0;
1154 const char* name = (const char*)jlong_to_ptr(nameAddress);
1155
1156 errno = 0;
1157 #ifdef __solaris__
1158 RESTARTABLE_RETURN_PTR(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen), p);
1159 #else
1160 RESTARTABLE(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen, &p), res);
1161 #endif
1162
1163 if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
1164 /* not found or error */
1165 if (errno != 0 && errno != ENOENT && errno != ESRCH)
1166 throwUnixException(env, errno);
1167 } else {
1168 uid = p->pw_uid;
1169 }
1170 free(pwbuf);
1171 }
1172
1173 return uid;
1174 }
1175
1176 JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_getgrnam0(JNIEnv * env,jclass this,jlong nameAddress)1177 Java_sun_nio_fs_UnixNativeDispatcher_getgrnam0(JNIEnv* env, jclass this,
1178 jlong nameAddress)
1179 {
1180 jint gid = -1;
1181 int buflen, retry;
1182
1183 /* initial size of buffer for group record */
1184 buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX);
1185 if (buflen == -1)
1186 buflen = ENT_BUF_SIZE;
1187
1188 do {
1189 struct group grent;
1190 struct group* g = NULL;
1191 int res = 0;
1192 char *grbuf;
1193 const char* name = (const char*)jlong_to_ptr(nameAddress);
1194
1195 grbuf = (char*)malloc(buflen);
1196 if (grbuf == NULL) {
1197 JNU_ThrowOutOfMemoryError(env, "native heap");
1198 return -1;
1199 }
1200
1201 errno = 0;
1202 #ifdef __solaris__
1203 RESTARTABLE_RETURN_PTR(getgrnam_r(name, &grent, grbuf, (size_t)buflen), g);
1204 #else
1205 RESTARTABLE(getgrnam_r(name, &grent, grbuf, (size_t)buflen, &g), res);
1206 #endif
1207
1208 retry = 0;
1209 if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
1210 /* not found or error */
1211 if (errno != 0 && errno != ENOENT && errno != ESRCH) {
1212 if (errno == ERANGE) {
1213 /* insufficient buffer size so need larger buffer */
1214 buflen += ENT_BUF_SIZE;
1215 retry = 1;
1216 } else {
1217 throwUnixException(env, errno);
1218 }
1219 }
1220 } else {
1221 gid = g->gr_gid;
1222 }
1223
1224 free(grbuf);
1225
1226 } while (retry);
1227
1228 return gid;
1229 }
1230
1231 // Android-changed: register native methods.
1232 #include <nativehelper/JNIHelp.h>
1233 #define NATIVE_METHOD(className, functionName, signature) \
1234 { #functionName, signature, (void*)(className ## _ ## functionName) }
1235
1236 static JNINativeMethod gMethods[] = {
1237 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, getcwd, "()[B"),
1238 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, dup, "(I)I"),
1239 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, open0, "(JII)I"),
1240 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, openat0, "(IJII)I"),
1241 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, close, "(I)V"),
1242 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fopen0, "(JJ)J"),
1243 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fclose, "(J)V"),
1244 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, link0, "(JJ)V"),
1245 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, unlink0, "(J)V"),
1246 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, unlinkat0, "(IJI)V"),
1247 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, mknod0, "(JIJ)V"),
1248 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, rename0, "(JJ)V"),
1249 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, renameat0, "(IJIJ)V"),
1250 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, mkdir0, "(JI)V"),
1251 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, rmdir0, "(J)V"),
1252 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, readlink0, "(J)[B"),
1253 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, realpath0, "(J)[B"),
1254 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, symlink0, "(JJ)V"),
1255 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, stat0, "(JLsun/nio/fs/UnixFileAttributes;)V"),
1256 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, stat1, "(J)I"),
1257 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, lstat0, "(JLsun/nio/fs/UnixFileAttributes;)V"),
1258 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fstat, "(ILsun/nio/fs/UnixFileAttributes;)V"),
1259 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fstatat0, "(IJILsun/nio/fs/UnixFileAttributes;)V"),
1260 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, chown0, "(JII)V"),
1261 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, lchown0, "(JII)V"),
1262 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fchown, "(III)V"),
1263 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, chmod0, "(JI)V"),
1264 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fchmod, "(II)V"),
1265 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, utimes0, "(JJJ)V"),
1266 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, futimes, "(IJJ)V"),
1267 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, opendir0, "(J)J"),
1268 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fdopendir, "(I)J"),
1269 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, closedir, "(J)V"),
1270 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, readdir, "(J)[B"),
1271 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, read, "(IJI)I"),
1272 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, write, "(IJI)I"),
1273 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, access0, "(JI)V"),
1274 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, exists0, "(J)Z"),
1275 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, getpwuid, "(I)[B"),
1276 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, getgrgid, "(I)[B"),
1277 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, getpwnam0, "(J)I"),
1278 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, getgrnam0, "(J)I"),
1279 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, statvfs0, "(JLsun/nio/fs/UnixFileStoreAttributes;)V"),
1280 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, pathconf0, "(JI)J"),
1281 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, fpathconf, "(II)J"),
1282 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, strerror, "(I)[B"),
1283 NATIVE_METHOD(Java_sun_nio_fs_UnixNativeDispatcher, init, "()I"),
1284 };
1285
register_java_sun_nio_fs_UnixNativeDispatcher(JNIEnv * env)1286 void register_java_sun_nio_fs_UnixNativeDispatcher(JNIEnv* env) {
1287 jniRegisterNativeMethods(env, "sun/nio/fs/UnixNativeDispatcher", gMethods, NELEM(gMethods));
1288 }
1289