Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
| 16 | |
| 17 | #define LOG_TAG "BackupHelperDispatcher_native" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include "JNIHelp.h" |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/uio.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | |
| 28 | #define VERSION_1_HEADER 0x01706c48 // 'Hlp'1 little endian |
| 29 | |
| 30 | namespace android |
| 31 | { |
| 32 | |
| 33 | struct chunk_header_v1 { |
| 34 | int headerSize; |
| 35 | int version; |
| 36 | int dataSize; // corresponds to Header.chunkSize |
| 37 | int nameLength; // not including the NULL terminator, which is not written to the file |
| 38 | }; |
| 39 | |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 40 | static jfieldID s_chunkSizeField = 0; |
| 41 | static jfieldID s_keyPrefixField = 0; |
| 42 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 43 | static jint |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 44 | readHeader_native(JNIEnv* env, jobject clazz, jobject headerObj, jobject fdObj) |
| 45 | { |
| 46 | chunk_header_v1 flattenedHeader; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 47 | ssize_t amt; |
| 48 | String8 keyPrefix; |
| 49 | char* buf; |
| 50 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 51 | int fd = jniGetFDFromFileDescriptor(env, fdObj); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 52 | |
| 53 | amt = read(fd, &flattenedHeader.headerSize, sizeof(flattenedHeader.headerSize)); |
| 54 | if (amt != sizeof(flattenedHeader.headerSize)) { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 55 | return (jint) -1; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | int remainingHeader = flattenedHeader.headerSize - sizeof(flattenedHeader.headerSize); |
| 59 | |
| 60 | if (flattenedHeader.headerSize < (int)sizeof(chunk_header_v1)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 61 | ALOGW("Skipping unknown header: %d bytes", flattenedHeader.headerSize); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 62 | if (remainingHeader > 0) { |
| 63 | lseek(fd, remainingHeader, SEEK_CUR); |
| 64 | // >0 means skip this chunk |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 65 | return (jint) 1; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | amt = read(fd, &flattenedHeader.version, |
| 70 | sizeof(chunk_header_v1)-sizeof(flattenedHeader.headerSize)); |
| 71 | if (amt <= 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 72 | ALOGW("Failed reading chunk header"); |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 73 | return (jint) -1; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 74 | } |
| 75 | remainingHeader -= sizeof(chunk_header_v1)-sizeof(flattenedHeader.headerSize); |
| 76 | |
| 77 | if (flattenedHeader.version != VERSION_1_HEADER) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 78 | ALOGW("Skipping unknown header version: 0x%08x, %d bytes", flattenedHeader.version, |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 79 | flattenedHeader.headerSize); |
| 80 | if (remainingHeader > 0) { |
| 81 | lseek(fd, remainingHeader, SEEK_CUR); |
| 82 | // >0 means skip this chunk |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 83 | return (jint) 1; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Joe Onorato | 12a4da3 | 2009-06-26 17:18:01 -0400 | [diff] [blame] | 87 | #if 0 |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 88 | ALOGD("chunk header:"); |
| 89 | ALOGD(" headerSize=%d", flattenedHeader.headerSize); |
| 90 | ALOGD(" version=0x%08x", flattenedHeader.version); |
| 91 | ALOGD(" dataSize=%d", flattenedHeader.dataSize); |
| 92 | ALOGD(" nameLength=%d", flattenedHeader.nameLength); |
Joe Onorato | 12a4da3 | 2009-06-26 17:18:01 -0400 | [diff] [blame] | 93 | #endif |
| 94 | |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 95 | if (flattenedHeader.dataSize < 0 || flattenedHeader.nameLength < 0 || |
| 96 | remainingHeader < flattenedHeader.nameLength) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 97 | ALOGW("Malformed V1 header remainingHeader=%d dataSize=%d nameLength=%d", remainingHeader, |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 98 | flattenedHeader.dataSize, flattenedHeader.nameLength); |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 99 | return (jint) -1; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | buf = keyPrefix.lockBuffer(flattenedHeader.nameLength); |
| 103 | if (buf == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 104 | ALOGW("unable to allocate %d bytes", flattenedHeader.nameLength); |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 105 | return (jint) -1; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | amt = read(fd, buf, flattenedHeader.nameLength); |
Joe Onorato | 12a4da3 | 2009-06-26 17:18:01 -0400 | [diff] [blame] | 109 | buf[flattenedHeader.nameLength] = 0; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 110 | |
| 111 | keyPrefix.unlockBuffer(flattenedHeader.nameLength); |
| 112 | |
| 113 | remainingHeader -= flattenedHeader.nameLength; |
| 114 | |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 115 | if (remainingHeader > 0) { |
| 116 | lseek(fd, remainingHeader, SEEK_CUR); |
| 117 | } |
| 118 | |
| 119 | env->SetIntField(headerObj, s_chunkSizeField, flattenedHeader.dataSize); |
| 120 | env->SetObjectField(headerObj, s_keyPrefixField, env->NewStringUTF(keyPrefix.string())); |
| 121 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 122 | return (jint) 0; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 125 | static jint |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 126 | skipChunk_native(JNIEnv* env, jobject clazz, jobject fdObj, jint bytesToSkip) |
| 127 | { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 128 | int fd = jniGetFDFromFileDescriptor(env, fdObj); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 129 | |
| 130 | lseek(fd, bytesToSkip, SEEK_CUR); |
| 131 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 132 | return (jint) 0; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int |
| 136 | padding_len(int len) |
| 137 | { |
| 138 | len = len % 4; |
| 139 | return len == 0 ? len : 4 - len; |
| 140 | } |
| 141 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 142 | static jint |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 143 | allocateHeader_native(JNIEnv* env, jobject clazz, jobject headerObj, jobject fdObj) |
| 144 | { |
| 145 | int pos; |
| 146 | jstring nameObj; |
| 147 | int nameLength; |
| 148 | int namePadding; |
| 149 | int headerSize; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 150 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 151 | int fd = jniGetFDFromFileDescriptor(env, fdObj); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 152 | |
| 153 | nameObj = (jstring)env->GetObjectField(headerObj, s_keyPrefixField); |
| 154 | |
| 155 | nameLength = env->GetStringUTFLength(nameObj); |
| 156 | namePadding = padding_len(nameLength); |
| 157 | |
| 158 | headerSize = sizeof(chunk_header_v1) + nameLength + namePadding; |
| 159 | |
| 160 | pos = lseek(fd, 0, SEEK_CUR); |
| 161 | |
| 162 | lseek(fd, headerSize, SEEK_CUR); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 163 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 164 | return (jint) pos; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 167 | static jint |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 168 | writeHeader_native(JNIEnv* env, jobject clazz, jobject headerObj, jobject fdObj, jint pos) |
| 169 | { |
| 170 | int err; |
| 171 | chunk_header_v1 header; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 172 | int namePadding; |
| 173 | int prevPos; |
| 174 | jstring nameObj; |
| 175 | const char* buf; |
| 176 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 177 | int fd = jniGetFDFromFileDescriptor(env, fdObj); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 178 | prevPos = lseek(fd, 0, SEEK_CUR); |
| 179 | |
| 180 | nameObj = (jstring)env->GetObjectField(headerObj, s_keyPrefixField); |
| 181 | header.nameLength = env->GetStringUTFLength(nameObj); |
| 182 | namePadding = padding_len(header.nameLength); |
| 183 | |
| 184 | header.headerSize = sizeof(chunk_header_v1) + header.nameLength + namePadding; |
| 185 | header.version = VERSION_1_HEADER; |
Joe Onorato | 12a4da3 | 2009-06-26 17:18:01 -0400 | [diff] [blame] | 186 | header.dataSize = prevPos - (pos + header.headerSize); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 187 | |
| 188 | lseek(fd, pos, SEEK_SET); |
| 189 | err = write(fd, &header, sizeof(chunk_header_v1)); |
| 190 | if (err != sizeof(chunk_header_v1)) { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 191 | return (jint) errno; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | buf = env->GetStringUTFChars(nameObj, NULL); |
| 195 | err = write(fd, buf, header.nameLength); |
| 196 | env->ReleaseStringUTFChars(nameObj, buf); |
| 197 | if (err != header.nameLength) { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 198 | return (jint) errno; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (namePadding != 0) { |
| 202 | int zero = 0; |
| 203 | err = write(fd, &zero, namePadding); |
| 204 | if (err != namePadding) { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 205 | return (jint) errno; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | lseek(fd, prevPos, SEEK_SET); |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 210 | return (jint) 0; |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static const JNINativeMethod g_methods[] = { |
| 214 | { "readHeader_native", |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 215 | "(Landroid/app/backup/BackupHelperDispatcher$Header;Ljava/io/FileDescriptor;)I", |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 216 | (void*)readHeader_native }, |
| 217 | { "skipChunk_native", |
| 218 | "(Ljava/io/FileDescriptor;I)I", |
| 219 | (void*)skipChunk_native }, |
| 220 | { "allocateHeader_native", |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 221 | "(Landroid/app/backup/BackupHelperDispatcher$Header;Ljava/io/FileDescriptor;)I", |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 222 | (void*)allocateHeader_native }, |
| 223 | { "writeHeader_native", |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 224 | "(Landroid/app/backup/BackupHelperDispatcher$Header;Ljava/io/FileDescriptor;I)I", |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 225 | (void*)writeHeader_native }, |
| 226 | }; |
| 227 | |
| 228 | int register_android_backup_BackupHelperDispatcher(JNIEnv* env) |
| 229 | { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 230 | jclass clazz = env->FindClass("android/app/backup/BackupHelperDispatcher$Header"); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 231 | LOG_FATAL_IF(clazz == NULL, |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 232 | "Unable to find class android.app.backup.BackupHelperDispatcher.Header"); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 233 | s_chunkSizeField = env->GetFieldID(clazz, "chunkSize", "I"); |
| 234 | LOG_FATAL_IF(s_chunkSizeField == NULL, |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 235 | "Unable to find chunkSize field in android.app.backup.BackupHelperDispatcher.Header"); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 236 | s_keyPrefixField = env->GetFieldID(clazz, "keyPrefix", "Ljava/lang/String;"); |
| 237 | LOG_FATAL_IF(s_keyPrefixField == NULL, |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 238 | "Unable to find keyPrefix field in android.app.backup.BackupHelperDispatcher.Header"); |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 239 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 240 | return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupHelperDispatcher", |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 241 | g_methods, NELEM(g_methods)); |
| 242 | } |
| 243 | |
| 244 | } |