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/10/01 20:47:12 UTC

svn commit: r820747 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_file.h os/unix/fsysio.c os/win32/fsysio.c

Author: mturk
Date: Thu Oct  1 18:47:12 2009
New Revision: 820747

URL: http://svn.apache.org/viewvc?rev=820747&view=rev
Log:
Add method for opening standard files

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h?rev=820747&r1=820746&r2=820747&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h Thu Oct  1 18:47:12 2009
@@ -79,37 +79,45 @@
 
 /* Removed XTHREAD and LARGEFILE */
 
-#define ACR_FOPEN_READ       0x00001  /**< Open the file for reading */
-#define ACR_FOPEN_WRITE      0x00002  /**< Open the file for writing */
-#define ACR_FOPEN_RDWR       0x00003  /**< Convenience for READ | WRITE */
-#define ACR_FOPEN_CREATE     0x00004  /**< Create the file if not there */
-#define ACR_FOPEN_APPEND     0x00008  /**< Append to the end of the file */
-#define ACR_FOPEN_TRUNCATE   0x00010  /**< Open the file and truncate
-                                         to 0 length */
-#define ACR_FOPEN_BINARY     0x00020  /**< Open the file in binary mode */
-#define ACR_FOPEN_EXCL       0x00040  /**< Open should fail if APR_CREATE
-                                         and file exists. */
-#define ACR_FOPEN_NONBLOCK   0x00080  /**< Open the file for non blocking I/O */
-#define ACR_FOPEN_DELONCLOSE 0x00100  /**< Delete the file after close */
+#define ACR_FOPEN_READ       0x000001  /**< Open the file for reading */
+#define ACR_FOPEN_WRITE      0x000002  /**< Open the file for writing */
+#define ACR_FOPEN_RDWR       0x000003  /**< Convenience for READ | WRITE */
+#define ACR_FOPEN_CREATE     0x000004  /**< Create the file if not there */
+#define ACR_FOPEN_APPEND     0x000008  /**< Append to the end of the file */
+#define ACR_FOPEN_TRUNCATE   0x000010  /**< Open the file and truncate
+                                            to 0 length */
+#define ACR_FOPEN_BINARY     0x000020  /**< Open the file in binary mode */
+#define ACR_FOPEN_EXCL       0x000040  /**< Open should fail if APR_CREATE
+                                            and file exists. */
+#define ACR_FOPEN_NONBLOCK   0x000080  /**< Open the file for non blocking I/O */
+#define ACR_FOPEN_DELONCLOSE 0x000100  /**< Delete the file after close */
 
-#define ACR_FOPEN_SHARELOCK  0x00400  /**< Platform dependent support for
+#define ACR_FOPEN_SHARELOCK  0x000400  /**< Platform dependent support for
                                          higher level locked read/write
                                          access to support writes across
                                          process/machines */
-#define ACR_FOPEN_NOCLEANUP  0x00800  /**< Do not register a cleanup
+#define ACR_FOPEN_NOCLEANUP  0x000800  /**< Do not register a cleanup
                                          when the file is opened */
-#define ACR_FOPEN_EXEC       0x02000  /**< Platform dependent flag to enable
-                                       * executable page memory mappings
+#define ACR_FOPEN_EXEC       0x002000  /**< Platform dependent flag to enable
+                                        * executable page memory mappings
+                                        */
+#define ACR_FOPEN_SPARSE     0x008000  /**< Platform dependent flag to enable
+                                        * sparse file support, see WARNING below
+                                        */
+
+#define ACR_FOPEN_SENDFILE   0x001000 /**< Advisory flag that this
+                                       * file should support sendfile
+                                       * operation.
                                        */
-#define ACR_FOPEN_SPARSE     0x08000  /**< Platform dependent flag to enable
-                                       * sparse file support, see WARNING below
+/** Internal File flags
+ * Should probably go inside acr_private
+ */
+#define ACR_STDFILE_FLAGS    0x070000 /**< Denotes that a file is one of std
+                                       * files
                                        */
