src/windows/native/java/io/io_util_md.c

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 206,216 **** return NULL; } return pathbuf; } ! jlong winFileHandleOpen(JNIEnv *env, jstring path, int flags) { const DWORD access = (flags & O_WRONLY) ? GENERIC_WRITE : (flags & O_RDWR) ? (GENERIC_READ | GENERIC_WRITE) : --- 206,216 ---- return NULL; } return pathbuf; } ! FD winFileHandleOpen(JNIEnv *env, jstring path, int flags) { const DWORD access = (flags & O_WRONLY) ? GENERIC_WRITE : (flags & O_RDWR) ? (GENERIC_READ | GENERIC_WRITE) :
*** 262,287 **** } void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags) { ! jlong h = winFileHandleOpen(env, path, flags); if (h >= 0) { SET_FD(this, h, fid); } } /* These are functions that use a handle fd instead of the old C style int fd as is used in HPI layer */ static int ! handleNonSeekAvailable(jlong, long *); static int ! handleStdinAvailable(jlong, long *); int ! handleAvailable(jlong fd, jlong *pbytes) { HANDLE h = (HANDLE)fd; DWORD type = 0; type = GetFileType(h); /* Handle is for keyboard or pipe */ --- 262,287 ---- } void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags) { ! FD h = winFileHandleOpen(env, path, flags); if (h >= 0) { SET_FD(this, h, fid); } } /* These are functions that use a handle fd instead of the old C style int fd as is used in HPI layer */ static int ! handleNonSeekAvailable(FD, long *); static int ! handleStdinAvailable(FD, long *); int ! handleAvailable(FD fd, jlong *pbytes) { HANDLE h = (HANDLE)fd; DWORD type = 0; type = GetFileType(h); /* Handle is for keyboard or pipe */
*** 315,325 **** } return FALSE; } static int ! handleNonSeekAvailable(jlong fd, long *pbytes) { /* This is used for available on non-seekable devices * (like both named and anonymous pipes, such as pipes * connected to an exec'd process). * Standard Input is a special case. * --- 315,325 ---- } return FALSE; } static int ! handleNonSeekAvailable(FD fd, long *pbytes) { /* This is used for available on non-seekable devices * (like both named and anonymous pipes, such as pipes * connected to an exec'd process). * Standard Input is a special case. *
*** 344,354 **** } return TRUE; } static int ! handleStdinAvailable(jlong fd, long *pbytes) { HANDLE han; DWORD numEventsRead = 0; /* Number of events read from buffer */ DWORD numEvents = 0; /* Number of events in buffer */ DWORD i = 0; /* Loop index */ DWORD curLength = 0; /* Position marker */ --- 344,354 ---- } return TRUE; } static int ! handleStdinAvailable(FD fd, long *pbytes) { HANDLE han; DWORD numEventsRead = 0; /* Number of events read from buffer */ DWORD numEvents = 0; /* Number of events in buffer */ DWORD i = 0; /* Loop index */ DWORD curLength = 0; /* Position marker */
*** 410,421 **** * FlushFileBuffers functions fails with "access denied" in such a * case. So we only signal an error if the error is *not* "access * denied". */ ! JNIEXPORT int ! handleSync(jlong fd) { /* * From the documentation: * * On Windows NT, the function FlushFileBuffers fails if hFile * is a handle to console output. That is because console --- 410,421 ---- * FlushFileBuffers functions fails with "access denied" in such a * case. So we only signal an error if the error is *not* "access * denied". */ ! int ! handleSync(FD fd) { /* * From the documentation: * * On Windows NT, the function FlushFileBuffers fails if hFile * is a handle to console output. That is because console
*** 441,451 **** return 0; } int ! handleSetLength(jlong fd, jlong length) { HANDLE h = (HANDLE)fd; long high = (long)(length >> 32); DWORD ret; if (h == (HANDLE)(-1)) return -1; --- 441,451 ---- return 0; } int ! handleSetLength(FD fd, jlong length) { HANDLE h = (HANDLE)fd; long high = (long)(length >> 32); DWORD ret; if (h == (HANDLE)(-1)) return -1;
*** 457,467 **** return 0; } JNIEXPORT jint ! handleRead(jlong fd, void *buf, jint len) { DWORD read = 0; BOOL result = 0; HANDLE h = (HANDLE)fd; if (h == INVALID_HANDLE_VALUE) { --- 457,467 ---- return 0; } JNIEXPORT jint ! handleRead(FD fd, void *buf, jint len) { DWORD read = 0; BOOL result = 0; HANDLE h = (HANDLE)fd; if (h == INVALID_HANDLE_VALUE) {
*** 480,490 **** return -1; } return (jint)read; } ! static jint writeInternal(jlong fd, const void *buf, jint len, jboolean append) { BOOL result = 0; DWORD written = 0; HANDLE h = (HANDLE)fd; if (h != INVALID_HANDLE_VALUE) { --- 480,490 ---- return -1; } return (jint)read; } ! static jint writeInternal(FD fd, const void *buf, jint len, jboolean append) { BOOL result = 0; DWORD written = 0; HANDLE h = (HANDLE)fd; if (h != INVALID_HANDLE_VALUE) {
*** 508,524 **** return -1; } return (jint)written; } ! JNIEXPORT ! jint handleWrite(jlong fd, const void *buf, jint len) { return writeInternal(fd, buf, len, JNI_FALSE); } ! JNIEXPORT ! jint handleAppend(jlong fd, const void *buf, jint len) { return writeInternal(fd, buf, len, JNI_TRUE); } jint handleClose(JNIEnv *env, jobject this, jfieldID fid) --- 508,522 ---- return -1; } return (jint)written; } ! jint handleWrite(FD fd, const void *buf, jint len) { return writeInternal(fd, buf, len, JNI_FALSE); } ! jint handleAppend(FD fd, const void *buf, jint len) { return writeInternal(fd, buf, len, JNI_TRUE); } jint handleClose(JNIEnv *env, jobject this, jfieldID fid)
*** 543,553 **** } return 0; } jlong ! handleLseek(jlong fd, jlong offset, jint whence) { LARGE_INTEGER pos, distance; DWORD lowPos = 0; long highPos = 0; DWORD op = FILE_CURRENT; --- 541,551 ---- } return 0; } jlong ! handleLseek(FD fd, jlong offset, jint whence) { LARGE_INTEGER pos, distance; DWORD lowPos = 0; long highPos = 0; DWORD op = FILE_CURRENT;
*** 567,571 **** --- 565,610 ---- if (SetFilePointerEx(h, distance, &pos, op) == 0) { return -1; } return long_to_jlong(pos.QuadPart); } + + size_t + getLastErrorString(char *buf, size_t len) + { + DWORD errval; + if (len > 0) { + if ((errval = GetLastError()) != 0) { + // DOS error + size_t n = (size_t)FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + errval, + 0, + buf, + (DWORD)len, + NULL); + if (n > 3) { + // Drop final '.', CR, LF + if (buf[n - 1] == '\n') n--; + if (buf[n - 1] == '\r') n--; + if (buf[n - 1] == '.') n--; + buf[n] = '\0'; + } + return n; + } + + if (errno != 0) { + // C runtime error that has no corresponding DOS error code + const char *err = strerror(errno); + size_t n = strlen(err); + if (n >= len) + n = len - 1; + + strncpy(buf, err, n); + buf[n] = '\0'; + return n; + } + } + + return 0; + }