jdk/src/solaris/native/java/io/io_util_md.c
author dxu
Wed, 27 Mar 2013 09:00:34 -0700
changeset 16716 6d3f9ce0d776
parent 16476 e269be167fae
child 22597 7515a991bb37
permissions -rw-r--r--
8010837: FileInputStream.available() throw IOException when encountering negative available values Summary: Remove the check in the native code to allow negative values Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
     2
 * Copyright (c) 2001, 2013, 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 "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <string.h>
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    32
#include <unistd.h>
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    33
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    34
#ifdef __solaris__
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    35
#include <sys/filio.h>
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    36
#endif
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    37
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    38
#if defined(__linux__) || defined(_ALLBSD_SOURCE)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    39
#include <sys/ioctl.h>
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    40
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    42
#ifdef MACOSX
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    43
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    44
#include <CoreFoundation/CoreFoundation.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    45
13360
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    46
__private_extern__
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    47
jstring newStringPlatform(JNIEnv *env, const char* str)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    48
{
13360
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    49
    jstring rv = NULL;
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    50
    CFMutableStringRef csref = CFStringCreateMutable(NULL, 0);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    51
    if (csref == NULL) {
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    52
        JNU_ThrowOutOfMemoryError(env, "native heap");
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    53
    } else {
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    54
        CFStringAppendCString(csref, str, kCFStringEncodingUTF8);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    55
        CFStringNormalize(csref, kCFStringNormalizationFormC);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    56
        int clen = CFStringGetLength(csref);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    57
        int ulen = (clen + 1) * 2;        // utf16 + zero padding
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    58
        char* chars = malloc(ulen);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    59
        if (chars == NULL) {
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    60
            CFRelease(csref);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    61
            JNU_ThrowOutOfMemoryError(env, "native heap");
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    62
        } else {
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    63
            if (CFStringGetCString(csref, chars, ulen, kCFStringEncodingUTF16)) {
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    64
                rv = (*env)->NewString(env, (jchar*)chars, clen);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    65
            }
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    66
            free(chars);
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    67
            CFRelease(csref);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    68
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    69
    }
13360
b62b5e86f92a 7130915: File.equals does not give expected results when path contains Non-English characters on Mac OS X
sherman
parents: 12047
diff changeset
    70
    return rv;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    71
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    72
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
    73
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    74
FD
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    75
handleOpen(const char *path, int oflag, int mode) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    76
    FD fd;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    77
    RESTARTABLE(open64(path, oflag, mode), fd);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    78
    if (fd != -1) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    79
        struct stat64 buf64;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    80
        int result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    81
        RESTARTABLE(fstat64(fd, &buf64), result);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    82
        if (result != -1) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    83
            if (S_ISDIR(buf64.st_mode)) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    84
                close(fd);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    85
                errno = EISDIR;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    86
                fd = -1;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    87
            }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    88
        } else {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    89
            close(fd);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    90
            fd = -1;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    91
        }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    92
    }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    93
    return fd;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    94
}
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
    95
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    WITH_PLATFORM_STRING(env, path, ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        FD fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 7668
diff changeset
   102
#if defined(__linux__) || defined(_ALLBSD_SOURCE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /* Remove trailing slashes, since the kernel won't */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        char *p = (char *)ps + strlen(ps) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        while ((p > ps) && (*p == '/'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            *p-- = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#endif
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   108
        fd = handleOpen(ps, flags, 0666);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   109
        if (fd != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            SET_FD(this, fd, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throwFileNotFoundException(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    } END_PLATFORM_STRING(env, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
fileClose(JNIEnv *env, jobject this, jfieldID fid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    FD fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* Set the fd to -1 before closing it so that the timing window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * of other threads using the wrong fd (closed but recycled fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * that gets re-opened with some other filename) is reduced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Practically the chance of its occurance is low, however, we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * taking extra precaution over here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    SET_FD(this, -1, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Don't close file descriptors 0, 1, or 2. If we close these stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * then a subsequent file open or socket will use them. Instead we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * just redirect these file descriptors to /dev/null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int devnull = open("/dev/null", O_WRONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (devnull < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            SET_FD(this, fd, fid); // restore fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            dup2(devnull, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            close(devnull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   147
    } else if (close(fd) == -1) {
6690
49644ab3ef0c 6988037: fileClose prints debug message is close fails
alanb
parents: 5506
diff changeset
   148
        JNU_ThrowIOExceptionWithLastError(env, "close failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   151
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   152
ssize_t
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   153
handleRead(FD fd, void *buf, jint len)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   154
{
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   155
    ssize_t result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   156
    RESTARTABLE(read(fd, buf, len), result);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   157
    return result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   158
}
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   159
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   160
ssize_t
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   161
handleWrite(FD fd, const void *buf, jint len)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   162
{
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   163
    ssize_t result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   164
    RESTARTABLE(write(fd, buf, len), result);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   165
    return result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   166
}
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   167
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   168
jint
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   169
handleAvailable(FD fd, jlong *pbytes)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   170
{
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   171
    int mode;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   172
    struct stat64 buf64;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   173
    jlong size = -1, current = -1;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   174
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   175
    int result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   176
    RESTARTABLE(fstat64(fd, &buf64), result);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   177
    if (result != -1) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   178
        mode = buf64.st_mode;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   179
        if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   180
            int n;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   181
            int result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   182
            RESTARTABLE(ioctl(fd, FIONREAD, &n), result);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   183
            if (result >= 0) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   184
                *pbytes = n;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   185
                return 1;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   186
            }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   187
        } else if (S_ISREG(mode)) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   188
            size = buf64.st_size;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   189
        }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   190
    }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   191
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   192
    if ((current = lseek64(fd, 0, SEEK_CUR)) == -1) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   193
        return 0;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   194
    }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   195
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   196
    if (size < current) {
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   197
        if ((size = lseek64(fd, 0, SEEK_END)) == -1)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   198
            return 0;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   199
        else if (lseek64(fd, current, SEEK_SET) == -1)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   200
            return 0;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   201
    }
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   202
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents: 16476
diff changeset
   203
    *pbytes = size - current;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents: 16476
diff changeset
   204
    return 1;
16476
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   205
}
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   206
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   207
jint
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   208
handleSetLength(FD fd, jlong length)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   209
{
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   210
    int result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   211
    RESTARTABLE(ftruncate64(fd, length), result);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   212
    return result;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   213
}
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   214
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   215
size_t
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   216
getLastErrorString(char *buf, size_t len)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   217
{
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   218
    if (errno == 0 || len < 1) return 0;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   219
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   220
    const char *err = strerror(errno);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   221
    size_t n = strlen(err);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   222
    if (n >= len)
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   223
        n = len - 1;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   224
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   225
    strncpy(buf, err, n);
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   226
    buf[n] = '\0';
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   227
    return n;
e269be167fae 8001334: Remove use of JVM_* functions from java.io code
dxu
parents: 14342
diff changeset
   228
}