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