1 /*
   2  * Copyright (c) 2003, 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_util.h"
  27 
  28 /*
  29  * Macros to use the right data type for file descriptors
  30  */
  31 #define FD jint
  32 
  33 /*
  34  * Prototypes for functions in io_util_md.c called from io_util.c,
  35  * FileDescriptor.c, FileInputStream.c, FileOutputStream.c,
  36  * UnixFileSystem_md.c
  37  */
  38 jint handleAppend(FD fd, const void *buf, jint len);
  39 jint handleWrite(FD fd, const void *buf, jint len);
  40 jint handleRead(FD fd, void *buf, jint len);
  41 jint handleAvailable(FD fd, jlong *pbytes);
  42 jint handleSetLength(FD fd, jlong length);
  43 
  44 FD handleOpen(const char *path, int oflag, int mode);
  45 
  46 /*
  47  * Macros to set/get fd from the java.io.FileDescriptor.  These
  48  * macros rely on having an appropriately defined 'this' object
  49  * within the scope in which they're used.
  50  * If GetObjectField returns null, SET_FD will stop and GET_FD
  51  * will simply return -1 to avoid crashing VM.
  52  */
  53 
  54 #define SET_FD(this, fd, fid) \
  55     if ((*env)->GetObjectField(env, (this), (fid)) != NULL) \
  56         (*env)->SetIntField(env, (*env)->GetObjectField(env, (this), (fid)),IO_fd_fdID, (fd))
  57 
  58 #define GET_FD(this, fid) \
  59     (*env)->GetObjectField(env, (this), (fid)) == NULL ? \
  60         -1 : (*env)->GetIntField(env, (*env)->GetObjectField(env, (this), (fid)), IO_fd_fdID)
  61 
  62 /*
  63  * Macros to set/get fd when inside java.io.FileDescriptor
  64  */
  65 #define THIS_FD(obj) (*env)->GetIntField(env, obj, IO_fd_fdID)
  66 
  67 /*
  68  * Route the routines
  69  */
  70 #define IO_Sync fsync
  71 #define IO_Read handleRead
  72 #define IO_Write handleWrite
  73 #define IO_Append handleAppend
  74 #define IO_Available handleAvailable
  75 #define IO_SetLength handleSetLength
  76 
  77 #ifdef _ALLBSD_SOURCE
  78 #define open64 open
  79 #define fstat64 fstat
  80 #define stat64 stat
  81 #define lseek64 lseek
  82 #define ftruncate64 ftruncate
  83 #define IO_Lseek lseek
  84 #else
  85 #define IO_Lseek lseek64
  86 #endif
  87 
  88 /*
  89  * On Solaris, the handle field is unused
  90  */
  91 #define SET_HANDLE(fd) return (jlong)-1
  92 
  93 /*
  94  * Retry the operation if it is interrupted
  95  */
  96 #define RESTARTABLE(_cmd, _result) do { \
  97   do { \
  98     _result = _cmd; \
  99   } while((_result == -1) && (errno == EINTR)); \
 100 } while(0)
 101 
 102 /*
 103  * IO helper function(s)
 104  */
 105 void fileClose(JNIEnv *env, jobject this, jfieldID fid);
 106 
 107 #ifdef MACOSX
 108 jstring newStringPlatform(JNIEnv *env, const char* str);
 109 #endif