< prev index next >

src/solaris/native/java/io/io_util_md.c

Print this page


   1 /*
   2  * Copyright (c) 2001, 2013, 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 "io_util.h"
  30 #include "io_util_md.h"
  31 #include <string.h>
  32 #include <unistd.h>
  33 
  34 #ifdef __solaris__
  35 #include <sys/filio.h>
  36 #endif
  37 
  38 #if defined(__linux__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
  39 #include <sys/ioctl.h>
  40 #endif
  41 
  42 #ifdef MACOSX
  43 
  44 #include <CoreFoundation/CoreFoundation.h>
  45 


 191 
 192     if ((current = lseek64(fd, 0, SEEK_CUR)) == -1) {
 193         return 0;
 194     }
 195 
 196     if (size < current) {
 197         if ((size = lseek64(fd, 0, SEEK_END)) == -1)
 198             return 0;
 199         else if (lseek64(fd, current, SEEK_SET) == -1)
 200             return 0;
 201     }
 202 
 203     *pbytes = size - current;
 204     return 1;
 205 }
 206 
 207 jint
 208 handleSetLength(FD fd, jlong length)
 209 {
 210     int result;



















 211     RESTARTABLE(ftruncate64(fd, length), result);
 212     return result;
 213 }
 214 
 215 size_t
 216 getLastErrorString(char *buf, size_t len)
 217 {
 218     if (errno == 0 || len < 1) return 0;
 219     getErrorString(errno, buf, len);
 220     return strlen(buf);
 221 }
   1 /*
   2  * Copyright (c) 2001, 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 "io_util.h"
  34 #include "io_util_md.h"
  35 #include <string.h>
  36 #include <unistd.h>
  37 
  38 #ifdef __solaris__
  39 #include <sys/filio.h>
  40 #endif
  41 
  42 #if defined(__linux__) || defined(_ALLBSD_SOURCE) || defined(_AIX)
  43 #include <sys/ioctl.h>
  44 #endif
  45 
  46 #ifdef MACOSX
  47 
  48 #include <CoreFoundation/CoreFoundation.h>
  49 


 195 
 196     if ((current = lseek64(fd, 0, SEEK_CUR)) == -1) {
 197         return 0;
 198     }
 199 
 200     if (size < current) {
 201         if ((size = lseek64(fd, 0, SEEK_END)) == -1)
 202             return 0;
 203         else if (lseek64(fd, current, SEEK_SET) == -1)
 204             return 0;
 205     }
 206 
 207     *pbytes = size - current;
 208     return 1;
 209 }
 210 
 211 jint
 212 handleSetLength(FD fd, jlong length)
 213 {
 214     int result;
 215 #if defined(__linux__)
 216     /*
 217      * On Linux, if the file size is being increased, then ftruncate64()
 218      * will modify the metadata value of the size without actually allocating
 219      * any blocks which can cause a SIGBUS error if the file is subsequently
 220      * memory-mapped.
 221      */
 222     struct stat64 sb;
 223 
 224     if (fstat64(fd, &sb) == 0 && length > sb.st_blocks*512) {
 225         RESTARTABLE(posix_fallocate(fd, 0, length), result);
 226         // Return on success or if errno is neither EOPNOTSUPP nor ENOSYS
 227         if (result == 0) {
 228             return 0;
 229         } else if (errno != EOPNOTSUPP && errno != ENOSYS) {
 230             return result;
 231         }
 232     }
 233 #endif
 234     RESTARTABLE(ftruncate64(fd, length), result);
 235     return result;
 236 }
 237 
 238 size_t
 239 getLastErrorString(char *buf, size_t len)
 240 {
 241     if (errno == 0 || len < 1) return 0;
 242     getErrorString(errno, buf, len);
 243     return strlen(buf);
 244 }
< prev index next >