1 /*
2 * Copyright (c) 2000, 2012, 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 "jvm.h"
29 #include "jlong.h"
30 #include "sun_nio_ch_FileDispatcherImpl.h"
31 #include "java_lang_Long.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <fcntl.h>
35 #include <sys/uio.h>
36 #include <unistd.h>
37 #if defined(__linux__)
38 #include <linux/fs.h>
39 #include <sys/ioctl.h>
40 #endif
41 #include "nio.h"
42 #include "nio_util.h"
43
44 #ifdef _ALLBSD_SOURCE
45 #define stat64 stat
156 * reading results in an error ("EBADF: The FileDescriptor parameter is
157 * not a valid file descriptor open for writing.").
158 * However, at this point it is not possibly anymore to read the
159 * 'writable' attribute of the corresponding file channel so we have to
160 * use 'fcntl'.
161 */
162 int getfl = fcntl(fd, F_GETFL);
163 if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
164 return 0;
165 }
166 #endif
167 result = fsync(fd);
168 }
169 return handle(env, result, "Force failed");
170 }
171
172 JNIEXPORT jint JNICALL
173 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
174 jobject fdo, jlong size)
175 {
176 return handle(env,
177 ftruncate64(fdval(env, fdo), size),
178 "Truncation failed");
179 }
180
181 JNIEXPORT jlong JNICALL
182 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
183 {
184 jint fd = fdval(env, fdo);
185 struct stat64 fbuf;
186
187 if (fstat64(fd, &fbuf) < 0)
188 return handle(env, -1, "Size failed");
189
190 #ifdef BLKGETSIZE64
191 if (S_ISBLK(fbuf.st_mode)) {
192 uint64_t size;
193 if (ioctl(fd, BLKGETSIZE64, &size) < 0)
194 return handle(env, -1, "Size failed");
195 return (jlong)size;
|
1 /*
2 * Copyright (c) 2000, 2018, 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 #if defined(__linux__)
27 #define _FILE_OFFSET_BITS 64
28 #endif
29
30 #include "jni.h"
31 #include "jni_util.h"
32 #include "jvm.h"
33 #include "jlong.h"
34 #include "sun_nio_ch_FileDispatcherImpl.h"
35 #include "java_lang_Long.h"
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <fcntl.h>
39 #include <sys/uio.h>
40 #include <unistd.h>
41 #if defined(__linux__)
42 #include <linux/fs.h>
43 #include <sys/ioctl.h>
44 #endif
45 #include "nio.h"
46 #include "nio_util.h"
47
48 #ifdef _ALLBSD_SOURCE
49 #define stat64 stat
160 * reading results in an error ("EBADF: The FileDescriptor parameter is
161 * not a valid file descriptor open for writing.").
162 * However, at this point it is not possibly anymore to read the
163 * 'writable' attribute of the corresponding file channel so we have to
164 * use 'fcntl'.
165 */
166 int getfl = fcntl(fd, F_GETFL);
167 if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
168 return 0;
169 }
170 #endif
171 result = fsync(fd);
172 }
173 return handle(env, result, "Force failed");
174 }
175
176 JNIEXPORT jint JNICALL
177 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
178 jobject fdo, jlong size)
179 {
180 return handle(env,
181 ftruncate64(fdval(env, fdo), size),
182 "Truncation failed");
183 }
184
185 JNIEXPORT jint JNICALL
186 Java_sun_nio_ch_FileDispatcherImpl_allocate0(JNIEnv *env, jobject this,
187 jobject fdo, jlong size)
188 {
189 #if defined(__linux__)
190 /*
191 * On Linux, if the file size is being increased, then ftruncate64()
192 * will modify the metadata value of the size without actually allocating
193 * any blocks which can cause a SIGBUS error if the file is subsequently
194 * memory-mapped.
195 */
196 // Return on success or if errno is neither EOPNOTSUPP nor ENOSYS
197 int result = posix_fallocate(fdval(env, fdo), 0, size);
198 if (result == 0) {
199 return 0;
200 } else if (errno != EOPNOTSUPP && errno != ENOSYS) {
201 return handle(env, result, "Allocation failed");
202 }
203 #endif
204 return handle(env,
205 ftruncate64(fdval(env, fdo), size),
206 "Truncation failed");
207 }
208
209 JNIEXPORT jlong JNICALL
210 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
211 {
212 jint fd = fdval(env, fdo);
213 struct stat64 fbuf;
214
215 if (fstat64(fd, &fbuf) < 0)
216 return handle(env, -1, "Size failed");
217
218 #ifdef BLKGETSIZE64
219 if (S_ISBLK(fbuf.st_mode)) {
220 uint64_t size;
221 if (ioctl(fd, BLKGETSIZE64, &size) < 0)
222 return handle(env, -1, "Size failed");
223 return (jlong)size;
|