893N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A#include "jni.h"
893N/A#include "jni_util.h"
893N/A#include "jlong.h"
893N/A
893N/A#include <unistd.h>
893N/A#include <errno.h>
893N/A
893N/A#include "sun_nio_fs_UnixCopyFile.h"
893N/A
893N/A#define RESTARTABLE(_cmd, _result) do { \
893N/A do { \
893N/A _result = _cmd; \
893N/A } while((_result == -1) && (errno == EINTR)); \
893N/A} while(0)
893N/A
893N/Astatic void throwUnixException(JNIEnv* env, int errnum) {
893N/A jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
893N/A "(I)V", errnum);
893N/A if (x != NULL) {
893N/A (*env)->Throw(env, x);
893N/A }
893N/A}
893N/A
893N/A/**
893N/A * Transfer all bytes from src to dst via user-space buffers
893N/A */
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_UnixCopyFile_transfer
893N/A (JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)
893N/A{
893N/A char buf[8192];
893N/A volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);
893N/A
893N/A for (;;) {
893N/A ssize_t n, pos, len;
893N/A RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
893N/A if (n <= 0) {
893N/A if (n < 0)
893N/A throwUnixException(env, errno);
893N/A return;
893N/A }
893N/A if (cancel != NULL && *cancel != 0) {
893N/A throwUnixException(env, ECANCELED);
893N/A return;
893N/A }
893N/A pos = 0;
893N/A len = n;
893N/A do {
893N/A char* bufp = buf;
893N/A bufp += pos;
893N/A RESTARTABLE(write((int)dst, bufp, len), n);
893N/A if (n == -1) {
893N/A throwUnixException(env, errno);
893N/A return;
893N/A }
893N/A pos += n;
893N/A len -= n;
893N/A } while (len > 0);
893N/A }
893N/A}