8293331: Refactor FileDispatcherImpl into operating system-specific components

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2022-09-22 15:59:05 +00:00
parent f751e6087d
commit 48cc15602b
14 changed files with 959 additions and 704 deletions

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.ch;
import java.io.FileDescriptor;
class FileDispatcherImpl extends UnixFileDispatcherImpl {
FileDispatcherImpl() {
super();
}
int maxDirectTransferSize() {
return 0x7ffff000; // 2,147,479,552 maximum for sendfile()
}
long transferTo(FileDescriptor src, long position, long count,
FileDescriptor dst, boolean append) {
return transferTo0(src, position, count, dst, append);
}
long transferFrom(FileDescriptor src, FileDescriptor dst,
long position, long count, boolean append) {
return transferFrom0(src, dst, position, count, append);
}
// -- Native methods --
static native long transferTo0(FileDescriptor src, long position,
long count, FileDescriptor dst,
boolean append);
static native long transferFrom0(FileDescriptor src, FileDescriptor dst,
long position, long count, boolean append);
static native void init0();
static {
init0();
}
}

View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <sys/sendfile.h>
#include <dlfcn.h>
#include "jni.h"
#include "nio.h"
#include "nio_util.h"
#include "sun_nio_ch_FileDispatcherImpl.h"
typedef ssize_t copy_file_range_func(int, loff_t*, int, loff_t*, size_t,
unsigned int);
static copy_file_range_func* my_copy_file_range_func = NULL;
JNIEXPORT void JNICALL
Java_sun_nio_ch_FileDispatcherImpl_init0(JNIEnv *env, jclass klass)
{
my_copy_file_range_func =
(copy_file_range_func*) dlsym(RTLD_DEFAULT, "copy_file_range");
}
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileDispatcherImpl_transferFrom0(JNIEnv *env, jobject this,
jobject srcFDO, jobject dstFDO,
jlong position, jlong count,
jboolean append)
{
if (my_copy_file_range_func == NULL)
return IOS_UNSUPPORTED;
// copy_file_range fails with EBADF when appending
if (append == JNI_TRUE)
return IOS_UNSUPPORTED_CASE;
jint srcFD = fdval(env, srcFDO);
jint dstFD = fdval(env, dstFDO);
off64_t offset = (off64_t)position;
size_t len = (size_t)count;
jlong n = my_copy_file_range_func(srcFD, NULL, dstFD, &offset, len, 0);
if (n < 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
if (errno == ENOSYS)
return IOS_UNSUPPORTED_CASE;
if ((errno == EBADF || errno == EINVAL || errno == EXDEV) &&
((ssize_t)count >= 0))
return IOS_UNSUPPORTED_CASE;
if (errno == EINTR) {
return IOS_INTERRUPTED;
}
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
return IOS_THROWN;
}
return n;
}
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileDispatcherImpl_transferTo0(JNIEnv *env, jobject this,
jobject srcFDO,
jlong position, jlong count,
jobject dstFDO, jboolean append)
{
jint srcFD = fdval(env, srcFDO);
jint dstFD = fdval(env, dstFDO);
// copy_file_range fails with EBADF when appending, and sendfile
// fails with EINVAL
if (append == JNI_TRUE)
return IOS_UNSUPPORTED_CASE;
off64_t offset = (off64_t)position;
jlong n;
if (my_copy_file_range_func != NULL) {
size_t len = (size_t)count;
n = my_copy_file_range_func(srcFD, &offset, dstFD, NULL, len, 0);
if (n < 0) {
switch (errno) {
case EINTR:
return IOS_INTERRUPTED;
case EINVAL:
case ENOSYS:
case EXDEV:
// ignore and try sendfile()
break;
default:
JNU_ThrowIOExceptionWithLastError(env, "Copy failed");
return IOS_THROWN;
}
}
if (n >= 0)
return n;
}
n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
if (n < 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
if ((errno == EINVAL) && ((ssize_t)count >= 0))
return IOS_UNSUPPORTED_CASE;
if (errno == EINTR) {
return IOS_INTERRUPTED;
}
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
return IOS_THROWN;
}
return n;
}