Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [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 "FileBackupHelper_native" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include "JNIHelp.h" |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 23 | #include <androidfw/BackupHelpers.h> |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android |
| 26 | { |
| 27 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 28 | // android.app.backup.BackupDataInput$EntityHeader |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 29 | static jfieldID s_keyField = 0; |
| 30 | static jfieldID s_dataSizeField = 0; |
| 31 | |
| 32 | static int |
| 33 | ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor) |
| 34 | { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 35 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 36 | if (fd == -1) { |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | return (int)new BackupDataReader(fd); |
| 41 | } |
| 42 | |
| 43 | static void |
| 44 | dtor_native(JNIEnv* env, jobject clazz, int r) |
| 45 | { |
| 46 | delete (BackupDataReader*)r; |
| 47 | } |
| 48 | |
| 49 | static jint |
| 50 | readNextHeader_native(JNIEnv* env, jobject clazz, int r, jobject entity) |
| 51 | { |
| 52 | int err; |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 53 | bool done; |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 54 | BackupDataReader* reader = (BackupDataReader*)r; |
| 55 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 56 | int type = 0; |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 57 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 58 | err = reader->ReadNextHeader(&done, &type); |
| 59 | if (done) { |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | if (err != 0) { |
| 64 | return err < 0 ? err : -1; |
| 65 | } |
| 66 | |
| 67 | switch (type) { |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 68 | case BACKUP_HEADER_ENTITY_V1: |
| 69 | { |
| 70 | String8 key; |
| 71 | size_t dataSize; |
| 72 | err = reader->ReadEntityHeader(&key, &dataSize); |
| 73 | if (err != 0) { |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 74 | return err < 0 ? err : -1; |
| 75 | } |
| 76 | // TODO: Set the fields in the entity object |
| 77 | jstring keyStr = env->NewStringUTF(key.string()); |
| 78 | env->SetObjectField(entity, s_keyField, keyStr); |
| 79 | env->SetIntField(entity, s_dataSizeField, dataSize); |
| 80 | return 0; |
| 81 | } |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 82 | default: |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 83 | ALOGD("Unknown header type: 0x%08x\n", type); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 84 | return -1; |
| 85 | } |
| 86 | |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 87 | // done |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | static jint |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 92 | readEntityData_native(JNIEnv* env, jobject clazz, int r, jbyteArray data, int offset, int size) |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 93 | { |
| 94 | int err; |
| 95 | BackupDataReader* reader = (BackupDataReader*)r; |
| 96 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 97 | if (env->GetArrayLength(data) < (size+offset)) { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 98 | // size mismatch |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | jbyte* dataBytes = env->GetByteArrayElements(data, NULL); |
| 103 | if (dataBytes == NULL) { |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 104 | return -2; |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 107 | err = reader->ReadEntityData(dataBytes+offset, size); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 108 | |
| 109 | env->ReleaseByteArrayElements(data, dataBytes, 0); |
| 110 | |
| 111 | return err; |
| 112 | } |
| 113 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 114 | static jint |
| 115 | skipEntityData_native(JNIEnv* env, jobject clazz, int r) |
| 116 | { |
| 117 | int err; |
| 118 | BackupDataReader* reader = (BackupDataReader*)r; |
| 119 | |
| 120 | err = reader->SkipEntityData(); |
| 121 | |
| 122 | return err; |
| 123 | } |
| 124 | |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 125 | static const JNINativeMethod g_methods[] = { |
| 126 | { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native }, |
| 127 | { "dtor", "(I)V", (void*)dtor_native }, |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 128 | { "readNextHeader_native", "(ILandroid/app/backup/BackupDataInput$EntityHeader;)I", |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 129 | (void*)readNextHeader_native }, |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 130 | { "readEntityData_native", "(I[BII)I", (void*)readEntityData_native }, |
| 131 | { "skipEntityData_native", "(I)I", (void*)skipEntityData_native }, |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | int register_android_backup_BackupDataInput(JNIEnv* env) |
| 135 | { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 136 | //ALOGD("register_android_backup_BackupDataInput"); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 137 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 138 | jclass clazz = env->FindClass("android/app/backup/BackupDataInput$EntityHeader"); |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 139 | LOG_FATAL_IF(clazz == NULL, "Unable to find class android.app.backup.BackupDataInput.EntityHeader"); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 140 | s_keyField = env->GetFieldID(clazz, "key", "Ljava/lang/String;"); |
| 141 | LOG_FATAL_IF(s_keyField == NULL, |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 142 | "Unable to find key field in android.app.backup.BackupDataInput.EntityHeader"); |
Joe Onorato | 03f4df4 | 2009-06-12 18:59:25 -0700 | [diff] [blame] | 143 | s_dataSizeField = env->GetFieldID(clazz, "dataSize", "I"); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 144 | LOG_FATAL_IF(s_dataSizeField == NULL, |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 145 | "Unable to find dataSize field in android.app.backup.BackupDataInput.EntityHeader"); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 146 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 147 | return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataInput", |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 148 | g_methods, NELEM(g_methods)); |
| 149 | } |
| 150 | |
| 151 | } |