jdk/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c
author igerasim
Sat, 13 Sep 2014 20:06:15 +0400
changeset 26618 f30d461d3162
parent 25859 3317bb8137f4
child 30689 446373feb21e
permissions -rw-r--r--
8054029: (fc) FileChannel.size() returns 0 for block devices on Linux Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13024
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 2057
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: 2057
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: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
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 "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"
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    30
#include "sun_nio_ch_FileDispatcherImpl.h"
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    31
#include "java_lang_Long.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <sys/uio.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    36
#include <unistd.h>
26618
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
    37
#if defined(__linux__)
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
    38
#include <linux/fs.h>
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
    39
#include <sys/ioctl.h>
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
    40
#endif
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    41
#include "nio.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    44
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    45
#define stat64 stat
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    46
#define flock64 flock
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    47
#define off64_t off_t
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    48
#define F_SETLKW64 F_SETLKW
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    49
#define F_SETLK64 F_SETLK
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    50
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    51
#define pread64 pread
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    52
#define pwrite64 pwrite
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    53
#define ftruncate64 ftruncate
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    54
#define fstat64 fstat
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    55
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    56
#define fdatasync fsync
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11908
diff changeset
    57
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
static int preCloseFD = -1;     /* File descriptor to which we dup other fd's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                   before closing them for real */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
JNIEXPORT void JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    64
Java_sun_nio_ch_FileDispatcherImpl_init(JNIEnv *env, jclass cl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int sp[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    preCloseFD = sp[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    close(sp[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    76
Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                             jobject fdo, jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    void *buf = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    return convertReturnVal(env, read(fd, buf, len), JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    86
Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    void *buf = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    return convertReturnVal(env, pread64(fd, buf, len, offset), JNI_TRUE);
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
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    96
Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                              jobject fdo, jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    return convertLongReturnVal(env, readv(fd, iov, len), JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   105
Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                              jobject fdo, jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    void *buf = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   115
Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void *buf = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    return convertReturnVal(env, pwrite64(fd, buf, len, offset), JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
JNIEXPORT jlong JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   125
Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                       jobject fdo, jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   133
static jlong
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   134
handle(JNIEnv *env, jlong rv, char *msg)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   135
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   136
    if (rv >= 0)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   137
        return rv;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   138
    if (errno == EINTR)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   139
        return IOS_INTERRUPTED;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   140
    JNU_ThrowIOExceptionWithLastError(env, msg);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   141
    return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   142
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   143
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   144
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   145
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
   146
                                          jobject fdo, jboolean md)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   147
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   148
    jint fd = fdval(env, fdo);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   149
    int result = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   150
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   151
    if (md == JNI_FALSE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   152
        result = fdatasync(fd);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   153
    } else {
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   154
#ifdef _AIX
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   155
        /* On AIX, calling fsync on a file descriptor that is opened only for
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   156
         * reading results in an error ("EBADF: The FileDescriptor parameter is
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   157
         * not a valid file descriptor open for writing.").
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   158
         * However, at this point it is not possibly anymore to read the
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   159
         * 'writable' attribute of the corresponding file channel so we have to
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   160
         * use 'fcntl'.
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   161
         */
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   162
        int getfl = fcntl(fd, F_GETFL);
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   163
        if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   164
            return 0;
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   165
        }
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 14342
diff changeset
   166
#endif
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   167
        result = fsync(fd);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   168
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   169
    return handle(env, result, "Force failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   170
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   171
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   172
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   173
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
   174
                                             jobject fdo, jlong size)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   175
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   176
    return handle(env,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   177
                  ftruncate64(fdval(env, fdo), size),
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   178
                  "Truncation failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   179
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   180
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   181
JNIEXPORT jlong JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   182
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
   183
{
26618
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   184
    jint fd = fdval(env, fdo);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   185
    struct stat64 fbuf;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   186
26618
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   187
    if (fstat64(fd, &fbuf) < 0)
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   188
        return handle(env, -1, "Size failed");
26618
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   189
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   190
#ifdef BLKGETSIZE64
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   191
    if (S_ISBLK(fbuf.st_mode)) {
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   192
        uint64_t size;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   193
        if (ioctl(fd, BLKGETSIZE64, &size) < 0)
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   194
            return handle(env, -1, "Size failed");
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   195
        return (jlong)size;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   196
    }
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   197
#endif
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents: 25859
diff changeset
   198
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   199
    return fbuf.st_size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   200
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   201
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   202
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   203
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
   204
                                      jboolean block, jlong pos, jlong size,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   205
                                      jboolean shared)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   206
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   207
    jint fd = fdval(env, fdo);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   208
    jint lockResult = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   209
    int cmd = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   210
    struct flock64 fl;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   211
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   212
    fl.l_whence = SEEK_SET;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   213
    if (size == (jlong)java_lang_Long_MAX_VALUE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   214
        fl.l_len = (off64_t)0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   215
    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   216
        fl.l_len = (off64_t)size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   217
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   218
    fl.l_start = (off64_t)pos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   219
    if (shared == JNI_TRUE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   220
        fl.l_type = F_RDLCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   221
    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   222
        fl.l_type = F_WRLCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   223
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   224
    if (block == JNI_TRUE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   225
        cmd = F_SETLKW64;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   226
    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   227
        cmd = F_SETLK64;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   228
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   229
    lockResult = fcntl(fd, cmd, &fl);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   230
    if (lockResult < 0) {
11908
384d1c31d539 7146506: (fc) Add EACCES check to the return of fcntl native method
littlee
parents: 5506
diff changeset
   231
        if ((cmd == F_SETLK64) && (errno == EAGAIN || errno == EACCES))
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   232
            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
   233
        if (errno == EINTR)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   234
            return sun_nio_ch_FileDispatcherImpl_INTERRUPTED;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   235
        JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   236
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   237
    return 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   238
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   239
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   240
JNIEXPORT void JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   241
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
   242
                                         jobject fdo, jlong pos, jlong size)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   243
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   244
    jint fd = fdval(env, fdo);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   245
    jint lockResult = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   246
    struct flock64 fl;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   247
    int cmd = F_SETLK64;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   248
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   249
    fl.l_whence = SEEK_SET;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   250
    if (size == (jlong)java_lang_Long_MAX_VALUE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   251
        fl.l_len = (off64_t)0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   252
    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   253
        fl.l_len = (off64_t)size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   254
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   255
    fl.l_start = (off64_t)pos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   256
    fl.l_type = F_UNLCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   257
    lockResult = fcntl(fd, cmd, &fl);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   258
    if (lockResult < 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   259
        JNU_ThrowIOExceptionWithLastError(env, "Release failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   260
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   261
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   262
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   263
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
static void closeFileDescriptor(JNIEnv *env, int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int result = close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
JNIEXPORT void JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   273
Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    closeFileDescriptor(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
JNIEXPORT void JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   280
Java_sun_nio_ch_FileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    if (preCloseFD >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (dup2(preCloseFD, fd) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
JNIEXPORT void JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   290
Java_sun_nio_ch_FileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    closeFileDescriptor(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}