android_backup_BackupDataInput.cpp revision 2fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2
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 23#include <utils/BackupHelpers.h> 24 25namespace android 26{ 27 28// java.io.FileDescriptor 29static jfieldID s_descriptorField = 0; 30 31// android.backup.BackupDataInput$EntityHeader 32static jfieldID s_keyField = 0; 33static jfieldID s_dataSizeField = 0; 34 35static int 36ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor) 37{ 38 int err; 39 40 int fd = env->GetIntField(fileDescriptor, s_descriptorField); 41 if (fd == -1) { 42 return NULL; 43 } 44 45 return (int)new BackupDataReader(fd); 46} 47 48static void 49dtor_native(JNIEnv* env, jobject clazz, int r) 50{ 51 delete (BackupDataReader*)r; 52} 53 54static jint 55readNextHeader_native(JNIEnv* env, jobject clazz, int r, jobject entity) 56{ 57 int err; 58 BackupDataReader* reader = (BackupDataReader*)r; 59 60 err = reader->Status(); 61 if (err != 0) { 62 return err < 0 ? err : -1; 63 } 64 65 int type = 0; 66 67 err = reader->ReadNextHeader(&type); 68 if (err == EIO) { 69 // Clean EOF with no footer block; just claim we're done 70 return 1; 71 } 72 73 if (err != 0) { 74 return err < 0 ? err : -1; 75 } 76 77 switch (type) { 78 case BACKUP_HEADER_APP_V1: 79 { 80 String8 packageName; 81 int cookie; 82 err = reader->ReadAppHeader(&packageName, &cookie); 83 if (err != 0) { 84 LOGD("ReadAppHeader() returned %d; aborting", err); 85 return err < 0 ? err : -1; 86 } 87 break; 88 } 89 case BACKUP_HEADER_ENTITY_V1: 90 { 91 String8 key; 92 size_t dataSize; 93 err = reader->ReadEntityHeader(&key, &dataSize); 94 if (err != 0) { 95 LOGD("ReadEntityHeader(); aborting", err); 96 return err < 0 ? err : -1; 97 } 98 // TODO: Set the fields in the entity object 99 jstring keyStr = env->NewStringUTF(key.string()); 100 env->SetObjectField(entity, s_keyField, keyStr); 101 env->SetIntField(entity, s_dataSizeField, dataSize); 102 return 0; 103 } 104 case BACKUP_FOOTER_APP_V1: 105 { 106 break; 107 } 108 default: 109 LOGD("Unknown header type: 0x%08x\n", type); 110 return -1; 111 } 112 113 // done 114 return 1; 115} 116 117static jint 118readEntityData_native(JNIEnv* env, jobject clazz, int r, jbyteArray data, int size) 119{ 120 int err; 121 BackupDataReader* reader = (BackupDataReader*)r; 122 123 if (env->GetArrayLength(data) < size) { 124 // size mismatch 125 return -1; 126 } 127 128 jbyte* dataBytes = env->GetByteArrayElements(data, NULL); 129 if (dataBytes == NULL) { 130 return -2; 131 } 132 133 err = reader->ReadEntityData(dataBytes, size); 134 135 env->ReleaseByteArrayElements(data, dataBytes, 0); 136 137 return err; 138} 139 140static const JNINativeMethod g_methods[] = { 141 { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native }, 142 { "dtor", "(I)V", (void*)dtor_native }, 143 { "readNextHeader_native", "(ILandroid/backup/BackupDataInput$EntityHeader;)I", 144 (void*)readNextHeader_native }, 145 { "readEntityData_native", "(I[BI)I", (void*)readEntityData_native }, 146}; 147 148int register_android_backup_BackupDataInput(JNIEnv* env) 149{ 150 //LOGD("register_android_backup_BackupDataInput"); 151 152 jclass clazz; 153 154 clazz = env->FindClass("java/io/FileDescriptor"); 155 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); 156 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I"); 157 LOG_FATAL_IF(s_descriptorField == NULL, 158 "Unable to find descriptor field in java.io.FileDescriptor"); 159 160 clazz = env->FindClass("android/backup/BackupDataInput$EntityHeader"); 161 LOG_FATAL_IF(clazz == NULL, "Unable to find class android.backup.BackupDataInput.EntityHeader"); 162 s_keyField = env->GetFieldID(clazz, "key", "Ljava/lang/String;"); 163 LOG_FATAL_IF(s_keyField == NULL, 164 "Unable to find key field in android.backup.BackupDataInput.EntityHeader"); 165 s_dataSizeField = env->GetFieldID(clazz, "dataSize", "I"); 166 LOG_FATAL_IF(s_dataSizeField == NULL, 167 "Unable to find dataSize field in android.backup.BackupDataInput.EntityHeader"); 168 169 return AndroidRuntime::registerNativeMethods(env, "android/backup/BackupDataInput", 170 g_methods, NELEM(g_methods)); 171} 172 173} 174