jdk/src/solaris/native/sun/nio/ch/FileChannelImpl.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 68 89f4f4a43855
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <sys/mman.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "sun_nio_ch_FileChannelImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "java_lang_Integer.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
static jfieldID chan_fd;        /* jobject 'fd' in sun.io.FileChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
typedef struct sendfilevec64 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    int     sfv_fd;         /* input fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    uint_t  sfv_flag;       /* Flags. see below */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    off64_t sfv_off;        /* offset to start reading from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    size_t  sfv_len;        /* amount of data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
} sendfilevec_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/* Function pointer for sendfilev on Solaris 8+ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
typedef ssize_t sendfile_func(int fildes, const struct sendfilevec64 *vec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                              int sfvcnt, size_t *xferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
sendfile_func* my_sendfile_func = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include <sys/sendfile.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/* Function pointer for sendfile64 on Linux 2.6 (and newer 2.4 kernels) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
typedef ssize_t sendfile64_func(int out_fd, int in_fd, off64_t *offset, size_t count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
sendfile64_func* my_sendfile64_func = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    jlong pageSize = sysconf(_SC_PAGESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    if (dlopen("/usr/lib/libsendfile.so.1", RTLD_GLOBAL | RTLD_LAZY) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        my_sendfile_func = (sendfile_func*) dlsym(RTLD_DEFAULT, "sendfilev64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    my_sendfile64_func = (sendfile64_func*) dlsym(RTLD_DEFAULT, "sendfile64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
static jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
handle(JNIEnv *env, jlong rv, char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (rv >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    JNU_ThrowIOExceptionWithLastError(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                     jint prot, jlong off, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    void *mapAddress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int protections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        protections = PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        flags = MAP_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        protections = PROT_WRITE | PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        flags = MAP_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        protections =  PROT_WRITE | PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        flags = MAP_PRIVATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    mapAddress = mmap64(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        0,                    /* Let OS decide location */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        len,                  /* Number of bytes to map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        protections,          /* File permissions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        flags,                /* Changes are shared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        fd,                   /* File descriptor of mapped file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        off);                 /* Offset into file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    if (mapAddress == MAP_FAILED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (errno == ENOMEM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            JNU_ThrowOutOfMemoryError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return handle(env, -1, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return ((jlong) (unsigned long) mapAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                       jlong address, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    void *a = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    return handle(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                  munmap(a, (size_t)len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                  "Unmap failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
Java_sun_nio_ch_FileChannelImpl_truncate0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                          jobject fdo, jlong size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    return handle(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                  ftruncate64(fdval(env, fdo), size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                  "Truncation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
Java_sun_nio_ch_FileChannelImpl_force0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                       jobject fdo, jboolean md)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    if (md == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        result = fdatasync(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        result = fsync(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    return handle(env, result, "Force failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
Java_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                          jobject fdo, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    jlong result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    if (offset < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        result = lseek64(fd, 0, SEEK_CUR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        result = lseek64(fd, offset, SEEK_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    return handle(env, result, "Position failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
Java_sun_nio_ch_FileChannelImpl_size0(JNIEnv *env, jobject this, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    struct stat64 fbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    if (fstat64(fdval(env, fdo), &fbuf) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return handle(env, -1, "Size failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    return fbuf.st_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
Java_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        jlong result = close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                            jint srcFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                            jlong position, jlong count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                            jint dstFD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jlong max = (jlong)java_lang_Integer_MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    jlong n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    if (my_sendfile64_func == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        off_t offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (position > max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return IOS_UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (count > max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            count = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        offset = (off_t)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        n = sendfile(dstFD, srcFD, &offset, (size_t)count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        off64_t offset = (off64_t)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        n = (*my_sendfile64_func)(dstFD, srcFD, &offset, (size_t)count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (errno == EAGAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if ((errno == EINVAL) && ((ssize_t)count >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return IOS_UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    if (my_sendfile_func == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return IOS_UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        sendfilevec_t sfv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        size_t numBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        jlong result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        sfv.sfv_fd = srcFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        sfv.sfv_flag = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        sfv.sfv_off = (off64_t)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        sfv.sfv_len = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        result = (*my_sendfile_func)(dstFD, &sfv, 1, &numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        /* Solaris sendfilev() will return -1 even if some bytes have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * transferred, so we check numBytes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (numBytes > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (errno == EAGAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if ((errno == EINVAL) && ((ssize_t)count >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                return IOS_UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
Java_sun_nio_ch_FileChannelImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                      jboolean block, jlong pos, jlong size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                      jboolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    jint lockResult = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    int cmd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    struct flock64 fl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    fl.l_whence = SEEK_SET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    fl.l_len = (off64_t)size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    fl.l_start = (off64_t)pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    if (shared == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        fl.l_type = F_RDLCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        fl.l_type = F_WRLCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    if (block == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        cmd = F_SETLKW64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        cmd = F_SETLK64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    lockResult = fcntl(fd, cmd, &fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    if (lockResult < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if ((cmd == F_SETLK64) && (errno == EAGAIN))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return sun_nio_ch_FileChannelImpl_NO_LOCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return sun_nio_ch_FileChannelImpl_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
Java_sun_nio_ch_FileChannelImpl_release0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                         jobject fdo, jlong pos, jlong size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    jint lockResult = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    struct flock64 fl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    int cmd = F_SETLK64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    fl.l_whence = SEEK_SET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    fl.l_len = (off64_t)size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    fl.l_start = (off64_t)pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    fl.l_type = F_UNLCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    lockResult = fcntl(fd, cmd, &fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    if (lockResult < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        JNU_ThrowIOExceptionWithLastError(env, "Release failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}