Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/unix/native/libnio/fs/UnixCopyFile.c
41133 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
/**
72
* Transfer all bytes from src to dst within the kernel if possible (Linux),
73
* otherwise via user-space buffers
74
*/
75
JNIEXPORT void JNICALL
76
Java_sun_nio_fs_UnixCopyFile_transfer
77
(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)
78
{
79
volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);
80
81
#if defined(__linux__)
82
// Transfer within the kernel
83
const size_t count = cancel != NULL ?
84
1048576 : // 1 MB to give cancellation a chance
85
0x7ffff000; // maximum number of bytes that sendfile() can transfer
86
ssize_t bytes_sent;
87
do {
88
RESTARTABLE(sendfile64(dst, src, NULL, count), bytes_sent);
89
if (bytes_sent == -1) {
90
throwUnixException(env, errno);
91
return;
92
}
93
if (cancel != NULL && *cancel != 0) {
94
throwUnixException(env, ECANCELED);
95
return;
96
}
97
} while (bytes_sent > 0);
98
#elif defined(_ALLBSD_SOURCE)
99
copyfile_state_t state;
100
if (cancel != NULL) {
101
state = copyfile_state_alloc();
102
copyfile_state_set(state, COPYFILE_STATE_STATUS_CB, fcopyfile_callback);
103
copyfile_state_set(state, COPYFILE_STATE_STATUS_CTX, (void*)cancel);
104
} else {
105
state = NULL;
106
}
107
if (fcopyfile(src, dst, state, COPYFILE_DATA) < 0) {
108
int errno_fcopyfile = errno;
109
if (state != NULL)
110
copyfile_state_free(state);
111
throwUnixException(env, errno_fcopyfile);
112
return;
113
}
114
if (state != NULL)
115
copyfile_state_free(state);
116
#else
117
// Transfer via user-space buffers
118
char buf[8192];
119
120
for (;;) {
121
ssize_t n, pos, len;
122
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
123
if (n <= 0) {
124
if (n < 0)
125
throwUnixException(env, errno);
126
return;
127
}
128
if (cancel != NULL && *cancel != 0) {
129
throwUnixException(env, ECANCELED);
130
return;
131
}
132
pos = 0;
133
len = n;
134
do {
135
char* bufp = buf;
136
bufp += pos;
137
RESTARTABLE(write((int)dst, bufp, len), n);
138
if (n == -1) {
139
throwUnixException(env, errno);
140
return;
141
}
142
pos += n;
143
len -= n;
144
} while (len > 0);
145
}
146
#endif
147
}
148
149