Added SystemInfo and FileSystems to query system and file system information, respectively.
This commit is contained in:
@@ -56,6 +56,7 @@ task nativeHeaders {
|
|||||||
args '-classpath', sourceSets.main.output.classesDir
|
args '-classpath', sourceSets.main.output.classesDir
|
||||||
args 'net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions'
|
args 'net.rubygrapefruit.platform.internal.jni.NativeLibraryFunctions'
|
||||||
args 'net.rubygrapefruit.platform.internal.jni.PosixFileFunctions'
|
args 'net.rubygrapefruit.platform.internal.jni.PosixFileFunctions'
|
||||||
|
args 'net.rubygrapefruit.platform.internal.jni.PosixFileSystemFunctions'
|
||||||
args 'net.rubygrapefruit.platform.internal.jni.PosixProcessFunctions'
|
args 'net.rubygrapefruit.platform.internal.jni.PosixProcessFunctions'
|
||||||
args 'net.rubygrapefruit.platform.internal.jni.PosixTerminalFunctions'
|
args 'net.rubygrapefruit.platform.internal.jni.PosixTerminalFunctions'
|
||||||
args 'net.rubygrapefruit.platform.internal.jni.TerminfoFunctions'
|
args 'net.rubygrapefruit.platform.internal.jni.TerminfoFunctions'
|
||||||
|
|||||||
@@ -17,5 +17,5 @@ void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, job
|
|||||||
|
|
||||||
JNIEXPORT jint JNICALL
|
JNIEXPORT jint JNICALL
|
||||||
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
|
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
|
||||||
return 4;
|
return 5;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,13 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <term.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
|
* 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);
|
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
|
* File functions
|
||||||
*/
|
*/
|
||||||
@@ -47,6 +83,38 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_stat(JNIEnv *en
|
|||||||
env->SetIntField(dest, modeField, 0777 & fileInfo.st_mode);
|
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
|
* Process functions
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) {
|
|||||||
mark_failed_with_code(env, message, GetLastError(), result);
|
mark_failed_with_code(env, message, GetLastError(), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getPlatform(JNIEnv *env, jclass target) {
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Process functions
|
* Process functions
|
||||||
*/
|
*/
|
||||||
|
|||||||
13
src/main/java/net/rubygrapefruit/platform/FileSystem.java
Normal file
13
src/main/java/net/rubygrapefruit/platform/FileSystem.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package net.rubygrapefruit.platform;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public interface FileSystem {
|
||||||
|
File getMountPoint();
|
||||||
|
|
||||||
|
String getFileSystemType();
|
||||||
|
|
||||||
|
boolean isRemote();
|
||||||
|
|
||||||
|
String getDeviceName();
|
||||||
|
}
|
||||||
12
src/main/java/net/rubygrapefruit/platform/FileSystems.java
Normal file
12
src/main/java/net/rubygrapefruit/platform/FileSystems.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package net.rubygrapefruit.platform;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface FileSystems extends NativeIntegration {
|
||||||
|
/**
|
||||||
|
* Returns the set of all file systems for the current machine.
|
||||||
|
*
|
||||||
|
* @throws NativeException On failure.
|
||||||
|
*/
|
||||||
|
List<FileSystem> getFileSystems() throws NativeException;
|
||||||
|
}
|
||||||
@@ -6,9 +6,18 @@ public class Main {
|
|||||||
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
|
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
|
||||||
System.out.println("* JVM: " + System.getProperty("java.vm.vendor") + ' ' + System.getProperty("java.version"));
|
System.out.println("* JVM: " + System.getProperty("java.vm.vendor") + ' ' + System.getProperty("java.version"));
|
||||||
|
|
||||||
|
SystemInfo systemInfo = Native.get(SystemInfo.class);
|
||||||
|
System.out.println("* Kernel: " + systemInfo.getKernelName() + ' ' + systemInfo.getKernelVersion() + ' ' + systemInfo.getMachineArchitecture());
|
||||||
|
|
||||||
Process process = Native.get(Process.class);
|
Process process = Native.get(Process.class);
|
||||||
System.out.println("* PID: " + process.getProcessId());
|
System.out.println("* PID: " + process.getProcessId());
|
||||||
|
|
||||||
|
FileSystems fileSystems = Native.get(FileSystems.class);
|
||||||
|
System.out.println("* File systems: ");
|
||||||
|
for (FileSystem fileSystem : fileSystems.getFileSystems()) {
|
||||||
|
System.out.println(" * " + fileSystem.getMountPoint() + ' ' + fileSystem.getFileSystemType() + ' ' + fileSystem.getDeviceName() + (fileSystem.isRemote() ? " remote" : " local"));
|
||||||
|
}
|
||||||
|
|
||||||
TerminalAccess terminalAccess = Native.get(TerminalAccess.class);
|
TerminalAccess terminalAccess = Native.get(TerminalAccess.class);
|
||||||
boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout);
|
boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout);
|
||||||
boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr);
|
boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr);
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ public class Native {
|
|||||||
loaded = true;
|
loaded = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T extends NativeIntegration> T get(Class<T> type) {
|
static <T extends NativeIntegration> T get(Class<T> type) {
|
||||||
@@ -70,6 +69,17 @@ public class Native {
|
|||||||
if (type.equals(TerminalAccess.class)) {
|
if (type.equals(TerminalAccess.class)) {
|
||||||
return type.cast(new TerminfoTerminalAccess());
|
return type.cast(new TerminfoTerminalAccess());
|
||||||
}
|
}
|
||||||
|
if (type.equals(SystemInfo.class)) {
|
||||||
|
MutableSystemInfo systemInfo = new MutableSystemInfo();
|
||||||
|
FunctionResult result = new FunctionResult();
|
||||||
|
NativeLibraryFunctions.getSystemInfo(systemInfo, result);
|
||||||
|
if (result.isFailed()) {
|
||||||
|
throw new NativeException(String.format("Could not fetch system information: %s",
|
||||||
|
result.getMessage()));
|
||||||
|
}
|
||||||
|
System.out.println("=> CHARACTER ENCODING: " + systemInfo.characterEncoding);
|
||||||
|
return type.cast(systemInfo);
|
||||||
|
}
|
||||||
} else if (platform.isWindows()) {
|
} else if (platform.isWindows()) {
|
||||||
if (type.equals(Process.class)) {
|
if (type.equals(Process.class)) {
|
||||||
return type.cast(new DefaultProcess());
|
return type.cast(new DefaultProcess());
|
||||||
@@ -78,6 +88,11 @@ public class Native {
|
|||||||
return type.cast(new WindowsTerminalAccess());
|
return type.cast(new WindowsTerminalAccess());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (platform.isOsX()) {
|
||||||
|
if (type.equals(FileSystems.class)) {
|
||||||
|
return type.cast(new PosixFileSystems());
|
||||||
|
}
|
||||||
|
}
|
||||||
throw new UnsupportedOperationException(String.format("Cannot load unsupported native integration %s.",
|
throw new UnsupportedOperationException(String.format("Cannot load unsupported native integration %s.",
|
||||||
type.getName()));
|
type.getName()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.rubygrapefruit.platform;
|
||||||
|
|
||||||
|
public interface SystemInfo extends NativeIntegration {
|
||||||
|
String getKernelName();
|
||||||
|
|
||||||
|
String getKernelVersion();
|
||||||
|
|
||||||
|
String getMachineArchitecture();
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package net.rubygrapefruit.platform.internal;
|
||||||
|
|
||||||
|
import net.rubygrapefruit.platform.FileSystem;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class DefaultFileSystem implements FileSystem {
|
||||||
|
private final File mountPoint;
|
||||||
|
private final String fileSystemType;
|
||||||
|
private final String deviceName;
|
||||||
|
private final boolean remote;
|
||||||
|
|
||||||
|
public DefaultFileSystem(File mountPoint, String fileSystemType, String deviceName, boolean remote) {
|
||||||
|
this.mountPoint = mountPoint;
|
||||||
|
this.fileSystemType = fileSystemType;
|
||||||
|
this.deviceName = deviceName;
|
||||||
|
this.remote = remote;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDeviceName() {
|
||||||
|
return deviceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getMountPoint() {
|
||||||
|
return mountPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFileSystemType() {
|
||||||
|
return fileSystemType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRemote() {
|
||||||
|
return remote;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package net.rubygrapefruit.platform.internal;
|
||||||
|
|
||||||
|
import net.rubygrapefruit.platform.FileSystem;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FileSystemList {
|
||||||
|
public final List<FileSystem> fileSystems = new ArrayList<FileSystem>();
|
||||||
|
|
||||||
|
public void add(String mountPoint, String fileSystemName, String deviceName, boolean remote) {
|
||||||
|
fileSystems.add(new DefaultFileSystem(new File(mountPoint), fileSystemName, deviceName, remote));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package net.rubygrapefruit.platform.internal;
|
||||||
|
|
||||||
|
import net.rubygrapefruit.platform.SystemInfo;
|
||||||
|
|
||||||
|
public class MutableSystemInfo implements SystemInfo {
|
||||||
|
public String osName;
|
||||||
|
public String osVersion;
|
||||||
|
public String characterEncoding;
|
||||||
|
public String machineArchitecture;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getKernelName() {
|
||||||
|
return osName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getKernelVersion() {
|
||||||
|
return osVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMachineArchitecture() {
|
||||||
|
return machineArchitecture;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,6 +35,10 @@ public abstract class Platform {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isOsX() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract String getLibraryName();
|
public abstract String getLibraryName();
|
||||||
|
|
||||||
private static class Windows extends Platform {
|
private static class Windows extends Platform {
|
||||||
@@ -70,6 +74,11 @@ public abstract class Platform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static class OsX extends Posix {
|
private static class OsX extends Posix {
|
||||||
|
@Override
|
||||||
|
public boolean isOsX() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getLibraryName() {
|
public String getLibraryName() {
|
||||||
return "libnative-platform.dylib";
|
return "libnative-platform.dylib";
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package net.rubygrapefruit.platform.internal;
|
||||||
|
|
||||||
|
import net.rubygrapefruit.platform.FileSystem;
|
||||||
|
import net.rubygrapefruit.platform.FileSystems;
|
||||||
|
import net.rubygrapefruit.platform.NativeException;
|
||||||
|
import net.rubygrapefruit.platform.internal.jni.PosixFileSystemFunctions;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PosixFileSystems implements FileSystems {
|
||||||
|
@Override
|
||||||
|
public List<FileSystem> getFileSystems() {
|
||||||
|
FunctionResult result = new FunctionResult();
|
||||||
|
FileSystemList fileSystems = new FileSystemList();
|
||||||
|
PosixFileSystemFunctions.listFileSystems(fileSystems, result);
|
||||||
|
if (result.isFailed()) {
|
||||||
|
throw new NativeException(String.format("Could not query file systems: %s", result.getMessage()));
|
||||||
|
}
|
||||||
|
return fileSystems.fileSystems;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
package net.rubygrapefruit.platform.internal.jni;
|
package net.rubygrapefruit.platform.internal.jni;
|
||||||
|
|
||||||
|
import net.rubygrapefruit.platform.internal.FunctionResult;
|
||||||
|
import net.rubygrapefruit.platform.internal.MutableSystemInfo;
|
||||||
|
|
||||||
public class NativeLibraryFunctions {
|
public class NativeLibraryFunctions {
|
||||||
public static final int VERSION = 4;
|
public static final int VERSION = 5;
|
||||||
|
|
||||||
public static native int getVersion();
|
public static native int getVersion();
|
||||||
|
|
||||||
|
public static native void getSystemInfo(MutableSystemInfo systemInfo, FunctionResult result);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package net.rubygrapefruit.platform.internal.jni;
|
||||||
|
|
||||||
|
import net.rubygrapefruit.platform.internal.FileSystemList;
|
||||||
|
import net.rubygrapefruit.platform.internal.FunctionResult;
|
||||||
|
|
||||||
|
public class PosixFileSystemFunctions {
|
||||||
|
public static native void listFileSystems(FileSystemList fileSystems, FunctionResult result);
|
||||||
|
}
|
||||||
15
src/test/groovy/net/rubygrapefruit/platform/FileSystemsTest.groovy
Executable file
15
src/test/groovy/net/rubygrapefruit/platform/FileSystemsTest.groovy
Executable file
@@ -0,0 +1,15 @@
|
|||||||
|
package net.rubygrapefruit.platform
|
||||||
|
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.rules.TemporaryFolder
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class FileSystemsTest extends Specification {
|
||||||
|
@Rule TemporaryFolder tmpDir
|
||||||
|
final FileSystems fileSystems = Native.get(FileSystems.class)
|
||||||
|
|
||||||
|
def "can query filesystem details"() {
|
||||||
|
expect:
|
||||||
|
fileSystems.fileSystems.collect() { it.mountPoint }.containsAll(File.listRoots())
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/test/groovy/net/rubygrapefruit/platform/SystemInfoTest.groovy
Executable file
17
src/test/groovy/net/rubygrapefruit/platform/SystemInfoTest.groovy
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
package net.rubygrapefruit.platform
|
||||||
|
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.rules.TemporaryFolder
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class SystemInfoTest extends Specification {
|
||||||
|
@Rule TemporaryFolder tmpDir
|
||||||
|
final SystemInfo systemInfo = Native.get(SystemInfo.class)
|
||||||
|
|
||||||
|
def "can query OS details"() {
|
||||||
|
expect:
|
||||||
|
systemInfo.kernelName
|
||||||
|
systemInfo.kernelVersion
|
||||||
|
systemInfo.machineArchitecture
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user