893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A#include <stdio.h>
893N/A#include <stdlib.h>
893N/A#include <limits.h>
893N/A#include <fcntl.h>
893N/A#include <dirent.h>
893N/A#include <unistd.h>
893N/A#include <pwd.h>
893N/A#include <grp.h>
893N/A#include <errno.h>
893N/A#include <dlfcn.h>
893N/A#include <sys/types.h>
893N/A#include <sys/stat.h>
893N/A#include <sys/statvfs.h>
893N/A#include <sys/time.h>
893N/A
893N/A#ifdef __solaris__
893N/A#include <strings.h>
893N/A#endif
893N/A
893N/A#ifdef __linux__
893N/A#include <string.h>
893N/A#endif
893N/A
4632N/A#ifdef _ALLBSD_SOURCE
4632N/A#include <string.h>
4632N/A
4632N/A#define stat64 stat
4632N/A#define statvfs64 statvfs
4632N/A
4632N/A#define open64 open
4632N/A#define fstat64 fstat
4632N/A#define lstat64 lstat
4632N/A#define dirent64 dirent
4632N/A#define readdir64_r readdir_r
4632N/A#endif
4632N/A
893N/A#include "jni.h"
893N/A#include "jni_util.h"
893N/A#include "jlong.h"
893N/A
893N/A#include "sun_nio_fs_UnixNativeDispatcher.h"
893N/A
3484N/A/**
3484N/A * Size of password or group entry when not available via sysconf
3484N/A */
3484N/A#define ENT_BUF_SIZE 1024
3484N/A
893N/A#define RESTARTABLE(_cmd, _result) do { \
893N/A do { \
893N/A _result = _cmd; \
893N/A } while((_result == -1) && (errno == EINTR)); \
893N/A} while(0)
893N/A
3484N/A#define RESTARTABLE_RETURN_PTR(_cmd, _result) do { \
3484N/A do { \
3484N/A _result = _cmd; \
3484N/A } while((_result == NULL) && (errno == EINTR)); \
3484N/A} while(0)
3484N/A
893N/Astatic jfieldID attrs_st_mode;
893N/Astatic jfieldID attrs_st_ino;
893N/Astatic jfieldID attrs_st_dev;
893N/Astatic jfieldID attrs_st_rdev;
893N/Astatic jfieldID attrs_st_nlink;
893N/Astatic jfieldID attrs_st_uid;
893N/Astatic jfieldID attrs_st_gid;
893N/Astatic jfieldID attrs_st_size;
893N/Astatic jfieldID attrs_st_atime;
893N/Astatic jfieldID attrs_st_mtime;
893N/Astatic jfieldID attrs_st_ctime;
893N/A
893N/Astatic jfieldID attrs_f_frsize;
893N/Astatic jfieldID attrs_f_blocks;
893N/Astatic jfieldID attrs_f_bfree;
893N/Astatic jfieldID attrs_f_bavail;
893N/A
893N/Astatic jfieldID entry_name;
893N/Astatic jfieldID entry_dir;
893N/Astatic jfieldID entry_fstype;
893N/Astatic jfieldID entry_options;
893N/Astatic jfieldID entry_dev;
893N/A
893N/A/**
1435N/A * System calls that may not be available at run time.
893N/A */
893N/Atypedef int openat64_func(int, const char *, int, ...);
893N/Atypedef int fstatat64_func(int, const char *, struct stat64 *, int);
893N/Atypedef int unlinkat_func(int, const char*, int);
893N/Atypedef int renameat_func(int, const char*, int, const char*);
893N/Atypedef int futimesat_func(int, const char *, const struct timeval *);
1435N/Atypedef DIR* fdopendir_func(int);
893N/A
893N/Astatic openat64_func* my_openat64_func = NULL;
893N/Astatic fstatat64_func* my_fstatat64_func = NULL;
893N/Astatic unlinkat_func* my_unlinkat_func = NULL;
893N/Astatic renameat_func* my_renameat_func = NULL;
893N/Astatic futimesat_func* my_futimesat_func = NULL;
1435N/Astatic fdopendir_func* my_fdopendir_func = NULL;
893N/A
893N/A/**
893N/A * fstatat missing from glibc on Linux. Temporary workaround
893N/A * for x86/x64.
893N/A */
893N/A#if defined(__linux__) && defined(__i386)
893N/A#define FSTATAT64_SYSCALL_AVAILABLE
893N/Astatic int fstatat64_wrapper(int dfd, const char *path,
893N/A struct stat64 *statbuf, int flag)
893N/A{
893N/A #ifndef __NR_fstatat64
893N/A #define __NR_fstatat64 300
893N/A #endif
893N/A return syscall(__NR_fstatat64, dfd, path, statbuf, flag);
893N/A}
893N/A#endif
893N/A
893N/A#if defined(__linux__) && defined(__x86_64__)
893N/A#define FSTATAT64_SYSCALL_AVAILABLE
893N/Astatic int fstatat64_wrapper(int dfd, const char *path,
893N/A struct stat64 *statbuf, int flag)
893N/A{
893N/A #ifndef __NR_newfstatat
893N/A #define __NR_newfstatat 262
893N/A #endif
893N/A return syscall(__NR_newfstatat, dfd, path, statbuf, flag);
893N/A}
893N/A#endif
893N/A
893N/A/**
893N/A * Call this to throw an internal UnixException when a system/library
893N/A * call fails
893N/A */
893N/Astatic void throwUnixException(JNIEnv* env, int errnum) {
893N/A jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
893N/A "(I)V", errnum);
893N/A if (x != NULL) {
893N/A (*env)->Throw(env, x);
893N/A }
893N/A}
893N/A
893N/A/**
1319N/A * Initialization
893N/A */
1319N/AJNIEXPORT jint JNICALL
1319N/AJava_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
893N/A{
1319N/A jint flags = 0;
893N/A jclass clazz;
893N/A
893N/A clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileAttributes");
893N/A if (clazz == NULL) {
1319N/A return 0;
893N/A }
893N/A attrs_st_mode = (*env)->GetFieldID(env, clazz, "st_mode", "I");
893N/A attrs_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J");
893N/A attrs_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J");
893N/A attrs_st_rdev = (*env)->GetFieldID(env, clazz, "st_rdev", "J");
893N/A attrs_st_nlink = (*env)->GetFieldID(env, clazz, "st_nlink", "I");
893N/A attrs_st_uid = (*env)->GetFieldID(env, clazz, "st_uid", "I");
893N/A attrs_st_gid = (*env)->GetFieldID(env, clazz, "st_gid", "I");
893N/A attrs_st_size = (*env)->GetFieldID(env, clazz, "st_size", "J");
893N/A attrs_st_atime = (*env)->GetFieldID(env, clazz, "st_atime", "J");
893N/A attrs_st_mtime = (*env)->GetFieldID(env, clazz, "st_mtime", "J");
893N/A attrs_st_ctime = (*env)->GetFieldID(env, clazz, "st_ctime", "J");
893N/A
893N/A clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileStoreAttributes");
893N/A if (clazz == NULL) {
1319N/A return 0;
893N/A }
893N/A attrs_f_frsize = (*env)->GetFieldID(env, clazz, "f_frsize", "J");
893N/A attrs_f_blocks = (*env)->GetFieldID(env, clazz, "f_blocks", "J");
893N/A attrs_f_bfree = (*env)->GetFieldID(env, clazz, "f_bfree", "J");
893N/A attrs_f_bavail = (*env)->GetFieldID(env, clazz, "f_bavail", "J");
893N/A
893N/A clazz = (*env)->FindClass(env, "sun/nio/fs/UnixMountEntry");
893N/A if (clazz == NULL) {
1319N/A return 0;
893N/A }
893N/A entry_name = (*env)->GetFieldID(env, clazz, "name", "[B");
893N/A entry_dir = (*env)->GetFieldID(env, clazz, "dir", "[B");
893N/A entry_fstype = (*env)->GetFieldID(env, clazz, "fstype", "[B");
893N/A entry_options = (*env)->GetFieldID(env, clazz, "opts", "[B");
893N/A entry_dev = (*env)->GetFieldID(env, clazz, "dev", "J");
893N/A
1435N/A /* system calls that might not be available at run time */
893N/A
4632N/A#if (defined(__solaris__) && defined(_LP64)) || defined(_ALLBSD_SOURCE)
893N/A /* Solaris 64-bit does not have openat64/fstatat64 */
893N/A my_openat64_func = (openat64_func*)dlsym(RTLD_DEFAULT, "openat");
893N/A my_fstatat64_func = (fstatat64_func*)dlsym(RTLD_DEFAULT, "fstatat");
893N/A#else
893N/A my_openat64_func = (openat64_func*) dlsym(RTLD_DEFAULT, "openat64");
893N/A my_fstatat64_func = (fstatat64_func*) dlsym(RTLD_DEFAULT, "fstatat64");
893N/A#endif
893N/A my_unlinkat_func = (unlinkat_func*) dlsym(RTLD_DEFAULT, "unlinkat");
893N/A my_renameat_func = (renameat_func*) dlsym(RTLD_DEFAULT, "renameat");
893N/A my_futimesat_func = (futimesat_func*) dlsym(RTLD_DEFAULT, "futimesat");
1435N/A my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir");
893N/A
893N/A#if defined(FSTATAT64_SYSCALL_AVAILABLE)
893N/A /* fstatat64 missing from glibc */
893N/A if (my_fstatat64_func == NULL)
893N/A my_fstatat64_func = (fstatat64_func*)&fstatat64_wrapper;
893N/A#endif
1319N/A
1319N/A if (my_openat64_func != NULL && my_fstatat64_func != NULL &&
1319N/A my_unlinkat_func != NULL && my_renameat_func != NULL &&
1435N/A my_futimesat_func != NULL && my_fdopendir_func != NULL)
1319N/A {
1319N/A flags |= sun_nio_fs_UnixNativeDispatcher_HAS_AT_SYSCALLS;
1319N/A }
1319N/A
1319N/A return flags;
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_getcwd(JNIEnv* env, jclass this) {
893N/A jbyteArray result = NULL;
893N/A char buf[PATH_MAX+1];
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A char* cwd = getcwd(buf, sizeof(buf));
893N/A if (cwd == NULL) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A jsize len = (jsize)strlen(buf);
893N/A result = (*env)->NewByteArray(env, len);
893N/A if (result != NULL) {
893N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)buf);
893N/A }
893N/A }
893N/A return result;
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray
893N/AJava_sun_nio_fs_UnixNativeDispatcher_strerror(JNIEnv* env, jclass this, jint error)
893N/A{
893N/A char* msg;
893N/A jsize len;
893N/A jbyteArray bytes;
893N/A
893N/A msg = strerror((int)error);
893N/A len = strlen(msg);
893N/A bytes = (*env)->NewByteArray(env, len);
893N/A if (bytes != NULL) {
893N/A (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)msg);
893N/A }
893N/A return bytes;
893N/A}
893N/A
893N/AJNIEXPORT jint
893N/AJava_sun_nio_fs_UnixNativeDispatcher_dup(JNIEnv* env, jclass this, jint fd) {
893N/A
893N/A int res = -1;
893N/A
893N/A RESTARTABLE(dup((int)fd), res);
893N/A if (fd == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return (jint)res;
893N/A}
893N/A
893N/AJNIEXPORT jlong JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fopen0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jlong modeAddress)
893N/A{
893N/A FILE* fp = NULL;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A const char* mode = (const char*)jlong_to_ptr(modeAddress);
893N/A
893N/A do {
893N/A fp = fopen(path, mode);
893N/A } while (fp == NULL && errno == EINTR);
893N/A
893N/A if (fp == NULL) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A
893N/A return ptr_to_jlong(fp);
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv* env, jclass this, jlong stream)
893N/A{
893N/A int res;
893N/A FILE* fp = jlong_to_ptr(stream);
893N/A
893N/A do {
893N/A res = fclose(fp);
893N/A } while (res == EOF && errno == EINTR);
893N/A if (res == EOF) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_open0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jint oflags, jint mode)
893N/A{
893N/A jint fd;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(open64(path, (int)oflags, (mode_t)mode), fd);
893N/A if (fd == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return fd;
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_openat0(JNIEnv* env, jclass this, jint dfd,
893N/A jlong pathAddress, jint oflags, jint mode)
893N/A{
893N/A jint fd;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A if (my_openat64_func == NULL) {
893N/A JNU_ThrowInternalError(env, "should not reach here");
893N/A return -1;
893N/A }
893N/A
893N/A RESTARTABLE((*my_openat64_func)(dfd, path, (int)oflags, (mode_t)mode), fd);
893N/A if (fd == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return fd;
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_close(JNIEnv* env, jclass this, jint fd) {
893N/A int err;
893N/A /* TDB - need to decide if EIO and other errors should cause exception */
893N/A RESTARTABLE(close((int)fd), err);
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_read(JNIEnv* env, jclass this, jint fd,
893N/A jlong address, jint nbytes)
893N/A{
893N/A ssize_t n;
893N/A void* bufp = jlong_to_ptr(address);
893N/A RESTARTABLE(read((int)fd, bufp, (size_t)nbytes), n);
893N/A if (n == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return (jint)n;
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_write(JNIEnv* env, jclass this, jint fd,
893N/A jlong address, jint nbytes)
893N/A{
893N/A ssize_t n;
893N/A void* bufp = jlong_to_ptr(address);
893N/A RESTARTABLE(write((int)fd, bufp, (size_t)nbytes), n);
893N/A if (n == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return (jint)n;
893N/A}
893N/A
893N/A/**
893N/A * Copy stat64 members into sun.nio.fs.UnixFileAttributes
893N/A */
893N/Astatic void prepAttributes(JNIEnv* env, struct stat64* buf, jobject attrs) {
893N/A (*env)->SetIntField(env, attrs, attrs_st_mode, (jint)buf->st_mode);
893N/A (*env)->SetLongField(env, attrs, attrs_st_ino, (jlong)buf->st_ino);
893N/A (*env)->SetLongField(env, attrs, attrs_st_dev, (jlong)buf->st_dev);
893N/A (*env)->SetLongField(env, attrs, attrs_st_rdev, (jlong)buf->st_rdev);
893N/A (*env)->SetIntField(env, attrs, attrs_st_nlink, (jint)buf->st_nlink);
893N/A (*env)->SetIntField(env, attrs, attrs_st_uid, (jint)buf->st_uid);
893N/A (*env)->SetIntField(env, attrs, attrs_st_gid, (jint)buf->st_gid);
893N/A (*env)->SetLongField(env, attrs, attrs_st_size, (jlong)buf->st_size);
1319N/A (*env)->SetLongField(env, attrs, attrs_st_atime, (jlong)buf->st_atime);
1319N/A (*env)->SetLongField(env, attrs, attrs_st_mtime, (jlong)buf->st_mtime);
1319N/A (*env)->SetLongField(env, attrs, attrs_st_ctime, (jlong)buf->st_ctime);
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_stat0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jobject attrs)
893N/A{
893N/A int err;
893N/A struct stat64 buf;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(stat64(path, &buf), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A prepAttributes(env, &buf, attrs);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_lstat0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jobject attrs)
893N/A{
893N/A int err;
893N/A struct stat64 buf;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(lstat64(path, &buf), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A prepAttributes(env, &buf, attrs);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fstat(JNIEnv* env, jclass this, jint fd,
893N/A jobject attrs)
893N/A{
893N/A int err;
893N/A struct stat64 buf;
893N/A
893N/A RESTARTABLE(fstat64((int)fd, &buf), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A prepAttributes(env, &buf, attrs);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd,
893N/A jlong pathAddress, jint flag, jobject attrs)
893N/A{
893N/A int err;
893N/A struct stat64 buf;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A if (my_fstatat64_func == NULL) {
893N/A JNU_ThrowInternalError(env, "should not reach here");
893N/A return;
893N/A }
893N/A RESTARTABLE((*my_fstatat64_func)((int)dfd, path, &buf, (int)flag), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A prepAttributes(env, &buf, attrs);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_chmod0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jint mode)
893N/A{
893N/A int err;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(chmod(path, (mode_t)mode), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fchmod(JNIEnv* env, jclass this, jint filedes,
893N/A jint mode)
893N/A{
893N/A int err;
893N/A
893N/A RESTARTABLE(fchmod((int)filedes, (mode_t)mode), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_chown0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jint uid, jint gid)
893N/A{
893N/A int err;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(chown(path, (uid_t)uid, (gid_t)gid), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_lchown0(JNIEnv* env, jclass this, jlong pathAddress, jint uid, jint gid)
893N/A{
893N/A int err;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(lchown(path, (uid_t)uid, (gid_t)gid), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fchown(JNIEnv* env, jclass this, jint filedes, jint uid, jint gid)
893N/A{
893N/A int err;
893N/A
893N/A RESTARTABLE(fchown(filedes, (uid_t)uid, (gid_t)gid), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_utimes0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jlong accessTime, jlong modificationTime)
893N/A{
893N/A int err;
893N/A struct timeval times[2];
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
1319N/A times[0].tv_sec = accessTime / 1000000;
1319N/A times[0].tv_usec = accessTime % 1000000;
893N/A
1319N/A times[1].tv_sec = modificationTime / 1000000;
1319N/A times[1].tv_usec = modificationTime % 1000000;
893N/A
893N/A RESTARTABLE(utimes(path, &times[0]), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv* env, jclass this, jint filedes,
893N/A jlong accessTime, jlong modificationTime)
893N/A{
893N/A struct timeval times[2];
893N/A int err = 0;
893N/A
1319N/A times[0].tv_sec = accessTime / 1000000;
1319N/A times[0].tv_usec = accessTime % 1000000;
893N/A
1319N/A times[1].tv_sec = modificationTime / 1000000;
1319N/A times[1].tv_usec = modificationTime % 1000000;
893N/A
4632N/A#ifdef _ALLBSD_SOURCE
4632N/A RESTARTABLE(futimes(filedes, &times[0]), err);
4632N/A#else
4632N/A if (my_futimesat_func == NULL) {
4632N/A JNU_ThrowInternalError(env, "my_ftimesat_func is NULL");
4632N/A return;
4632N/A }
4632N/A RESTARTABLE((*my_futimesat_func)(filedes, NULL, &times[0]), err);
4632N/A#endif
4632N/A if (err == -1) {
4632N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jlong JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_opendir0(JNIEnv* env, jclass this,
893N/A jlong pathAddress)
893N/A{
893N/A DIR* dir;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A dir = opendir(path);
893N/A if (dir == NULL) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return ptr_to_jlong(dir);
893N/A}
893N/A
893N/AJNIEXPORT jlong JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fdopendir(JNIEnv* env, jclass this, int dfd) {
893N/A DIR* dir;
893N/A
1435N/A if (my_fdopendir_func == NULL) {
1435N/A JNU_ThrowInternalError(env, "should not reach here");
1435N/A return (jlong)-1;
1435N/A }
1435N/A
893N/A /* EINTR not listed as a possible error */
1435N/A dir = (*my_fdopendir_func)((int)dfd);
893N/A if (dir == NULL) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return ptr_to_jlong(dir);
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_closedir(JNIEnv* env, jclass this, jlong dir) {
893N/A int err;
893N/A DIR* dirp = jlong_to_ptr(dir);
893N/A
893N/A RESTARTABLE(closedir(dirp), err);
893N/A if (errno == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_readdir(JNIEnv* env, jclass this, jlong value) {
893N/A struct dirent64* result;
4811N/A struct {
4811N/A struct dirent64 buf;
4811N/A char name_extra[PATH_MAX + 1 - sizeof result->d_name];
4811N/A } entry;
4811N/A struct dirent64* ptr = &entry.buf;
893N/A int res;
893N/A DIR* dirp = jlong_to_ptr(value);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A /* TDB: reentrant version probably not required here */
893N/A res = readdir64_r(dirp, ptr, &result);
893N/A if (res != 0) {
893N/A throwUnixException(env, res);
893N/A return NULL;
893N/A } else {
893N/A if (result == NULL) {
893N/A return NULL;
893N/A } else {
893N/A jsize len = strlen(ptr->d_name);
893N/A jbyteArray bytes = (*env)->NewByteArray(env, len);
893N/A if (bytes != NULL) {
893N/A (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)(ptr->d_name));
893N/A }
893N/A return bytes;
893N/A }
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_mkdir0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jint mode)
893N/A{
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if (mkdir(path, (mode_t)mode) == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_rmdir0(JNIEnv* env, jclass this,
893N/A jlong pathAddress)
893N/A{
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if (rmdir(path) == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_link0(JNIEnv* env, jclass this,
893N/A jlong existingAddress, jlong newAddress)
893N/A{
893N/A int err;
893N/A const char* existing = (const char*)jlong_to_ptr(existingAddress);
893N/A const char* newname = (const char*)jlong_to_ptr(newAddress);
893N/A
893N/A RESTARTABLE(link(existing, newname), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_unlink0(JNIEnv* env, jclass this,
893N/A jlong pathAddress)
893N/A{
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if (unlink(path) == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_unlinkat0(JNIEnv* env, jclass this, jint dfd,
893N/A jlong pathAddress, jint flags)
893N/A{
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A if (my_unlinkat_func == NULL) {
893N/A JNU_ThrowInternalError(env, "should not reach here");
893N/A return;
893N/A }
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if ((*my_unlinkat_func)((int)dfd, path, (int)flags) == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_rename0(JNIEnv* env, jclass this,
893N/A jlong fromAddress, jlong toAddress)
893N/A{
893N/A const char* from = (const char*)jlong_to_ptr(fromAddress);
893N/A const char* to = (const char*)jlong_to_ptr(toAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if (rename(from, to) == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_renameat0(JNIEnv* env, jclass this,
893N/A jint fromfd, jlong fromAddress, jint tofd, jlong toAddress)
893N/A{
893N/A const char* from = (const char*)jlong_to_ptr(fromAddress);
893N/A const char* to = (const char*)jlong_to_ptr(toAddress);
893N/A
893N/A if (my_renameat_func == NULL) {
893N/A JNU_ThrowInternalError(env, "should not reach here");
893N/A return;
893N/A }
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if ((*my_renameat_func)((int)fromfd, from, (int)tofd, to) == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_symlink0(JNIEnv* env, jclass this,
893N/A jlong targetAddress, jlong linkAddress)
893N/A{
893N/A const char* target = (const char*)jlong_to_ptr(targetAddress);
893N/A const char* link = (const char*)jlong_to_ptr(linkAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if (symlink(target, link) == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_readlink0(JNIEnv* env, jclass this,
893N/A jlong pathAddress)
893N/A{
893N/A jbyteArray result = NULL;
893N/A char target[PATH_MAX+1];
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A int n = readlink(path, target, sizeof(target));
893N/A if (n == -1) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A jsize len;
893N/A if (n == sizeof(target)) {
893N/A n--;
893N/A }
893N/A target[n] = '\0';
893N/A len = (jsize)strlen(target);
893N/A result = (*env)->NewByteArray(env, len);
893N/A if (result != NULL) {
893N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)target);
893N/A }
893N/A }
893N/A return result;
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_realpath0(JNIEnv* env, jclass this,
893N/A jlong pathAddress)
893N/A{
893N/A jbyteArray result = NULL;
893N/A char resolved[PATH_MAX+1];
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A /* EINTR not listed as a possible error */
893N/A if (realpath(path, resolved) == NULL) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A jsize len = (jsize)strlen(resolved);
893N/A result = (*env)->NewByteArray(env, len);
893N/A if (result != NULL) {
893N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)resolved);
893N/A }
893N/A }
893N/A return result;
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_access0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jint amode)
893N/A{
893N/A int err;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(access(path, (int)amode), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_statvfs0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jobject attrs)
893N/A{
893N/A int err;
893N/A struct statvfs64 buf;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A
893N/A RESTARTABLE(statvfs64(path, &buf), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A (*env)->SetLongField(env, attrs, attrs_f_frsize, long_to_jlong(buf.f_frsize));
893N/A (*env)->SetLongField(env, attrs, attrs_f_blocks, long_to_jlong(buf.f_blocks));
893N/A (*env)->SetLongField(env, attrs, attrs_f_bfree, long_to_jlong(buf.f_bfree));
893N/A (*env)->SetLongField(env, attrs, attrs_f_bavail, long_to_jlong(buf.f_bavail));
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jlong JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_pathconf0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jint name)
893N/A{
893N/A long err;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A err = pathconf(path, (int)name);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return (jlong)err;
893N/A}
893N/A
893N/AJNIEXPORT jlong JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_fpathconf(JNIEnv* env, jclass this,
893N/A jint fd, jint name)
893N/A{
893N/A long err;
893N/A
893N/A err = fpathconf((int)fd, (int)name);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return (jlong)err;
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_mknod0(JNIEnv* env, jclass this,
893N/A jlong pathAddress, jint mode, jlong dev)
893N/A{
893N/A int err;
893N/A const char* path = (const char*)jlong_to_ptr(pathAddress);
893N/A
893N/A RESTARTABLE(mknod(path, (mode_t)mode, (dev_t)dev), err);
893N/A if (err == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_getpwuid(JNIEnv* env, jclass this, jint uid)
893N/A{
893N/A jbyteArray result = NULL;
893N/A int buflen;
3484N/A char* pwbuf;
893N/A
3484N/A /* allocate buffer for password record */
893N/A buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX);
3484N/A if (buflen == -1)
3484N/A buflen = ENT_BUF_SIZE;
3484N/A pwbuf = (char*)malloc(buflen);
3484N/A if (pwbuf == NULL) {
3484N/A JNU_ThrowOutOfMemoryError(env, "native heap");
893N/A } else {
3484N/A struct passwd pwent;
3484N/A struct passwd* p = NULL;
3484N/A int res = 0;
893N/A
3484N/A errno = 0;
3484N/A #ifdef __solaris__
3484N/A RESTARTABLE_RETURN_PTR(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen), p);
3484N/A #else
3484N/A RESTARTABLE(getpwuid_r((uid_t)uid, &pwent, pwbuf, (size_t)buflen, &p), res);
3484N/A #endif
893N/A
3484N/A if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
3484N/A /* not found or error */
4225N/A if (errno == 0)
4225N/A errno = ENOENT;
4225N/A throwUnixException(env, errno);
3484N/A } else {
3484N/A jsize len = strlen(p->pw_name);
3484N/A result = (*env)->NewByteArray(env, len);
3484N/A if (result != NULL) {
3484N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(p->pw_name));
893N/A }
893N/A }
3484N/A free(pwbuf);
893N/A }
3484N/A
893N/A return result;
893N/A}
893N/A
893N/A
893N/AJNIEXPORT jbyteArray JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_getgrgid(JNIEnv* env, jclass this, jint gid)
893N/A{
893N/A jbyteArray result = NULL;
893N/A int buflen;
3484N/A int retry;
893N/A
3484N/A /* initial size of buffer for group record */
893N/A buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX);
3484N/A if (buflen == -1)
3484N/A buflen = ENT_BUF_SIZE;
3484N/A
3484N/A do {
3484N/A struct group grent;
3484N/A struct group* g = NULL;
3484N/A int res = 0;
3484N/A
893N/A char* grbuf = (char*)malloc(buflen);
893N/A if (grbuf == NULL) {
893N/A JNU_ThrowOutOfMemoryError(env, "native heap");
3484N/A return NULL;
3484N/A }
3484N/A
3484N/A errno = 0;
3484N/A #ifdef __solaris__
3484N/A RESTARTABLE_RETURN_PTR(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen), g);
3484N/A #else
3484N/A RESTARTABLE(getgrgid_r((gid_t)gid, &grent, grbuf, (size_t)buflen, &g), res);
3484N/A #endif
893N/A
3484N/A retry = 0;
3484N/A if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
3484N/A /* not found or error */
4225N/A if (errno == ERANGE) {
4225N/A /* insufficient buffer size so need larger buffer */
4225N/A buflen += ENT_BUF_SIZE;
4225N/A retry = 1;
4225N/A } else {
4225N/A if (errno == 0)
4225N/A errno = ENOENT;
4225N/A throwUnixException(env, errno);
893N/A }
3484N/A } else {
3484N/A jsize len = strlen(g->gr_name);
3484N/A result = (*env)->NewByteArray(env, len);
3484N/A if (result != NULL) {
3484N/A (*env)->SetByteArrayRegion(env, result, 0, len, (jbyte*)(g->gr_name));
3484N/A }
893N/A }
3484N/A
3484N/A free(grbuf);
3484N/A
3484N/A } while (retry);
3484N/A
893N/A return result;
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_getpwnam0(JNIEnv* env, jclass this,
893N/A jlong nameAddress)
893N/A{
893N/A jint uid = -1;
893N/A int buflen;
893N/A char* pwbuf;
893N/A
3484N/A /* allocate buffer for password record */
893N/A buflen = (int)sysconf(_SC_GETPW_R_SIZE_MAX);
3484N/A if (buflen == -1)
3484N/A buflen = ENT_BUF_SIZE;
893N/A pwbuf = (char*)malloc(buflen);
893N/A if (pwbuf == NULL) {
893N/A JNU_ThrowOutOfMemoryError(env, "native heap");
3484N/A } else {
3484N/A struct passwd pwent;
3484N/A struct passwd* p = NULL;
3484N/A int res = 0;
3484N/A const char* name = (const char*)jlong_to_ptr(nameAddress);
893N/A
3484N/A errno = 0;
3484N/A #ifdef __solaris__
3484N/A RESTARTABLE_RETURN_PTR(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen), p);
3484N/A #else
3484N/A RESTARTABLE(getpwnam_r(name, &pwent, pwbuf, (size_t)buflen, &p), res);
3484N/A #endif
893N/A
3484N/A if (res != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
3484N/A /* not found or error */
5309N/A if (errno != 0 && errno != ENOENT && errno != ESRCH)
3484N/A throwUnixException(env, errno);
3484N/A } else {
3484N/A uid = p->pw_uid;
3484N/A }
3484N/A free(pwbuf);
893N/A }
893N/A
893N/A return uid;
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_UnixNativeDispatcher_getgrnam0(JNIEnv* env, jclass this,
893N/A jlong nameAddress)
893N/A{
893N/A jint gid = -1;
3484N/A int buflen, retry;
893N/A
3484N/A /* initial size of buffer for group record */
893N/A buflen = (int)sysconf(_SC_GETGR_R_SIZE_MAX);
3484N/A if (buflen == -1)
3484N/A buflen = ENT_BUF_SIZE;
3484N/A
3484N/A do {
3484N/A struct group grent;
3484N/A struct group* g = NULL;
3484N/A int res = 0;
3484N/A char *grbuf;
3484N/A const char* name = (const char*)jlong_to_ptr(nameAddress);
3484N/A
3484N/A grbuf = (char*)malloc(buflen);
3484N/A if (grbuf == NULL) {
3484N/A JNU_ThrowOutOfMemoryError(env, "native heap");
3484N/A return -1;
3484N/A }
893N/A
3484N/A errno = 0;
3484N/A #ifdef __solaris__
3484N/A RESTARTABLE_RETURN_PTR(getgrnam_r(name, &grent, grbuf, (size_t)buflen), g);
3484N/A #else
3484N/A RESTARTABLE(getgrnam_r(name, &grent, grbuf, (size_t)buflen, &g), res);
3484N/A #endif
893N/A
3484N/A retry = 0;
3484N/A if (res != 0 || g == NULL || g->gr_name == NULL || *(g->gr_name) == '\0') {
3484N/A /* not found or error */
5309N/A if (errno != 0 && errno != ENOENT && errno != ESRCH) {
3484N/A if (errno == ERANGE) {
3484N/A /* insufficient buffer size so need larger buffer */
3484N/A buflen += ENT_BUF_SIZE;
3484N/A retry = 1;
3484N/A } else {
3484N/A throwUnixException(env, errno);
3484N/A }
3484N/A }
3484N/A } else {
3484N/A gid = g->gr_gid;
3484N/A }
3484N/A
3484N/A free(grbuf);
3484N/A
3484N/A } while (retry);
893N/A
893N/A return gid;
893N/A}