Split OS X specific functions out of posix.cpp.

This commit is contained in:
Adam Murdoch
2012-08-12 07:50:08 +10:00
parent 462350d8ae
commit b494c52441
3 changed files with 71 additions and 59 deletions

42
src/main/cpp/osx.cpp Normal file
View File

@@ -0,0 +1,42 @@
#ifdef __APPLE__
#include "native.h"
#include "generic.h"
#include <stdlib.h>
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
/*
* File system functions
*/
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) {
int fs_count = getfsstat(NULL, 0, MNT_NOWAIT);
if (fs_count < 0) {
mark_failed_with_errno(env, "could not stat file systems", result);
return;
}
size_t len = fs_count * sizeof(struct statfs);
struct statfs* buf = (struct statfs*)malloc(len);
if (getfsstat(buf, len, MNT_NOWAIT) < 0 ) {
mark_failed_with_errno(env, "could not stat file systems", result);
free(buf);
return;
}
jclass info_class = env->GetObjectClass(info);
jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V");
for (int i = 0; i < fs_count; i++) {
jstring mount_point = env->NewStringUTF(buf[i].f_mntonname);
jstring file_system_type = env->NewStringUTF(buf[i].f_fstypename);
jstring device_name = env->NewStringUTF(buf[i].f_mntfromname);
jboolean remote = (buf[i].f_flags & MNT_LOCAL) == 0;
env->CallVoidMethod(info, method, mount_point, file_system_type, device_name, remote);
}
free(buf);
}
#endif

View File

@@ -14,9 +14,6 @@
#include <sys/utsname.h>
#include <xlocale.h>
#include <locale.h>
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
/*
* Marks the given result as failed, using the current value of errno
@@ -83,38 +80,6 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_stat(JNIEnv *en
env->SetIntField(dest, modeField, 0777 & fileInfo.st_mode);
}
/*
* File system functions
*/
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileSystemFunctions_listFileSystems(JNIEnv *env, jclass target, jobject info, jobject result) {
int fs_count = getfsstat(NULL, 0, MNT_NOWAIT);
if (fs_count < 0) {
mark_failed_with_errno(env, "could not stat file systems", result);
return;
}
size_t len = fs_count * sizeof(struct statfs);
struct statfs* buf = (struct statfs*)malloc(len);
if (getfsstat(buf, len, MNT_NOWAIT) < 0 ) {
mark_failed_with_errno(env, "could not stat file systems", result);
free(buf);
return;
}
jclass info_class = env->GetObjectClass(info);
jmethodID method = env->GetMethodID(info_class, "add", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V");
for (int i = 0; i < fs_count; i++) {
jstring mount_point = env->NewStringUTF(buf[i].f_mntonname);
jstring file_system_type = env->NewStringUTF(buf[i].f_fstypename);
jstring device_name = env->NewStringUTF(buf[i].f_mntfromname);
jboolean remote = (buf[i].f_flags & MNT_LOCAL) == 0;
env->CallVoidMethod(info, method, mount_point, file_system_type, device_name, remote);
}
free(buf);
}
/*
* Process functions
*/

View File

@@ -1,24 +1,29 @@
#ifndef __INCLUDE_GENERIC_H__
#define __INCLUDE_GENERIC_H__
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Marks the given result as failed, using the given error message
*/
extern void mark_failed_with_message(JNIEnv *env, const char* message, jobject result);
/*
* Marks the given result as failed, using the given error message and error code
*/
extern void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, jobject result);
#ifdef __cplusplus
}
#endif
#endif
#ifndef __INCLUDE_GENERIC_H__
#define __INCLUDE_GENERIC_H__
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Marks the given result as failed, using the given error message
*/
extern void mark_failed_with_message(JNIEnv *env, const char* message, jobject result);
/*
* Marks the given result as failed, using the given error message and the current value of errno/GetLastError()
*/
extern void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result);
/*
* Marks the given result as failed, using the given error message and error code
*/
extern void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, jobject result);
#ifdef __cplusplus
}
#endif
#endif