1 /*
2  * Copyright (c) 2003, 2019, 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
23  * questions.
24  */
25 
26 #include "jni.h"
27 #include "jni_util.h"
28 
29 /*
30  * Macros to use the right data type for file descriptors
31  */
32 #define FD jint
33 
34 /*
35  * Prototypes for functions in io_util_md.c called from io_util.c,
36  * FileDescriptor.c, FileInputStream.c, FileOutputStream.c,
37  * UnixFileSystem_md.c
38  */
39 ssize_t handleWrite(FD fd, const void *buf, jint len);
40 ssize_t handleRead(FD fd, void *buf, jint len);
41 jint handleAvailable(FD fd, jlong *pbytes);
42 jint handleSetLength(FD fd, jlong length);
43 jlong handleGetLength(FD fd);
44 FD handleOpen(const char *path, int oflag, int mode);
45 
46 /*
47  * Functions to get fd from the java.io.FileDescriptor field
48  * of an object.  These functions rely on having an appropriately
49  * defined object with a FileDescriptor object at the fid offset.
50  * If the FD object is null, return -1 to avoid crashing VM.
51  */
52 FD getFD(JNIEnv *env, jobject cur, jfieldID fid);
53 
54 /*
55  * Macros to set/get fd when inside java.io.FileDescriptor
56  */
57 #define THIS_FD(obj) (*env)->GetIntField(env, obj, IO_fd_fdID)
58 
59 /*
60  * Route the routines
61  */
62 #define IO_Sync fsync
63 #define IO_Read handleRead
64 #define IO_Write handleWrite
65 #define IO_Append handleWrite
66 #define IO_Available handleAvailable
67 #define IO_SetLength handleSetLength
68 #define IO_GetLength handleGetLength
69 
70 #ifdef _ALLBSD_SOURCE
71 #define open64 open
72 #define fstat64 fstat
73 #define stat64 stat
74 #define lseek64 lseek
75 #define ftruncate64 ftruncate
76 #define IO_Lseek lseek
77 #else
78 #define IO_Lseek lseek64
79 #endif
80 
81 /*
82  * On Solaris, the handle field is unused
83  */
84 #define SET_HANDLE(fd) return (jlong)-1
85 
86 /*
87  * Retry the operation if it is interrupted
88  */
89 #define RESTARTABLE(_cmd, _result)                \
90     do {                                          \
91         _result = _cmd;                           \
92     } while ((_result == -1) && (errno == EINTR))
93 
94 void fileDescriptorClose(JNIEnv *env, jobject this);
95 
96 #ifdef MACOSX
97 jstring newStringPlatform(JNIEnv *env, const char* str);
98 #endif
99