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/27 09:52:08 UTC

svn commit: r884787 - /commons/sandbox/runtime/trunk/src/main/native/os/unix/pipe.c

Author: mturk
Date: Fri Nov 27 08:52:08 2009
New Revision: 884787

URL: http://svn.apache.org/viewvc?rev=884787&view=rev
Log:
Rewrite posix pipe so it looks more like windows implementation

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/pipe.c

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=884787&r1=884786&r2=884787&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 Fri Nov 27 08:52:08 2009
@@ -150,39 +150,79 @@
     return rc;
 }
 
-static int do_popen(JNIEnv *_E, int fd, int flags,
-                    jobject *fdo)
+static int create_pipepair(JNIEnv *_E, acr_file_t **rd, acr_file_t **wr,
+                           int flags)
 {
-    int rc =  0;
-    int fo;
-    int ff = flags & 0xFF;
-    acr_file_t *fp = NULL;
+    int rc = 0;
+    int pd[2] = { -1, -1 };
 
-    if (ff == ACR_PIPE_FULL_NONBLOCK ||
-       (ff == ACR_PIPE_WRITE_BLOCK && (flags & ACR_PIPE_RD)) ||
-       (ff == ACR_PIPE_READ_BLOCK  && (flags & ACR_PIPE_WR))) {
-        if ((rc = acr_nonblock(fd, 1)))
+    if (pipe(pd) == -1)
+        return ACR_GET_OS_ERROR();
+    *wr = NULL;
+    /* We have two handles created.
+     * Create wrapper objects and events
+     */
+    *rd = ACR_CALLOC(acr_file_t, 1);
+    if (!*rd) {
+        rc = ACR_ENOMEM;
+        goto finally;
+    }
+    (*rd)->fd     = pd[0];
+    (*rd)->name   = NULL;
+    (*rd)->flags  = flags;
+    (*rd)->type   = ACR_FT_PIPE;
+    if (flags == ACR_PIPE_WRITE_BLOCK ||
+        flags == ACR_PIPE_FULL_NONBLOCK) {
+        if ((rc = acr_nonblock(pd[0], 1)))
             goto finally;
+        (*rd)->blocking = BLK_OFF;
+        (*rd)->timeout  = 0;
     }
-    fp = ACR_CALLOC(acr_file_t, 1);
-    if (!fp) {
+    else {
+        (*rd)->blocking = BLK_ON;
+        (*rd)->timeout  = -1;
+    }
+
+    *wr = ACR_CALLOC(acr_file_t, 1);
+    if (!*wr) {
         rc = ACR_ENOMEM;
         goto finally;
     }
-    fp->fd     = fd;
-    fp->name   = NULL;
-    fp->flags  = flags;
-    fp->type   = ACR_FT_PIPE;
-    if (ff == ACR_PIPE_FULL_NONBLOCK ||
-       (ff == ACR_PIPE_WRITE_BLOCK && (flags & ACR_PIPE_RD)) ||
-       (ff == ACR_PIPE_READ_BLOCK  && (flags & ACR_PIPE_WR))) {
-        fp->blocking = BLK_OFF;
-        fp->timeout  = 0;
+    (*wr)->fd     = pd[1];
+    (*wr)->name   = NULL;
+    (*wr)->flags  = flags;
+    (*wr)->type   = ACR_FT_PIPE;
+    if (flags == ACR_PIPE_WRITE_BLOCK ||
+        flags == ACR_PIPE_FULL_NONBLOCK) {
+        if ((rc = acr_nonblock(pd[1], 1)))
+            goto finally;
+        (*wr)->blocking = BLK_OFF;
+        (*wr)->timeout  = 0;
     }
     else {
-        fp->blocking = BLK_ON;
-        fp->timeout  = -1;
+        (*wr)->blocking = BLK_ON;
+        (*wr)->timeout  = -1;
     }
+
+    return 0;
+finally:
+    if (*rd) {
+        close((*rd)->fd);
+        x_free(*rd);
+    }
+    if (*wr) {
+        close((*wr)->fd);
+        x_free(*wr);
+    }
+    return rc;
+}
+
+static int do_popen(JNIEnv *_E, acr_file_t *fp, int flags,
+                    jobject *fdo)
+{
+    int rc =  0;
+    int fo;
+
     fo = acr_ioh_open(fp, ACR_DT_FILE, 0, file_cleanup);
     if (fo < 0) {
         rc = ACR_GET_OS_ERROR();
@@ -196,16 +236,11 @@
         rc = ACR_GET_OS_ERROR();
         goto finally;
     }
-    /* Create Pipe object */
     fp->descriptor = (*_E)->NewWeakGlobalRef(_E, *fdo);
 
 finally:
-    if (rc) {
-        if (fp)
-            acr_ioh_clear(fo);
-        else
-            close(fd);
-    }
+    if (rc)
+        acr_ioh_clear(fo);
     return rc;
 }
 
@@ -213,11 +248,11 @@
                                               jint flags)
 {
     int rc = 0;
-    jobject fd[2] = {NULL, NULL };
-    int     pd[2] = {  -1,   -1 };
+    jobject     fd[2];
+    acr_file_t *pd[2] = { NULL, NULL };
 
-    if (pipe(pd) == -1) {
-        ACR_THROW_IO_ERRNO();
+    if ((rc = create_pipepair(_E, &pd[0], &pd[1], flags))) {
+        ACR_THROW_IO_IF_ERR(rc);
         return NULL;
     }
     rc = do_popen(_E, pd[0], flags & ACR_PIPE_RD, &fd[0]);
@@ -229,7 +264,7 @@
         ACR_DescriptorCleanup(_E, fd[0]);
         goto finally;
     }
-    return ACR_NewPipeObject(_E, flags, fd[0], fd[1], NULL);
+    return ACR_NewPipeObject(_E, flags, fd[0], fd[1], pd[0]->name);
 
 finally:
     ACR_THROW_IO_IF_ERR(rc);