-
-#define ACR_FOPEN_SENDFILE_ENABLED 0x01000 /**< Advisory flag that this
-                                            * file should support sendfile
-                                            * operation.
-                                            */
-
+#define ACR_STDIN_FLAG       0x010000
+#define ACR_STDOUT_FLAG      0x020000
+#define ACR_STDERR_FLAG      0x040000
 /**
  * @defgroup apr_file_attrs_set_flags File Attribute Flags
  * @{

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c?rev=820747&r1=820746&r2=820747&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c Thu Oct  1 18:47:12 2009
@@ -34,15 +34,16 @@
     if (type != ACR_DT_FILE) {
         return ACR_EBADF;
     }
-    if (fp->fd > 0) {
+    if (fp->fd >= 0) {
         if (close(fp->fd))
             rc = ACR_GET_OS_ERROR();
         else
             rc = ACR_SUCCESS;
     }
-    x_free(fp->name);
-    fp->name = NULL;
-
+    if (fp->name) {
+        x_free(fp->name);
+        fp->name = NULL;
+    }
     if (flags & ACR_IOH_CLEAR)
         x_free(fp);
     else
@@ -50,6 +51,34 @@
     return rc;
 }
 
+static int file_cclose(void *file, int type, unsigned int flags)
+{
+    int rc = ACR_SUCCESS;
+    acr_file_t *fp = (acr_file_t *)file;
+
+    if (type != ACR_DT_FILE) {
+        return ACR_EBADF;
+    }
+    if (fp->name) {
+        x_free(fp->name);
+        fp->name = NULL;
+    }
+    if (flags & ACR_IOH_CLEAR)
+        x_free(fp);
+    else {
+        /* This is from Descriptor.close() call
+         */
+        if (fp->fd >= 0) {
+            if (close(fp->fd))
+                rc = ACR_GET_OS_ERROR();
+        }
+        else
+            rc = ACR_EBADF;
+        fp->fd = -1;
+    }
+    return rc;
+}
+
 static int descriptor_cleanup(ACR_JNISTDARGS,
                               acr_descriptor_cb_type_e cm,
                               acr_descriptor_cb_t *dp)
@@ -58,10 +87,6 @@
     switch (cm) {
         case ACR_DESC_FINALIZE:
             if (dp->di > 0) {
-                acr_file_t *fp = ACR_IOH_FDATA(dp->di);
-                if (fp->flags & ACR_FOPEN_NOCLEANUP) {
-                    fp->fd = -1;
-                }
                 rc = acr_ioh_clear(dp->di);
             }
             else
@@ -198,7 +223,7 @@
         fp->timeout  = -1;
     }
     if (flags & ACR_FOPEN_NOCLEANUP)
-        fo = acr_ioh_open(fp, ACR_DT_FILE, 0, NULL);
+        fo = acr_ioh_open(fp, ACR_DT_FILE, 0, file_cclose);
     else
         fo = acr_ioh_open(fp, ACR_DT_FILE, 0, file_cleanup);
     if (fo < 0) {
@@ -264,6 +289,67 @@
     return fdo;
 }
 
+ACR_IO_EXPORT_DECLARE(jobject, FileSystem, open2)(ACR_JNISTDARGS,
+                                                  jint which,
+                                                  jint flags)
+{
+    int rc =  0;
+    int fd = -1;
+    int fo;
+    jobject fdo = NULL;
+    acr_file_t *fp = NULL;
+
+    flags |= ACR_FOPEN_NOCLEANUP;
+    switch (which) {
+        case 0:
+            fd = STDIN_FILENO;
+            flags |= ACR_FOPEN_READ | ACR_STDIN_FLAG;
+        break;
+        case 1:
+            fd = STDOUT_FILENO;
+            flags |= ACR_FOPEN_WRITE | ACR_STDOUT_FLAG;
+        break;
+        case 2:
+            fd = STDERR_FILENO;
+            flags |= ACR_FOPEN_WRITE | ACR_STDERR_FLAG;
+        break;
+        default:
+            return NULL;
+        break;
+    }
+    fp = ACR_CALLOC(acr_file_t, 1);
+    if (!fp) {
+        rc = ACR_ENOMEM;
+        goto finally;
+    }
+    fp->fd       = fd;
+    fp->flags    = flags;
+    fp->type     = ACR_FT_PIPE;
+    fp->blocking = BLK_ON;
+    fp->timeout  = -1;
+    fo = acr_ioh_open(fp, ACR_DT_FILE, 0, NULL);
+    if (fo < 0) {
+        rc = ACR_GET_OS_ERROR();
+        goto finally;
+    }
+    /* Create File Descriptor Object */
+    fdo = ACR_DescriptorCreate(_E, ACR_DT_FILE, fo, file_cclose,
+                               descriptor_cleanup);
+    if (!fdo) {
+        rc = ACR_GET_OS_ERROR();
+        x_free(fp);
+        ACR_THROW_IO_IF_ERR(rc);
+        return NULL;
+    }
+
+finally:
+    if (rc) {
+        ACR_THROW_IO_IF_ERR(rc);
+        x_free(fp);
+    }
+    return fdo;
+}
+
 static int wait_for_io_or_timeout(acr_file_t *f, int for_read)
 {
     int rc, timeout = f->timeout < 0 ? -1 : (int)(f->timeout / 1000);

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c?rev=820747&r1=820746&r2=820747&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c Thu Oct  1 18:47:12 2009
@@ -51,6 +51,34 @@
     return rc;
 }
 
