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 11:26:37 UTC
svn commit: r819484 - in /commons/sandbox/runtime/trunk/src:
main/java/org/apache/commons/runtime/io/ main/native/os/unix/
test/org/apache/commons/runtime/
Author: mturk
Date: Mon Sep 28 09:26:23 2009
New Revision: 819484
URL: http://svn.apache.org/viewvc?rev=819484&view=rev
Log:
Add FileSystem class that wraps FileSystemProvider
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMap.java (with props)
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/File.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java
commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.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=819484&r1=819483&r2=819484&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 Sep 28 09:26:23 2009
@@ -488,5 +488,6 @@
{
return inode0(getPath());
}
+
}
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java?rev=819484&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java Mon Sep 28 09:26:23 2009
@@ -0,0 +1,55 @@
+/* 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;
+
+/**
+ * File system class.
+ */
+public class FileSystem
+{
+
+ private static FileSystemProvider fs;
+ private FileSystem()
+ {
+ // No instance.
+ }
+
+ static {
+ fs = new FileSystemProvider();
+ }
+ /**
+ * 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
+ {
+ return fs.open(path, mode);
+ }
+
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: 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=819484&r1=819483&r2=819484&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java Mon Sep 28 09:26:23 2009
@@ -31,9 +31,9 @@
class FileSystemProvider
{
- private FileSystemProvider()
+ protected FileSystemProvider()
{
- // No instance.
+ // Nothing.
}
private static native Descriptor open0(String path, int mode)
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMap.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMap.java?rev=819484&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMap.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMap.java Mon Sep 28 09:26:23 2009
@@ -0,0 +1,108 @@
+/* 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 java.io.IOException;
+import java.util.Enumeration;
+import java.util.EnumSet;
+import java.util.Vector;
+import org.apache.commons.runtime.Descriptor;
+import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.exception.ClosedDescriptorException;
+import org.apache.commons.runtime.exception.OperatingSystemException;
+import org.apache.commons.runtime.exception.UnsupportedOperatingSystemException;
+
+/**
+ * Represents the memory mapped file.
+ * <p>
+ * </p>
+ */
+public final class MemoryMap implements java.io.Closeable
+{
+ /*
+ * Reference to the OS memory map provider.
+ */
+ private MemoryMapProvider mp;
+ private MemoryMap()
+ {
+ // No Instance
+ }
+
+ /**
+ * Create new MemoryMapPrivider from the abstract {@code File}.
+ * <p>
+ * </p>
+ * @param path Abstract path name of the existing file to map.
+ * @param mode Open mode flags.
+ */
+ public MemoryMap(File path, EnumSet<FileOpenMode> mode)
+ throws IOException, IllegalArgumentException, OutOfMemoryError
+ {
+ mp = new MemoryMapProvider(path, mode);
+ }
+
+ /**
+ * Create new MemoryMapPrivider from the esisting filr {@code Descriptor}.
+ * <p>
+ * </p>
+ * @param fd Valid file descriptor.
+ * @param mode Open mode flags.
+ */
+ public MemoryMap(Descriptor fd, EnumSet<FileOpenMode> mode)
+ throws IOException, IllegalArgumentException, OutOfMemoryError,
+ ClosedDescriptorException
+ {
+ mp = new MemoryMapProvider(fd, mode);
+ }
+
+ /**
+ * Map the region of file.
+ * @param offset Offset from the file begin.
+ * @param size Map size in bytes.
+ * @return new {@code Pointer} object
+ */
+ public Pointer map(long offset, long size)
+ throws IOException, SecurityException, IllegalArgumentException,
+ ClosedDescriptorException
+ {
+ return mp.map(offset, size);
+ }
+
+ /**
+ * Map the entire file starting from offset.
+ * @param offset Offset from the file begin.
+ * @return new {@code Pointer} object
+ */
+ public Pointer map(long offset)
+ throws IOException, SecurityException, IllegalArgumentException,
+ ClosedDescriptorException
+ {
+ return mp.map(offset);
+ }
+
+ public boolean unmap(Pointer memory)
+ throws IOException
+ {
+ return mp.unmap(memory);
+ }
+
+ public void close()
+ throws IOException
+ {
+ mp.close();
+ }
+}
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMap.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java?rev=819484&r1=819483&r2=819484&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/MemoryMapProvider.java Mon Sep 28 09:26:23 2009
@@ -31,7 +31,7 @@
* <p>
* </p>
*/
-public final class MemoryMapProvider implements java.io.Closeable
+class MemoryMapProvider implements java.io.Closeable
{
/*
* Reference to the OS memory mapped file descriptor
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=819484&r1=819483&r2=819484&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 Mon Sep 28 09:26:23 2009
@@ -80,7 +80,7 @@
return rc;
}
-static int do_fopen(ACR_JNISTDARGS, char *fname, int flags, int mode,
+static int do_fopen(ACR_JNISTDARGS, char *fname, int flags, int prot,
jobject *fdo)
{
int rc = 0;
@@ -99,7 +99,7 @@
/* Neither READ nor WRITE was specified.
* We don't know how to open the file.
*/
- return ACR_EACCES;
+ return ACR_EINVAL;
}
if (flags & ACR_FOPEN_CREATE) {
oflags |= O_CREAT;
@@ -107,7 +107,7 @@
oflags |= O_EXCL;
}
if ((flags & ACR_FOPEN_EXCL) && !(flags & ACR_FOPEN_CREATE)) {
- return ACR_EACCES;
+ return ACR_EINVAL;
}
if (flags & ACR_FOPEN_APPEND)
oflags |= O_APPEND;
@@ -126,21 +126,21 @@
#if defined(_LARGEFILE64_SOURCE)
oflags |= O_LARGEFILE;
#endif
- if (mode == ACR_FPROT_OS_DEFAULT)
+ if (prot == ACR_FPROT_OS_DEFAULT)
fd = open(fname, oflags, 0666);
else
- fd = open(fname, oflags, ACR_UnixPermsToMode(mode));
+ fd = open(fname, oflags, ACR_UnixPermsToMode(prot));
if (fd < 0) {
return ACR_GET_OS_ERROR();
}
if (!(flags & ACR_FOPEN_NOCLEANUP)) {
- int fflags;
- if ((fflags = fcntl(fd, F_GETFD)) == -1) {
+ int mode;
+ if ((mode = fcntl(fd, F_GETFD)) == -1) {
rc = ACR_GET_OS_ERROR();
goto finally;
}
- fflags |= FD_CLOEXEC;
- if (fcntl(fd, F_SETFD, fflags) == -1) {
+ mode |= FD_CLOEXEC;
+ if (fcntl(fd, F_SETFD, mode) == -1) {
rc = ACR_GET_OS_ERROR();
goto finally;
}
@@ -151,7 +151,7 @@
goto finally;;
}
fp->fd = fd;
- fp->name = fname;
+ fp->name = ACR_StrdupA(_E, THROW_NMARK, fname);
fp->flags = flags;
fp->type = ACR_FT_REG; /* Presume it's a regular file */
if (!(flags & ACR_FOPEN_NOCLEANUP))
@@ -173,7 +173,10 @@
if (rc) {
if (fd >= 0)
close(fd);
- x_free(fp);
+ if (fp) {
+ x_free(fp->name);
+ x_free(fp);
+ }
}
return rc;
@@ -188,30 +191,32 @@
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);
+ if (rc && !ACR_STATUS_IS_EEXIST(rc) && !(flags & ACR_FOPEN_EXCL)) {
+ /* Don't Throw in case of EXCL and EEXIST
+ */
+ ACR_THROW_IO_IF_ERR(rc);
+ }
return fdo;
}
ACR_IO_EXPORT_DECLARE(jobject, FileSystemProvider, open1)(ACR_JNISTDARGS,
jstring fname,
jint flags,
- jint mode)
+ jint prot)
{
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;
- }
+ rc = do_fopen(_E, _O, J2S(fname), flags, prot, &fdo);
} END_WITH_CSTR(fname);
- ACR_THROW_IO_IF_ERR(rc);
+ if (rc && !ACR_STATUS_IS_EEXIST(rc) && !(flags & ACR_FOPEN_EXCL)) {
+ /* Don't Throw in case of EXCL and EEXIST
+ */
+ ACR_THROW_IO_IF_ERR(rc);
+ }
return fdo;
}
Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=819484&r1=819483&r2=819484&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Mon Sep 28 09:26:23 2009
@@ -61,6 +61,7 @@
// suite.addTest(TestSystem.suite());
//
suite.addTest(TestSignal.suite());
+ suite.addTest(TestFileSys.suite());
return suite;
}
Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java?rev=819484&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java (added)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java Mon Sep 28 09:26:23 2009
@@ -0,0 +1,75 @@
+/* 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;
+
+import org.apache.commons.runtime.exception.*;
+import org.apache.commons.runtime.io.*;
+import junit.framework.*;
+import java.io.IOException;
+import java.util.EnumSet;
+
+/**
+ * File System Test.
+ *
+ */
+public class TestFileSys extends TestCase
+{
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(TestFileSys.class);
+ return suite;
+ }
+
+ protected void setUp()
+ throws Exception
+ {
+ Loader.load();
+ }
+
+ public void testFileSysOpenDef()
+ throws Exception
+ {
+ File file = new File("ftest1.txt");
+ Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE));
+
+ assertFalse("Descriptor", fd == null);
+ System.out.println();
+ assertEquals("Descriptor type", DescriptorType.FILE, fd.getType());
+ fd.close();
+ }
+
+ public void testFileSysOpenExisting()
+ throws Exception
+ {
+ File file = new File("ftest1.txt");
+ Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
+ assertFalse("Descriptor", fd != null);
+
+ }
+
+ public void testFileSysOpenAgain()
+ throws Exception
+ {
+ File file = new File("ftest1.txt");
+ file.delete();
+ Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
+ assertFalse("Descriptor", fd == null);
+
+ }
+
+}
+
Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java?rev=819484&r1=819483&r2=819484&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemoryMap.java Mon Sep 28 09:26:23 2009
@@ -45,7 +45,7 @@
throws Throwable
{
File f = new File("org/apache/commons/runtime/TestMemoryMap.class");
- MemoryMapProvider map = new MemoryMapProvider(f, EnumSet.of(FileOpenMode.READ));
+ MemoryMap map = new MemoryMap(f, EnumSet.of(FileOpenMode.READ));
Pointer p = map.map(0, 128);
assertEquals("Class header [0]", (byte)0xCA, (byte)p.peek(0));
assertEquals("Class header [1]", (byte)0xFE, (byte)p.peek(1));
@@ -58,7 +58,7 @@
throws Throwable
{
File f = new File("org/apache/commons/runtime/TestMemoryMap.class");
- MemoryMapProvider map = new MemoryMapProvider(f, EnumSet.of(FileOpenMode.READ));
+ MemoryMap map = new MemoryMap(f, EnumSet.of(FileOpenMode.READ));
Pointer p = map.map(0);
System.out.println();
System.out.println("Map size is " + p.sizeof());
@@ -75,7 +75,7 @@
// TODO: Use at least Platform.GRANULARITY file size
//
File f = new File("org/apache/commons/runtime/TestMemoryMap.class");
- MemoryMapProvider map = new MemoryMapProvider(f, EnumSet.of(FileOpenMode.READ));
+ MemoryMap map = new MemoryMap(f, EnumSet.of(FileOpenMode.READ));
Pointer p1 = map.map(0, 128);
Pointer p2 = map.map(0, 128);
Pointer pa = null;