Added PosixFile.symlink() and readLink().
This commit is contained in:
@@ -17,5 +17,5 @@ void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, job
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
|
||||
return 6;
|
||||
return 7;
|
||||
}
|
||||
|
||||
@@ -80,6 +80,48 @@ Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_stat(JNIEnv *en
|
||||
env->SetIntField(dest, modeField, 0777 & fileInfo.st_mode);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_symlink(JNIEnv *env, jclass target, jbyteArray path, jbyteArray contents, jobject result) {
|
||||
jbyte* pathUtf8 = env->GetByteArrayElements(path, NULL);
|
||||
jbyte* contentsUtf8 = env->GetByteArrayElements(contents, NULL);
|
||||
int retval = symlink((const char*)contentsUtf8, (const char*)pathUtf8);
|
||||
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
||||
env->ReleaseByteArrayElements(contents, contentsUtf8, JNI_ABORT);
|
||||
if (retval != 0) {
|
||||
mark_failed_with_errno(env, "could not symlink", result);
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT jbyteArray JNICALL
|
||||
Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_readlink(JNIEnv *env, jclass target, jbyteArray path, jobject result) {
|
||||
struct stat link_info;
|
||||
jbyte* pathUtf8 = env->GetByteArrayElements(path, NULL);
|
||||
int retval = lstat((const char*)pathUtf8, &link_info);
|
||||
if (retval != 0) {
|
||||
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
||||
mark_failed_with_errno(env, "could not lstat file", result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jbyteArray contents = env->NewByteArray(link_info.st_size);
|
||||
if (contents == NULL) {
|
||||
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
||||
mark_failed_with_message(env, "could not create array", result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jbyte* contentsUtf8 = env->GetByteArrayElements(contents, NULL);
|
||||
retval = readlink((const char*)pathUtf8, (char*)contentsUtf8, link_info.st_size);
|
||||
env->ReleaseByteArrayElements(path, pathUtf8, JNI_ABORT);
|
||||
if (retval < 0) {
|
||||
mark_failed_with_errno(env, "could not readlink", result);
|
||||
env->ReleaseByteArrayElements(contents, contentsUtf8, JNI_ABORT);
|
||||
return NULL;
|
||||
}
|
||||
env->ReleaseByteArrayElements(contents, contentsUtf8, JNI_COMMIT);
|
||||
return contents;
|
||||
}
|
||||
|
||||
/*
|
||||
* Process functions
|
||||
*/
|
||||
|
||||
@@ -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