Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/FileDispatcherImpl.c
32288 views
1
/*
2
* Copyright (c) 2000, 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
#include <windows.h>
27
#include "jni.h"
28
#include "jni_util.h"
29
#include "jvm.h"
30
#include "jlong.h"
31
#include "sun_nio_ch_FileDispatcherImpl.h"
32
#include <io.h>
33
#include "nio.h"
34
#include "nio_util.h"
35
#include "jlong.h"
36
37
38
/**************************************************************
39
* FileDispatcherImpl.c
40
*/
41
42
JNIEXPORT jint JNICALL
43
Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz, jobject fdo,
44
jlong address, jint len)
45
{
46
DWORD read = 0;
47
BOOL result = 0;
48
HANDLE h = (HANDLE)(handleval(env, fdo));
49
50
if (h == INVALID_HANDLE_VALUE) {
51
JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
52
return IOS_THROWN;
53
}
54
result = ReadFile(h, /* File handle to read */
55
(LPVOID)address, /* address to put data */
56
len, /* number of bytes to read */
57
&read, /* number of bytes read */
58
NULL); /* no overlapped struct */
59
if (result == 0) {
60
int error = GetLastError();
61
if (error == ERROR_BROKEN_PIPE) {
62
return IOS_EOF;
63
}
64
if (error == ERROR_NO_DATA) {
65
return IOS_UNAVAILABLE;
66
}
67
JNU_ThrowIOExceptionWithLastError(env, "Read failed");
68
return IOS_THROWN;
69
}
70
return convertReturnVal(env, (jint)read, JNI_TRUE);
71
}
72
73
JNIEXPORT jlong JNICALL
74
Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz, jobject fdo,
75
jlong address, jint len)
76
{
77
DWORD read = 0;
78
BOOL result = 0;
79
jlong totalRead = 0;
80
LPVOID loc;
81
int i = 0;
82
DWORD num = 0;
83
struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
84
HANDLE h = (HANDLE)(handleval(env, fdo));
85
86
if (h == INVALID_HANDLE_VALUE) {
87
JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
88
return IOS_THROWN;
89
}
90
91
for(i=0; i<len; i++) {
92
loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
93
num = iovecp[i].iov_len;
94
result = ReadFile(h, /* File handle to read */
95
loc, /* address to put data */
96
num, /* number of bytes to read */
97
&read, /* number of bytes read */
98
NULL); /* no overlapped struct */
99
if (read > 0) {
100
totalRead += read;
101
}
102
if (read < num) {
103
break;
104
}
105
}
106
107
if (result == 0) {
108
int error = GetLastError();
109
if (error == ERROR_BROKEN_PIPE) {
110
return IOS_EOF;
111
}
112
if (error == ERROR_NO_DATA) {
113
return IOS_UNAVAILABLE;
114
}
115
JNU_ThrowIOExceptionWithLastError(env, "Read failed");
116
return IOS_THROWN;
117
}
118
119
return convertLongReturnVal(env, totalRead, JNI_TRUE);
120
}
121
122
JNIEXPORT jint JNICALL
123
Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo,
124
jlong address, jint len, jlong offset)
125
{
126
DWORD read = 0;
127
BOOL result = 0;
128
HANDLE h = (HANDLE)(handleval(env, fdo));
129
DWORD lowPos = 0;
130
long highPos = 0;
131
DWORD lowOffset = 0;
132
long highOffset = 0;
133
134
if (h == INVALID_HANDLE_VALUE) {
135
JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
136
return IOS_THROWN;
137
}
138
139
lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
140
if (lowPos == ((DWORD)-1)) {
141
if (GetLastError() != ERROR_SUCCESS) {
142
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
143
return IOS_THROWN;
144
}
145
}
146
147
lowOffset = (DWORD)offset;
148
highOffset = (DWORD)(offset >> 32);
149
lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
150
if (lowOffset == ((DWORD)-1)) {
151
if (GetLastError() != ERROR_SUCCESS) {
152
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
153
return IOS_THROWN;
154
}
155
}
156
157
result = ReadFile(h, /* File handle to read */
158
(LPVOID)address, /* address to put data */
159
len, /* number of bytes to read */
160
&read, /* number of bytes read */
161
NULL); /* struct with offset */
162
163
if (result == 0) {
164
int error = GetLastError();
165
if (error == ERROR_BROKEN_PIPE) {
166
return IOS_EOF;
167
}
168
if (error == ERROR_NO_DATA) {
169
return IOS_UNAVAILABLE;
170
}
171
JNU_ThrowIOExceptionWithLastError(env, "Read failed");
172
return IOS_THROWN;
173
}
174
175
lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
176
if (lowPos == ((DWORD)-1)) {
177
if (GetLastError() != ERROR_SUCCESS) {
178
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
179
return IOS_THROWN;
180
}
181
}
182
return convertReturnVal(env, (jint)read, JNI_TRUE);
183
}
184
185
JNIEXPORT jint JNICALL
186
Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz, jobject fdo,
187
jlong address, jint len, jboolean append)
188
{
189
BOOL result = 0;
190
DWORD written = 0;
191
HANDLE h = (HANDLE)(handleval(env, fdo));
192
193
if (h != INVALID_HANDLE_VALUE) {
194
OVERLAPPED ov;
195
LPOVERLAPPED lpOv;
196
if (append == JNI_TRUE) {
197
ov.Offset = (DWORD)0xFFFFFFFF;
198
ov.OffsetHigh = (DWORD)0xFFFFFFFF;
199
ov.hEvent = NULL;
200
lpOv = &ov;
201
} else {
202
lpOv = NULL;
203
}
204
result = WriteFile(h, /* File handle to write */
205
(LPCVOID)address, /* pointers to the buffers */
206
len, /* number of bytes to write */
207
&written, /* receives number of bytes written */
208
lpOv); /* overlapped struct */
209
}
210
211
if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
212
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
213
return IOS_THROWN;
214
}
215
216
return convertReturnVal(env, (jint)written, JNI_FALSE);
217
}
218
219
JNIEXPORT jlong JNICALL
220
Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz, jobject fdo,
221
jlong address, jint len, jboolean append)
222
{
223
BOOL result = 0;
224
DWORD written = 0;
225
HANDLE h = (HANDLE)(handleval(env, fdo));
226
jlong totalWritten = 0;
227
228
if (h != INVALID_HANDLE_VALUE) {
229
LPVOID loc;
230
int i = 0;
231
DWORD num = 0;
232
struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
233
OVERLAPPED ov;
234
LPOVERLAPPED lpOv;
235
if (append == JNI_TRUE) {
236
ov.Offset = (DWORD)0xFFFFFFFF;
237
ov.OffsetHigh = (DWORD)0xFFFFFFFF;
238
ov.hEvent = NULL;
239
lpOv = &ov;
240
} else {
241
lpOv = NULL;
242
}
243
for(i=0; i<len; i++) {
244
loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
245
num = iovecp[i].iov_len;
246
result = WriteFile(h, /* File handle to write */
247
loc, /* pointers to the buffers */
248
num, /* number of bytes to write */
249
&written,/* receives number of bytes written */
250
lpOv); /* overlapped struct */
251
if (written > 0) {
252
totalWritten += written;
253
}
254
if (written < num) {
255
break;
256
}
257
}
258
}
259
260
if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
261
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
262
return IOS_THROWN;
263
}
264
265
return convertLongReturnVal(env, totalWritten, JNI_FALSE);
266
}
267
268
JNIEXPORT jint JNICALL
269
Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
270
jlong address, jint len, jlong offset)
271
{
272
BOOL result = 0;
273
DWORD written = 0;
274
HANDLE h = (HANDLE)(handleval(env, fdo));
275
DWORD lowPos = 0;
276
long highPos = 0;
277
DWORD lowOffset = 0;
278
long highOffset = 0;
279
280
lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
281
if (lowPos == ((DWORD)-1)) {
282
if (GetLastError() != ERROR_SUCCESS) {
283
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
284
return IOS_THROWN;
285
}
286
}
287
288
lowOffset = (DWORD)offset;
289
highOffset = (DWORD)(offset >> 32);
290
lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
291
if (lowOffset == ((DWORD)-1)) {
292
if (GetLastError() != ERROR_SUCCESS) {
293
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
294
return IOS_THROWN;
295
}
296
}
297
298
result = WriteFile(h, /* File handle to write */
299
(LPCVOID)address, /* pointers to the buffers */
300
len, /* number of bytes to write */
301
&written, /* receives number of bytes written */
302
NULL); /* no overlapped struct */
303
304
if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
305
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
306
return IOS_THROWN;
307
}
308
309
lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
310
if (lowPos == ((DWORD)-1)) {
311
if (GetLastError() != ERROR_SUCCESS) {
312
JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
313
return IOS_THROWN;
314
}
315
}
316
317
return convertReturnVal(env, (jint)written, JNI_FALSE);
318
}
319
320
JNIEXPORT jlong JNICALL
321
Java_sun_nio_ch_FileDispatcherImpl_seek0(JNIEnv *env, jclass clazz,
322
jobject fdo, jlong offset)
323
{
324
BOOL result = 0;
325
HANDLE h = (HANDLE)(handleval(env, fdo));
326
LARGE_INTEGER where;
327
DWORD whence;
328
329
if (offset < 0) {
330
where.QuadPart = 0;
331
whence = FILE_CURRENT;
332
} else {
333
where.QuadPart = offset;
334
whence = FILE_BEGIN;
335
}
336
337
result = SetFilePointerEx(h, where, &where, whence);
338
if (result == 0) {
339
JNU_ThrowIOExceptionWithLastError(env, "SetFilePointerEx failed");
340
return IOS_THROWN;
341
}
342
return (jlong)where.QuadPart;
343
}
344
345
JNIEXPORT jint JNICALL
346
Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv *env, jobject this,
347
jobject fdo, jboolean md)
348
{
349
int result = 0;
350
HANDLE h = (HANDLE)(handleval(env, fdo));
351
352
if (h != INVALID_HANDLE_VALUE) {
353
result = FlushFileBuffers(h);
354
if (result == 0) {
355
int error = GetLastError();
356
if (error != ERROR_ACCESS_DENIED) {
357
JNU_ThrowIOExceptionWithLastError(env, "Force failed");
358
return IOS_THROWN;
359
}
360
}
361
} else {
362
JNU_ThrowIOExceptionWithLastError(env, "Force failed");
363
return IOS_THROWN;
364
}
365
return 0;
366
}
367
368
JNIEXPORT jint JNICALL
369
Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
370
jobject fdo, jlong size)
371
{
372
DWORD lowPos = 0;
373
long highPos = 0;
374
BOOL result = 0;
375
HANDLE h = (HANDLE)(handleval(env, fdo));
376
377
lowPos = (DWORD)size;
378
highPos = (long)(size >> 32);
379
lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
380
if (lowPos == ((DWORD)-1)) {
381
if (GetLastError() != ERROR_SUCCESS) {
382
JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
383
return IOS_THROWN;
384
}
385
}
386
result = SetEndOfFile(h);
387
if (result == 0) {
388
JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
389
return IOS_THROWN;
390
}
391
return 0;
392
}
393
394
JNIEXPORT jlong JNICALL
395
Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
396
{
397
DWORD sizeLow = 0;
398
DWORD sizeHigh = 0;
399
HANDLE h = (HANDLE)(handleval(env, fdo));
400
401
sizeLow = GetFileSize(h, &sizeHigh);
402
if (sizeLow == ((DWORD)-1)) {
403
if (GetLastError() != ERROR_SUCCESS) {
404
JNU_ThrowIOExceptionWithLastError(env, "Size failed");
405
return IOS_THROWN;
406
}
407
}
408
return (((jlong)sizeHigh) << 32) | sizeLow;
409
}
410
411
JNIEXPORT jint JNICALL
412
Java_sun_nio_ch_FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
413
jboolean block, jlong pos, jlong size,
414
jboolean shared)
415
{
416
HANDLE h = (HANDLE)(handleval(env, fdo));
417
DWORD lowPos = (DWORD)pos;
418
long highPos = (long)(pos >> 32);
419
DWORD lowNumBytes = (DWORD)size;
420
DWORD highNumBytes = (DWORD)(size >> 32);
421
BOOL result;
422
DWORD flags = 0;
423
OVERLAPPED o;
424
o.hEvent = 0;
425
o.Offset = lowPos;
426
o.OffsetHigh = highPos;
427
if (block == JNI_FALSE) {
428
flags |= LOCKFILE_FAIL_IMMEDIATELY;
429
}
430
if (shared == JNI_FALSE) {
431
flags |= LOCKFILE_EXCLUSIVE_LOCK;
432
}
433
result = LockFileEx(h, flags, 0, lowNumBytes, highNumBytes, &o);
434
if (result == 0) {
435
int error = GetLastError();
436
if (error != ERROR_LOCK_VIOLATION) {
437
JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
438
return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
439
}
440
if (flags & LOCKFILE_FAIL_IMMEDIATELY) {
441
return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
442
}
443
JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
444
return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
445
}
446
return sun_nio_ch_FileDispatcherImpl_LOCKED;
447
}
448
449
JNIEXPORT void JNICALL
450
Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv *env, jobject this,
451
jobject fdo, jlong pos, jlong size)
452
{
453
HANDLE h = (HANDLE)(handleval(env, fdo));
454
DWORD lowPos = (DWORD)pos;
455
long highPos = (long)(pos >> 32);
456
DWORD lowNumBytes = (DWORD)size;
457
DWORD highNumBytes = (DWORD)(size >> 32);
458
jint result = 0;
459
OVERLAPPED o;
460
o.hEvent = 0;
461
o.Offset = lowPos;
462
o.OffsetHigh = highPos;
463
result = UnlockFileEx(h, 0, lowNumBytes, highNumBytes, &o);
464
if (result == 0 && GetLastError() != ERROR_NOT_LOCKED) {
465
JNU_ThrowIOExceptionWithLastError(env, "Release failed");
466
}
467
}
468
469
static void closeFile(JNIEnv *env, jlong fd) {
470
HANDLE h = (HANDLE)fd;
471
if (h != INVALID_HANDLE_VALUE) {
472
int result = CloseHandle(h);
473
if (result < 0)
474
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
475
}
476
}
477
478
JNIEXPORT void JNICALL
479
Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
480
{
481
jlong fd = handleval(env, fdo);
482
closeFile(env, fd);
483
}
484
485
JNIEXPORT void JNICALL
486
Java_sun_nio_ch_FileDispatcherImpl_closeByHandle(JNIEnv *env, jclass clazz,
487
jlong fd)
488
{
489
closeFile(env, fd);
490
}
491
492
JNIEXPORT jlong JNICALL
493
Java_sun_nio_ch_FileDispatcherImpl_duplicateHandle(JNIEnv *env, jclass this, jlong handle)
494
{
495
HANDLE hProcess = GetCurrentProcess();
496
HANDLE hFile = jlong_to_ptr(handle);
497
HANDLE hResult;
498
BOOL res = DuplicateHandle(hProcess, hFile, hProcess, &hResult, 0, FALSE,
499
DUPLICATE_SAME_ACCESS);
500
if (res == 0)
501
JNU_ThrowIOExceptionWithLastError(env, "DuplicateHandle failed");
502
return ptr_to_jlong(hResult);
503
}
504
505