jdk/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c
author igerasim
Mon, 02 Mar 2015 11:07:47 +0300
changeset 29228 dfa75715dfa7
parent 26732 9b27273e6131
child 41601 d04193dcb0f7
permissions -rw-r--r--
8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win] Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7515
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jlong.h"
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    31
#include "sun_nio_ch_FileDispatcherImpl.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "nio_util.h"
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
    35
#include "jlong.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**************************************************************
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    39
 * FileDispatcherImpl.c
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    43
Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                      jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    result = ReadFile(h,          /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                      (LPVOID)address,    /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                      len,        /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                      &read,      /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                      NULL);      /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    return convertReturnVal(env, (jint)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
JNIEXPORT jlong JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    74
Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                       jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jlong totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    LPVOID loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    DWORD num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    for(i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        num = iovecp[i].iov_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        result = ReadFile(h,                /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                          loc,              /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                          num,              /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                          &read,            /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                          NULL);            /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (read > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            totalRead += read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (read < num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    return convertLongReturnVal(env, totalRead, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   123
Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    HANDLE h = (HANDLE)(handleval(env, fdo));
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   129
    LARGE_INTEGER currPos;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   130
    OVERLAPPED ov;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   137
    currPos.QuadPart = 0;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   138
    result = SetFilePointerEx(h, currPos, &currPos, FILE_CURRENT);
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   139
    if (result == 0) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   140
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   141
        return IOS_THROWN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   144
    ZeroMemory(&ov, sizeof(ov));
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   145
    ov.Offset = (DWORD)offset;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   146
    ov.OffsetHigh = (DWORD)(offset >> 32);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    result = ReadFile(h,                /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                      (LPVOID)address,  /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                      len,              /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                      &read,            /* number of bytes read */
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   152
                      &ov);             /* position to read from */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   162
        if (error != ERROR_HANDLE_EOF) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   163
            JNU_ThrowIOExceptionWithLastError(env, "Read failed");
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   164
            return IOS_THROWN;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   165
        }
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   166
    }
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   167
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   168
    result = SetFilePointerEx(h, currPos, NULL, FILE_BEGIN);
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   169
    if (result == 0) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   170
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    return convertReturnVal(env, (jint)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   178
Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz, jobject fdo,
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   179
                                          jlong address, jint len, jboolean append)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    if (h != INVALID_HANDLE_VALUE) {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   186
        OVERLAPPED ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   187
        LPOVERLAPPED lpOv;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   188
        if (append == JNI_TRUE) {
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   189
            ZeroMemory(&ov, sizeof(ov));
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   190
            ov.Offset = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   191
            ov.OffsetHigh = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   192
            lpOv = &ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   193
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   194
            lpOv = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   195
        }
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   196
        result = WriteFile(h,                /* File handle to write */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   197
                           (LPCVOID)address, /* pointer to the buffer */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   198
                           len,              /* number of bytes to write */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   199
                           &written,         /* receives number of bytes written */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   200
                           lpOv);            /* overlapped struct */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    return convertReturnVal(env, (jint)written, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
JNIEXPORT jlong JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   211
Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz, jobject fdo,
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   212
                                           jlong address, jint len, jboolean append)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    jlong totalWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        LPVOID loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        DWORD num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   224
        OVERLAPPED ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   225
        LPOVERLAPPED lpOv;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   226
        if (append == JNI_TRUE) {
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   227
            ZeroMemory(&ov, sizeof(ov));
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   228
            ov.Offset = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   229
            ov.OffsetHigh = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   230
            lpOv = &ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   231
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   232
            lpOv = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   233
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for(i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            num = iovecp[i].iov_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            result = WriteFile(h,       /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                               loc,     /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                               num,     /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                               &written,/* receives number of bytes written */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   241
                               lpOv);   /* overlapped struct */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (written > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                totalWritten += written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (written < num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    return convertLongReturnVal(env, totalWritten, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   259
Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    HANDLE h = (HANDLE)(handleval(env, fdo));
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   265
    LARGE_INTEGER currPos;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   266
    OVERLAPPED ov;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   268
    currPos.QuadPart = 0;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   269
    result = SetFilePointerEx(h, currPos, &currPos, FILE_CURRENT);
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   270
    if (result == 0) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   271
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   272
        return IOS_THROWN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   275
    ZeroMemory(&ov, sizeof(ov));
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   276
    ov.Offset = (DWORD)offset;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   277
    ov.OffsetHigh = (DWORD)(offset >> 32);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   279
    result = WriteFile(h,                /* File handle to write */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   280
                       (LPCVOID)address, /* pointer to the buffer */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   281
                       len,              /* number of bytes to write */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   282
                       &written,         /* receives number of bytes written */
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   283
                       &ov);             /* position to write at */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   290
    result = SetFilePointerEx(h, currPos, NULL, FILE_BEGIN);
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   291
    if (result == 0) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   292
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   293
        return IOS_THROWN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    return convertReturnVal(env, (jint)written, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   299
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   300
Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv *env, jobject this,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   301
                                          jobject fdo, jboolean md)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   302
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   303
    int result = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   304
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   305
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   306
    if (h != INVALID_HANDLE_VALUE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   307
        result = FlushFileBuffers(h);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   308
        if (result == 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   309
            int error = GetLastError();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   310
            if (error != ERROR_ACCESS_DENIED) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   311
                JNU_ThrowIOExceptionWithLastError(env, "Force failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   312
                return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   313
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   314
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   315
    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   316
        JNU_ThrowIOExceptionWithLastError(env, "Force failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   317
        return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   318
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   319
    return 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   320
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   321
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   322
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   323
Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   324
                                             jobject fdo, jlong size)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   325
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   326
    BOOL result = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   327
    HANDLE h = (HANDLE)(handleval(env, fdo));
29228
dfa75715dfa7 8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win]
igerasim
parents: 26732
diff changeset
   328
    FILE_END_OF_FILE_INFO eofInfo;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   329
29228
dfa75715dfa7 8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win]
igerasim
parents: 26732
diff changeset
   330
    eofInfo.EndOfFile.QuadPart = size;
dfa75715dfa7 8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win]
igerasim
parents: 26732
diff changeset
   331
    result = SetFileInformationByHandle(h,
dfa75715dfa7 8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win]
igerasim
parents: 26732
diff changeset
   332
                                        FileEndOfFileInfo,
dfa75715dfa7 8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win]
igerasim
parents: 26732
diff changeset
   333
                                        &eofInfo,
dfa75715dfa7 8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win]
igerasim
parents: 26732
diff changeset
   334
                                        sizeof(eofInfo));
dfa75715dfa7 8055915: (ch) FileDispatcherImpl.truncate0 should use SetFileInformationByHandle [win]
igerasim
parents: 26732
diff changeset
   335
    if (result == FALSE) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   336
        JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   337
        return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   338
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   339
    return 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   340
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   341
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   342
JNIEXPORT jlong JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   343
Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   344
{
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   345
    BOOL result = 0;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   346
    HANDLE h = (HANDLE)(handleval(env, fdo));
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   347
    LARGE_INTEGER size;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   348
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   349
    result = GetFileSizeEx(h, &size);
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   350
    if (result == 0) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   351
        JNU_ThrowIOExceptionWithLastError(env, "Size failed");
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   352
        return IOS_THROWN;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   353
    }
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   354
    return (jlong)size.QuadPart;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   355
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   356
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   357
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   358
Java_sun_nio_ch_FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   359
                                      jboolean block, jlong pos, jlong size,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   360
                                      jboolean shared)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   361
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   362
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   363
    DWORD lowPos = (DWORD)pos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   364
    long highPos = (long)(pos >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   365
    DWORD lowNumBytes = (DWORD)size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   366
    DWORD highNumBytes = (DWORD)(size >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   367
    BOOL result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   368
    DWORD flags = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   369
    OVERLAPPED o;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   370
    o.hEvent = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   371
    o.Offset = lowPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   372
    o.OffsetHigh = highPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   373
    if (block == JNI_FALSE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   374
        flags |= LOCKFILE_FAIL_IMMEDIATELY;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   375
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   376
    if (shared == JNI_FALSE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   377
        flags |= LOCKFILE_EXCLUSIVE_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   378
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   379
    result = LockFileEx(h, flags, 0, lowNumBytes, highNumBytes, &o);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   380
    if (result == 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   381
        int error = GetLastError();
25806
7a0cf527248e 8053931: (fc) FileDispatcherImpl.lock0 does not handle ERROR_IO_PENDING [win]
alanb
parents: 7668
diff changeset
   382
        if (error == ERROR_IO_PENDING) {
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   383
            DWORD dwBytes;
25806
7a0cf527248e 8053931: (fc) FileDispatcherImpl.lock0 does not handle ERROR_IO_PENDING [win]
alanb
parents: 7668
diff changeset
   384
            result = GetOverlappedResult(h, &o, &dwBytes, TRUE);
7a0cf527248e 8053931: (fc) FileDispatcherImpl.lock0 does not handle ERROR_IO_PENDING [win]
alanb
parents: 7668
diff changeset
   385
            if (result != 0) {
7a0cf527248e 8053931: (fc) FileDispatcherImpl.lock0 does not handle ERROR_IO_PENDING [win]
alanb
parents: 7668
diff changeset
   386
                return sun_nio_ch_FileDispatcherImpl_LOCKED;
7a0cf527248e 8053931: (fc) FileDispatcherImpl.lock0 does not handle ERROR_IO_PENDING [win]
alanb
parents: 7668
diff changeset
   387
            }
7a0cf527248e 8053931: (fc) FileDispatcherImpl.lock0 does not handle ERROR_IO_PENDING [win]
alanb
parents: 7668
diff changeset
   388
            error = GetLastError();
7a0cf527248e 8053931: (fc) FileDispatcherImpl.lock0 does not handle ERROR_IO_PENDING [win]
alanb
parents: 7668
diff changeset
   389
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   390
        if (error != ERROR_LOCK_VIOLATION) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   391
            JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   392
            return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   393
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   394
        if (flags & LOCKFILE_FAIL_IMMEDIATELY) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   395
            return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   396
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   397
        JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   398
        return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   399
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   400
    return sun_nio_ch_FileDispatcherImpl_LOCKED;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   401
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   402
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   403
JNIEXPORT void JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   404
Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv *env, jobject this,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   405
                                        jobject fdo, jlong pos, jlong size)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   406
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   407
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   408
    DWORD lowPos = (DWORD)pos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   409
    long highPos = (long)(pos >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   410
    DWORD lowNumBytes = (DWORD)size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   411
    DWORD highNumBytes = (DWORD)(size >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   412
    jint result = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   413
    OVERLAPPED o;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   414
    o.hEvent = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   415
    o.Offset = lowPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   416
    o.OffsetHigh = highPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   417
    result = UnlockFileEx(h, 0, lowNumBytes, highNumBytes, &o);
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   418
    if (result == 0) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   419
        int error = GetLastError();
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   420
        if (error == ERROR_IO_PENDING) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   421
            DWORD dwBytes;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   422
            result = GetOverlappedResult(h, &o, &dwBytes, TRUE);
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   423
            if (result == 0) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   424
                return;
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   425
            }
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   426
            error = GetLastError();
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   427
        }
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   428
        if (error != ERROR_NOT_LOCKED) {
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   429
            JNU_ThrowIOExceptionWithLastError(env, "Release failed");
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   430
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   431
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   432
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   433
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
static void closeFile(JNIEnv *env, jlong fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        int result = CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
JNIEXPORT void JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   444
Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    jlong fd = handleval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    closeFile(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
JNIEXPORT void JNICALL
26732
9b27273e6131 8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
igerasim
parents: 25859
diff changeset
   451
Java_sun_nio_ch_FileDispatcherImpl_closeByHandle(JNIEnv *env, jclass clazz, jlong fd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    closeFile(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
}
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   455
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   456
JNIEXPORT jlong JNICALL
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   457
Java_sun_nio_ch_FileDispatcherImpl_duplicateHandle(JNIEnv *env, jclass this, jlong handle)
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   458
{
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   459
    HANDLE hProcess = GetCurrentProcess();
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   460
    HANDLE hFile = jlong_to_ptr(handle);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   461
    HANDLE hResult;
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   462
    BOOL res = DuplicateHandle(hProcess, hFile, hProcess, &hResult, 0, FALSE,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   463
                               DUPLICATE_SAME_ACCESS);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   464
    if (res == 0)
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   465
       JNU_ThrowIOExceptionWithLastError(env, "DuplicateHandle failed");
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   466
    return ptr_to_jlong(hResult);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   467
}