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/30 16:06:59 UTC
svn commit: r820273 - in /commons/sandbox/runtime/trunk/src:
main/java/org/apache/commons/runtime/io/ main/native/include/
main/native/include/arch/windows/ main/native/os/unix/
main/native/os/win32/ main/native/shared/ test/org/apache/commons/runtime/
Author: mturk
Date: Wed Sep 30 14:06:58 2009
New Revision: 820273
URL: http://svn.apache.org/viewvc?rev=820273&view=rev
Log:
Remove FileSystemProvider and use FileSystem directly
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java
- copied, changed from r819962, commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java
Removed:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileOpenMode.java
commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
commons/sandbox/runtime/trunk/src/main/native/include/acr_file.h
commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/mutex.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/sema.c
commons/sandbox/runtime/trunk/src/main/native/shared/fsysio.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileOpenMode.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileOpenMode.java?rev=820273&r1=820272&r2=820273&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileOpenMode.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileOpenMode.java Wed Sep 30 14:06:58 2009
@@ -41,8 +41,8 @@
BINARY( 0x00020),
/** Open should fail if {@code CREATE} and file exists. */
EXCL( 0x00040),
- /** Open the file for buffered I/O. */
- BUFFERED( 0x00080),
+ /** Open the file for non blocking I/O. */
+ NONBLOCK( 0x00080),
/** Delete the file after close. */
DELONCLOSE( 0x00100),
/** Platform dependent support for higher level locked read/write
Copied: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java (from r819962, 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/FileSystem.java?p2=commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystem.java&p1=commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSystemProvider.java&r1=819962&r2=820273&rev=820273&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/FileSystem.java Wed Sep 30 14:06:58 2009
@@ -22,20 +22,31 @@
import java.util.EnumSet;
/**
- * Internal File system provider class.
+ * File system provider class.
* <p>
- * Apache Commons Runtime {@code FileSystemProvider} provides static
+ * Apache Commons Runtime {@code FileSystem} provides static
* method API for native file portable layer.
* </p>
*/
-class FileSystemProvider
+public final class FileSystem
{
- protected FileSystemProvider()
+ private static native void err0(Descriptor fd);
+
+ protected FileSystem()
{
// Nothing.
}
+ private static native boolean eof0(int fd);
+ public static boolean eof(Descriptor fd)
+ throws IOException
+ {
+ if (!fd.valid())
+ throw new ClosedDescriptorException();
+ return eof0(fd.fd());
+ }
+
private static native Descriptor open0(String path, int mode)
throws IOException;
/**
@@ -80,6 +91,7 @@
return open1(path.getPath(), imode, iprot);
}
+
private static native int read0(int fd);
/**
* Read one byte from the Descriptor.
@@ -96,7 +108,35 @@
{
if (!fd.valid())
throw new ClosedDescriptorException();
- return read0(fd.fd());
+ int rd = read0(fd.fd());
+ if (rd < 1)
+ err0(fd);
+ return rd;
+ }
+
+ private static native int read1(int fd, byte [] b, int off, int len);
+ /**
+ * Reads up to {@code len} bytes of data from this file into an
+ * array of bytes. If {@code len} is not zero, the method blocks
+ * until some input is available; otherwise, no bytes are read and
+ * {@code 0} is returned.
+ * <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, byte [] b, int off, int len)
+ throws IOException
+ {
+ if (!fd.valid())
+ throw new ClosedDescriptorException();
+ int rd = read1(fd.fd(), b, off, len);
+ if (rd < len)
+ err0(fd);
+ return rd;
}
private static native int write0(int fd, int val);
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h?rev=820273&r1=820272&r2=820273&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h Wed Sep 30 14:06:58 2009
@@ -1365,6 +1365,7 @@
|| (s) == ERROR_MAX_THRDS_REACHED \
|| (s) == ERROR_LOCK_VIOLATION \
|| (s) == ERROR_RETRY \
+ || (s) == ERROR_IO_PENDING \
|| (s) == WSAEWOULDBLOCK)
#define ACR_STATUS_IS_EINTR(s) ((s) == ACR_EINTR \
|| (s) == WSAEINTR)
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=820273&r1=820272&r2=820273&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 Wed Sep 30 14:06:58 2009
@@ -193,9 +193,9 @@
/* The rest of the structure is platform specific.
*/
#if defined(WIN32)
-
+ LARGE_INTEGER pos;
#else
-
+ acr_off_t pos;
#endif
} acr_file_t;
Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=820273&r1=820272&r2=820273&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h Wed Sep 30 14:06:58 2009
@@ -107,6 +107,16 @@
#define ACR_WANT_FNMATCH 1
+#define ACR_OVERLAPPED_ADD(O, V) \
+ if ((O)) { \
+ LARGE_INTEGER _li; \
+ _li.LowPart = (O)->Offset; \
+ _li.HighPart = (O)->OffsetHigh; \
+ _li.QuadPart = _li.QuadPart + (V); \
+ (O)->Offset = _li.LowPart; \
+ (O)->OffsetHigh = _li.HighPart; \
+ } else (void)0
+
/**
* Local functions from wusec.c
*/
@@ -158,6 +168,8 @@
#define ACR_SIG_WAITER_DONE() InterlockedDecrement(¤t_signal_listeners)
#define ACR_SIGNAL_NWAITERS() InterlockedCompareExchange(¤t_signal_listeners, 0, 0)
+#define WAIT_ABANDONED_1 (WAIT_ABANDONED_0 + 1)
+#define WAIT_OBJECT_1 (WAIT_OBJECT_0 + 1)
static ACR_INLINE DWORD ACR_WaitForObjectOrSignal(HANDLE handle, DWORD dwTimeout)
{
DWORD rc;
@@ -168,7 +180,6 @@
ACR_SIG_WAITER_INIT();
rc = WaitForMultipleObjectsEx(2, wh, FALSE, dwTimeout, TRUE);
ACR_SIG_WAITER_DONE();
-
return rc;
}
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=820273&r1=820272&r2=820273&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 Wed Sep 30 14:06:58 2009
@@ -207,9 +207,9 @@
return rc;
}
-ACR_IO_EXPORT_DECLARE(jobject, FileSystemProvider, open0)(ACR_JNISTDARGS,
- jstring fname,
- jint flags)
+ACR_IO_EXPORT_DECLARE(jobject, FileSystem, open0)(ACR_JNISTDARGS,
+ jstring fname,
+ jint flags)
{
int rc = 0;
jobject fdo = NULL;
@@ -226,10 +226,10 @@
return fdo;
}
-ACR_IO_EXPORT_DECLARE(jobject, FileSystemProvider, open1)(ACR_JNISTDARGS,
- jstring fname,
- jint flags,
- jint prot)
+ACR_IO_EXPORT_DECLARE(jobject, FileSystem, open1)(ACR_JNISTDARGS,
+ jstring fname,
+ jint flags,
+ jint prot)
{
int rc = 0;
jobject fdo = NULL;
@@ -263,9 +263,9 @@
return errno;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, lock0)(ACR_JNISTDARGS,
- jint file,
- jint type)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, lock0)(ACR_JNISTDARGS,
+ jint file,
+ jint type)
{
int rc;
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -328,11 +328,11 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, lock1)(ACR_JNISTDARGS,
- jint file,
- jint type,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, lock1)(ACR_JNISTDARGS,
+ jint file,
+ jint type,
+ jlong off,
+ jlong len)
{
int rc;
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -395,8 +395,8 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, unlock0)(ACR_JNISTDARGS,
- jint file)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, unlock0)(ACR_JNISTDARGS,
+ jint file)
{
int rc;
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -438,10 +438,10 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, unlock1)(ACR_JNISTDARGS,
- jint file,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, unlock1)(ACR_JNISTDARGS,
+ jint file,
+ jlong off,
+ jlong len)
{
int rc;
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -483,10 +483,10 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jlong, FileSystemProvider, seek0)(ACR_JNISTDARGS,
- jint file,
- jint where,
- jlong off)
+ACR_IO_EXPORT_DECLARE(jlong, FileSystem, seek0)(ACR_JNISTDARGS,
+ jint file,
+ jint where,
+ jlong off)
{
acr_off_t rc;
acr_off_t os = (acr_off_t)off;
@@ -523,9 +523,9 @@
return (jlong)rc;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, trunc0)(ACR_JNISTDARGS,
- jint file,
- jlong off)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, trunc0)(ACR_JNISTDARGS,
+ jint file,
+ jlong off)
{
acr_off_t os = (acr_off_t)off;
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -542,8 +542,8 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, read0)(ACR_JNISTDARGS,
- jint file)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, read0)(ACR_JNISTDARGS,
+ jint file)
{
unsigned char c;
ssize_t rd;
@@ -574,8 +574,11 @@
}
if (rd == -1) {
f->err = ACR_GET_OS_ERROR();
- if (ACR_STATUS_IS_EAGAIN(f->err))
- return 0;
+ if (!ACR_STATUS_IS_EAGAIN(f->err)) {
+ /* Throw only if not EAGAIN
+ */
+ ACR_THROW_IO_IF_ERR(f->err);
+ }
}
else if (rd == 0) {
f->err = 0;
@@ -588,11 +591,11 @@
return -1;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, read1)(ACR_JNISTDARGS,
- jint file,
- jbyteArray buf,
- jint off,
- jint len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, read1)(ACR_JNISTDARGS,
+ jint file,
+ jbyteArray buf,
+ jint off,
+ jint len)
{
jbyte *bb = NULL;
jbyte *bc = NULL;
@@ -674,11 +677,11 @@
return (jint)rd;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, read2)(ACR_JNISTDARGS,
- jint file,
- jobject ptr,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, read2)(ACR_JNISTDARGS,
+ jint file,
+ jobject ptr,
+ jlong off,
+ jlong len)
{
size_t pl;
size_t po = (size_t)off;
@@ -734,11 +737,11 @@
return (jint)rd;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, read3)(ACR_JNISTDARGS,
- jint file,
- jobject dbb,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, read3)(ACR_JNISTDARGS,
+ jint file,
+ jobject dbb,
+ jlong off,
+ jlong len)
{
#if defined(_DEBUG)
size_t pl;
@@ -799,9 +802,9 @@
return (jint)rd;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, write0)(ACR_JNISTDARGS,
- jint file,
- jint b)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, write0)(ACR_JNISTDARGS,
+ jint file,
+ jint b)
{
unsigned char c = (unsigned char)(b & 0xFF);
ssize_t wr;
@@ -837,11 +840,11 @@
return wr;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, write1)(ACR_JNISTDARGS,
- jint file,
- jbyteArray buf,
- jint off,
- jint len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, write1)(ACR_JNISTDARGS,
+ jint file,
+ jbyteArray buf,
+ jint off,
+ jint len)
{
jbyte *bb;
size_t po = (size_t)off;
@@ -885,11 +888,11 @@
return (jint)wr;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, write2)(ACR_JNISTDARGS,
- jint file,
- jobject ptr,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, write2)(ACR_JNISTDARGS,
+ jint file,
+ jobject ptr,
+ jlong off,
+ jlong len)
{
size_t pl;
size_t po = (size_t)off;
@@ -937,11 +940,11 @@
return (jint)wr;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, write3)(ACR_JNISTDARGS,
- jint file,
- jobject dbb,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, write3)(ACR_JNISTDARGS,
+ jint file,
+ jobject dbb,
+ jlong off,
+ jlong len)
{
#if defined(_DEBUG)
size_t pl;
@@ -995,11 +998,11 @@
}
#define ACR_IOVEC_ON_STACK 32
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, write4)(ACR_JNISTDARGS,
- jint file,
- jobjectArray vec,
- jint off,
- jint len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, write4)(ACR_JNISTDARGS,
+ jint file,
+ jobjectArray vec,
+ jint off,
+ jint len)
{
size_t i;
size_t pl;
@@ -1087,11 +1090,11 @@
return (jint)wr;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, write5)(ACR_JNISTDARGS,
- jint file,
- jobjectArray vec,
- jint off,
- jint len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, write5)(ACR_JNISTDARGS,
+ jint file,
+ jobjectArray vec,
+ jint off,
+ jint len)
{
size_t i;
size_t pl;
@@ -1158,11 +1161,11 @@
return (jint)wr;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, fullw0)(ACR_JNISTDARGS,
- jint file,
- jbyteArray buf,
- jint off,
- jint len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, fullw0)(ACR_JNISTDARGS,
+ jint file,
+ jbyteArray buf,
+ jint off,
+ jint len)
{
jbyte *bb;
jbyte *wb;
@@ -1213,11 +1216,11 @@
return (jint)wt;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, fullw1)(ACR_JNISTDARGS,
- jint file,
- jobject ptr,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, fullw1)(ACR_JNISTDARGS,
+ jint file,
+ jobject ptr,
+ jlong off,
+ jlong len)
{
size_t pl;
size_t po = (size_t)off;
@@ -1271,11 +1274,11 @@
return (jint)wr;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, fullw2)(ACR_JNISTDARGS,
- jint file,
- jobject dbb,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, fullw2)(ACR_JNISTDARGS,
+ jint file,
+ jobject dbb,
+ jlong off,
+ jlong len)
{
#if defined(_DEBUG)
size_t pl;
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=820273&r1=820272&r2=820273&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 Wed Sep 30 14:06:58 2009
@@ -16,8 +16,6 @@
#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"
@@ -26,6 +24,10 @@
#include "acr_file.h"
#include "acr_fileio.h"
+#define ACR_WANT_LATE_DLL
+#include "acr_arch.h"
+#include "acr_port.h"
+
static int file_cleanup(void *file, int type, unsigned int flags)
{
int rc = ACR_EBADF;
@@ -88,7 +90,7 @@
DWORD aflags = 0;
DWORD oflags = 0;
DWORD cflags = 0;
- DWORD sflags = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
+ DWORD sflags = FILE_SHARE_READ | FILE_SHARE_WRITE;
SECURITY_ATTRIBUTES sa;
acr_file_t *fp = NULL;
@@ -105,7 +107,6 @@
return ACR_EINVAL;
}
if (flags & ACR_FOPEN_CREATE) {
- oflags |= O_CREAT;
if (flags & ACR_FOPEN_EXCL)
cflags = CREATE_NEW;
else if (flags & ACR_FOPEN_TRUNCATE)
@@ -113,6 +114,10 @@
else
cflags = OPEN_ALWAYS;
}
+ else if (flags & ACR_FOPEN_TRUNCATE)
+ cflags = TRUNCATE_EXISTING;
+ else
+ cflags = OPEN_EXISTING;
if ((flags & ACR_FOPEN_EXCL) && !(flags & ACR_FOPEN_CREATE)) {
return ACR_EINVAL;
}
@@ -124,6 +129,12 @@
}
if (flags & ACR_FOPEN_NONBLOCK)
aflags |= FILE_FLAG_OVERLAPPED;
+ if (!(flags & ACR_FOPEN_EXCL)) {
+ /* Don't allow delete access if file was opened
+ * excluseively
+ */
+ sflags |= FILE_SHARE_DELETE;
+ }
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
if (prot == ACR_FPROT_OS_DEFAULT) {
@@ -195,9 +206,9 @@
return rc;
}
-ACR_IO_EXPORT_DECLARE(jobject, FileSystemProvider, open0)(ACR_JNISTDARGS,
- jstring fname,
- jint flags)
+ACR_IO_EXPORT_DECLARE(jobject, FileSystem, open0)(ACR_JNISTDARGS,
+ jstring fname,
+ jint flags)
{
int rc = 0;
jobject fdo = NULL;
@@ -214,10 +225,10 @@
return fdo;
}
-ACR_IO_EXPORT_DECLARE(jobject, FileSystemProvider, open1)(ACR_JNISTDARGS,
- jstring fname,
- jint flags,
- jint prot)
+ACR_IO_EXPORT_DECLARE(jobject, FileSystem, open1)(ACR_JNISTDARGS,
+ jstring fname,
+ jint flags,
+ jint prot)
{
int rc = 0;
jobject fdo = NULL;
@@ -234,9 +245,9 @@
return fdo;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, lock0)(ACR_JNISTDARGS,
- jint file,
- jint type)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, lock0)(ACR_JNISTDARGS,
+ jint file,
+ jint type)
{
OVERLAPPED opp;
DWORD flags = 0;
@@ -268,13 +279,15 @@
rc = 0;
switch (ws) {
case WAIT_IO_COMPLETION:
- case WAIT_OBJECT_0 + 0:
+ case WAIT_ABANDONED_0:
+ case WAIT_ABANDONED_1:
+ case WAIT_OBJECT_0:
/* Signal event is set.
* Get it's status.
*/
rc = ACR_DeliverSignals();
break;
- case WAIT_OBJECT_0 + 1:
+ case WAIT_OBJECT_1:
/* We got the lock */
return 0;
break;
@@ -295,11 +308,11 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, lock1)(ACR_JNISTDARGS,
- jint file,
- jint type,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, lock1)(ACR_JNISTDARGS,
+ jint file,
+ jint type,
+ jlong off,
+ jlong len)
{
LARGE_INTEGER lii;
OVERLAPPED opp;
@@ -334,13 +347,15 @@
rc = 0;
switch (ws) {
case WAIT_IO_COMPLETION:
- case WAIT_OBJECT_0 + 0:
+ case WAIT_ABANDONED_0:
+ case WAIT_ABANDONED_1:
+ case WAIT_OBJECT_0:
/* Signal event is set.
* Get it's status.
*/
rc = ACR_DeliverSignals();
break;
- case WAIT_OBJECT_0 + 1:
+ case WAIT_OBJECT_1:
/* We got the lock */
return 0;
break;
@@ -361,8 +376,8 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, unlock0)(ACR_JNISTDARGS,
- jint file)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, unlock0)(ACR_JNISTDARGS,
+ jint file)
{
OVERLAPPED opp;
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -379,10 +394,10 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, unlock1)(ACR_JNISTDARGS,
- jint file,
- jlong off,
- jlong len)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, unlock1)(ACR_JNISTDARGS,
+ jint file,
+ jlong off,
+ jlong len)
{
LARGE_INTEGER lii;
OVERLAPPED opp;
@@ -403,10 +418,10 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jlong, FileSystemProvider, seek0)(ACR_JNISTDARGS,
- jint file,
- jint where,
- jlong off)
+ACR_IO_EXPORT_DECLARE(jlong, FileSystem, seek0)(ACR_JNISTDARGS,
+ jint file,
+ jint where,
+ jlong off)
{
LARGE_INTEGER os;
LARGE_INTEGER op;
@@ -445,9 +460,9 @@
return (jlong)op.QuadPart;
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, trunc0)(ACR_JNISTDARGS,
- jint file,
- jlong off)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, trunc0)(ACR_JNISTDARGS,
+ jint file,
+ jlong off)
{
LARGE_INTEGER os;
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -465,3 +480,363 @@
else
return 0;
}
+
+static DWORD overlapped_wait(acr_file_t *f, DWORD *nbytes)
+{
+ DWORD ws;
+ DWORD rc = 0;
+ DWORD tv = 0;
+
+ if (f->timeout > 0)
+ tv = (DWORD)(f->timeout / 1000);
+ else if (f->timeout == -1)
+ tv = INFINITE;
+
+ do {
+ switch (ws = ACR_WaitForObjectOrSignal(f->overlap.hEvent, tv)) {
+ case WAIT_IO_COMPLETION:
+ case WAIT_ABANDONED_0:
+ case WAIT_ABANDONED_1:
+ case WAIT_OBJECT_0:
+ /* Signal event is set.
+ * Get it's status.
+ */
+ rc = ACR_DeliverSignals();
+ break;
+ case WAIT_OBJECT_1:
+ /* Operation success */
+ rc = 0;
+ break;
+ case WAIT_FAILED:
+ /* We got the error while waiting
+ */
+ rc = ACR_GET_OS_ERROR();
+ break;
+ case WAIT_TIMEOUT:
+ rc = ACR_TIMEUP;
+ break;
+ default:
+ rc = ACR_EINVAL;
+ break;
+ }
+ } while (rc == ACR_EINTR);
+
+ if (rc) {
+ /* There is one case that represents entirely
+ * successful operations, otherwise we will cancel
+ * the operation in progress.
+ */
+ CancelIo(f->fd);
+ }
+ if (!GetOverlappedResult(f->fd,
+ &f->overlap,
+ nbytes,
+ FALSE)) {
+ rc = GetLastError();
+ if (rc == ERROR_IO_INCOMPLETE ||
+ rc == ERROR_OPERATION_ABORTED)
+ rc = ACR_TIMEUP;
+ }
+ return rc;
+}
+
+static DWORD overlapped_wait_all(acr_file_t *f, DWORD *nbytes)
+{
+ DWORD ws;
+ DWORD rc = 0;
+ DWORD tv = INFINITE;
+
+ do {
+ switch (ws = ACR_WaitForObjectOrSignal(f->overlap.hEvent, tv)) {
+ case WAIT_IO_COMPLETION:
+ case WAIT_ABANDONED_0:
+ case WAIT_ABANDONED_1:
+ case WAIT_OBJECT_0:
+ /* Signal event is set.
+ * Get it's status.
+ */
+ rc = ACR_DeliverSignals();
+ break;
+ case WAIT_OBJECT_1:
+ /* Operation success */
+ rc = 0;
+ break;
+ case WAIT_FAILED:
+ /* We got the error while waiting
+ */
+ rc = ACR_GET_OS_ERROR();
+ break;
+ default:
+ rc = ACR_EINVAL;
+ break;
+ }
+ } while (rc == ACR_EINTR);
+
+ if (rc) {
+ /* There is one case that represents entirely
+ * successful operations, otherwise we will cancel
+ * the operation in progress.
+ */
+ CancelIo(f->fd);
+ }
+ if (!GetOverlappedResult(f->fd,
+ &f->overlap,
+ nbytes,
+ FALSE)) {
+ rc = GetLastError();
+ if (rc == ERROR_IO_INCOMPLETE ||
+ rc == ERROR_OPERATION_ABORTED)
+ rc = ACR_TIMEUP;
+ }
+ return rc;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, read0)(ACR_JNISTDARGS,
+ jint file)
+{
+ unsigned char c;
+ DWORD rd = 0;
+ DWORD rc;
+ LPOVERLAPPED lpo = NULL;
+ acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+ if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+ ACR_THROW_IO_IF_ERR(ACR_EFTYPE);
+ return -1;
+ }
+ if (IS_INVALID_HANDLE(f)) {
+ ACR_THROW_IO_IF_ERR(ACR_EBADF);
+ return -1;
+ }
+ if (f->eof) {
+ return -1;
+ }
+
+ if (f->blocking == BLK_OFF) {
+ if (f->timeout == 0 && f->type == ACR_DT_PIPE) {
+ if (!PeekNamedPipe(f->fd, NULL, 0, NULL, &rd, NULL)) {
+ rc = GetLastError();
+ if (rc == ERROR_BROKEN_PIPE) {
+ f->eof = 1;
+ return -1;
+ }
+ else {
+ ACR_THROW_IO_IF_ERR(rc);
+ return -1;
+ }
+ }
+ else {
+ if (rd == 0) {
+ f->err= ERROR_IO_PENDING;
+ return -1;
+ }
+ }
+ }
+ lpo = &f->overlap;
+ if (IS_INVALID_HANDLE(lpo->hEvent))
+ lpo->hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
+ if (IS_INVALID_HANDLE(lpo->hEvent))
+ return ACR_GET_OS_ERROR();
+ lpo->Offset = f->pos.LowPart;
+ lpo->OffsetHigh = f->pos.HighPart;
+ }
+ f->err = 0;
+ if (ReadFile(f->fd, &c, 1, &rd, lpo)) {
+ /* All done. Update the position and return
+ */
+ if (rd) {
+ f->pos.QuadPart += rd;
+ return c;
+ }
+ else {
+ f->eof = 1;
+ return -1;
+ }
+ }
+ switch (f->err = GetLastError()) {
+ case ERROR_HANDLE_EOF:
+ case ERROR_BROKEN_PIPE:
+ f->err = 0;
+ f->eof = 1;
+ break;
+ case ERROR_IO_PENDING:
+ switch (rc = overlapped_wait(f, &rd)) {
+ case 0:
+ f->err = 0;
+ if (rd) {
+ f->pos.QuadPart += rd;
+ return c;
+ }
+ else
+ f->eof = 1;
+ break;
+ case ERROR_HANDLE_EOF:
+ case ERROR_BROKEN_PIPE:
+ f->err = 0;
+ f->eof = 1;
+ break;
+ case ACR_TIMEUP:
+ f->err = ACR_TIMEUP;
+ break;
+ default:
+ f->err = rc;
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+ if (f->err && f->err != ACR_TIMEUP) {
+ /* Throw only if not TIMEUP
+ */
+ ACR_THROW_IO_IF_ERR(f->err);
+ }
+ return -1;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, read1)(ACR_JNISTDARGS,
+ jint file,
+ jbyteArray buf,
+ jint off,
+ jint len)
+{
+ jbyte *bb = NULL;
+ jbyte *bc = NULL;
+ DWORD po = (DWORD)off;
+ DWORD cs = (DWORD)len;
+ DWORD rd = 0;
+ DWORD rc;
+ LPOVERLAPPED lpo = NULL;
+ jbyte onstack[ACR_PBUFF_SIZ];
+ acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+ if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+ ACR_THROW_IO_IF_ERR(ACR_EFTYPE);
+ return -1;
+ }
+ if (IS_INVALID_HANDLE(f)) {
+ ACR_THROW_IO_IF_ERR(ACR_EBADF);
+ return -1;
+ }
+ if (f->eof) {
+ return -1;
+ }
+ if (f->blocking == BLK_OFF) {
+ if (f->timeout == 0 && f->type == ACR_DT_PIPE) {
+ if (!PeekNamedPipe(f->fd, NULL, 0, NULL, &rd, NULL)) {
+ rc = GetLastError();
+ if (rc == ERROR_BROKEN_PIPE) {
+ f->eof = 1;
+ return -1;
+ }
+ else {
+ ACR_THROW_IO_IF_ERR(rc);
+ return -1;
+ }
+ }
+ else {
+ if (rd == 0) {
+ f->err = ACR_TIMEUP;
+ return 0;
+ }
+ else {
+ /* Update read size that can be done
+ * without blocking
+ */
+ if (cs > rd)
+ cs = rd;
+ }
+ }
+ }
+ lpo = &f->overlap;
+ if (IS_INVALID_HANDLE(lpo->hEvent))
+ lpo->hEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
+ if (IS_INVALID_HANDLE(lpo->hEvent)) {
+ ACR_THROW_IO_ERRNO();
+ return -1;
+ }
+ lpo->Offset = f->pos.LowPart;
+ lpo->OffsetHigh = f->pos.HighPart;
+ }
+ if (cs > (DWORD)sizeof(onstack)) {
+ if (cs > (1024 * 1024)) {
+ bc = (*_E)->GetByteArrayElements(_E, buf, NULL);
+ if (bc)
+ bb = bc + po;
+ }
+ else
+ bb = ACR_Malloc(_E, THROW_FMARK, cs);
+ }
+ else
+ bb = onstack;
+ if (!bb) {
+ /* Exception was already thrown */
+ return -1;
+ }
+ f->err = 0;
+ if (ReadFile(f->fd, bb, cs, &rd, lpo)) {
+ /* All done. Update the position and return
+ */
+ if (rd)
+ f->pos.QuadPart += rd;
+ else
+ f->eof = 1;
+ goto finally;
+ }
+ switch (f->err = GetLastError()) {
+ case ERROR_HANDLE_EOF:
+ case ERROR_BROKEN_PIPE:
+ f->err = 0;
+ f->eof = 1;
+ break;
+ case ERROR_IO_PENDING:
+ switch (rc = overlapped_wait(f, &rd)) {
+ case 0:
+ f->err = 0;
+ if (rd) {
+ f->pos.QuadPart += rd;
+ goto finally;
+ }
+ else
+ f->eof = 1;
+ break;
+ case ERROR_HANDLE_EOF:
+ case ERROR_BROKEN_PIPE:
+ f->err = 0;
+ f->eof = 1;
+ break;
+ case ACR_TIMEUP:
+ f->err = ACR_TIMEUP;
+ break;
+ default:
+ f->err = rc;
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+
+finally:
+ if (rd) {
+ if (bc) {
+ (*_E)->ReleaseByteArrayElements(_E, buf, bc, 0);
+ return (jint)rd;
+ }
+ else {
+ (*_E)->SetByteArrayRegion(_E, buf, (jsize)po, (jsize)rd, bb);
+ }
+ }
+ if (bb != onstack) {
+ if (bc)
+ (*_E)->ReleaseByteArrayElements(_E, buf, bc, JNI_ABORT);
+ else
+ x_free(bb);
+ }
+ if (f->eof)
+ return -1;
+ if (f->err) {
+ return f->err == ACR_TIMEUP ? 0 : -1;
+ }
+ return (jint)rd;
+}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/mutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/mutex.c?rev=820273&r1=820272&r2=820273&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/mutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/mutex.c Wed Sep 30 14:06:58 2009
@@ -133,14 +133,14 @@
ws = ACR_WaitForObjectOrSignal(m, INFINITE);
switch (ws) {
case WAIT_IO_COMPLETION:
- case WAIT_OBJECT_0 + 0:
+ case WAIT_OBJECT_0:
/* Signal event is set.
* Get it's satus.
*/
rc = ACR_DeliverSignals();
break;
- case WAIT_ABANDONED_0 + 1:
- case WAIT_OBJECT_0 + 1:
+ case WAIT_ABANDONED_1:
+ case WAIT_OBJECT_1:
/* We got the lock */
return 0;
break;
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/sema.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/sema.c?rev=820273&r1=820272&r2=820273&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/sema.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/sema.c Wed Sep 30 14:06:58 2009
@@ -170,13 +170,14 @@
ws = ACR_WaitForObjectOrSignal(s, INFINITE);
switch (ws) {
case WAIT_IO_COMPLETION:
- case WAIT_OBJECT_0 + 0:
+ case WAIT_OBJECT_0:
/* Signal event is set.
* Get it's satus.
*/
rc = ACR_DeliverSignals();
break;
- case WAIT_OBJECT_0 + 1:
+ case WAIT_ABANDONED_1:
+ case WAIT_OBJECT_1:
/* We got the lock */
return 0;
break;
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/fsysio.c?rev=820273&r1=820272&r2=820273&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/fsysio.c Wed Sep 30 14:06:58 2009
@@ -24,20 +24,31 @@
#include "acr_fileio.h"
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, err0)(ACR_JNISTDARGS,
- jint file)
+ACR_IO_EXPORT_DECLARE(void, FileSystem, err0)(ACR_JNISTDARGS,
+ jobject file)
{
- acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+ int fd;
+ int rc = 0;
- if (ACR_IOH_FTYPE(file) != ACR_DT_FILE)
- return ACR_EFTYPE;
- if (IS_INVALID_HANDLE(f))
- return ACR_EBADF;
- return f->err;
+ fd = ACR_DescriptorGetInt(_E, file);
+ if (fd < 0)
+ rc = ACR_EBADF;
+ else {
+ if (ACR_IOH_FTYPE(fd) != ACR_DT_FILE)
+ rc = ACR_EFTYPE;
+ else {
+ acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(fd);
+ if (IS_INVALID_HANDLE(f))
+ rc = ACR_EBADF;
+ else
+ rc = f->err;
+ }
+ }
+ ACR_DescriptorSetErr(_E, file, rc);
}
-ACR_IO_EXPORT_DECLARE(jboolean, FileSystemProvider, eof0)(ACR_JNISTDARGS,
- jint file)
+ACR_IO_EXPORT_DECLARE(jboolean, FileSystem, eof0)(ACR_JNISTDARGS,
+ jint file)
{
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -52,9 +63,9 @@
return V2Z(f->eof);
}
-ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, tmset0)(ACR_JNISTDARGS,
- jint file,
- jlong timeout)
+ACR_IO_EXPORT_DECLARE(jint, FileSystem, tmset0)(ACR_JNISTDARGS,
+ jint file,
+ jlong timeout)
{
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
@@ -71,8 +82,8 @@
return 0;
}
-ACR_IO_EXPORT_DECLARE(jlong, FileSystemProvider, tmget0)(ACR_JNISTDARGS,
- jint file)
+ACR_IO_EXPORT_DECLARE(jlong, FileSystem, tmget0)(ACR_JNISTDARGS,
+ jint file)
{
acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
Modified: 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=820273&r1=820272&r2=820273&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java Wed Sep 30 14:06:58 2009
@@ -18,8 +18,10 @@
import org.apache.commons.runtime.exception.*;
import org.apache.commons.runtime.io.*;
+import org.apache.commons.runtime.util.*;
import junit.framework.*;
import java.io.IOException;
+import java.io.FileOutputStream;
import java.util.EnumSet;
/**
@@ -44,7 +46,7 @@
throws Exception
{
File file = new File("ftest1.txt");
- Descriptor fd = FileSystem.getInstance().open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE));
+ Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE));
assertFalse("Descriptor", fd == null);
System.out.println();
@@ -56,7 +58,7 @@
throws Exception
{
File file = new File("ftest1.txt");
- Descriptor fd = FileSystem.getInstance().open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
+ Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
assertFalse("Descriptor", fd != null);
}
@@ -66,9 +68,48 @@
{
File file = new File("ftest1.txt");
file.delete();
- Descriptor fd = FileSystem.getInstance().open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
+ Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.RDWR, FileOpenMode.CREATE, FileOpenMode.EXCL));
assertFalse("Descriptor", fd == null);
+ fd.close();
+ file.delete();
+ }
+ static private void makeRandomFile(String name, int len)
+ throws IOException
+ {
+
+ File file = new File(name);
+ // Delete any previous instance
+ file.delete();
+ file.createNewFile();
+ FileOutputStream os = new FileOutputStream(file);
+
+ byte [] rnd = new byte[1024];
+ while (len > 0) {
+ Utils.generateRandom(rnd);
+ int siz = rnd.length;
+ if (siz > len)
+ siz = len;
+ os.write(rnd, 0, siz);
+ len -= siz;
+ }
+ os.close();
+ }
+
+ public void testFileSysReadA()
+ throws Exception
+ {
+ File file = new File("ftest1.rnd");
+ makeRandomFile(file.getPath(), 1024 * 128);
+
+ Descriptor fd = FileSystem.open(file, EnumSet.of(FileOpenMode.READ, FileOpenMode.NONBLOCK));
+ assertFalse("Descriptor", fd == null);
+ byte [] buf = new byte[1024 * 128];
+
+ int rd = FileSystem.read(fd, buf, 0, buf.length);
+ System.out.println();
+ System.out.println("Readed " + rd + " bytes from " + file.getPath());
+ fd.close();
}
}