+static int file_cclose(void *file, int type, unsigned int flags)
+{
+    int rc = ACR_SUCCESS;
+    acr_file_t *fp = (acr_file_t *)file;
+
+    if (type != ACR_DT_FILE) {
+        return ACR_EBADF;
+    }
+    if (fp->name) {
+        x_free(fp->name);
+        fp->name = NULL;
+    }
+    if (flags & ACR_IOH_CLEAR)
+        x_free(fp);
+    else {
+        /* This is from Descriptor.close() call
+         */
+        if (IS_VALID_HANDLE(fp->fd)) {
+            if (!CloseHandle(fp->fd))
+                rc = ACR_GET_OS_ERROR();
+        }
+        else
+            rc = ACR_EBADF;
+        fp->fd = INVALID_HANDLE_VALUE;
+    }
+    return rc;
+}
+
 static int descriptor_cleanup(ACR_JNISTDARGS,
                               acr_descriptor_cb_type_e cm,
                               acr_descriptor_cb_t *dp)
@@ -59,10 +87,6 @@
     switch (cm) {
         case ACR_DESC_FINALIZE:
             if (dp->di > 0) {
-                acr_file_t *fp = ACR_IOH_FDATA(dp->di);
-                if (fp->flags & ACR_FOPEN_NOCLEANUP) {
-                    fp->fd = INVALID_HANDLE_VALUE;
-                }
                 rc = acr_ioh_clear(dp->di);
             }
             else
@@ -136,7 +160,7 @@
     }
     if (flags & ACR_FOPEN_DELONCLOSE)
         aflags |= FILE_FLAG_DELETE_ON_CLOSE;
-    if (flags & ACR_FOPEN_SENDFILE_ENABLED) {
+    if (flags & ACR_FOPEN_SENDFILE) {
         flags  |= ACR_FOPEN_NONBLOCK;
         aflags |= FILE_FLAG_SEQUENTIAL_SCAN;
     }
@@ -190,7 +214,7 @@
         fp->timeout  = -1;
     }
     if (flags & ACR_FOPEN_NOCLEANUP)
-        fo = acr_ioh_open(fp, ACR_DT_FILE, 0, NULL);
+        fo = acr_ioh_open(fp, ACR_DT_FILE, 0, file_cclose);
     else
         fo = acr_ioh_open(fp, ACR_DT_FILE, 0, file_cleanup);
     if (fo < 0) {
@@ -300,6 +324,77 @@
     return fdo;
 }
 
+ACR_IO_EXPORT_DECLARE(jobject, FileSystem, open2)(ACR_JNISTDARGS,
+                                                  jint which,
+                                                  jint flags)
+{
+    DWORD  rc =  0;
+    HANDLE fd = INVALID_HANDLE_VALUE;
+    int fo;
+    jobject fdo = NULL;
+    acr_file_t *fp = NULL;
+
+    flags |= ACR_FOPEN_NOCLEANUP;
+    switch (which) {
+        case 0:
+            fd = GetStdHandle(STD_INPUT_HANDLE);
+            flags |= ACR_FOPEN_READ | ACR_STDIN_FLAG;
+        break;
+        case 1:
+            fd = GetStdHandle(STD_OUTPUT_HANDLE);
+            flags |= ACR_FOPEN_WRITE | ACR_STDOUT_FLAG;
+        break;
+        case 2:
+            fd = GetStdHandle(STD_ERRROR_HANDLE);
+            flags |= ACR_FOPEN_WRITE | ACR_STDERR_FLAG;
+        break;
+        default:
+            return NULL;
+        break;
+    }
+    if (fd == INVALID_HANDLE_VALUE) {
+        ACR_THROW_IO_IF_ERRNO();
+        return NULL;
+    }
+    if (fd == NULL) {
+        /* Process doesn't have associated standard
+         * handles. Use INVALID_HANDLE_VALUE like APR
+         */
+        fd = INVALID_HANDLE_VALUE;
+    }
+    fp = ACR_CALLOC(acr_file_t, 1);
+    if (!fp) {
+        rc = ACR_ENOMEM;
+        goto finally;
+    }
+    fp->fd       = fd;
+    fp->flags    = flags;
+    fp->type     = ACR_FT_PIPE;
+    fp->blocking = BLK_ON;
+    fp->timeout  = -1;
+    fo = acr_ioh_open(fp, ACR_DT_FILE, 0, NULL);
+    if (fo < 0) {
+        rc = ACR_GET_OS_ERROR();
+        goto finally;
+    }
+    /* Create File Descriptor Object */
+    fdo = ACR_DescriptorCreate(_E, ACR_DT_FILE, fo, file_cclose,
+                               descriptor_cleanup);
+    if (!fdo) {
+        rc = ACR_GET_OS_ERROR();
+        x_free(fp);
+        ACR_THROW_IO_IF_ERR(rc);
+        return NULL;
+    }
+
+finally:
+    if (rc) {
+        ACR_THROW_IO_IF_ERR(rc);
+        x_free(fp);
+    }
+    return fdo;
+}
+
 static int do_lock(acr_file_t *f, DWORD flags)
 {
     if (f->blocking == BLK_OFF) {