author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 7971 | ca5a32210b1b |
child 12047 | 320a714614e9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7971
diff
changeset
|
2 |
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include "jni.h" |
|
27 |
#include "jni_util.h" |
|
28 |
#include "jvm.h" |
|
29 |
#include "jlong.h" |
|
30 |
#include <sys/mman.h> |
|
31 |
#include <sys/stat.h> |
|
32 |
#include "sun_nio_ch_FileChannelImpl.h" |
|
33 |
#include "java_lang_Integer.h" |
|
34 |
#include "nio.h" |
|
35 |
#include "nio_util.h" |
|
36 |
#include <dlfcn.h> |
|
37 |
||
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
38 |
#if defined(__linux__) || defined(__solaris__) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
39 |
#include <sys/sendfile.h> |
2 | 40 |
#endif |
41 |
||
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
42 |
static jfieldID chan_fd; /* jobject 'fd' in sun.io.FileChannelImpl */ |
2 | 43 |
|
44 |
JNIEXPORT jlong JNICALL |
|
45 |
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz) |
|
46 |
{ |
|
47 |
jlong pageSize = sysconf(_SC_PAGESIZE); |
|
48 |
chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;"); |
|
49 |
return pageSize; |
|
50 |
} |
|
51 |
||
52 |
static jlong |
|
53 |
handle(JNIEnv *env, jlong rv, char *msg) |
|
54 |
{ |
|
55 |
if (rv >= 0) |
|
56 |
return rv; |
|
57 |
if (errno == EINTR) |
|
58 |
return IOS_INTERRUPTED; |
|
59 |
JNU_ThrowIOExceptionWithLastError(env, msg); |
|
60 |
return IOS_THROWN; |
|
61 |
} |
|
62 |
||
63 |
||
64 |
JNIEXPORT jlong JNICALL |
|
65 |
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, |
|
66 |
jint prot, jlong off, jlong len) |
|
67 |
{ |
|
68 |
void *mapAddress = 0; |
|
69 |
jobject fdo = (*env)->GetObjectField(env, this, chan_fd); |
|
70 |
jint fd = fdval(env, fdo); |
|
71 |
int protections = 0; |
|
72 |
int flags = 0; |
|
73 |
||
74 |
if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) { |
|
75 |
protections = PROT_READ; |
|
76 |
flags = MAP_SHARED; |
|
77 |
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) { |
|
78 |
protections = PROT_WRITE | PROT_READ; |
|
79 |
flags = MAP_SHARED; |
|
80 |
} else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) { |
|
81 |
protections = PROT_WRITE | PROT_READ; |
|
82 |
flags = MAP_PRIVATE; |
|
83 |
} |
|
84 |
||
85 |
mapAddress = mmap64( |
|
86 |
0, /* Let OS decide location */ |
|
87 |
len, /* Number of bytes to map */ |
|
88 |
protections, /* File permissions */ |
|
89 |
flags, /* Changes are shared */ |
|
90 |
fd, /* File descriptor of mapped file */ |
|
91 |
off); /* Offset into file */ |
|
92 |
||
93 |
if (mapAddress == MAP_FAILED) { |
|
94 |
if (errno == ENOMEM) { |
|
95 |
JNU_ThrowOutOfMemoryError(env, "Map failed"); |
|
96 |
return IOS_THROWN; |
|
97 |
} |
|
98 |
return handle(env, -1, "Map failed"); |
|
99 |
} |
|
100 |
||
101 |
return ((jlong) (unsigned long) mapAddress); |
|
102 |
} |
|
103 |
||
104 |
||
105 |
JNIEXPORT jint JNICALL |
|
106 |
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this, |
|
107 |
jlong address, jlong len) |
|
108 |
{ |
|
109 |
void *a = (void *)jlong_to_ptr(address); |
|
110 |
return handle(env, |
|
111 |
munmap(a, (size_t)len), |
|
112 |
"Unmap failed"); |
|
113 |
} |
|
114 |
||
115 |
||
116 |
JNIEXPORT jlong JNICALL |
|
117 |
Java_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this, |
|
118 |
jobject fdo, jlong offset) |
|
119 |
{ |
|
120 |
jint fd = fdval(env, fdo); |
|
121 |
jlong result = 0; |
|
122 |
||
123 |
if (offset < 0) { |
|
124 |
result = lseek64(fd, 0, SEEK_CUR); |
|
125 |
} else { |
|
126 |
result = lseek64(fd, offset, SEEK_SET); |
|
127 |
} |
|
128 |
return handle(env, result, "Position failed"); |
|
129 |
} |
|
130 |
||
131 |
||
132 |
JNIEXPORT void JNICALL |
|
133 |
Java_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo) |
|
134 |
{ |
|
135 |
jint fd = fdval(env, fdo); |
|
136 |
if (fd != -1) { |
|
137 |
jlong result = close(fd); |
|
138 |
if (result < 0) { |
|
139 |
JNU_ThrowIOExceptionWithLastError(env, "Close failed"); |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
JNIEXPORT jlong JNICALL |
|
145 |
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this, |
|
146 |
jint srcFD, |
|
147 |
jlong position, jlong count, |
|
148 |
jint dstFD) |
|
149 |
{ |
|
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
150 |
#if defined(__linux__) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
151 |
off64_t offset = (off64_t)position; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
152 |
jlong n = sendfile64(dstFD, srcFD, &offset, (size_t)count); |
2 | 153 |
if (n < 0) { |
154 |
if (errno == EAGAIN) |
|
155 |
return IOS_UNAVAILABLE; |
|
156 |
if ((errno == EINVAL) && ((ssize_t)count >= 0)) |
|
157 |
return IOS_UNSUPPORTED_CASE; |
|
158 |
if (errno == EINTR) { |
|
159 |
return IOS_INTERRUPTED; |
|
160 |
} |
|
161 |
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed"); |
|
162 |
return IOS_THROWN; |
|
163 |
} |
|
164 |
return n; |
|
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
165 |
#elif defined (__solaris__) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
166 |
sendfilevec64_t sfv; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
167 |
size_t numBytes = 0; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
168 |
jlong result; |
2 | 169 |
|
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
170 |
sfv.sfv_fd = srcFD; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
171 |
sfv.sfv_flag = 0; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
172 |
sfv.sfv_off = (off64_t)position; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
173 |
sfv.sfv_len = count; |
2 | 174 |
|
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
175 |
result = sendfilev64(dstFD, &sfv, 1, &numBytes); |
2 | 176 |
|
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
177 |
/* Solaris sendfilev() will return -1 even if some bytes have been |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
178 |
* transferred, so we check numBytes first. |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
179 |
*/ |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
180 |
if (numBytes > 0) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
181 |
return numBytes; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
182 |
if (result < 0) { |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
183 |
if (errno == EAGAIN) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
184 |
return IOS_UNAVAILABLE; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
185 |
if (errno == EOPNOTSUPP) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
186 |
return IOS_UNSUPPORTED_CASE; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
187 |
if ((errno == EINVAL) && ((ssize_t)count >= 0)) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
188 |
return IOS_UNSUPPORTED_CASE; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
189 |
if (errno == EINTR) |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
190 |
return IOS_INTERRUPTED; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
191 |
JNU_ThrowIOExceptionWithLastError(env, "Transfer failed"); |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
192 |
return IOS_THROWN; |
2 | 193 |
} |
7971
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
194 |
return result; |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
195 |
#else |
ca5a32210b1b
7002957: (fc) FileChannel.transferTo fails to load libsendfile on Solaris 64-bit
alanb
parents:
5506
diff
changeset
|
196 |
return IOS_UNSUPPORTED_CASE; |
2 | 197 |
#endif |
198 |
} |