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