1 /*
2  * Copyright (c) 1997, 2021, 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 <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <limits.h>
32 
33 #include "jni.h"
34 #include "jni_util.h"
35 #include "jlong.h"
36 #include "io_util.h"
37 #include "io_util_md.h"
38 
39 #include "jvm.h"
40 
41 
42 #include "io_util_md.h"
43 #include <nativehelper/JNIHelp.h>
44 
45 #define NATIVE_METHOD(className, functionName, signature) \
46 { #functionName, signature, (void*)(className ## _ ## functionName) }
47 
48 /*******************************************************************/
49 /*  BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
50 /*******************************************************************/
51 
52 jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
53 
54 /**************************************************************
55  * Input stream
56  */
57 
58 
FileInputStream_initIDs(JNIEnv * env)59 static void FileInputStream_initIDs(JNIEnv *env) {
60     jclass clazz = (*env)->FindClass(env, "java/io/FileInputStream");
61     fis_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
62 }
63 
64 // BEGIN Android-removed: Open files using IoBridge to share BlockGuard & StrictMode logic.
65 // http://b/112107427
66 /*
67 JNIEXPORT void JNICALL
68 Java_java_io_FileInputStream_open0(JNIEnv *env, jobject this, jstring path) {
69     fileOpen(env, this, path, fis_fd, O_RDONLY);
70 }
71 */
72 // END Android-removed: Open files using IoBridge to share BlockGuard & StrictMode logic.
73 
74 JNIEXPORT jlong JNICALL
Java_java_io_FileInputStream_length0(JNIEnv * env,jobject this)75 Java_java_io_FileInputStream_length0(JNIEnv *env, jobject this) {
76 
77     FD fd;
78     jlong length = jlong_zero;
79 
80     fd = getFD(env, this, fis_fd);
81     if (fd == -1) {
82         JNU_ThrowIOException(env, "Stream Closed");
83         return -1;
84     }
85     if ((length = IO_GetLength(fd)) == -1) {
86         JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");
87     }
88     return length;
89 }
90 
91 JNIEXPORT jlong JNICALL
Java_java_io_FileInputStream_position0(JNIEnv * env,jobject this)92 Java_java_io_FileInputStream_position0(JNIEnv *env, jobject this) {
93     FD fd;
94     jlong ret;
95 
96     fd = getFD(env, this, fis_fd);
97     if (fd == -1) {
98         JNU_ThrowIOException(env, "Stream Closed");
99         return -1;
100     }
101     if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
102         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
103     }
104     return ret;
105 }
106 
107 JNIEXPORT jlong JNICALL
Java_java_io_FileInputStream_skip0(JNIEnv * env,jobject this,jlong toSkip)108 Java_java_io_FileInputStream_skip0(JNIEnv *env, jobject this, jlong toSkip) {
109     jlong cur = jlong_zero;
110     jlong end = jlong_zero;
111     FD fd = getFD(env, this, fis_fd);
112     if (fd == -1) {
113         JNU_ThrowIOException (env, "Stream Closed");
114         return 0;
115     }
116     if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
117       if (errno == ESPIPE) {
118         JNU_ThrowByName(env, "java/io/FileInputStream$UseManualSkipException", NULL);
119       } else {
120         JNU_ThrowIOExceptionWithLastError(env, "Seek error");
121       }
122     } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
123         JNU_ThrowIOExceptionWithLastError(env, "Seek error");
124     }
125     return (end - cur);
126 }
127 
available(int fd,jlong * bytes)128 static int available(int fd, jlong *bytes) {
129 // BEGIN Android-added: Fuchsia does not support FIONREAD. http://b/120566512
130 #if defined(__Fuchsia__)
131   *bytes = 0;
132   return 1;
133 #else
134 // END Android-added: Fuchsia does not support FIONREAD. http://b/120566512
135   int n;
136   // Unlike the original OpenJdk implementation, we use FIONREAD for all file
137   // types. For regular files, this is specified to return the difference
138   // between the current position and the file size. Note that this can be
139   // negative if we're positioned past the end of the file. We must return 0
140   // in that case.
141   if (ioctl(fd, FIONREAD, &n) != -1) {
142     if (n < 0) {
143       n = 0;
144     }
145     *bytes = n;
146     return 1;
147   }
148 
149   // FIONREAD is specified to return ENOTTY when fd refers to a file
150   // type for which this ioctl isn't implemented.
151   if (errno == ENOTTY) {
152     *bytes = 0;
153     return 1;
154   }
155 
156   // Raise an exception for all other error types.
157   return 0;
158 // Android-added: Fuchsia does not support the FIONREAD code. http://b/120566512
159 #endif
160 }
161 
162 JNIEXPORT jint JNICALL
Java_java_io_FileInputStream_available0(JNIEnv * env,jobject this)163 Java_java_io_FileInputStream_available0(JNIEnv *env, jobject this) {
164     jlong ret;
165     FD fd = getFD(env, this, fis_fd);
166     if (fd == -1) {
167         JNU_ThrowIOException (env, "Stream Closed");
168         return 0;
169     }
170     if (available(fd, &ret)) {
171         if (ret > INT_MAX) {
172             ret = (jlong) INT_MAX;
173         } else if (ret < 0) {
174             ret = 0;
175         }
176         return jlong_to_jint(ret);
177     }
178     JNU_ThrowIOExceptionWithLastError(env, NULL);
179     return 0;
180 }
181 
182 static JNINativeMethod gMethods[] = {
183   NATIVE_METHOD(Java_java_io_FileInputStream, length0, "()J"),
184   NATIVE_METHOD(Java_java_io_FileInputStream, position0, "()J"),
185   NATIVE_METHOD(Java_java_io_FileInputStream, skip0, "(J)J"),
186   NATIVE_METHOD(Java_java_io_FileInputStream, available0, "()I"),
187 };
188 
register_java_io_FileInputStream(JNIEnv * env)189 void register_java_io_FileInputStream(JNIEnv* env) {
190     jniRegisterNativeMethods(env, "java/io/FileInputStream", gMethods, NELEM(gMethods));
191     FileInputStream_initIDs(env);
192 }
193