Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include "JNIHelp.h" |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <android_runtime/AndroidRuntime.h> |
| 28 | |
| 29 | namespace android |
| 30 | { |
| 31 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 32 | static struct parcel_file_descriptor_offsets_t |
| 33 | { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 34 | jfieldID mFileDescriptor; |
| 35 | } gParcelFileDescriptorOffsets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 37 | static jobject android_os_ParcelFileDescriptor_getFileDescriptorFromFd(JNIEnv* env, |
| 38 | jobject clazz, jint origfd) |
| 39 | { |
| 40 | int fd = dup(origfd); |
| 41 | if (fd < 0) { |
| 42 | jniThrowException(env, "java/io/IOException", strerror(errno)); |
| 43 | return NULL; |
| 44 | } |
Dianne Hackborn | 9394613 | 2011-05-23 14:08:12 -0700 | [diff] [blame] | 45 | return jniCreateFileDescriptor(env, fd); |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static jobject android_os_ParcelFileDescriptor_getFileDescriptorFromFdNoDup(JNIEnv* env, |
| 49 | jobject clazz, jint fd) |
| 50 | { |
Dianne Hackborn | 9394613 | 2011-05-23 14:08:12 -0700 | [diff] [blame] | 51 | return jniCreateFileDescriptor(env, fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 54 | static void android_os_ParcelFileDescriptor_createPipeNative(JNIEnv* env, |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 55 | jobject clazz, jobjectArray outFds) |
| 56 | { |
| 57 | int fds[2]; |
| 58 | if (pipe(fds) < 0) { |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 59 | int therr = errno; |
| 60 | jniThrowException(env, "java/io/IOException", strerror(therr)); |
| 61 | return; |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | for (int i=0; i<2; i++) { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 65 | jobject fdObj = jniCreateFileDescriptor(env, fds[i]); |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 66 | env->SetObjectArrayElement(outFds, i, fdObj); |
| 67 | } |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 68 | } |
| 69 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 70 | static jint getFd(JNIEnv* env, jobject clazz) |
| 71 | { |
| 72 | jobject descriptor = env->GetObjectField(clazz, gParcelFileDescriptorOffsets.mFileDescriptor); |
| 73 | if (descriptor == NULL) return -1; |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 74 | return jniGetFDFromFileDescriptor(env, descriptor); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 75 | } |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | static jlong android_os_ParcelFileDescriptor_getStatSize(JNIEnv* env, |
| 78 | jobject clazz) |
| 79 | { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 80 | jint fd = getFd(env, clazz); |
| 81 | if (fd < 0) { |
| 82 | jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor"); |
| 83 | return -1; |
| 84 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 85 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | struct stat st; |
| 87 | if (fstat(fd, &st) != 0) { |
| 88 | return -1; |
| 89 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { |
| 92 | return st.st_size; |
| 93 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | static jlong android_os_ParcelFileDescriptor_seekTo(JNIEnv* env, |
| 99 | jobject clazz, jlong pos) |
| 100 | { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 101 | jint fd = getFd(env, clazz); |
| 102 | if (fd < 0) { |
| 103 | jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor"); |
| 104 | return -1; |
| 105 | } |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | return lseek(fd, pos, SEEK_SET); |
| 108 | } |
| 109 | |
Dianne Hackborn | c9119f5 | 2011-02-28 18:03:26 -0800 | [diff] [blame] | 110 | static jlong android_os_ParcelFileDescriptor_getFdNative(JNIEnv* env, jobject clazz) |
| 111 | { |
| 112 | jint fd = getFd(env, clazz); |
| 113 | if (fd < 0) { |
| 114 | jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor"); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | return fd; |
| 119 | } |
| 120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | static const JNINativeMethod gParcelFileDescriptorMethods[] = { |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 122 | {"getFileDescriptorFromFd", "(I)Ljava/io/FileDescriptor;", |
| 123 | (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromFd}, |
| 124 | {"getFileDescriptorFromFdNoDup", "(I)Ljava/io/FileDescriptor;", |
| 125 | (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromFdNoDup}, |
Dianne Hackborn | ea2117bd | 2011-05-20 10:37:34 -0700 | [diff] [blame] | 126 | {"createPipeNative", "([Ljava/io/FileDescriptor;)V", |
Dianne Hackborn | 23fdaf6 | 2010-08-06 12:16:55 -0700 | [diff] [blame] | 127 | (void*)android_os_ParcelFileDescriptor_createPipeNative}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | {"getStatSize", "()J", |
| 129 | (void*)android_os_ParcelFileDescriptor_getStatSize}, |
| 130 | {"seekTo", "(J)J", |
Dianne Hackborn | c9119f5 | 2011-02-28 18:03:26 -0800 | [diff] [blame] | 131 | (void*)android_os_ParcelFileDescriptor_seekTo}, |
| 132 | {"getFdNative", "()I", |
| 133 | (void*)android_os_ParcelFileDescriptor_getFdNative} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | const char* const kParcelFileDescriptorPathName = "android/os/ParcelFileDescriptor"; |
| 137 | |
| 138 | int register_android_os_ParcelFileDescriptor(JNIEnv* env) |
| 139 | { |
Elliott Hughes | 4390758 | 2011-04-12 14:25:23 -0700 | [diff] [blame] | 140 | jclass clazz = env->FindClass(kParcelFileDescriptorPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor"); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 142 | gParcelFileDescriptorOffsets.mFileDescriptor = env->GetFieldID(clazz, "mFileDescriptor", "Ljava/io/FileDescriptor;"); |
| 143 | LOG_FATAL_IF(gParcelFileDescriptorOffsets.mFileDescriptor == NULL, |
| 144 | "Unable to find mFileDescriptor field in android.os.ParcelFileDescriptor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | |
| 146 | return AndroidRuntime::registerNativeMethods( |
| 147 | env, kParcelFileDescriptorPathName, |
| 148 | gParcelFileDescriptorMethods, NELEM(gParcelFileDescriptorMethods)); |
| 149 | } |
| 150 | |
| 151 | } |