src/java.base/unix/native/libnio/ch/FileChannelImpl.c
author mbaesken
Tue, 16 Jul 2019 16:01:10 +0200
changeset 55753 b9798272720b
parent 49897 117501815bed
child 57804 9b7b9f16dfd9
permissions -rw-r--r--
8227737: avoid implicit-function-declaration on AIX Reviewed-by: clanger, goetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55753
b9798272720b 8227737: avoid implicit-function-declaration on AIX
mbaesken
parents: 49897
diff changeset
     2
 * Copyright (c) 2000, 2019, 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: 2446
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: 2446
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: 2446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
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 <sys/mman.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/stat.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    28
#include <fcntl.h>
49897
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    29
#include <sys/types.h>
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    30
#include <unistd.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
    32
#if defined(__linux__) || defined(__solaris__)
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
    33
#include <sys/sendfile.h>
24575
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
    34
#elif defined(_AIX)
55753
b9798272720b 8227737: avoid implicit-function-declaration on AIX
mbaesken
parents: 49897
diff changeset
    35
#include <string.h>
24575
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
    36
#include <sys/socket.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    37
#elif defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    38
#include <sys/socket.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    39
#include <sys/uio.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    40
#define lseek64 lseek
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
    41
#define mmap64 mmap
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
49897
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    44
#include "jni.h"
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    45
#include "jni_util.h"
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    46
#include "jlong.h"
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    47
#include "nio.h"
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    48
#include "nio_util.h"
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    49
#include "sun_nio_ch_FileChannelImpl.h"
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    50
#include "java_lang_Integer.h"
117501815bed 8202261: (fc) FileChannel.map and RandomAccessFile.setLength should not preallocate space
alanb
parents: 47428
diff changeset
    51
47428
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents: 47216
diff changeset
    52
static jfieldID chan_fd;        /* jobject 'fd' in sun.nio.ch.FileChannelImpl */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    jlong pageSize = sysconf(_SC_PAGESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
static jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
handle(JNIEnv *env, jlong rv, char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    if (rv >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    JNU_ThrowIOExceptionWithLastError(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                     jint prot, jlong off, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    void *mapAddress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int protections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        protections = PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        flags = MAP_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        protections = PROT_WRITE | PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        flags = MAP_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        protections =  PROT_WRITE | PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        flags = MAP_PRIVATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    mapAddress = mmap64(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        0,                    /* Let OS decide location */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        len,                  /* Number of bytes to map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        protections,          /* File permissions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        flags,                /* Changes are shared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        fd,                   /* File descriptor of mapped file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        off);                 /* Offset into file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (mapAddress == MAP_FAILED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (errno == ENOMEM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            JNU_ThrowOutOfMemoryError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return handle(env, -1, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    return ((jlong) (unsigned long) mapAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                       jlong address, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void *a = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    return handle(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                  munmap(a, (size_t)len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                  "Unmap failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 25859
diff changeset
   127
                                            jobject srcFDO,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                            jlong position, jlong count,
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 25859
diff changeset
   129
                                            jobject dstFDO)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
{
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 25859
diff changeset
   131
    jint srcFD = fdval(env, srcFDO);
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 25859
diff changeset
   132
    jint dstFD = fdval(env, dstFDO);
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 25859
diff changeset
   133
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   134
#if defined(__linux__)
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   135
    off64_t offset = (off64_t)position;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   136
    jlong n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (errno == EAGAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if ((errno == EINVAL) && ((ssize_t)count >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return IOS_UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    return n;
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   149
#elif defined (__solaris__)
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   150
    sendfilevec64_t sfv;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   151
    size_t numBytes = 0;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   152
    jlong result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   154
    sfv.sfv_fd = srcFD;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   155
    sfv.sfv_flag = 0;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   156
    sfv.sfv_off = (off64_t)position;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   157
    sfv.sfv_len = count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   159
    result = sendfilev64(dstFD, &sfv, 1, &numBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   161
    /* Solaris sendfilev() will return -1 even if some bytes have been
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   162
     * transferred, so we check numBytes first.
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   163
     */
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   164
    if (numBytes > 0)
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   165
        return numBytes;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   166
    if (result < 0) {
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   167
        if (errno == EAGAIN)
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   168
            return IOS_UNAVAILABLE;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   169
        if (errno == EOPNOTSUPP)
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   170
            return IOS_UNSUPPORTED_CASE;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   171
        if ((errno == EINVAL) && ((ssize_t)count >= 0))
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   172
            return IOS_UNSUPPORTED_CASE;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   173
        if (errno == EINTR)
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   174
            return IOS_INTERRUPTED;
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   175
        JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   176
        return IOS_THROWN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   178
    return result;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   179
#elif defined(__APPLE__)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   180
    off_t numBytes;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   181
    int result;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   182
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   183
    numBytes = count;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   184
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   185
    result = sendfile(srcFD, dstFD, position, &numBytes, NULL, 0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   186
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   187
    if (numBytes > 0)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   188
        return numBytes;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   189
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   190
    if (result == -1) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   191
        if (errno == EAGAIN)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   192
            return IOS_UNAVAILABLE;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   193
        if (errno == EOPNOTSUPP || errno == ENOTSOCK || errno == ENOTCONN)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   194
            return IOS_UNSUPPORTED_CASE;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   195
        if ((errno == EINVAL) && ((ssize_t)count >= 0))
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   196
            return IOS_UNSUPPORTED_CASE;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   197
        if (errno == EINTR)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   198
            return IOS_INTERRUPTED;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   199
        JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   200
        return IOS_THROWN;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   201
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   202
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 9035
diff changeset
   203
    return result;
24575
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   204
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   205
#elif defined(_AIX)
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   206
    jlong max = (jlong)java_lang_Integer_MAX_VALUE;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   207
    struct sf_parms sf_iobuf;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   208
    jlong result;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   209
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   210
    if (position > max)
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   211
        return IOS_UNSUPPORTED_CASE;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   212
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   213
    if (count > max)
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   214
        count = max;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   215
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   216
    memset(&sf_iobuf, 0, sizeof(sf_iobuf));
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   217
    sf_iobuf.file_descriptor = srcFD;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   218
    sf_iobuf.file_offset = (off_t)position;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   219
    sf_iobuf.file_bytes = count;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   220
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   221
    result = send_file(&dstFD, &sf_iobuf, SF_SYNC_CACHE);
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   222
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   223
    /* AIX send_file() will return 0 when this operation complete successfully,
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   224
     * return 1 when partial bytes transfered and return -1 when an error has
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   225
     * Occured.
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   226
     */
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   227
    if (result == -1) {
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   228
        if (errno == EWOULDBLOCK)
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   229
            return IOS_UNAVAILABLE;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   230
        if ((errno == EINVAL) && ((ssize_t)count >= 0))
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   231
            return IOS_UNSUPPORTED_CASE;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   232
        if (errno == EINTR)
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   233
            return IOS_INTERRUPTED;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   234
        if (errno == ENOTSOCK)
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   235
            return IOS_UNSUPPORTED;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   236
        JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   237
        return IOS_THROWN;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   238
    }
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   239
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   240
    if (sf_iobuf.bytes_sent > 0)
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   241
        return (jlong)sf_iobuf.bytes_sent;
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   242
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   243
    return IOS_UNSUPPORTED_CASE;
7971
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   244
#else
ca5a32210b1b 7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents: 5506
diff changeset
   245
    return IOS_UNSUPPORTED_CASE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
24575
c1b197bb86c2 8043495: Add native FileChannelImpl.transferTo0() implementation for AIX
luchsh
parents: 14342
diff changeset
   248