- Added Process.getPid() and Terminal.isTerminal().

- Added a test command-line app.
This commit is contained in:
Adam Murdoch
2012-07-29 17:05:06 +10:00
parent 1d56f93e64
commit c009527afa
12 changed files with 141 additions and 25 deletions

View File

@@ -1,12 +1,13 @@
package net.rubygrapefruit.platform;
import net.rubygrapefruit.platform.internal.FileStat;
import net.rubygrapefruit.platform.internal.FunctionResult;
import net.rubygrapefruit.platform.internal.PosixFileFunctions;
import net.rubygrapefruit.platform.internal.*;
import java.io.File;
import java.io.IOException;
/**
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
*/
public class Platform {
private static final Object lock = new Object();
private static boolean loaded;
@@ -23,26 +24,50 @@ public class Platform {
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()));
if (type.equals(PosixFile.class)) {
return type.cast(new DefaultPosixFile());
}
if (type.equals(Process.class)) {
return type.cast(new DefaultProcess());
}
if (type.equals(Terminal.class)) {
return type.cast(new Terminal(){
@Override
public boolean isTerminal(Output output) {
return PosixTerminalFunctions.isatty(output.ordinal());
}
}
});
}
throw new UnsupportedOperationException(String.format("Cannot load unknown native integration %s.",
type.getName()));
}
@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;
private static class DefaultPosixFile implements PosixFile {
@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;
}
}
private static class DefaultProcess implements Process {
@Override
public int getPid() throws NativeException {
return PosixProcessFunctions.getPid();
}
}
}