Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/unix/native/libnio/fs/UnixCopyFile.c
67760 views
1
/*
2
* Copyright (c) 2008, 2021, 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
#include "jlong.h"
29
30
#include <unistd.h>
31
#include <errno.h>
32
33
#if defined(__linux__)
34
#include <sys/sendfile.h>
35
#elif defined(_ALLBSD_SOURCE)
36
#include <copyfile.h>
37
#endif
38
#include "sun_nio_fs_UnixCopyFile.h"
39
40
#define RESTARTABLE(_cmd, _result) do { \
41
do { \
42
_result = _cmd; \
43
} while((_result == -1) && (errno == EINTR)); \
44
} while(0)
45
46
static void throwUnixException(JNIEnv* env, int errnum) {
47
jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
48
"(I)V", errnum);
49
if (x != NULL) {
50
(*env)->Throw(env, x);
51
}
52
}
53
54
#if defined(_ALLBSD_SOURCE)
55
int fcopyfile_callback(int what, int stage, copyfile_state_t state,
56
const char* src, const char* dst, void* cancel)
57
{
58
if (what == COPYFILE_COPY_DATA) {
59
if (stage == COPYFILE_ERR
60
|| (stage == COPYFILE_PROGRESS && *((int*)cancel) != 0)) {
61
// errno will be set to ECANCELED if the operation is cancelled,
62
// or to the appropriate error number if there is an error,
63
// but in either case we need to quit.
64
return COPYFILE_QUIT;
65
}
66
}
67
return COPYFILE_CONTINUE;
68
}
69
#endif
70
71
// Transfer via user-space buffers
72
void transfer(JNIEnv* env, jint dst, jint src, volatile jint* cancel)
73
{
74
char buf[8192];
75
76
for (;;) {
77
ssize_t n, pos, len;
78
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
79
if (n <= 0) {
80
if (n < 0)
81
throwUnixException(env, errno);
82
return;
83
}
84
if (cancel != NULL && *cancel != 0) {
85
throwUnixException(env, ECANCELED);
86
return;
87
}
88
pos = 0;
89
len = n;
90
do {
91
char* bufp = buf;
92
bufp += pos;
93
RESTARTABLE(write((int)dst, bufp, len), n);
94
if (n == -1) {
95
throwUnixException(env, errno);
96
return;
97
}
98
pos += n;
99
len -= n;
100
} while (len > 0);
101
}
102
}
103
104
/**
105
* Transfer all bytes from src to dst within the kernel if possible (Linux),
106
* otherwise via user-space buffers
107
*/
108
JNIEXPORT void JNICALL
109
Java_sun_nio_fs_UnixCopyFile_transfer
110
(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)
111
{
112
volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);
113
114
#if defined(__linux__)
115
// Transfer within the kernel
116
const size_t count = cancel != NULL ?
117
1048576 : // 1 MB to give cancellation a chance
118
0x7ffff000; // maximum number of bytes that sendfile() can transfer
119
ssize_t bytes_sent;
120
do {
121
RESTARTABLE(sendfile64(dst, src, NULL, count), bytes_sent);
122
if (bytes_sent == -1) {
123
if (errno == EINVAL || errno == ENOSYS) {
124
// Fall back to copying via user-space buffers
125
transfer(env, dst, src, cancel);
126
} else {
127
throwUnixException(env, errno);
128
}
129
return;
130
}
131
if (cancel != NULL && *cancel != 0) {
132
throwUnixException(env, ECANCELED);
133
return;
134
}
135
} while (bytes_sent > 0);
136
#elif defined(_ALLBSD_SOURCE)
137
copyfile_state_t state;
138
if (cancel != NULL) {
139
state = copyfile_state_alloc();
140
copyfile_state_set(state, COPYFILE_STATE_STATUS_CB, fcopyfile_callback);
141
copyfile_state_set(state, COPYFILE_STATE_STATUS_CTX, (void*)cancel);
142
} else {
143
state = NULL;
144
}
145
if (fcopyfile(src, dst, state, COPYFILE_DATA) < 0) {
146
int errno_fcopyfile = errno;
147
if (state != NULL)
148
copyfile_state_free(state);
149
throwUnixException(env, errno_fcopyfile);
150
return;
151
}
152
if (state != NULL)
153
copyfile_state_free(state);
154
#else
155
transfer(env, dst, src, cancel);
156
#endif
157
}
158
159