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/11/21 16:05:42 UTC

svn commit: r882920 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/FileSystemIo.java java/org/apache/commons/runtime/io/Pipe.java native/os/unix/pipe.c

Author: mturk
Date: Sat Nov 21 15:05:40 2009
New Revision: 882920

URL: http://svn.apache.org/viewvc?rev=882920&view=rev
Log:
Implement remaining Pipe methods

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemIo.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Pipe.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/pipe.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemIo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemIo.java?rev=882920&r1=882919&r2=882920&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemIo.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemIo.java Sat Nov 21 15:05:40 2009
@@ -109,7 +109,7 @@
     {
         if (!fd.valid())
             throw new ClosedDescriptorException();
-        int rc = tmget0(fd.fd());
+        int rc = tmset0(fd.fd(), timeout);
         return rc == 0 ? true : false;
     }
 

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Pipe.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Pipe.java?rev=882920&r1=882919&r2=882920&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Pipe.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Pipe.java Sat Nov 21 15:05:40 2009
@@ -65,7 +65,18 @@
     }
 
     PipeIoMode getMode()
+        throws IOException
     {
+        if (!valid())
+            throw new ClosedDescriptorException();
+        if (isBlocking()) {
+            if (mode == PipeIoMode.FULL_NONBLOCK)
+                mode = PipeIoMode.FULL_BLOCK;
+        }
+        else {
+            if (mode != PipeIoMode.FULL_NONBLOCK)
+                mode = PipeIoMode.FULL_NONBLOCK;
+        }
         return mode;
     }
 
@@ -100,6 +111,8 @@
         return create0(mode.valueOf());
     }
 
+    private static native boolean blocking0(int fd)
+        throws IOException;
     /**
      * Test the pipe blocking mode.
      *
@@ -116,7 +129,7 @@
     {
         if (!valid())
             throw new ClosedDescriptorException();
-        return mode != PipeIoMode.FULL_NONBLOCK;
+        return blocking0(fd.fd());
     }
 
     @Override
@@ -182,4 +195,31 @@
         fd.sync();
     }
 
+    private static native int tmget0(int fd)
+        throws IOException;
+    /**
+     * Returns file timeout.
+     */
+    public int getTimeout()
+        throws IOException
+    {
+        if (!fd.valid())
+            throw new ClosedDescriptorException();
+        return tmget0(fd.fd());
+    }
+
+    private static native int tmset0(int fd, int timeout)
+        throws IOException;
+    /**
+     * Returns file timeout.
+     */
+    public boolean setTimeout(int timeout)
+        throws IOException
+    {
+        if (!fd.valid())
+            throw new ClosedDescriptorException();
+        int rc = tmset0(fd.fd(), timeout);
+        return rc == 0 ? true : false;
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/pipe.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pipe.c?rev=882920&r1=882919&r2=882920&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/pipe.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pipe.c Sat Nov 21 15:05:40 2009
@@ -157,33 +157,42 @@
     return rc;
 }
 
