You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/04/20 12:18:50 UTC

svn commit: r766640 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/io/File.java main/native/os/unix/file.c test/org/apache/commons/runtime/TestFile.java

Author: mturk
Date: Mon Apr 20 10:18:50 2009
New Revision: 766640

URL: http://svn.apache.org/viewvc?rev=766640&view=rev
Log:
Add symlink support for File

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java?rev=766640&r1=766639&r2=766640&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java Mon Apr 20 10:18:50 2009
@@ -31,7 +31,12 @@
 public class File extends java.io.File {
 
     // Native methods from libacr.
-    private static native int ftype0(String pathname);
+    private static native int       ftype0(String pathname)
+                                        throws IOException;
+    private static native boolean   mkslink0(String target, String link)
+                                        throws IOException;
+    private static native String    target0(String link)
+                                        throws IOException;
 
     // Catched FileType Enum integer value.
     private int fileType = -1;
@@ -183,4 +188,30 @@
         return fileType == 6;
     }
 
+    public static boolean createSymbolicLink(String targetName, String linkName)
+        throws IOException
+    {
+        return mkslink0(targetName, linkName);
+    }
+
+    public File createSymbolicLink(String linkName)
+        throws IOException
+    {
+        mkslink0(getPath(), linkName);
+        // False means EEXIST, so just consider it opened
+        // TODO: readlink and see if target matches.
+        if (getPath().equals(target0(linkName))) {
+            return new File(linkName, 6);
+        }
+        else {
+            throw new IOException("Symlink exists and points to a different path");
+        }
+    }
+
+    public File getTarget()
+        throws IOException
+    {
+        return new File(target0(getPath()));
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c?rev=766640&r1=766639&r2=766640&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/file.c Mon Apr 20 10:18:50 2009
@@ -117,3 +117,50 @@
 
     return type;
 }
+
+ACR_JNI_EXPORT_DECLARE(jboolean, io_File, mkslink0)(ACR_JNISTDARGS,
+                                                    jstring target,
+                                                    jstring lnkname)
+{
+    jboolean rc = JNI_FALSE;
+
+    UNREFERENCED_O;
+    WITH_CSTR(target) {
+    WITH_CSTR(lnkname) {
+        if (symlink(J2S(target), J2S(lnkname))) {
+            int err = ACR_GET_OS_ERROR();
+            if (err != EEXIST) {
+                ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, err);
+            }
+        }
+        else
+            rc = JNI_TRUE;
+    } END_WITH_CSTR(lnkname);
+    } END_WITH_CSTR(target);
+
+    return rc;
+}
+
+ACR_JNI_EXPORT_DECLARE(jstring, io_File, target0)(ACR_JNISTDARGS,
+                                                  jstring lnkname)
+{
+    jstring rv = NULL;
+
+    UNREFERENCED_O;
+    WITH_CSTR(lnkname) {
+        char buf[PATH_MAX];
+        ssize_t rd;
+
+        rd = readlink(J2S(lnkname), buf, PATH_MAX - 1);
+        if (rd < 0) {
+            int err = ACR_GET_OS_ERROR();
+            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, err);
+        }
+        else {
+            buf[rd] = '\0';
+            rv = ACR_NewJavaStringA(_E, buf);
+        }
+    } END_WITH_CSTR(lnkname);
+
+    return rv;
+}

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java?rev=766640&r1=766639&r2=766640&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFile.java Mon Apr 20 10:18:50 2009
@@ -68,4 +68,65 @@
         assertEquals("Type", FileType.BLK, t);
     }
 
+    public void testMakeSymlink()
+        throws Exception
+    {
+        File target = new File("foo");
+        File symlnk = target.createSymbolicLink("bar");
+        FileType t = symlnk.getFileType();
+        assertEquals("Name", "bar", symlnk.getPath());
+        assertEquals("Type", FileType.LNK, t);
+        symlnk.delete();
+    }
+
+    public void testReadSymlink()
+        throws Exception
+    {
+        File target = new File("foo");
+        File symlnk = target.createSymbolicLink("bar");
+        FileType t = symlnk.getFileType();
+        assertEquals("Name", "bar", symlnk.getPath());
+        assertEquals("Type", FileType.LNK, t);
+        File second = new File("foo");
+        File link2 = second.createSymbolicLink("bar");
+        assertEquals("Name", "bar", link2.getPath());
+
+        symlnk.delete();
+
+    }
+
+    public void testReadSymlinkExists()
+        throws Exception
+    {
+        File target = new File("foo");
+        File symlnk = target.createSymbolicLink("bar");
+        FileType t = symlnk.getFileType();
+        assertEquals("Name", "bar", symlnk.getPath());
+        assertEquals("Type", FileType.LNK, t);
+        File second = new File("foo2");
+        try {
+            File link2 = second.createSymbolicLink("bar");
+            fail("Exception not thrown");
+        } catch (Exception ex) {
+            // This is expected 
+        } finally {        
+            symlnk.delete();
+        }
+    }
+
+    public void testGetTarget()
+        throws Exception
+    {
+        File source = new File("foo");
+        File symlnk = source.createSymbolicLink("bar");
+        FileType t = symlnk.getFileType();
+        assertEquals("Name", "bar", symlnk.getPath());
+        assertEquals("Type", FileType.LNK, t);
+        File target = symlnk.getTarget();
+        assertEquals("Target", source.getPath(), target.getPath());
+
+        symlnk.delete();
+
+    }
+
 }