1 /*
   2  * Copyright (c) 1997, 2014, 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 
  29 extern jfieldID IO_fd_fdID;
  30 extern jfieldID IO_handle_fdID;
  31 
  32 #ifdef _ALLBSD_SOURCE
  33 #include <fcntl.h>
  34 #ifndef O_SYNC
  35 #define O_SYNC  O_FSYNC
  36 #endif
  37 #ifndef O_DSYNC
  38 #define O_DSYNC O_FSYNC
  39 #endif
  40 #elif !defined(O_DSYNC) || !defined(O_SYNC)
  41 #define O_SYNC  (0x0800)
  42 #define O_DSYNC (0x2000)
  43 #endif
  44 
  45 /*
  46  * IO helper functions
  47  */
  48 
  49 jint readSingle(JNIEnv *env, jobject this, jfieldID fid);
  50 jint readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
  51                jint len, jfieldID fid);
  52 void writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid);
  53 void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
  54                 jint len, jboolean append, jfieldID fid);
  55 void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags);
  56 void throwFileNotFoundException(JNIEnv *env, jstring path);
  57 
  58 /*
  59  * Macros for managing platform strings.  The typical usage pattern is:
  60  *
  61  *     WITH_PLATFORM_STRING(env, string, var) {
  62  *         doSomethingWith(var);
  63  *     } END_PLATFORM_STRING(env, var);
  64  *
  65  *  where  env      is the prevailing JNIEnv,
  66  *         string   is a JNI reference to a java.lang.String object, and
  67  *         var      is the char * variable that will point to the string,
  68  *                  after being converted into the platform encoding.
  69  *
  70  * The related macro WITH_FIELD_PLATFORM_STRING first extracts the string from
  71  * a given field of a given object:
  72  *
  73  *     WITH_FIELD_PLATFORM_STRING(env, object, id, var) {
  74  *         doSomethingWith(var);
  75  *     } END_PLATFORM_STRING(env, var);
  76  *
  77  *  where  env      is the prevailing JNIEnv,
  78  *         object   is a jobject,
  79  *         id       is the field ID of the String field to be extracted, and
  80  *         var      is the char * variable that will point to the string.
  81  *
  82  * Uses of these macros may be nested as long as each WITH_.._STRING macro
  83  * declares a unique variable.
  84  */
  85 
  86 #define WITH_PLATFORM_STRING(env, strexp, var)                                \
  87     if (1) {                                                                  \
  88         const char *var;                                                      \
  89         jstring _##var##str = (strexp);                                       \
  90         if (_##var##str == NULL) {                                            \
  91             JNU_ThrowNullPointerException((env), NULL);                       \
  92             goto _##var##end;                                                 \
  93         }                                                                     \
  94         var = JNU_GetStringPlatformChars((env), _##var##str, NULL);           \
  95         if (var == NULL) goto _##var##end;
  96 
  97 #define WITH_FIELD_PLATFORM_STRING(env, object, id, var)                      \
  98     WITH_PLATFORM_STRING(env,                                                 \
  99                          ((object == NULL)                                    \
 100                           ? NULL                                              \
 101                           : (*(env))->GetObjectField((env), (object), (id))), \
 102                          var)
 103 
 104 #define END_PLATFORM_STRING(env, var)                                         \
 105         JNU_ReleaseStringPlatformChars(env, _##var##str, var);                \
 106     _##var##end: ;                                                            \
 107     } else ((void)NULL)
 108 
 109 
 110 /* Macros for transforming Java Strings into native Unicode strings.
 111  * Works analogously to WITH_PLATFORM_STRING.
 112  */
 113 
 114 #define WITH_UNICODE_STRING(env, strexp, var)                                 \
 115     if (1) {                                                                  \
 116         const jchar *var;                                                     \
 117         jstring _##var##str = (strexp);                                       \
 118         if (_##var##str == NULL) {                                            \
 119             JNU_ThrowNullPointerException((env), NULL);                       \
 120             goto _##var##end;                                                 \
 121         }                                                                     \
 122         var = (*(env))->GetStringChars((env), _##var##str, NULL);             \
 123         if (var == NULL) goto _##var##end;
 124 
 125 #define END_UNICODE_STRING(env, var)                                          \
 126         (*(env))->ReleaseStringChars(env, _##var##str, var);                  \
 127     _##var##end: ;                                                            \
 128     } else ((void)NULL)