+static int pipeblock(int fd, int on)
+{
+    int rc = 0;
+#ifdef O_NONBLOCK
+    /* Use non-blocking I/O
+     */
+    int mode;
+    if ((mode = fcntl(fd, F_GETFL, 0)) == -1) {
+        return ACR_GET_OS_ERROR();
+    }
+    if (on)
+        mode &= ~O_NONBLOCK;
+    else
+        mode |=  O_NONBLOCK;
+    if (fcntl(fd, F_SETFL, mode) == -1) {
+        return ACR_GET_OS_ERROR();
+    }
+#else
+    /* Non blocking files are unsupported.
+     */
+    rc = ACR_ENOTIMPL;
+#endif
+    return rc;
+}
+
 static int do_popen(JNIEnv *_E, int fd, int flags,
                     jobject *fdo)
 {
     int rc =  0;
     int fo;
+    jobject od;
     acr_file_t *fp = NULL;
 
     if ((flags & 0xFF) == ACR_PIPE_FULL_NONBLOCK) {
-#ifdef O_NONBLOCK
-        /* Use non-blocking I/O
-         */
-        int mode;
-        if ((mode = fcntl(fd, F_GETFL, 0)) == -1) {
-            rc = ACR_GET_OS_ERROR();
+        if ((rc = pipeblock(fd, 0)))
             goto finally;
-        }
-        mode |= O_NONBLOCK;
-        if (fcntl(fd, F_SETFL, mode) == -1) {
-            rc = ACR_GET_OS_ERROR();
-            goto finally;
-        }
-#else
-        /* Non blocking files are unsupported.
-         */
-        rc = ACR_ENOTIMPL;
-        goto finally;
-#endif
     }
     fp = ACR_CALLOC(acr_file_t, 1);
     if (!fp) {
@@ -208,15 +217,23 @@
         goto finally;
     }
     /* Create File Descriptor Object */
-    *fdo = ACR_DescriptorCreate(_E, ACR_DT_FILE, fo, NULL,
-                                descriptor_cleanup);
-    if (!*fdo) {
+    od = ACR_DescriptorCreate(_E, ACR_DT_FILE, fo, NULL,
+                              descriptor_cleanup);
+    if (!od) {
         rc = ACR_GET_OS_ERROR();
         goto finally;
     }
+    /* Create Pipe object */
     fp->descriptor = (*_E)->NewWeakGlobalRef(_E, *fdo);
+    *fdo = ACR_NewPipeObject(_E, flags, od, NULL);
+    if (!*fdo) {
+        rc = ACR_GET_OS_ERROR();
+        goto finally;
+    }
 finally:
     if (rc) {
+        if (fp && fp->descriptor)
+            (*_E)->DeleteWeakGlobalRef(_E, fp->descriptor);
         if (fd >= 0)
             close(fd);
         x_free(fp);
@@ -266,3 +283,69 @@
     ACR_THROW_IO_IF_ERR(rc);
     return NULL;
 }
+
+ACR_IO_EXPORT_DECLARE(jboolean, Pipe, blocking0)(ACR_JNISTDARGS,
+                                                 jint pipe)
+{
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(pipe);
+
+    if (ACR_IOH_FTYPE(pipe) != ACR_DT_FILE) {
+        ACR_THROW_IO_IF_ERR(ACR_EFTYPE);
+        return JNI_FALSE;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_IO_IF_ERR(ACR_EBADF);
+        return JNI_FALSE;
+    }
+    return f->blocking == BLK_ON ? JNI_TRUE : JNI_FALSE;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, Pipe, tmset0)(ACR_JNISTDARGS,
+                                          jint pipe, jint timeout)
+{
+    int rc;
+    acr_file_t *f = ACR_IOH_FDATA(pipe);
+
+    if (ACR_IOH_FTYPE(pipe) != ACR_DT_FILE)
+        return ACR_EFTYPE;
+    if (IS_INVALID_HANDLE(f))
+        return ACR_EBADF;
+    if (f->type != ACR_FT_PIPE)
+        return ACR_EFTYPE;
+    f->timeout = timeout;
+    if (timeout >= 0) {
+        if (f->blocking != BLK_OFF) {
+            /* Pipe is in the blocking state
+             */
+            if ((rc = pipeblock(f->fd, 0)) == 0)
+                f->blocking = BLK_OFF;
+            return rc;
+        }
+    }
+    else {
+        if (f->blocking != BLK_ON) {
+            /* Pipe is non-blocking
+             */
+            if ((rc = pipeblock(f->fd, 1)) == 0)
+                f->blocking = BLK_ON;
+            return rc;
+        }
+    }
+    return 0;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, Pipe, tmget0)(ACR_JNISTDARGS,
+                                          jint pipe)
+{
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(pipe);
+
+    if (ACR_IOH_FTYPE(pipe) != ACR_DT_FILE) {
+        ACR_THROW_IO_IF_ERR(ACR_EFTYPE);
+        return 0;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_IO_IF_ERR(ACR_EBADF);
+        return 0;
+    }
+    return (jint)f->timeout;
+}