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
167 * use 'fcntl'.
168 */
169 int getfl = fcntl(fd, F_GETFL);
170 if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
171 return 0;
172 }
173 #endif /* _AIX */
174 result = fsync(fd);
175 }
176 #endif /* not-MACOSX */
177 return handle(env, result, "Force failed");
178 }
179
180 JNIEXPORT jint JNICALL
181 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
182 jobject fdo, jlong size)
183 {
184 return handle(env,
185 ftruncate64(fdval(env, fdo), size),
186 "Truncation failed");
187 }
188
189 JNIEXPORT jlong JNICALL
190 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
191 {
192 jint fd = fdval(env, fdo);
193 struct stat64 fbuf;
194
195 if (fstat64(fd, &fbuf) < 0)
196 return handle(env, -1, "Size failed");
197
198 #ifdef BLKGETSIZE64
199 if (S_ISBLK(fbuf.st_mode)) {
200 uint64_t size;
201 if (ioctl(fd, BLKGETSIZE64, &size) < 0)
202 return handle(env, -1, "Size failed");
203 return (jlong)size;
204 }
205 #endif
206
|
1 /*
2 * Copyright (c) 2000, 2016, 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
167 * use 'fcntl'.
168 */
169 int getfl = fcntl(fd, F_GETFL);
170 if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
171 return 0;
172 }
173 #endif /* _AIX */
174 result = fsync(fd);
175 }
176 #endif /* not-MACOSX */
177 return handle(env, result, "Force failed");
178 }
179
180 JNIEXPORT jint JNICALL
181 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
182 jobject fdo, jlong size)
183 {
184 return handle(env,
185 ftruncate64(fdval(env, fdo), size),
186 "Truncation failed");
187 }
188
189 JNIEXPORT jint JNICALL
190 Java_sun_nio_ch_FileDispatcherImpl_allocate0(JNIEnv *env, jobject this,
191 jobject fdo, jlong size)
192 {
193 #if defined(__linux__)
194 /*
195 * On Linux, if the file size is being increased, then ftruncate64()
196 * will modify the metadata value of the size without actually allocating
197 * any blocks which can cause a SIGBUS error if the file is subsequently
198 * memory-mapped.
199 */
200 return handle(env,
201 fallocate64(fdval(env, fdo), 0, 0, size),
202 "Allocation failed");
203 #else
204 return handle(env,
205 ftruncate64(fdval(env, fdo), size),
206 "Truncation failed");
207 #endif
208 }
209
210 JNIEXPORT jlong JNICALL
211 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
212 {
213 jint fd = fdval(env, fdo);
214 struct stat64 fbuf;
215
216 if (fstat64(fd, &fbuf) < 0)
217 return handle(env, -1, "Size failed");
218
219 #ifdef BLKGETSIZE64
220 if (S_ISBLK(fbuf.st_mode)) {
221 uint64_t size;
222 if (ioctl(fd, BLKGETSIZE64, &size) < 0)
223 return handle(env, -1, "Size failed");
224 return (jlong)size;
225 }
226 #endif
227
|