Path: blob/master/src/java.base/unix/native/libnio/fs/UnixCopyFile.c
67760 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jlong.h"2829#include <unistd.h>30#include <errno.h>3132#if defined(__linux__)33#include <sys/sendfile.h>34#elif defined(_ALLBSD_SOURCE)35#include <copyfile.h>36#endif37#include "sun_nio_fs_UnixCopyFile.h"3839#define RESTARTABLE(_cmd, _result) do { \40do { \41_result = _cmd; \42} while((_result == -1) && (errno == EINTR)); \43} while(0)4445static void throwUnixException(JNIEnv* env, int errnum) {46jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",47"(I)V", errnum);48if (x != NULL) {49(*env)->Throw(env, x);50}51}5253#if defined(_ALLBSD_SOURCE)54int fcopyfile_callback(int what, int stage, copyfile_state_t state,55const char* src, const char* dst, void* cancel)56{57if (what == COPYFILE_COPY_DATA) {58if (stage == COPYFILE_ERR59|| (stage == COPYFILE_PROGRESS && *((int*)cancel) != 0)) {60// errno will be set to ECANCELED if the operation is cancelled,61// or to the appropriate error number if there is an error,62// but in either case we need to quit.63return COPYFILE_QUIT;64}65}66return COPYFILE_CONTINUE;67}68#endif6970// Transfer via user-space buffers71void transfer(JNIEnv* env, jint dst, jint src, volatile jint* cancel)72{73char buf[8192];7475for (;;) {76ssize_t n, pos, len;77RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);78if (n <= 0) {79if (n < 0)80throwUnixException(env, errno);81return;82}83if (cancel != NULL && *cancel != 0) {84throwUnixException(env, ECANCELED);85return;86}87pos = 0;88len = n;89do {90char* bufp = buf;91bufp += pos;92RESTARTABLE(write((int)dst, bufp, len), n);93if (n == -1) {94throwUnixException(env, errno);95return;96}97pos += n;98len -= n;99} while (len > 0);100}101}102103/**104* Transfer all bytes from src to dst within the kernel if possible (Linux),105* otherwise via user-space buffers106*/107JNIEXPORT void JNICALL108Java_sun_nio_fs_UnixCopyFile_transfer109(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)110{111volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);112113#if defined(__linux__)114// Transfer within the kernel115const size_t count = cancel != NULL ?1161048576 : // 1 MB to give cancellation a chance1170x7ffff000; // maximum number of bytes that sendfile() can transfer118ssize_t bytes_sent;119do {120RESTARTABLE(sendfile64(dst, src, NULL, count), bytes_sent);121if (bytes_sent == -1) {122if (errno == EINVAL || errno == ENOSYS) {123// Fall back to copying via user-space buffers124transfer(env, dst, src, cancel);125} else {126throwUnixException(env, errno);127}128return;129}130if (cancel != NULL && *cancel != 0) {131throwUnixException(env, ECANCELED);132return;133}134} while (bytes_sent > 0);135#elif defined(_ALLBSD_SOURCE)136copyfile_state_t state;137if (cancel != NULL) {138state = copyfile_state_alloc();139copyfile_state_set(state, COPYFILE_STATE_STATUS_CB, fcopyfile_callback);140copyfile_state_set(state, COPYFILE_STATE_STATUS_CTX, (void*)cancel);141} else {142state = NULL;143}144if (fcopyfile(src, dst, state, COPYFILE_DATA) < 0) {145int errno_fcopyfile = errno;146if (state != NULL)147copyfile_state_free(state);148throwUnixException(env, errno_fcopyfile);149return;150}151if (state != NULL)152copyfile_state_free(state);153#else154transfer(env, dst, src, cancel);155#endif156}157158159