Path: blob/master/src/java.base/unix/native/libnio/fs/UnixCopyFile.c
41133 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/**71* Transfer all bytes from src to dst within the kernel if possible (Linux),72* otherwise via user-space buffers73*/74JNIEXPORT void JNICALL75Java_sun_nio_fs_UnixCopyFile_transfer76(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)77{78volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);7980#if defined(__linux__)81// Transfer within the kernel82const size_t count = cancel != NULL ?831048576 : // 1 MB to give cancellation a chance840x7ffff000; // maximum number of bytes that sendfile() can transfer85ssize_t bytes_sent;86do {87RESTARTABLE(sendfile64(dst, src, NULL, count), bytes_sent);88if (bytes_sent == -1) {89throwUnixException(env, errno);90return;91}92if (cancel != NULL && *cancel != 0) {93throwUnixException(env, ECANCELED);94return;95}96} while (bytes_sent > 0);97#elif defined(_ALLBSD_SOURCE)98copyfile_state_t state;99if (cancel != NULL) {100state = copyfile_state_alloc();101copyfile_state_set(state, COPYFILE_STATE_STATUS_CB, fcopyfile_callback);102copyfile_state_set(state, COPYFILE_STATE_STATUS_CTX, (void*)cancel);103} else {104state = NULL;105}106if (fcopyfile(src, dst, state, COPYFILE_DATA) < 0) {107int errno_fcopyfile = errno;108if (state != NULL)109copyfile_state_free(state);110throwUnixException(env, errno_fcopyfile);111return;112}113if (state != NULL)114copyfile_state_free(state);115#else116// Transfer via user-space buffers117char buf[8192];118119for (;;) {120ssize_t n, pos, len;121RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);122if (n <= 0) {123if (n < 0)124throwUnixException(env, errno);125return;126}127if (cancel != NULL && *cancel != 0) {128throwUnixException(env, ECANCELED);129return;130}131pos = 0;132len = n;133do {134char* bufp = buf;135bufp += pos;136RESTARTABLE(write((int)dst, bufp, len), n);137if (n == -1) {138throwUnixException(env, errno);139return;140}141pos += n;142len -= n;143} while (len > 0);144}145#endif146}147148149