jdk/src/windows/native/java/io/io_util_md.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8186 d0e2cc8b3073
child 11116 3e486ce0019d
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8186
diff changeset
     2
 * Copyright (c) 2001, 2011, 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: 798
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: 798
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: 798
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 798
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 798
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 <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <wchar.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <wincon.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
extern jboolean onNT = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
    45
static DWORD MAX_INPUT_EVENTS = 2000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
initializeWindowsVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    OSVERSIONINFO ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    ver.dwOSVersionInfoSize = sizeof(ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    GetVersionEx(&ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        onNT = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        onNT = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/* If this returns NULL then an exception is pending */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
WCHAR*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
fileToNTPath(JNIEnv *env, jobject file, jfieldID id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    jstring path = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    if (file != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        path = (*env)->GetObjectField(env, file, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    return pathToNTPath(env, path, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
6885
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    69
/* Returns the working directory for the given drive, or NULL */
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    70
WCHAR*
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    71
currentDir(int di) {
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    72
    UINT dt;
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    73
    WCHAR root[4];
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    74
    // verify drive is valid as _wgetdcwd in the VC++ 2010 runtime
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    75
    // library does not handle invalid drives.
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    76
    root[0] = L'A' + (WCHAR)(di - 1);
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    77
    root[1] = L':';
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    78
    root[2] = L'\\';
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    79
    root[3] = L'\0';
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    80
    dt = GetDriveTypeW(root);
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    81
    if (dt == DRIVE_UNKNOWN || dt == DRIVE_NO_ROOT_DIR) {
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    82
        return NULL;
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    83
    } else {
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    84
        return _wgetdcwd(di, NULL, MAX_PATH);
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    85
    }
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    86
}
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
    87
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
/* We cache the length of current working dir here to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
   calling _wgetcwd() every time we need to resolve a relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
   path. This piece of code needs to be revisited if chdir
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
   makes its way into java runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
currentDirLength(const WCHAR* ps, int pathlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    WCHAR *dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (pathlen > 2 && ps[1] == L':' && ps[2] != L'\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        //drive-relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        WCHAR d = ps[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int dirlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int di = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ((d >= L'a') && (d <= L'z')) di = d - L'a' + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        else if ((d >= L'A') && (d <= L'Z')) di = d - L'A' + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        else return 0; /* invalid drive name. */
6885
5605d7d60c9c 6983520: java/io/pathNames/GeneralWin32.java fails with jdk7-b108 (win)
alanb
parents: 6690
diff changeset
   105
        dir = currentDir(di);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (dir != NULL){
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   107
            dirlen = (int)wcslen(dir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            free(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return dirlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        static int curDirLenCached = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        //relative to both drive and directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (curDirLenCached == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            int dirlen = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            dir = _wgetcwd(NULL, MAX_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (dir != NULL) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   118
                curDirLenCached = (int)wcslen(dir);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                free(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return curDirLenCached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
785
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   126
/*
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   127
  The "abpathlen" is the size of the buffer needed by _wfullpath. If the
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   128
  "path" is a relative path, it is "the length of the current dir" + "the
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   129
  length of the path", if it's "absolute" already, it's the same as
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   130
  pathlen which is the length of "path".
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   131
 */
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   132
WCHAR* prefixAbpath(const WCHAR* path, int pathlen, int abpathlen) {
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   133
    WCHAR* pathbuf = NULL;
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   134
    WCHAR* abpath = NULL;
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   135
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   136
    abpathlen += 10;  //padding
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   137
    abpath = (WCHAR*)malloc(abpathlen * sizeof(WCHAR));
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   138
    if (abpath) {
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   139
        /* Collapse instances of "foo\.." and ensure absoluteness before
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   140
           going down to prefixing.
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   141
        */
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   142
        if (_wfullpath(abpath, path, abpathlen)) {
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   143
            pathbuf = getPrefixed(abpath, abpathlen);
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   144
        } else {
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   145
            /* _wfullpath fails if the pathlength exceeds 32k wchar.
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   146
               Instead of doing more fancy things we simply copy the
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   147
               ps into the return buffer, the subsequent win32 API will
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   148
               probably fail with FileNotFoundException, which is expected
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   149
            */
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   150
            pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   151
            if (pathbuf != 0) {
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   152
                wcscpy(pathbuf, path);
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   153
            }
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   154
        }
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   155
        free(abpath);
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   156
    }
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   157
    return pathbuf;
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   158
}
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   159
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
/* If this returns NULL then an exception is pending */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
WCHAR*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
pathToNTPath(JNIEnv *env, jstring path, jboolean throwFNFE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    int pathlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    WCHAR *pathbuf = NULL;
785
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   165
    int max_path = 248; /* CreateDirectoryW() has the limit of 248 */
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   166
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    WITH_UNICODE_STRING(env, path, ps) {
8186
d0e2cc8b3073 7017454: Residual warnings in sun/nio/** and java/io native code (win64)
alanb
parents: 7668
diff changeset
   168
        pathlen = (int)wcslen(ps);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (pathlen != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (pathlen > 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                (ps[0] == L'\\' && ps[1] == L'\\' ||   //UNC
785
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   172
                 ps[1] == L':' && ps[2] == L'\\'))     //absolute
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   173
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                 if (pathlen > max_path - 1) {
785
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   175
                     pathbuf = prefixAbpath(ps, pathlen, pathlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                     pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                     if (pathbuf != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                         wcscpy(pathbuf, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                /* If the path came in as a relative path, need to verify if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                   its absolute form is bigger than max_path or not, if yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                   need to (1)convert it to absolute and (2)prefix. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                   obviously a burden to all relative paths (The current dir/len
785
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   187
                   for "drive & directory" relative path is cached, so we only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                   calculate it once but for "drive-relative path we call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                   _wgetdcwd() and wcslen() everytime), but a hit we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                   to take if we want to support relative path beyond max_path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                   There is no way to predict how long the absolute path will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                   (therefor allocate the sufficient memory block) before calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                   _wfullpath(), we have to get the length of "current" dir first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                WCHAR *abpath = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                int dirlen = currentDirLength(ps, pathlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (dirlen + pathlen + 1 > max_path - 1) {
785
36c29b2692f1 6481955: Uncanonicalized absolute filepath with length 248-260 no longer works (win)
sherman
parents: 47
diff changeset
   198
                    pathbuf = prefixAbpath(ps, pathlen, dirlen + pathlen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    if (pathbuf != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        wcscpy(pathbuf, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    } END_UNICODE_STRING(env, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    if (pathlen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (throwFNFE == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throwFileNotFoundException(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            pathbuf[0] = L'\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    if (pathbuf == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    return pathbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
winFileHandleOpen(JNIEnv *env, jstring path, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    const DWORD access =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        (flags & O_WRONLY) ?  GENERIC_WRITE :
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   230
        (flags & O_RDWR)   ? (GENERIC_READ | GENERIC_WRITE) :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        GENERIC_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    const DWORD sharing =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        FILE_SHARE_READ | FILE_SHARE_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    const DWORD disposition =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        /* Note: O_TRUNC overrides O_CREAT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        (flags & O_TRUNC) ? CREATE_ALWAYS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        (flags & O_CREAT) ? OPEN_ALWAYS   :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        OPEN_EXISTING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    const DWORD  maybeWriteThrough =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        (flags & (O_SYNC | O_DSYNC)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        FILE_FLAG_WRITE_THROUGH :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        FILE_ATTRIBUTE_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    const DWORD maybeDeleteOnClose =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        (flags & O_TEMPORARY) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        FILE_FLAG_DELETE_ON_CLOSE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        FILE_ATTRIBUTE_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    const DWORD flagsAndAttributes = maybeWriteThrough | maybeDeleteOnClose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    HANDLE h = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    if (onNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        WCHAR *pathbuf = pathToNTPath(env, path, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (pathbuf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            /* Exception already pending */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        h = CreateFileW(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            pathbuf,            /* Wide char path name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            access,             /* Read and/or write permission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            sharing,            /* File sharing flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            NULL,               /* Security attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            disposition,        /* creation disposition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            flagsAndAttributes, /* flags and attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        WITH_PLATFORM_STRING(env, path, _ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            h = CreateFile(_ps, access, sharing, NULL, disposition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                           flagsAndAttributes, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } END_PLATFORM_STRING(env, _ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (error == ERROR_TOO_MANY_OPEN_FILES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            JNU_ThrowByName(env, JNU_JAVAIOPKG "IOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            "Too many open files");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throwFileNotFoundException(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    return (jlong) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    jlong h = winFileHandleOpen(env, path, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    if (h >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        SET_FD(this, h, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
/* These are functions that use a handle fd instead of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
   old C style int fd as is used in HPI layer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
handleNonSeekAvailable(jlong, long *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
handleStdinAvailable(jlong, long *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
handleAvailable(jlong fd, jlong *pbytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    DWORD type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    type = GetFileType(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /* Handle is for keyboard or pipe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    if (type == FILE_TYPE_CHAR || type == FILE_TYPE_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        long lpbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        HANDLE stdInHandle = GetStdHandle(STD_INPUT_HANDLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (stdInHandle == h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            ret = handleStdinAvailable(fd, &lpbytes); /* keyboard */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            ret = handleNonSeekAvailable(fd, &lpbytes); /* pipe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        (*pbytes) = (jlong)(lpbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /* Handle is for regular file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    if (type == FILE_TYPE_DISK) {
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   322
        jlong current, end;
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   323
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   324
        LARGE_INTEGER filesize;
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   325
        current = handleLseek(fd, 0, SEEK_CUR);
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   326
        if (current < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   329
        if (GetFileSizeEx(h, &filesize) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   332
        end = long_to_jlong(filesize.QuadPart);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        *pbytes = end - current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
handleNonSeekAvailable(jlong fd, long *pbytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /* This is used for available on non-seekable devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * (like both named and anonymous pipes, such as pipes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *  connected to an exec'd process).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Standard Input is a special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    HANDLE han;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    if ((han = (HANDLE) fd) == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    if (! PeekNamedPipe(han, NULL, 0, NULL, pbytes, NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        /* PeekNamedPipe fails when at EOF.  In that case we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * simply make *pbytes = 0 which is consistent with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         * behavior we get on Solaris when an fd is at EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * The only alternative is to raise and Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * which isn't really warranted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (GetLastError() != ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        *pbytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
handleStdinAvailable(jlong fd, long *pbytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    HANDLE han;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    DWORD numEventsRead = 0;    /* Number of events read from buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    DWORD numEvents = 0;        /* Number of events in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    DWORD i = 0;                /* Loop index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    DWORD curLength = 0;        /* Position marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    DWORD actualLength = 0;     /* Number of bytes readable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    BOOL error = FALSE;         /* Error holder */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    INPUT_RECORD *lpBuffer;     /* Pointer to records of input events */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    DWORD bufferSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    if ((han = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /* Construct an array of input records in the console buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    error = GetNumberOfConsoleInputEvents(han, &numEvents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    if (error == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return handleNonSeekAvailable(fd, pbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /* lpBuffer must fit into 64K or else PeekConsoleInput fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    if (numEvents > MAX_INPUT_EVENTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        numEvents = MAX_INPUT_EVENTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    bufferSize = numEvents * sizeof(INPUT_RECORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    if (bufferSize == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        bufferSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    lpBuffer = malloc(bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    if (lpBuffer == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    error = PeekConsoleInput(han, lpBuffer, numEvents, &numEventsRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    if (error == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        free(lpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /* Examine input records for the number of bytes available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    for(i=0; i<numEvents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (lpBuffer[i].EventType == KEY_EVENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            KEY_EVENT_RECORD *keyRecord = (KEY_EVENT_RECORD *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                          &(lpBuffer[i].Event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (keyRecord->bKeyDown == TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                CHAR *keyPressed = (CHAR *) &(keyRecord->uChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                curLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                if (*keyPressed == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    actualLength = curLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    if(lpBuffer != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        free(lpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    *pbytes = (long) actualLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * This is documented to succeed on read-only files, but Win32's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * FlushFileBuffers functions fails with "access denied" in such a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * case.  So we only signal an error if the error is *not* "access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * denied".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
JNIEXPORT int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
handleSync(jlong fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * From the documentation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *     On Windows NT, the function FlushFileBuffers fails if hFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *     is a handle to console output. That is because console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *     output is not buffered. The function returns FALSE, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *     GetLastError returns ERROR_INVALID_HANDLE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * On the other hand, on Win95, it returns without error.  I cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * assume that 0, 1, and 2 are console, because if someone closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * System.out and then opens a file, they might get file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * 1.  An error on *that* version of 1 should be reported, whereas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * an error on System.out (which was the original 1) should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * ignored.  So I use isatty() to ensure that such an error was due
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * to this bogosity, and if it was, I ignore the error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    HANDLE handle = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    if (!FlushFileBuffers(handle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (GetLastError() != ERROR_ACCESS_DENIED) {    /* from winerror.h */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
handleSetLength(jlong fd, jlong length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    long high = (long)(length >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    DWORD ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    if (h == (HANDLE)(-1)) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    ret = SetFilePointer(h, (long)(length), &high, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if (ret == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    if (SetEndOfFile(h) == FALSE) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
JNIEXPORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
size_t
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
handleRead(jlong fd, void *buf, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    result = ReadFile(h,          /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                      buf,        /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                      len,        /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                      &read,      /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                      NULL);      /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return 0; /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    return read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   505
static size_t writeInternal(jlong fd, const void *buf, jint len, jboolean append)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    if (h != INVALID_HANDLE_VALUE) {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   511
        OVERLAPPED ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   512
        LPOVERLAPPED lpOv;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   513
        if (append == JNI_TRUE) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   514
            ov.Offset = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   515
            ov.OffsetHigh = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   516
            ov.hEvent = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   517
            lpOv = &ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   518
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   519
            lpOv = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   520
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   521
        result = WriteFile(h,                /* File handle to write */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   522
                           buf,              /* pointers to the buffers */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   523
                           len,              /* number of bytes to write */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   524
                           &written,         /* receives number of bytes written */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   525
                           lpOv);            /* overlapped struct */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   530
    return (size_t)written;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   531
}
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   532
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   533
JNIEXPORT
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   534
size_t handleWrite(jlong fd, const void *buf, jint len) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   535
    return writeInternal(fd, buf, len, JNI_FALSE);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   536
}
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   537
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   538
JNIEXPORT
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   539
size_t handleAppend(jlong fd, const void *buf, jint len) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 6885
diff changeset
   540
    return writeInternal(fd, buf, len, JNI_TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
handleClose(JNIEnv *env, jobject this, jfieldID fid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    FD fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   549
    if (h == INVALID_HANDLE_VALUE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /* Set the fd to -1 before closing it so that the timing window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * of other threads using the wrong fd (closed but recycled fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * that gets re-opened with some other filename) is reduced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Practically the chance of its occurance is low, however, we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * taking extra precaution over here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    SET_FD(this, -1, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    if (CloseHandle(h) == 0) { /* Returns zero on failure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        JNU_ThrowIOExceptionWithLastError(env, "close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
handleLseek(jlong fd, jlong offset, jint whence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
{
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   570
    LARGE_INTEGER pos, distance;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    DWORD lowPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    DWORD op = FILE_CURRENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    if (whence == SEEK_END) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        op = FILE_END;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    if (whence == SEEK_CUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        op = FILE_CURRENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    if (whence == SEEK_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        op = FILE_BEGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   586
    distance.QuadPart = offset;
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   587
    if (SetFilePointerEx(h, distance, &pos, op) == 0) {
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   588
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents: 7515
diff changeset
   590
    return long_to_jlong(pos.QuadPart);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
}