src/java.base/share/native/libjava/RandomAccessFile.c
author rriggs
Fri, 02 Mar 2018 18:08:50 -0500
changeset 49123 d8057fc6fffc
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198970: jnu_util.c compilation error on Solaris Reviewed-by: mchung, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34890
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents: 26190
diff changeset
     2
 * Copyright (c) 1997, 2015, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "java_io_RandomAccessFile.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * static method to store field ID's in initializers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
Java_java_io_RandomAccessFile_initIDs(JNIEnv *env, jclass fdClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    raf_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
JNIEXPORT void JNICALL
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
    50
Java_java_io_RandomAccessFile_open0(JNIEnv *env,
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
    51
                                    jobject this, jstring path, jint mode)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if (mode & java_io_RandomAccessFile_O_RDONLY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        flags = O_RDONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    else if (mode & java_io_RandomAccessFile_O_RDWR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        flags = O_RDWR | O_CREAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (mode & java_io_RandomAccessFile_O_SYNC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            flags |= O_SYNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        else if (mode & java_io_RandomAccessFile_O_DSYNC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            flags |= O_DSYNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 34890
diff changeset
    63
#ifdef WIN32
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 34890
diff changeset
    64
    if (mode & java_io_RandomAccessFile_O_TEMPORARY)
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 34890
diff changeset
    65
        flags |= O_TEMPORARY;
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 34890
diff changeset
    66
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    fileOpen(env, this, path, raf_fd, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
JNIEXPORT jint JNICALL
22946
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 19438
diff changeset
    71
Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    return readSingle(env, this, raf_fd);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    jobject this, jbyteArray bytes, jint off, jint len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    return readBytes(env, this, bytes, off, len, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
JNIEXPORT void JNICALL
22946
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 19438
diff changeset
    82
Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    83
    writeSingle(env, this, byte, JNI_FALSE, raf_fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    jobject this, jbyteArray bytes, jint off, jint len) {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    89
    writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    jlong ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JNU_ThrowIOException(env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {
34890
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents: 26190
diff changeset
   110
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    FD fd;
34890
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents: 26190
diff changeset
   112
    jlong length = jlong_zero;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        JNU_ThrowIOException(env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
34890
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents: 26190
diff changeset
   119
    if ((length = IO_GetLength(fd)) == -1) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents: 26190
diff changeset
   120
        JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
34890
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents: 26190
diff changeset
   122
    return length;
2
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 void JNICALL
19438
7de6ae3cecad 8023203: Wrap RandomAccessFile.seek native method into a Java helper method
dxu
parents: 7668
diff changeset
   126
Java_java_io_RandomAccessFile_seek0(JNIEnv *env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    jobject this, jlong pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        JNU_ThrowIOException(env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    if (pos < jlong_zero) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        JNU_ThrowIOException(env, "Negative seek offset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    } else if (IO_Lseek(fd, pos, SEEK_SET) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
Java_java_io_RandomAccessFile_setLength(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                        jlong newLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    jlong cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    fd = GET_FD(this, raf_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        JNU_ThrowIOException(env, "Stream Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    if (IO_SetLength(fd, newLength) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    if (cur > newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 fail:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    JNU_ThrowIOExceptionWithLastError(env, "setLength failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}