blob: 2fb0076ff1e1590af1647124f65d066a69f9bb6f [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
Christopher Tate45281862010-03-05 15:46:30 -080028// android.app.backup.BackupDataInput$EntityHeader
Joe Onorato1cf58742009-06-12 11:06:24 -070029static jfieldID s_keyField = 0;
30static jfieldID s_dataSizeField = 0;
31
32static int
33ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor)
34{
Elliott Hughesa3804cf2011-04-11 16:50:19 -070035 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
Joe Onorato1cf58742009-06-12 11:06:24 -070036 if (fd == -1) {
37 return NULL;
38 }
39
40 return (int)new BackupDataReader(fd);
41}
42
43static void
44dtor_native(JNIEnv* env, jobject clazz, int r)
45{
46 delete (BackupDataReader*)r;
47}
48
49static jint
50readNextHeader_native(JNIEnv* env, jobject clazz, int r, jobject entity)
51{
52 int err;
Joe Onorato5f15d152009-06-16 16:31:35 -040053 bool done;
Joe Onorato1cf58742009-06-12 11:06:24 -070054 BackupDataReader* reader = (BackupDataReader*)r;
55
Christopher Tate2fdd4282009-06-12 15:20:04 -070056 int type = 0;
Joe Onorato1cf58742009-06-12 11:06:24 -070057
Joe Onorato5f15d152009-06-16 16:31:35 -040058 err = reader->ReadNextHeader(&done, &type);
59 if (done) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070060 return 1;
61 }
62
63 if (err != 0) {
64 return err < 0 ? err : -1;
65 }
66
67 switch (type) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070068 case BACKUP_HEADER_ENTITY_V1:
69 {
70 String8 key;
71 size_t dataSize;
72 err = reader->ReadEntityHeader(&key, &dataSize);
73 if (err != 0) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070074 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 Tate2fdd4282009-06-12 15:20:04 -070082 default:
Steve Block5baa3a62011-12-20 16:23:08 +000083 ALOGD("Unknown header type: 0x%08x\n", type);
Christopher Tate2fdd4282009-06-12 15:20:04 -070084 return -1;
85 }
86
Joe Onorato1cf58742009-06-12 11:06:24 -070087 // done
88 return 1;
89}
90
91static jint
Joe Onorato5f15d152009-06-16 16:31:35 -040092readEntityData_native(JNIEnv* env, jobject clazz, int r, jbyteArray data, int offset, int size)
Joe Onorato1cf58742009-06-12 11:06:24 -070093{
94 int err;
95 BackupDataReader* reader = (BackupDataReader*)r;
96
Joe Onorato5f15d152009-06-16 16:31:35 -040097 if (env->GetArrayLength(data) < (size+offset)) {
Joe Onorato1cf58742009-06-12 11:06:24 -070098 // size mismatch
99 return -1;
100 }
101
102 jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
103 if (dataBytes == NULL) {
Christopher Tate2fdd4282009-06-12 15:20:04 -0700104 return -2;
Joe Onorato1cf58742009-06-12 11:06:24 -0700105 }
106
Joe Onorato5f15d152009-06-16 16:31:35 -0400107 err = reader->ReadEntityData(dataBytes+offset, size);
Joe Onorato1cf58742009-06-12 11:06:24 -0700108
109 env->ReleaseByteArrayElements(data, dataBytes, 0);
110
111 return err;
112}
113
Joe Onorato5f15d152009-06-16 16:31:35 -0400114static jint
115skipEntityData_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 Onorato1cf58742009-06-12 11:06:24 -0700125static const JNINativeMethod g_methods[] = {
126 { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native },
127 { "dtor", "(I)V", (void*)dtor_native },
Christopher Tate45281862010-03-05 15:46:30 -0800128 { "readNextHeader_native", "(ILandroid/app/backup/BackupDataInput$EntityHeader;)I",
Joe Onorato1cf58742009-06-12 11:06:24 -0700129 (void*)readNextHeader_native },
Joe Onorato5f15d152009-06-16 16:31:35 -0400130 { "readEntityData_native", "(I[BII)I", (void*)readEntityData_native },
131 { "skipEntityData_native", "(I)I", (void*)skipEntityData_native },
Joe Onorato1cf58742009-06-12 11:06:24 -0700132};
133
134int register_android_backup_BackupDataInput(JNIEnv* env)
135{
Steve Block5baa3a62011-12-20 16:23:08 +0000136 //ALOGD("register_android_backup_BackupDataInput");
Joe Onorato1cf58742009-06-12 11:06:24 -0700137
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700138 jclass clazz = env->FindClass("android/app/backup/BackupDataInput$EntityHeader");
Christopher Tate45281862010-03-05 15:46:30 -0800139 LOG_FATAL_IF(clazz == NULL, "Unable to find class android.app.backup.BackupDataInput.EntityHeader");
Joe Onorato1cf58742009-06-12 11:06:24 -0700140 s_keyField = env->GetFieldID(clazz, "key", "Ljava/lang/String;");
141 LOG_FATAL_IF(s_keyField == NULL,
Christopher Tate45281862010-03-05 15:46:30 -0800142 "Unable to find key field in android.app.backup.BackupDataInput.EntityHeader");
Joe Onorato03f4df42009-06-12 18:59:25 -0700143 s_dataSizeField = env->GetFieldID(clazz, "dataSize", "I");
Joe Onorato1cf58742009-06-12 11:06:24 -0700144 LOG_FATAL_IF(s_dataSizeField == NULL,
Christopher Tate45281862010-03-05 15:46:30 -0800145 "Unable to find dataSize field in android.app.backup.BackupDataInput.EntityHeader");
Joe Onorato1cf58742009-06-12 11:06:24 -0700146
Christopher Tate45281862010-03-05 15:46:30 -0800147 return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataInput",
Joe Onorato1cf58742009-06-12 11:06:24 -0700148 g_methods, NELEM(g_methods));
149}
150
151}