Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/java/io/io_util_md.c
32287 views
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
23
* questions.
24
*/
25
26
#if defined(__linux__)
27
#define _FILE_OFFSET_BITS 64
28
#endif
29
30
#include "jni.h"
31
#include "jni_util.h"
32
#include "jvm.h"
33
#include "io_util.h"
34
#include "io_util_md.h"
35
#include <string.h>
36
#include <unistd.h>
37
38
#ifdef __solaris__
39
#include <sys/filio.h>
40
#endif
41
42
#if defined(__linux__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
43
#include <sys/ioctl.h>
44
#endif
45
46
#ifdef __ANDROID__
47
#define open64 open
48
#endif
49
50
#ifdef MACOSX
51
52
#include <CoreFoundation/CoreFoundation.h>
53
54
__private_extern__
55
jstring newStringPlatform(JNIEnv *env, const char* str)
56
{
57
jstring rv = NULL;
58
CFMutableStringRef csref = CFStringCreateMutable(NULL, 0);
59
if (csref == NULL) {
60
JNU_ThrowOutOfMemoryError(env, "native heap");
61
} else {
62
CFStringAppendCString(csref, str, kCFStringEncodingUTF8);
63
CFStringNormalize(csref, kCFStringNormalizationFormC);
64
int clen = CFStringGetLength(csref);
65
int ulen = (clen + 1) * 2; // utf16 + zero padding
66
char* chars = malloc(ulen);
67
if (chars == NULL) {
68
CFRelease(csref);
69
JNU_ThrowOutOfMemoryError(env, "native heap");
70
} else {
71
if (CFStringGetCString(csref, chars, ulen, kCFStringEncodingUTF16)) {
72
rv = (*env)->NewString(env, (jchar*)chars, clen);
73
}
74
free(chars);
75
CFRelease(csref);
76
}
77
}
78
return rv;
79
}
80
#endif
81
82
FD
83
handleOpen(const char *path, int oflag, int mode) {
84
FD fd;
85
RESTARTABLE(open64(path, oflag, mode), fd);
86
if (fd != -1) {
87
struct stat64 buf64;
88
int result;
89
RESTARTABLE(fstat64(fd, &buf64), result);
90
if (result != -1) {
91
if (S_ISDIR(buf64.st_mode)) {
92
close(fd);
93
errno = EISDIR;
94
fd = -1;
95
}
96
} else {
97
close(fd);
98
fd = -1;
99
}
100
}
101
return fd;
102
}
103
104
void
105
fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
106
{
107
WITH_PLATFORM_STRING(env, path, ps) {
108
FD fd;
109
110
#if defined(__linux__) || defined(_ALLBSD_SOURCE)
111
/* Remove trailing slashes, since the kernel won't */
112
char *p = (char *)ps + strlen(ps) - 1;
113
while ((p > ps) && (*p == '/'))
114
*p-- = '\0';
115
#endif
116
fd = handleOpen(ps, flags, 0666);
117
if (fd != -1) {
118
SET_FD(this, fd, fid);
119
} else {
120
throwFileNotFoundException(env, path);
121
}
122
} END_PLATFORM_STRING(env, ps);
123
}
124
125
void
126
fileClose(JNIEnv *env, jobject this, jfieldID fid)
127
{
128
FD fd = GET_FD(this, fid);
129
if (fd == -1) {
130
return;
131
}
132
133
/* Set the fd to -1 before closing it so that the timing window
134
* of other threads using the wrong fd (closed but recycled fd,
135
* that gets re-opened with some other filename) is reduced.
136
* Practically the chance of its occurance is low, however, we are
137
* taking extra precaution over here.
138
*/
139
SET_FD(this, -1, fid);
140
141
/*
142
* Don't close file descriptors 0, 1, or 2. If we close these stream
143
* then a subsequent file open or socket will use them. Instead we
144
* just redirect these file descriptors to /dev/null.
145
*/
146
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
147
int devnull = open("/dev/null", O_WRONLY);
148
if (devnull < 0) {
149
SET_FD(this, fd, fid); // restore fd
150
JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
151
} else {
152
dup2(devnull, fd);
153
close(devnull);
154
}
155
} else if (close(fd) == -1) {
156
JNU_ThrowIOExceptionWithLastError(env, "close failed");
157
}
158
}
159
160
ssize_t
161
handleRead(FD fd, void *buf, jint len)
162
{
163
ssize_t result;
164
RESTARTABLE(read(fd, buf, len), result);
165
return result;
166
}
167
168
ssize_t
169
handleWrite(FD fd, const void *buf, jint len)
170
{
171
ssize_t result;
172
RESTARTABLE(write(fd, buf, len), result);
173
return result;
174
}
175
176
jint
177
handleAvailable(FD fd, jlong *pbytes)
178
{
179
int mode;
180
struct stat64 buf64;
181
jlong size = -1, current = -1;
182
183
int result;
184
RESTARTABLE(fstat64(fd, &buf64), result);
185
if (result != -1) {
186
mode = buf64.st_mode;
187
if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
188
int n;
189
int result;
190
RESTARTABLE(ioctl(fd, FIONREAD, &n), result);
191
if (result >= 0) {
192
*pbytes = n;
193
return 1;
194
}
195
} else if (S_ISREG(mode)) {
196
size = buf64.st_size;
197
}
198
}
199
200
if ((current = lseek64(fd, 0, SEEK_CUR)) == -1) {
201
return 0;
202
}
203
204
if (size < current) {
205
if ((size = lseek64(fd, 0, SEEK_END)) == -1)
206
return 0;
207
else if (lseek64(fd, current, SEEK_SET) == -1)
208
return 0;
209
}
210
211
*pbytes = size - current;
212
return 1;
213
}
214
215
jint
216
handleSetLength(FD fd, jlong length)
217
{
218
int result;
219
RESTARTABLE(ftruncate64(fd, length), result);
220
return result;
221
}
222
223
size_t
224
getLastErrorString(char *buf, size_t len)
225
{
226
if (errno == 0 || len < 1) return 0;
227
getErrorString(errno, buf, len);
228
return strlen(buf);
229
}
230
231