1 /*
2 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
206 throwFileNotFoundException(env, path);
207 }
208 return NULL;
209 } else {
210 pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
211 if (pathbuf != NULL) {
212 pathbuf[0] = L'\0';
213 } else {
214 JNU_ThrowOutOfMemoryError(env, 0);
215 return NULL;
216 }
217 }
218 }
219 if (pathbuf == 0) {
220 JNU_ThrowOutOfMemoryError(env, 0);
221 return NULL;
222 }
223 return pathbuf;
224 }
225
226 FD
227 winFileHandleOpen(JNIEnv *env, jstring path, int flags)
228 {
229 const DWORD access =
230 (flags & O_WRONLY) ? GENERIC_WRITE :
231 (flags & O_RDWR) ? (GENERIC_READ | GENERIC_WRITE) :
232 GENERIC_READ;
233 const DWORD sharing =
234 FILE_SHARE_READ | FILE_SHARE_WRITE;
235 const DWORD disposition =
236 /* Note: O_TRUNC overrides O_CREAT */
237 (flags & O_TRUNC) ? CREATE_ALWAYS :
238 (flags & O_CREAT) ? OPEN_ALWAYS :
239 OPEN_EXISTING;
240 const DWORD maybeWriteThrough =
241 (flags & (O_SYNC | O_DSYNC)) ?
242 FILE_FLAG_WRITE_THROUGH :
243 FILE_ATTRIBUTE_NORMAL;
244 const DWORD maybeDeleteOnClose =
245 (flags & O_TEMPORARY) ?
246 FILE_FLAG_DELETE_ON_CLOSE :
558 if (h == INVALID_HANDLE_VALUE) {
559 return;
560 }
561
562 /* Set the fd to -1 before closing it so that the timing window
563 * of other threads using the wrong fd (closed but recycled fd,
564 * that gets re-opened with some other filename) is reduced.
565 * Practically the chance of its occurance is low, however, we are
566 * taking extra precaution over here.
567 */
568 (*env)->SetLongField(env, this, IO_handle_fdID, -1);
569 if ((*env)->ExceptionOccurred(env)) {
570 return;
571 }
572
573 if (CloseHandle(h) == 0) { /* Returns zero on failure */
574 JNU_ThrowIOExceptionWithLastError(env, "close failed");
575 }
576 }
577
578 jlong
579 handleLseek(FD fd, jlong offset, jint whence)
580 {
581 LARGE_INTEGER pos, distance;
582 DWORD lowPos = 0;
583 long highPos = 0;
584 DWORD op = FILE_CURRENT;
585 HANDLE h = (HANDLE)fd;
586
587 if (whence == SEEK_END) {
588 op = FILE_END;
589 }
590 if (whence == SEEK_CUR) {
591 op = FILE_CURRENT;
592 }
593 if (whence == SEEK_SET) {
594 op = FILE_BEGIN;
595 }
596
597 distance.QuadPart = offset;
598 if (SetFilePointerEx(h, distance, &pos, op) == 0) {
|
1 /*
2 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
206 throwFileNotFoundException(env, path);
207 }
208 return NULL;
209 } else {
210 pathbuf = (WCHAR*)malloc(sizeof(WCHAR));
211 if (pathbuf != NULL) {
212 pathbuf[0] = L'\0';
213 } else {
214 JNU_ThrowOutOfMemoryError(env, 0);
215 return NULL;
216 }
217 }
218 }
219 if (pathbuf == 0) {
220 JNU_ThrowOutOfMemoryError(env, 0);
221 return NULL;
222 }
223 return pathbuf;
224 }
225
226 JNIEXPORT FD JNICALL
227 winFileHandleOpen(JNIEnv *env, jstring path, int flags)
228 {
229 const DWORD access =
230 (flags & O_WRONLY) ? GENERIC_WRITE :
231 (flags & O_RDWR) ? (GENERIC_READ | GENERIC_WRITE) :
232 GENERIC_READ;
233 const DWORD sharing =
234 FILE_SHARE_READ | FILE_SHARE_WRITE;
235 const DWORD disposition =
236 /* Note: O_TRUNC overrides O_CREAT */
237 (flags & O_TRUNC) ? CREATE_ALWAYS :
238 (flags & O_CREAT) ? OPEN_ALWAYS :
239 OPEN_EXISTING;
240 const DWORD maybeWriteThrough =
241 (flags & (O_SYNC | O_DSYNC)) ?
242 FILE_FLAG_WRITE_THROUGH :
243 FILE_ATTRIBUTE_NORMAL;
244 const DWORD maybeDeleteOnClose =
245 (flags & O_TEMPORARY) ?
246 FILE_FLAG_DELETE_ON_CLOSE :
558 if (h == INVALID_HANDLE_VALUE) {
559 return;
560 }
561
562 /* Set the fd to -1 before closing it so that the timing window
563 * of other threads using the wrong fd (closed but recycled fd,
564 * that gets re-opened with some other filename) is reduced.
565 * Practically the chance of its occurance is low, however, we are
566 * taking extra precaution over here.
567 */
568 (*env)->SetLongField(env, this, IO_handle_fdID, -1);
569 if ((*env)->ExceptionOccurred(env)) {
570 return;
571 }
572
573 if (CloseHandle(h) == 0) { /* Returns zero on failure */
574 JNU_ThrowIOExceptionWithLastError(env, "close failed");
575 }
576 }
577
578 JNIEXPORT jlong JNICALL
579 handleLseek(FD fd, jlong offset, jint whence)
580 {
581 LARGE_INTEGER pos, distance;
582 DWORD lowPos = 0;
583 long highPos = 0;
584 DWORD op = FILE_CURRENT;
585 HANDLE h = (HANDLE)fd;
586
587 if (whence == SEEK_END) {
588 op = FILE_END;
589 }
590 if (whence == SEEK_CUR) {
591 op = FILE_CURRENT;
592 }
593 if (whence == SEEK_SET) {
594 op = FILE_BEGIN;
595 }
596
597 distance.QuadPart = offset;
598 if (SetFilePointerEx(h, distance, &pos, op) == 0) {
|