Added PosixFile.symlink() and readLink().

This commit is contained in:
Adam Murdoch
2012-09-01 16:11:30 +10:00
parent cd24d5fb51
commit 4e8db25017
8 changed files with 161 additions and 13 deletions

View File

@@ -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;
}

View File

@@ -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
*/

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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);
}

View File

@@ -31,25 +31,81 @@ class PosixFileTest extends Specification {
file.getMode(testFile) == 0740
}
def "throws exception on failure to set mode"() {
def file = new File(tmpDir.root, "unknown")
def "cannot set mode on file that does not exist"() {
def testFile = new File(tmpDir.root, "unknown")
when:
this.file.setMode(file, 0660)
file.setMode(testFile, 0660)
then:
NativeException e = thrown()
e.message == "Could not set UNIX mode on $file: could not chmod file (errno 2)"
e.message == "Could not set UNIX mode on $testFile: could not chmod file (errno 2)"
}
def "throws exception on failure to get mode"() {
def file = new File(tmpDir.root, "unknown")
def "cannot get mode on file that does not exist"() {
def testFile = new File(tmpDir.root, "unknown")
when:
this.file.getMode(file)
file.getMode(testFile)
then:
NativeException e = thrown()
e.message == "Could not get UNIX mode on $file: could not stat file (errno 2)"
e.message == "Could not get UNIX mode on $testFile: could not stat file (errno 2)"
}
def "can create symbolic link"() {
def testFile = new File(tmpDir.root, "test.txt")
testFile.text = "hi"
def symlinkFile = new File(tmpDir.root, "symlink")
when:
file.symlink(symlinkFile, testFile.name)
then:
symlinkFile.file
symlinkFile.text == "hi"
symlinkFile.canonicalFile == testFile.canonicalFile
}
def "can read symbolic link"() {
def symlinkFile = new File(tmpDir.root, "symlink")
when:
file.symlink(symlinkFile, "target")
then:
file.readLink(symlinkFile) == "target"
}
def "cannot read a symlink that does not exist"() {
def symlinkFile = new File(tmpDir.root, "symlink")
when:
file.readLink(symlinkFile)
then:
NativeException e = thrown()
e.message == "Could not read symlink $symlinkFile: could not lstat file (errno 2)"
}
def "cannot read a symlink that is not a symlink"() {
def symlinkFile = tmpDir.newFile("not-a-symlink.txt")
when:
file.readLink(symlinkFile)
then:
NativeException e = thrown()
e.message == "Could not read symlink $symlinkFile: could not readlink (errno 22)"
}
def "can create and read symlink with unicode in its name"() {
def symlinkFile = new File(tmpDir.root, "symlink\u03b2")
when:
file.symlink(symlinkFile, "target\u03b2")
then:
file.readLink(symlinkFile) == "target\u03b2"
}
}