initial version

This commit is contained in:
Adam Murdoch
2012-07-29 16:35:28 +10:00
commit 1d56f93e64
10 changed files with 221 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
package net.rubygrapefruit.platform;
public class NativeException extends RuntimeException {
public NativeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,4 @@
package net.rubygrapefruit.platform;
public interface NativeIntegration {
}

View File

@@ -0,0 +1,48 @@
package net.rubygrapefruit.platform;
import net.rubygrapefruit.platform.internal.FileStat;
import net.rubygrapefruit.platform.internal.FunctionResult;
import net.rubygrapefruit.platform.internal.PosixFileFunctions;
import java.io.File;
import java.io.IOException;
public class Platform {
private static final Object lock = new Object();
private static boolean loaded;
static <T extends NativeIntegration> T get(Class<T> type) {
synchronized (lock) {
if (!loaded) {
System.setProperty("java.library.path", new File("build/binaries").getAbsolutePath());
try {
System.load(new File("build/binaries/libnative-platform.dylib").getCanonicalPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
loaded = true;
}
}
return type.cast(new UnixFileMode() {
@Override
public void setMode(File file, int perms) {
FunctionResult result = new FunctionResult();
PosixFileFunctions.chmod(file.getPath(), perms, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not set UNIX mode on %s. Errno is %d.", file, result.getErrno()));
}
}
@Override
public int getMode(File file) {
FunctionResult result = new FunctionResult();
FileStat stat = new FileStat();
PosixFileFunctions.stat(file.getPath(), stat, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not get UNIX mode on %s. Errno is %d.", file, result.getErrno()));
}
return stat.mode;
}
});
}
}

View File

@@ -0,0 +1,9 @@
package net.rubygrapefruit.platform;
import java.io.File;
public interface UnixFileMode extends NativeIntegration {
void setMode(File path, int perms) throws NativeException;
int getMode(File path) throws NativeException;
}

View File

@@ -0,0 +1,5 @@
package net.rubygrapefruit.platform.internal;
public class FileStat {
public int mode;
}

View File

@@ -0,0 +1,13 @@
package net.rubygrapefruit.platform.internal;
public class FunctionResult {
int errno;
public boolean isFailed() {
return errno != 0;
}
public int getErrno() {
return errno;
}
}

View File

@@ -0,0 +1,7 @@
package net.rubygrapefruit.platform.internal;
public class PosixFileFunctions {
public static native void chmod(String file, int perms, FunctionResult result);
public static native void stat(String file, FileStat stat, FunctionResult result);
}