/* * public static native FileDescriptor open(String path, int flags, int mode); */ JNIEXPORT jobject JNICALL Java_org_apache_hadoop_io_nativeio_NativeIO_open( JNIEnv *env, jclass clazz, jstring j_path, jint flags, jint mode) { jobject ret = NULL; const char *path = (*env)->GetStringUTFChars(env, j_path, NULL); if (path == NULL) goto cleanup; // JVM throws Exception for us int fd; if (flags & O_CREAT) { fd = open(path, flags, mode); } else { fd = open(path, flags); } if (fd == -1) { throw_ioe(env, errno); goto cleanup; } ret = fd_create(env, fd); cleanup: if (path != NULL) { (*env)->ReleaseStringUTFChars(env, j_path, path); } return ret; }
/** * public static native void chmod(String path, int mode) throws IOException; */ JNIEXPORT void JNICALL Java_org_apache_hadoop_io_nativeio_NativeIO_chmod( JNIEnv *env, jclass clazz, jstring j_path, jint mode) { const char *path = (*env)->GetStringUTFChars(env, j_path, NULL); if (path == NULL) return; // JVM throws Exception for us if (chmod(path, mode) != 0) { throw_ioe(env, errno); } (*env)->ReleaseStringUTFChars(env, j_path, path); }
/* * public static native Stat fstat(FileDescriptor fd); */ JNIEXPORT jobject JNICALL Java_org_apache_hadoop_io_nativeio_NativeIO_fstat( JNIEnv *env, jclass clazz, jobject fd_object) { jobject ret = NULL; int fd = fd_get(env, fd_object); struct stat s; int rc = fstat(fd, &s); if (rc != 0) { throw_ioe(env, errno); return ret; } return process_stat(env, s); }
/* * Processes the result of stat() and creates the Java level * NativeIO$Stat object */ static jobject process_stat(JNIEnv *env, struct stat s) { jobject ret = NULL; char *pw_buf = NULL; int pw_lock_locked = 0; size_t pw_buflen = get_pw_buflen(); if ((pw_buf = malloc(pw_buflen)) == NULL) { THROW(env, "java/lang/OutOfMemoryError", "Couldn't allocate memory for pw buffer"); goto cleanup; } if (pw_lock_object != NULL) { if ((*env)->MonitorEnter(env, pw_lock_object) != JNI_OK) { goto cleanup; } pw_lock_locked = 1; } // Grab username struct passwd pwd, *pwdp; int rc; while ((rc = getpwuid_r(s.st_uid, &pwd, pw_buf, pw_buflen, &pwdp)) != 0) { if (rc != ERANGE) { throw_ioe(env, rc); goto cleanup; } free(pw_buf); pw_buflen *= 2; if ((pw_buf = malloc(pw_buflen)) == NULL) { THROW(env, "java/lang/OutOfMemoryError", "Couldn't allocate memory for pw buffer"); goto cleanup; } } assert(pwdp == &pwd); jstring jstr_username = (*env)->NewStringUTF(env, pwd.pw_name); if (jstr_username == NULL) goto cleanup; // Grab group struct group grp, *grpp; while ((rc = getgrgid_r(s.st_gid, &grp, pw_buf, pw_buflen, &grpp)) != 0) { if (rc != ERANGE) { throw_ioe(env, rc); goto cleanup; } free(pw_buf); pw_buflen *= 2; if ((pw_buf = malloc(pw_buflen)) == NULL) { THROW(env, "java/lang/OutOfMemoryError", "Couldn't allocate memory for pw buffer"); goto cleanup; } } assert(grpp == &grp); jstring jstr_groupname = (*env)->NewStringUTF(env, grp.gr_name); PASS_EXCEPTIONS_GOTO(env, cleanup); // Construct result ret = (*env)->NewObject(env, stat_clazz, stat_ctor, jstr_username, jstr_groupname, s.st_mode, s.st_nlink, s.st_ino); cleanup: if (pw_buf != NULL) free(pw_buf); if (pw_lock_locked) { (*env)->MonitorExit(env, pw_lock_object); } return ret; }
/* * public static native Stat fstat(FileDescriptor fd); */ JNIEXPORT jobject JNICALL Java_org_apache_hadoop_io_nativeio_NativeIO_fstat( JNIEnv *env, jclass clazz, jobject fd_object) { jobject ret = NULL; char *pw_buf = NULL; int fd = fd_get(env, fd_object); PASS_EXCEPTIONS_GOTO(env, cleanup); struct stat s; int rc = fstat(fd, &s); if (rc != 0) { throw_ioe(env, errno); goto cleanup; } size_t pw_buflen = get_pw_buflen(); if ((pw_buf = malloc(pw_buflen)) == NULL) { THROW(env, "java/lang/OutOfMemoryError", "Couldn't allocate memory for pw buffer"); goto cleanup; } // Grab username struct passwd pwd, *pwdp; while ((rc = getpwuid_r(s.st_uid, &pwd, pw_buf, pw_buflen, &pwdp)) != 0) { if (rc != ERANGE) { throw_ioe(env, rc); goto cleanup; } free(pw_buf); pw_buflen *= 2; if ((pw_buf = malloc(pw_buflen)) == NULL) { THROW(env, "java/lang/OutOfMemoryError", "Couldn't allocate memory for pw buffer"); goto cleanup; } } assert(pwdp == &pwd); jstring jstr_username = (*env)->NewStringUTF(env, pwd.pw_name); if (jstr_username == NULL) goto cleanup; // Grab group struct group grp, *grpp; while ((rc = getgrgid_r(s.st_gid, &grp, pw_buf, pw_buflen, &grpp)) != 0) { if (rc != ERANGE) { throw_ioe(env, rc); goto cleanup; } free(pw_buf); pw_buflen *= 2; if ((pw_buf = malloc(pw_buflen)) == NULL) { THROW(env, "java/lang/OutOfMemoryError", "Couldn't allocate memory for pw buffer"); goto cleanup; } } assert(grpp == &grp); jstring jstr_groupname = (*env)->NewStringUTF(env, grp.gr_name); PASS_EXCEPTIONS_GOTO(env, cleanup); // Construct result ret = (*env)->NewObject(env, stat_clazz, stat_ctor, jstr_username, jstr_groupname, s.st_mode); cleanup: if (pw_buf != NULL) free(pw_buf); return ret; }