blob: cf8a8e8a5efa44b6819bf610927a7d7f899b333a [file] [log] [blame]
Joe Onorato1cf58742009-06-12 11:06:24 -07001/*
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;
Joe Onorato5f15d152009-06-16 16:31:35 -040058 bool done;
Joe Onorato1cf58742009-06-12 11:06:24 -070059 BackupDataReader* reader = (BackupDataReader*)r;
60
Christopher Tate2fdd4282009-06-12 15:20:04 -070061 int type = 0;
Joe Onorato1cf58742009-06-12 11:06:24 -070062
Joe Onorato5f15d152009-06-16 16:31:35 -040063 err = reader->ReadNextHeader(&done, &type);
64 if (done) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070065 return 1;
66 }
67
68 if (err != 0) {
69 return err < 0 ? err : -1;
70 }
71
72 switch (type) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070073 case BACKUP_HEADER_ENTITY_V1:
74 {
75 String8 key;
76 size_t dataSize;
77 err = reader->ReadEntityHeader(&key, &dataSize);
78 if (err != 0) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070079 return err < 0 ? err : -1;
80 }
81 // TODO: Set the fields in the entity object
82 jstring keyStr = env->NewStringUTF(key.string());
83 env->SetObjectField(entity, s_keyField, keyStr);
84 env->SetIntField(entity, s_dataSizeField, dataSize);
85 return 0;
86 }
Christopher Tate2fdd4282009-06-12 15:20:04 -070087 default:
88 LOGD("Unknown header type: 0x%08x\n", type);
89 return -1;
90 }
91
Joe Onorato1cf58742009-06-12 11:06:24 -070092 // done
93 return 1;
94}
95
96static jint
Joe Onorato5f15d152009-06-16 16:31:35 -040097readEntityData_native(JNIEnv* env, jobject clazz, int r, jbyteArray data, int offset, int size)
Joe Onorato1cf58742009-06-12 11:06:24 -070098{
99 int err;
100 BackupDataReader* reader = (BackupDataReader*)r;
101
Joe Onorato5f15d152009-06-16 16:31:35 -0400102 if (env->GetArrayLength(data) < (size+offset)) {
Joe Onorato1cf58742009-06-12 11:06:24 -0700103 // size mismatch
104 return -1;
105 }
106
107 jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
108 if (dataBytes == NULL) {
Christopher Tate2fdd4282009-06-12 15:20:04 -0700109 return -2;
Joe Onorato1cf58742009-06-12 11:06:24 -0700110 }
111
Joe Onorato5f15d152009-06-16 16:31:35 -0400112 err = reader->ReadEntityData(dataBytes+offset, size);
Joe Onorato1cf58742009-06-12 11:06:24 -0700113
114 env->ReleaseByteArrayElements(data, dataBytes, 0);
115
116 return err;
117}
118
Joe Onorato5f15d152009-06-16 16:31:35 -0400119static jint
120skipEntityData_native(JNIEnv* env, jobject clazz, int r)
121{
122 int err;
123 BackupDataReader* reader = (BackupDataReader*)r;
124
125 err = reader->SkipEntityData();
126
127 return err;
128}
129
Joe Onorato1cf58742009-06-12 11:06:24 -0700130static const JNINativeMethod g_methods[] = {
131 { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native },
132 { "dtor", "(I)V", (void*)dtor_native },
133 { "readNextHeader_native", "(ILandroid/backup/BackupDataInput$EntityHeader;)I",
134 (void*)readNextHeader_native },
Joe Onorato5f15d152009-06-16 16:31:35 -0400135 { "readEntityData_native", "(I[BII)I", (void*)readEntityData_native },
136 { "skipEntityData_native", "(I)I", (void*)skipEntityData_native },
Joe Onorato1cf58742009-06-12 11:06:24 -0700137};
138
139int register_android_backup_BackupDataInput(JNIEnv* env)
140{
Christopher Tate2fdd4282009-06-12 15:20:04 -0700141 //LOGD("register_android_backup_BackupDataInput");
Joe Onorato1cf58742009-06-12 11:06:24 -0700142
143 jclass clazz;
144
145 clazz = env->FindClass("java/io/FileDescriptor");
146 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
147 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
148 LOG_FATAL_IF(s_descriptorField == NULL,
149 "Unable to find descriptor field in java.io.FileDescriptor");
150
151 clazz = env->FindClass("android/backup/BackupDataInput$EntityHeader");
152 LOG_FATAL_IF(clazz == NULL, "Unable to find class android.backup.BackupDataInput.EntityHeader");
153 s_keyField = env->GetFieldID(clazz, "key", "Ljava/lang/String;");
154 LOG_FATAL_IF(s_keyField == NULL,
155 "Unable to find key field in android.backup.BackupDataInput.EntityHeader");
Joe Onorato03f4df42009-06-12 18:59:25 -0700156 s_dataSizeField = env->GetFieldID(clazz, "dataSize", "I");
Joe Onorato1cf58742009-06-12 11:06:24 -0700157 LOG_FATAL_IF(s_dataSizeField == NULL,
158 "Unable to find dataSize field in android.backup.BackupDataInput.EntityHeader");
159
160 return AndroidRuntime::registerNativeMethods(env, "android/backup/BackupDataInput",
161 g_methods, NELEM(g_methods));
162}
163
164}