commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r819991 - /commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c
Date Tue, 29 Sep 2009 15:43:34 GMT
Author: mturk
Date: Tue Sep 29 15:43:33 2009
New Revision: 819991

URL: http://svn.apache.org/viewvc?rev=819991&view=rev
Log:
Implement basic win32 file sys methods

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c

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=819991&r1=819990&r2=819991&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 Tue Sep 29 15:43:33 2009
@@ -233,3 +233,235 @@
     }
     return fdo;
 }
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, lock0)(ACR_JNISTDARGS,
+                                                        jint file,
+                                                        jint type)
+{
+    OVERLAPPED  opp;
+    DWORD flags = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE)
+        return ACR_EFTYPE;
+    if (IS_INVALID_HANDLE(f))
+        return ACR_EBADF;
+
+    if (type & ACR_FLOCK_NONBLOCK)
+        flags = LOCKFILE_FAIL_IMMEDIATELY;
+    else if (f->blocking == BLK_OFF) {
+        if (IS_INVALID_HANDLE(f->overlap.hEvent)) {
+            f->overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
+            if (IS_INVALID_HANDLE(f->overlap.hEvent))
+                return ACR_GET_OS_ERROR();
+        }
+    }
+    if ((type & ACR_FLOCK_TYPEMASK) != ACR_FLOCK_SHARED)
+        flags |= LOCKFILE_EXCLUSIVE_LOCK;
+    f->overlap.Offset     = 0;
+    f->overlap.OffsetHigh = 0;
+    if (!LockFileEx(f->fd, flags, 0, 0xFFFFFFFF, 0xFFFFFFFF, &opp)) {
+        int rc = ACR_GET_OS_ERROR();
+        if (rc == ERROR_IO_PENDING) {
+            do {
+                DWORD ws = ACR_WaitForObjectOrSignal(f->overlap.hEvent, INFINITE);
+                rc = 0;
+                switch (ws) {
+                    case WAIT_IO_COMPLETION:
+                    case WAIT_OBJECT_0 + 0:
+                        /* Signal event is set.
+                         * Get it's status.
+                         */
+                        rc = ACR_DeliverSignals();
+                    break;
+                    case WAIT_OBJECT_0 + 1:
+                        /* We got the lock */
+                        return 0;
+                    break;
+                    case WAIT_FAILED:
+                        /* We got the error while waiting
+                         */
+                        rc = ACR_GET_OS_ERROR();
+                    break;
+                    default:
+                        rc = ACR_ENOLOCK;
+                    break;
+                }
+            } while (rc == ACR_EINTR);
+        }
+        return rc;
+    }
+    else
+        return 0;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, lock1)(ACR_JNISTDARGS,
+                                                       jint file,
+                                                       jint type,
+                                                       jlong off,
+                                                       jlong len)
+{
+    LARGE_INTEGER lii;
+    OVERLAPPED    opp;
+    DWORD flags = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE)
+        return ACR_EFTYPE;
+    if (IS_INVALID_HANDLE(f))
+        return ACR_EBADF;
+
+    if (type & ACR_FLOCK_NONBLOCK)
+        flags = LOCKFILE_FAIL_IMMEDIATELY;
+    else if (f->blocking == BLK_OFF) {
+        if (IS_INVALID_HANDLE(f->overlap.hEvent)) {
+            f->overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
+            if (IS_INVALID_HANDLE(f->overlap.hEvent))
+                return ACR_GET_OS_ERROR();
+        }
+    }
+    if ((type & ACR_FLOCK_TYPEMASK) != ACR_FLOCK_SHARED)
+        flags |= LOCKFILE_EXCLUSIVE_LOCK;
+
+    f->overlap.Offset     = (DWORD)(off);
+    f->overlap.OffsetHigh = (DWORD)(off >> 32);
+    lii.QuadPart = len;
+    if (!LockFileEx(f->fd, flags, 0, lii.LowPart, lii.HighPart, &opp)) {
+        int rc = ACR_GET_OS_ERROR();
+        if (rc == ERROR_IO_PENDING) {
+            do {
+                DWORD ws = ACR_WaitForObjectOrSignal(f->overlap.hEvent, INFINITE);
+                rc = 0;
+                switch (ws) {
+                    case WAIT_IO_COMPLETION:
+                    case WAIT_OBJECT_0 + 0:
+                        /* Signal event is set.
+                         * Get it's status.
+                         */
+                        rc = ACR_DeliverSignals();
+                    break;
+                    case WAIT_OBJECT_0 + 1:
+                        /* We got the lock */
+                        return 0;
+                    break;
+                    case WAIT_FAILED:
+                        /* We got the error while waiting
+                         */
+                        rc = ACR_GET_OS_ERROR();
+                    break;
+                    default:
+                        rc = ACR_ENOLOCK;
+                    break;
+                }
+            } while (rc == ACR_EINTR);
+        }
+        return rc;
+    }
+    else
+        return 0;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, unlock0)(ACR_JNISTDARGS,
+                                                         jint file)
+{
+    OVERLAPPED  opp;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE)
+        return ACR_EFTYPE;
+    if (IS_INVALID_HANDLE(f))
+        return ACR_EBADF;
+
+    memset(&opp, 0, sizeof(OVERLAPPED));
+    if (!UnlockFileEx(f->fd, 0, 0xFFFFFFFF, 0xFFFFFFFF, &opp))
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, unlock1)(ACR_JNISTDARGS,
+                                                         jint file,
+                                                         jlong off,
+                                                         jlong len)
+{
+    LARGE_INTEGER lii;
+    OVERLAPPED    opp;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE)
+        return ACR_EFTYPE;
+    if (IS_INVALID_HANDLE(f))
+        return ACR_EBADF;
+
+    memset(&opp, 0, sizeof(OVERLAPPED));
+    opp.Offset     = (DWORD)(off);
+    opp.OffsetHigh = (DWORD)(off >> 32);
+    lii.QuadPart   = len;
+    if (!UnlockFileEx(f->fd, 0, lii.LowPart, lii.HighPart, &opp))
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_IO_EXPORT_DECLARE(jlong, FileSystemProvider, seek0)(ACR_JNISTDARGS,
+                                                        jint file,
+                                                        jint where,
+                                                        jlong off)
+{
+    LARGE_INTEGER os;
+    LARGE_INTEGER op;
+    DWORD         whence;
+    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;
+    }
+    switch (where) {
+        case ACR_FSEEK_CUR:
+            whence = FILE_CURRENT;
+        break;
+        case ACR_FSEEK_SET:
+            whence = FILE_BEGIN;
+        break;
+        case ACR_FSEEK_END:
+            whence = FILE_END;
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(ACR_EINVAL);
+            return -1;
+        break;
+    }
+    os.QuadPart = off;
+    op.QuadPart = 0;
+    if (!SetFilePointerEx(f->fd, os, &op, whence)) {
+        ACR_THROW_IO_ERRNO();
+        return -1;
+    }
+    return (jlong)op.QuadPart;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemProvider, trunc0)(ACR_JNISTDARGS,
+                                                        jint file,
+                                                        jlong off)
+{
+    LARGE_INTEGER os;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE)
+        return ACR_EFTYPE;
+    if (IS_INVALID_HANDLE(f))
+        return ACR_EBADF;
+
+    os.QuadPart = off;
+    if (!SetFilePointerEx(f->fd, os, NULL, FILE_BEGIN))
+        return ACR_GET_OS_ERROR();
+    if (!SetEndOfFile(f->fd))
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}



Mime
View raw message