Added PosixFile.symlink() and readLink().
This commit is contained in:
@@ -19,4 +19,18 @@ public interface PosixFile extends NativeIntegration {
|
||||
* @throws NativeException On failure.
|
||||
*/
|
||||
int getMode(File path) throws NativeException;
|
||||
|
||||
/**
|
||||
* Creates a symbolic link.
|
||||
*
|
||||
* @throws NativeException On failure.
|
||||
*/
|
||||
void symlink(File link, String contents) throws NativeException;
|
||||
|
||||
/**
|
||||
* Reads the contents of a symbolic link.
|
||||
*
|
||||
* @throws NativeException On failure.
|
||||
*/
|
||||
String readLink(File link) throws NativeException;
|
||||
}
|
||||
|
||||
@@ -40,12 +40,43 @@ public class DefaultPosixFile implements PosixFile {
|
||||
return stat.mode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readLink(File link) throws NativeException {
|
||||
FunctionResult result = new FunctionResult();
|
||||
byte[] encodedContents = PosixFileFunctions.readlink(encode(link), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not read symlink %s: %s", link, result.getMessage()));
|
||||
}
|
||||
return decode(encodedContents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void symlink(File link, String contents) throws NativeException {
|
||||
FunctionResult result = new FunctionResult();
|
||||
PosixFileFunctions.symlink(encode(link), encode(contents), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not create symlink %s: %s", link, result.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private String decode(byte[] path) {
|
||||
try {
|
||||
return new String(path, 0, path.length, characterEncoding);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new NativeException(String.format("Could not decode path using encoding %s.", characterEncoding), e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] encode(File file) {
|
||||
return encode(file.getPath());
|
||||
}
|
||||
|
||||
private byte[] encode(String path) {
|
||||
byte[] encodedName;
|
||||
try {
|
||||
encodedName = file.getPath().getBytes(characterEncoding);
|
||||
encodedName = path.getBytes(characterEncoding);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new NativeException(String.format("Could not encode path for file '%s' using encoding %s.", file.getName(), characterEncoding), e);
|
||||
throw new NativeException(String.format("Could not encode path '%s' using encoding %s.", path, characterEncoding), e);
|
||||
}
|
||||
byte[] buffer = new byte[encodedName.length + 1];
|
||||
System.arraycopy(encodedName, 0, buffer, 0, encodedName.length);
|
||||
|
||||
@@ -4,7 +4,7 @@ import net.rubygrapefruit.platform.internal.FunctionResult;
|
||||
import net.rubygrapefruit.platform.internal.MutableSystemInfo;
|
||||
|
||||
public class NativeLibraryFunctions {
|
||||
public static final int VERSION = 6;
|
||||
public static final int VERSION = 7;
|
||||
|
||||
public static native int getVersion();
|
||||
|
||||
|
||||
@@ -7,4 +7,8 @@ public class PosixFileFunctions {
|
||||
public static native void chmod(byte[] file, int perms, FunctionResult result);
|
||||
|
||||
public static native void stat(byte[] file, FileStat stat, FunctionResult result);
|
||||
|
||||
public static native void symlink(byte[] file, byte[] content, FunctionResult result);
|
||||
|
||||
public static native byte[] readlink(byte[] file, FunctionResult result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user