Added SystemInfo and FileSystems to query system and file system information, respectively.

This commit is contained in:
Adam Murdoch
2012-08-12 06:17:58 +10:00
parent 5f3761dd41
commit 3a58e27eeb
18 changed files with 612 additions and 327 deletions

View File

@@ -10,6 +10,13 @@
#include <sys/ioctl.h>
#include <curses.h>
#include <term.h>
#include <langinfo.h>
#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
@@ -18,6 +25,35 @@ void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) {
mark_failed_with_code(env, message, errno, result);
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getSystemInfo(JNIEnv *env, jclass target, jobject info, jobject result) {
jclass infoClass = env->GetObjectClass(info);
// Empty string means load locale from environment.
locale_t locale = newlocale(LC_CTYPE_MASK, "", LC_GLOBAL_LOCALE);
if (locale == NULL) {
mark_failed_with_message(env, "could not create locale", result);
return;
}
jfieldID characterEncodingField = env->GetFieldID(infoClass, "characterEncoding", "Ljava/lang/String;");
env->SetObjectField(info, characterEncodingField, env->NewStringUTF(nl_langinfo_l(CODESET, locale)));
freelocale(locale);
struct utsname machine_info;
if (uname(&machine_info) != 0) {
mark_failed_with_errno(env, "could not query machine details", result);
return;
}
jfieldID osNameField = env->GetFieldID(infoClass, "osName", "Ljava/lang/String;");
env->SetObjectField(info, osNameField, env->NewStringUTF(machine_info.sysname));
jfieldID osVersionField = env->GetFieldID(infoClass, "osVersion", "Ljava/lang/String;");
env->SetObjectField(info, osVersionField, env->NewStringUTF(machine_info.release));
jfieldID machineArchitectureField = env->GetFieldID(infoClass, "machineArchitecture", "Ljava/lang/String;");
env->SetObjectField(info, machineArchitectureField, env->NewStringUTF(machine_info.machine));
}
/*
* File functions
*/
@@ -47,6 +83,38 @@ 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
*/