| 1 | /* |
| 2 | * Copyright (c) 2008, 2009, 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 | #include "sun_nio_fs_UnixCopyFile.h" |
| 34 | |
| 35 | #define RESTARTABLE(_cmd, _result) do { \ |
| 36 | do { \ |
| 37 | _result = _cmd; \ |
| 38 | } while((_result == -1) && (errno == EINTR)); \ |
| 39 | } while(0) |
| 40 | |
| 41 | static void throwUnixException(JNIEnv* env, int errnum) { |
| 42 | jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException" , |
| 43 | "(I)V" , errnum); |
| 44 | if (x != NULL) { |
| 45 | (*env)->Throw(env, x); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Transfer all bytes from src to dst via user-space buffers |
| 51 | */ |
| 52 | JNIEXPORT void JNICALL |
| 53 | Java_sun_nio_fs_UnixCopyFile_transfer |
| 54 | (JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress) |
| 55 | { |
| 56 | char buf[8192]; |
| 57 | volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress); |
| 58 | |
| 59 | for (;;) { |
| 60 | ssize_t n, pos, len; |
| 61 | RESTARTABLE(read((int)src, &buf, sizeof(buf)), n); |
| 62 | if (n <= 0) { |
| 63 | if (n < 0) |
| 64 | throwUnixException(env, errno); |
| 65 | return; |
| 66 | } |
| 67 | if (cancel != NULL && *cancel != 0) { |
| 68 | throwUnixException(env, ECANCELED); |
| 69 | return; |
| 70 | } |
| 71 | pos = 0; |
| 72 | len = n; |
| 73 | do { |
| 74 | char* bufp = buf; |
| 75 | bufp += pos; |
| 76 | RESTARTABLE(write((int)dst, bufp, len), n); |
| 77 | if (n == -1) { |
| 78 | throwUnixException(env, errno); |
| 79 | return; |
| 80 | } |
| 81 | pos += n; |
| 82 | len -= n; |
| 83 | } while (len > 0); |
| 84 | } |
| 85 | } |
| 86 | |