import java.io.File ; import java.io.IOException ; public class Symlink { private static final String[] FAILURES = {null, "get path", "lstat", "new buf", "get buf", "readlink", "size mismatch", "malloc buf", "symlink"} ; static { System.loadLibrary("symlink") ; } private Symlink() {} public static byte[] readlink(File f) throws IOException { String path = f.getAbsolutePath() ; byte[][] bufHolder = new byte[1][] ; int status = readlink(path, bufHolder) ; validateStatus(status) ; return bufHolder[0] ; } private static native int readlink(String path, byte[][] bufHolder) ; public static void writelink(File f, byte[] buf) throws IOException { String path = f.getCanonicalPath() ; int status = symlink(path, buf) ; validateStatus(status) ; } private static native int symlink(String path, byte[] buf) ; private static void validateStatus(int status) throws IOException { if (status < 0) { int failure = (-status) >> 16 ; int errno = (-status) & 0xffff ; throw new IOException("failure: " + FAILURES[failure] + ", errno: " + Errno.NAMES[errno]) ; } } }