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/09/28 09:46:30 UTC

svn commit: r819470 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/io/FileSystemProvider.java main/native/Makefile.in main/native/os/unix/fsysio.c test/org/apache/commons/runtime/TestPrivate.java

Author: mturk
Date: Mon Sep 28 07:46:30 2009
New Revision: 819470

URL: http://svn.apache.org/viewvc?rev=819470&view=rev
Log:
Add fsys provider stub

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java?rev=819470&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java Mon Sep 28 07:46:30 2009
@@ -0,0 +1,122 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.io;
+
+import org.apache.commons.runtime.Descriptor;
+import org.apache.commons.runtime.exception.ClosedDescriptorException;
+import java.io.IOException;
+import java.util.EnumSet;
+
+/**
+ * Internal File system provider class.
+ * <p>
+ * Apache Commons Runtime {@code FileSystemProvider} provides static
+ * method API for native file portable layer.
+ * </p>
+ */
+class FileSystemProvider
+{
+
+    private FileSystemProvider()
+    {
+        // No instance.
+    }
+
+    private static native Descriptor open0(String path, int mode)
+        throws IOException;
+    /**
+     * Open file Descriptor.
+     * <p>
+     * The underlying OS file must be closed by {@code Descriptor.close()}.
+     * </p>
+     *
+     * @return opened file descriptor.
+     * @throws IOException on error.
+     */
+    public static Descriptor open(File path, EnumSet<FileOpenMode> mode)
+        throws IOException, IllegalArgumentException
+    {
+        int imode = FileOpenMode.bitmapOf(mode);
+        if (imode == 0)
+            throw new IllegalArgumentException();
+        // Call the native method.
+        return open0(path.getPath(), imode);
+    }
+
+    private static native Descriptor open1(String path, int mode, int prot)
+        throws IOException;
+    /**
+     * Open file Descriptor.
+     * <p>
+     * The underlying OS file must be closed by {@code Descriptor.close()}.
+     * </p>
+     *
+     * @return opened file descriptor.
+     * @throws IOException on error.
+     */
+    public static Descriptor open(File path, EnumSet<FileOpenMode> mode,
+                                  EnumSet<FileProtection> prot)
+        throws IOException, IllegalArgumentException
+    {
+        int imode = FileOpenMode.bitmapOf(mode);
+        int iprot = FileProtection.bitmapOf(prot);
+        if (imode == 0)
+            throw new IllegalArgumentException();
+        // Call the native method.
+        return open1(path.getPath(), imode, iprot);
+    }
+
+    private static native int read0(Descriptor fd);
+    /**
+     * Read one byte from the Descriptor.
+     * <p>
+     * On error {@code -1} is returned and {@code Descriptor.errno()}
+     * holds the error code.
+     * </p>
+     *
+     * return byte read or {@code -1} on error.
+     * @throws ClosedDescriptorException if the {@code fd) is closed.
+     */
+    public static int read(Descriptor fd)
+        throws IOException
+    {
+        if (!fd.valid())
+            throw new ClosedDescriptorException();
+        return read0(fd);
+    }
+
+    private static native int write0(Descriptor fd, int val);
+     /**
+     * Write one byte to the Descriptor.
+     * <p>
+     * On error {@code -1} is returned and {@code Descriptor.errno()}
+     * holds the error code.
+     * </p>
+     *
+     * @return Number of bytes written
+     * @throws ClosedDescriptorException if the {@code fd) is closed.
+     */
+    public static int write(Descriptor fd, int val)
+        throws IOException
+    {
+        if (!fd.valid())
+            throw new ClosedDescriptorException();
+        return write0(fd, val);
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=819470&r1=819469&r2=819470&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Mon Sep 28 07:46:30 2009
@@ -119,6 +119,7 @@
 LINUX_OBJS= \
 	$(SRCDIR)/os/unix/dso.$(OBJ) \
 	$(SRCDIR)/os/unix/file.$(OBJ) \
+	$(SRCDIR)/os/unix/fsysio.$(OBJ) \
 	$(SRCDIR)/os/unix/main.$(OBJ) \
 	$(SRCDIR)/os/unix/ios.$(OBJ) \
 	$(SRCDIR)/os/unix/group.$(OBJ) \
@@ -145,6 +146,7 @@
 	$(SRCDIR)/os/unix/dso.$(OBJ) \
 	$(SRCDIR)/os/unix/execmem.$(OBJ) \
 	$(SRCDIR)/os/unix/file.$(OBJ) \
+	$(SRCDIR)/os/unix/fsysio.$(OBJ) \
 	$(SRCDIR)/os/unix/main.$(OBJ) \
 	$(SRCDIR)/os/unix/ios.$(OBJ) \
 	$(SRCDIR)/os/unix/group.$(OBJ) \
@@ -169,6 +171,7 @@
 	$(SRCDIR)/os/unix/dso.$(OBJ) \
 	$(SRCDIR)/os/unix/execmem.$(OBJ) \
 	$(SRCDIR)/os/unix/file.$(OBJ) \
+	$(SRCDIR)/os/unix/fsysio.$(OBJ) \
 	$(SRCDIR)/os/unix/main.$(OBJ) \
 	$(SRCDIR)/os/unix/ios.$(OBJ) \
 	$(SRCDIR)/os/unix/group.$(OBJ) \
@@ -193,6 +196,7 @@
 	$(SRCDIR)/os/unix/dso.$(OBJ) \
 	$(SRCDIR)/os/unix/execmem.$(OBJ) \
 	$(SRCDIR)/os/unix/file.$(OBJ) \
+	$(SRCDIR)/os/unix/fsysio.$(OBJ) \
 	$(SRCDIR)/os/unix/main.$(OBJ) \
 	$(SRCDIR)/os/unix/ios.$(OBJ) \
 	$(SRCDIR)/os/unix/group.$(OBJ) \

Added: 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=819470&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c Mon Sep 28 07:46:30 2009
@@ -0,0 +1,217 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_port.h"
+#include "acr_error.h"
+#include "acr_string.h"
+#include "acr_memory.h"
+#include "acr_descriptor.h"
+#include "acr_pointer.h"
+#include "acr_file.h"
+#include "acr_fileio.h"
+#include "acr_port.h"
+
+static int file_cleanup(void *file, int type, unsigned int flags)
+{
+    int rc = ACR_EBADF;
+    acr_file_t *fp = (acr_file_t *)file;
+
+    if (type != ACR_DT_FILE) {
+        return ACR_EBADF;
+    }
+    if (fp->fd > 0) {
+        if (close(fp->fd))
+            rc = ACR_GET_OS_ERROR();
+        else
+            rc = ACR_SUCCESS;
+    }
+    x_free(fp->name);
+    x_free(fp);
+    return rc;
+}
+
+static int descriptor_cleanup(ACR_JNISTDARGS,
+                              acr_descriptor_cb_type_e cm,
+                              acr_descriptor_cb_t *dp)
+{
+    int rc = ACR_SUCCESS;
+    switch (cm) {
+        case ACR_DESC_CLOSE:
+            if (dp->di > 0) {
+                acr_file_t *fp = ACR_IOH_FDATA(dp->di);
+                if (fp->flags & ACR_FOPEN_NOCLEANUP) {
+                    rc = file_cleanup(fp, ACR_DT_FILE, 0);
+                    ACR_IOH_FDATA(dp->di) = NULL;
+                }
+                rc == 0 ? rc = acr_ioh_close(dp->di) : acr_ioh_close(dp->di);
+            }
+            else
+                rc = ACR_EBADF;
+        break;
+        case ACR_DESC_SYNC:
+            if (dp->di > 0) {
+                acr_file_t *fp = ACR_IOH_FDATA(dp->di);
+                if (fsync(fp->fd) < 0)
+                    rc = ACR_GET_OS_ERROR();
+            }
+            else
+                rc = ACR_EBADF;
+        break;
+        default:
+            rc = ACR_ENOTIMPL;
+        break;
+    }
+    return rc;
+}
+
+static int do_fopen(ACR_JNISTDARGS, char *fname, int flags, int mode,
+                        jobject *fdo)
+{
+    int rc = 0;
+    int fo = -1;
+    int fd = -1;
+    int oflags = 0;
+    acr_file_t *fp = NULL;
+
+    if ((flags & ACR_FOPEN_READ) && (flags &ACR_FOPEN_WRITE))
+        oflags = O_RDWR;
+    else if (flags & ACR_FOPEN_READ)
+        oflags = O_RDONLY;
+    else if (flags & ACR_FOPEN_WRITE)
+        oflags = O_WRONLY;
+    else {
+        /* Neither READ nor WRITE was specified.
+            * We don't know how to open the file.
+            */
+        return ACR_EACCES;
+    }
+    if (flags & ACR_FOPEN_CREATE) {
+        oflags |= O_CREAT;
+        if (flags & ACR_FOPEN_EXCL)
+            oflags |= O_EXCL;
+    }
+    if ((flags & ACR_FOPEN_EXCL) && !(flags & ACR_FOPEN_CREATE)) {
+        return ACR_EACCES;
+    }
+    if (flags & ACR_FOPEN_APPEND)
+        oflags |= O_APPEND;
+    if (flags & ACR_FOPEN_TRUNCATE)
+        oflags |= O_TRUNC;
+#ifdef O_BINARY
+    if (flags & ACR_FOPEN_BINARY)
+        oflags |= O_BINARY;
+#endif
+#ifdef O_CLOEXEC
+    /* Introduced in Linux 2.6.23. Silently ignored on earlier Linux kernels.
+        */
+    if (!(flags & ACR_FOPEN_NOCLEANUP))
+        oflags |= O_CLOEXEC;
+#endif
+#if defined(_LARGEFILE64_SOURCE)
+    oflags |= O_LARGEFILE;
+#endif
+    if (mode == ACR_FPROT_OS_DEFAULT)
+        fd = open(fname, oflags, 0666);
+    else
+        fd = open(fname, oflags, ACR_UnixPermsToMode(mode));
+    if (fd < 0) {
+        return ACR_GET_OS_ERROR();
+    }
+    if (!(flags & ACR_FOPEN_NOCLEANUP)) {
+        int fflags;
+        if ((fflags = fcntl(fd, F_GETFD)) == -1) {
+            rc = ACR_GET_OS_ERROR();
+            goto finally;
+        }
+        fflags |= FD_CLOEXEC;
+        if (fcntl(fd, F_SETFD, fflags) == -1) {
+            rc = ACR_GET_OS_ERROR();
+            goto finally;
+        }
+    }
+    fp = ACR_CALLOC(acr_file_t, 1);
+    if (!fp) {
+        rc = ACR_ENOMEM;
+        goto finally;;
+    }
+    fp->fd     = fd;
+    fp->name   = fname;
+    fp->flags  = flags;
+    fp->type   = ACR_FT_REG; /* Presume it's a regular file */
+    if (!(flags & ACR_FOPEN_NOCLEANUP))
+        fo = acr_ioh_open(fp, ACR_DT_FILE, 0, file_cleanup);
+    else
+        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, NULL,
+                                descriptor_cleanup);
+    if (!*fdo) {
+        rc = ACR_GET_OS_ERROR();
+        goto finally;
+    }
+finally:
+    if (rc) {
+        if (fd >= 0)
+            close(fd);
+        x_free(fp);
+    }
+
+    return rc;
+}
+
+ACR_IO_EXPORT_DECLARE(jobject, FileSystemProvider, open0)(ACR_JNISTDARGS,
+                                                          jstring fname,
+                                                          jint flags)
+{
+    int rc = 0;
+    jobject fdo = NULL;
+
+    WITH_CSTR(fname) {
+        rc = do_fopen(_E, _O, J2S(fname), flags, ACR_FPROT_OS_DEFAULT, &fdo);
+        if (rc == ACR_SUCCESS) {
+            J2S(fname) = NULL;
+        }
+    } END_WITH_CSTR(fname);
+
+    ACR_THROW_IO_IF_ERR(rc);
+    return fdo;
+}
+
+ACR_IO_EXPORT_DECLARE(jobject, FileSystemProvider, open1)(ACR_JNISTDARGS,
+                                                          jstring fname,
+                                                          jint flags,
+                                                          jint mode)
+{
+    int rc = 0;
+    jobject fdo = NULL;
+
+    WITH_CSTR(fname) {
+        rc = do_fopen(_E, _O, J2S(fname), flags, mode, &fdo);
+        if (rc == ACR_SUCCESS) {
+            J2S(fname) = NULL;
+        }
+    } END_WITH_CSTR(fname);
+
+    ACR_THROW_IO_IF_ERR(rc);
+    return fdo;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=819470&r1=819469&r2=819470&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Mon Sep 28 07:46:30 2009
@@ -900,9 +900,15 @@
         assertEquals("Name", "foo", f.getName());
     }
 
+    // Some platforms become pretty unstable even on sucessful
+    // handling of the SIGSEGV. The point is that this only
+    // helps in determining the cause of the fault,so that
+    // process can exit.
+    /*
     public void testMempotect()
         throws Throwable
     {
+
         if (Native.HAS_MAINTAINER_MODE) {
             try {
                 test030(0);
@@ -913,6 +919,7 @@
             }
         }
     }
+    */
 
     public void testMszAnsi()
         throws Throwable