jdk/src/windows/native/java/io/io_util_md.c
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 47 c8f0e41aea68
child 798 75b3a644ef91
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 47
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/* We cache the length of current working dir here to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   calling _wgetcwd() every time we need to resolve a relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   path. This piece of code needs to be revisited if chdir
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   makes its way into java runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
currentDirLength(const WCHAR* ps, int pathlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    WCHAR *dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (pathlen > 2 && ps[1] == L':' && ps[2] != L'\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        //drive-relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        WCHAR d = ps[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int dirlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int di = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if ((d >= L'a') && (d <= L'z')) di = d - L'a' + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        else if ((d >= L'A') && (d <= L'Z')) di = d - L'A' + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        else return 0; /* invalid drive name. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        dir = _wgetdcwd(di, NULL, MAX_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (dir != NULL){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            dirlen = wcslen(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            free(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return dirlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        static int curDirLenCached = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        //relative to both drive and directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (curDirLenCached == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            int dirlen = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            dir = _wgetcwd(NULL, MAX_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (dir != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                curDirLenCached = wcslen(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                free(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return curDirLenCached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
/* If this returns NULL then an exception is pending */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
WCHAR*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
pathToNTPath(JNIEnv *env, jstring path, jboolean throwFNFE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    int pathlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    WCHAR *pathbuf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    int max_path = 248;   /* Since CreateDirectoryW() has the limit of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                             248 instead of the normal MAX_PATH, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                             use 248 as the max_path to satisfy both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    WITH_UNICODE_STRING(env, path, ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        pathlen = wcslen(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (pathlen != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (pathlen > 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                (ps[0] == L'\\' && ps[1] == L'\\' ||   //UNC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                 ps[1] == L':' && ps[2] == L'\\')) {   //absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                 if (pathlen > max_path - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                     pathbuf = getPrefixed(ps, pathlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                     pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                     if (pathbuf != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                         wcscpy(pathbuf, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                /* If the path came in as a relative path, need to verify if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                   its absolute form is bigger than max_path or not, if yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                   need to (1)convert it to absolute and (2)prefix. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                   obviously a burden to all relative paths (The current dir/len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                   for "dirve & directory" relative path is cached, so we only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                   calculate it once but for "drive-relative path we call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                   _wgetdcwd() and wcslen() everytime), but a hit we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                   to take if we want to support relative path beyond max_path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                   There is no way to predict how long the absolute path will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                   (therefor allocate the sufficient memory block) before calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                   _wfullpath(), we have to get the length of "current" dir first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                WCHAR *abpath = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                int dirlen = currentDirLength(ps, pathlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                if (dirlen + pathlen + 1 > max_path - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    int abpathlen = dirlen + pathlen + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    abpath = (WCHAR*)malloc(abpathlen * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    if (abpath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        if (_wfullpath(abpath, ps, abpathlen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                            pathbuf = getPrefixed(abpath, abpathlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                            /* _wfullpath fails if the pathlength exceeds 32k wchar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                               Instead of doing more fancy things we simply copy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                               ps into the return buffer, the subsequent win32 API will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                               probably fail with FileNotFoundException, which is expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            if (pathbuf != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                wcscpy(pathbuf, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        free(abpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    pathbuf = (WCHAR*)malloc((pathlen + 6) * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    if (pathbuf != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        wcscpy(pathbuf, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    } END_UNICODE_STRING(env, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    if (pathlen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (throwFNFE == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throwFileNotFoundException(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            pathbuf[0] = L'\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    if (pathbuf == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    return pathbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
winFileHandleOpen(JNIEnv *env, jstring path, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
{
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   193
    /* To implement O_APPEND, we use the strategy from
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   194
       http://msdn2.microsoft.com/en-us/library/aa363858.aspx
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   195
       "You can get atomic append by opening a file with
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   196
       FILE_APPEND_DATA access and _without_ FILE_WRITE_DATA access.
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   197
       If you do this then all writes will ignore the current file
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   198
       pointer and be done at the end-of file." */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    const DWORD access =
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   200
        (flags & O_APPEND) ? (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA) :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        (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
   202
        (flags & O_RDWR)   ? (GENERIC_READ | GENERIC_WRITE) :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        GENERIC_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    const DWORD sharing =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        FILE_SHARE_READ | FILE_SHARE_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    const DWORD disposition =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /* Note: O_TRUNC overrides O_CREAT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        (flags & O_TRUNC) ? CREATE_ALWAYS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        (flags & O_CREAT) ? OPEN_ALWAYS   :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        OPEN_EXISTING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    const DWORD  maybeWriteThrough =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        (flags & (O_SYNC | O_DSYNC)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        FILE_FLAG_WRITE_THROUGH :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        FILE_ATTRIBUTE_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    const DWORD maybeDeleteOnClose =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        (flags & O_TEMPORARY) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        FILE_FLAG_DELETE_ON_CLOSE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        FILE_ATTRIBUTE_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    const DWORD flagsAndAttributes = maybeWriteThrough | maybeDeleteOnClose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    HANDLE h = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    if (onNT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        WCHAR *pathbuf = pathToNTPath(env, path, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (pathbuf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            /* Exception already pending */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        h = CreateFileW(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            pathbuf,            /* Wide char path name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            access,             /* Read and/or write permission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            sharing,            /* File sharing flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            NULL,               /* Security attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            disposition,        /* creation disposition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            flagsAndAttributes, /* flags and attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        free(pathbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        WITH_PLATFORM_STRING(env, path, _ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            h = CreateFile(_ps, access, sharing, NULL, disposition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                           flagsAndAttributes, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } END_PLATFORM_STRING(env, _ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (error == ERROR_TOO_MANY_OPEN_FILES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            JNU_ThrowByName(env, JNU_JAVAIOPKG "IOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            "Too many open files");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        throwFileNotFoundException(env, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    return (jlong) h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    jlong h = winFileHandleOpen(env, path, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if (h >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        SET_FD(this, h, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
/* These are functions that use a handle fd instead of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
   old C style int fd as is used in HPI layer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
handleNonSeekAvailable(jlong, long *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
handleStdinAvailable(jlong, long *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
handleAvailable(jlong fd, jlong *pbytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    jlong current, end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    DWORD type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    type = GetFileType(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /* Handle is for keyboard or pipe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    if (type == FILE_TYPE_CHAR || type == FILE_TYPE_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        long lpbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        HANDLE stdInHandle = GetStdHandle(STD_INPUT_HANDLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (stdInHandle == h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            ret = handleStdinAvailable(fd, &lpbytes); /* keyboard */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            ret = handleNonSeekAvailable(fd, &lpbytes); /* pipe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        (*pbytes) = (jlong)(lpbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /* Handle is for regular file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    if (type == FILE_TYPE_DISK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        DWORD sizeLow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        DWORD sizeHigh = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        DWORD lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        current = (((jlong)highPos) << 32) | lowPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        end = GetFileSize(h, &sizeHigh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (sizeLow == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        *pbytes = end - current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
handleNonSeekAvailable(jlong fd, long *pbytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /* This is used for available on non-seekable devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * (like both named and anonymous pipes, such as pipes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *  connected to an exec'd process).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Standard Input is a special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    HANDLE han;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    if ((han = (HANDLE) fd) == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    if (! PeekNamedPipe(han, NULL, 0, NULL, pbytes, NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        /* PeekNamedPipe fails when at EOF.  In that case we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * simply make *pbytes = 0 which is consistent with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * behavior we get on Solaris when an fd is at EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * The only alternative is to raise and Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * which isn't really warranted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (GetLastError() != ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        *pbytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
handleStdinAvailable(jlong fd, long *pbytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    HANDLE han;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    DWORD numEventsRead = 0;    /* Number of events read from buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    DWORD numEvents = 0;        /* Number of events in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    DWORD i = 0;                /* Loop index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    DWORD curLength = 0;        /* Position marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    DWORD actualLength = 0;     /* Number of bytes readable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    BOOL error = FALSE;         /* Error holder */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    INPUT_RECORD *lpBuffer;     /* Pointer to records of input events */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    DWORD bufferSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    if ((han = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /* Construct an array of input records in the console buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    error = GetNumberOfConsoleInputEvents(han, &numEvents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    if (error == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return handleNonSeekAvailable(fd, pbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /* lpBuffer must fit into 64K or else PeekConsoleInput fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    if (numEvents > MAX_INPUT_EVENTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        numEvents = MAX_INPUT_EVENTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    bufferSize = numEvents * sizeof(INPUT_RECORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    if (bufferSize == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        bufferSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    lpBuffer = malloc(bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    if (lpBuffer == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    error = PeekConsoleInput(han, lpBuffer, numEvents, &numEventsRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    if (error == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        free(lpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /* Examine input records for the number of bytes available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    for(i=0; i<numEvents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (lpBuffer[i].EventType == KEY_EVENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            KEY_EVENT_RECORD *keyRecord = (KEY_EVENT_RECORD *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                          &(lpBuffer[i].Event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (keyRecord->bKeyDown == TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                CHAR *keyPressed = (CHAR *) &(keyRecord->uChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                curLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                if (*keyPressed == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    actualLength = curLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    if(lpBuffer != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        free(lpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    *pbytes = (long) actualLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
 * This is documented to succeed on read-only files, but Win32's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
 * FlushFileBuffers functions fails with "access denied" in such a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
 * case.  So we only signal an error if the error is *not* "access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
 * denied".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
JNIEXPORT int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
handleSync(jlong fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * From the documentation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *     On Windows NT, the function FlushFileBuffers fails if hFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *     is a handle to console output. That is because console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *     output is not buffered. The function returns FALSE, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *     GetLastError returns ERROR_INVALID_HANDLE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * On the other hand, on Win95, it returns without error.  I cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * assume that 0, 1, and 2 are console, because if someone closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * System.out and then opens a file, they might get file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * 1.  An error on *that* version of 1 should be reported, whereas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * an error on System.out (which was the original 1) should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * ignored.  So I use isatty() to ensure that such an error was due
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * to this bogosity, and if it was, I ignore the error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    HANDLE handle = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    if (!FlushFileBuffers(handle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (GetLastError() != ERROR_ACCESS_DENIED) {    /* from winerror.h */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
handleSetLength(jlong fd, jlong length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    long high = (long)(length >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    DWORD ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    if (h == (HANDLE)(-1)) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    ret = SetFilePointer(h, (long)(length), &high, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    if (ret == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    if (SetEndOfFile(h) == FALSE) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
JNIEXPORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
size_t
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
handleRead(jlong fd, void *buf, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    result = ReadFile(h,          /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                      buf,        /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                      len,        /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                      &read,      /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                      NULL);      /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return 0; /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
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
    return read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
JNIEXPORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
size_t
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
handleWrite(jlong fd, const void *buf, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        result = WriteFile(h,           /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                      buf,              /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                      len,              /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                      &written,         /* receives number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                      NULL);            /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    return written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
handleClose(JNIEnv *env, jobject this, jfieldID fid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    FD fd = GET_FD(this, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 45
diff changeset
   505
    if (h == INVALID_HANDLE_VALUE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /* Set the fd to -1 before closing it so that the timing window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * of other threads using the wrong fd (closed but recycled fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * that gets re-opened with some other filename) is reduced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Practically the chance of its occurance is low, however, we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * taking extra precaution over here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    SET_FD(this, -1, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    if (CloseHandle(h) == 0) { /* Returns zero on failure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        SET_FD(this, fd, fid); // restore fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        JNU_ThrowIOExceptionWithLastError(env, "close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
handleLseek(jlong fd, jlong offset, jint whence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    DWORD lowPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    DWORD op = FILE_CURRENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    if (whence == SEEK_END) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        op = FILE_END;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    if (whence == SEEK_CUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        op = FILE_CURRENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    if (whence == SEEK_SET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        op = FILE_BEGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    lowPos = (DWORD)offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    highPos = (long)(offset >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    lowPos = SetFilePointer(h, lowPos, &highPos, op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    return (((jlong)highPos) << 32) | lowPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
}