blob: aa8acc16bc3f25042e8c82131747f2834dca352f [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
Steven Moreland2279b252017-07-19 09:50:45 -070020#include <nativehelper/JNIHelp.h>
Joe Onorato1cf58742009-06-12 11:06:24 -070021#include <android_runtime/AndroidRuntime.h>
22
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080023#include <androidfw/BackupHelpers.h>
Joe Onorato1cf58742009-06-12 11:06:24 -070024
Andreas Gampe987f79f2014-11-18 17:29:46 -080025#include "core_jni_helpers.h"
26
Joe Onorato1cf58742009-06-12 11:06:24 -070027namespace android
28{
29
Christopher Tate45281862010-03-05 15:46:30 -080030// android.app.backup.BackupDataInput$EntityHeader
Joe Onorato1cf58742009-06-12 11:06:24 -070031static jfieldID s_keyField = 0;
32static jfieldID s_dataSizeField = 0;
33
Ashok Bhat58b8b242014-01-02 16:52:41 +000034static jlong
Joe Onorato1cf58742009-06-12 11:06:24 -070035ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor)
36{
Elliott Hughesa3804cf2011-04-11 16:50:19 -070037 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
Joe Onorato1cf58742009-06-12 11:06:24 -070038 if (fd == -1) {
Ashok Bhat58b8b242014-01-02 16:52:41 +000039 return (jlong)NULL;
Joe Onorato1cf58742009-06-12 11:06:24 -070040 }
41
Ashok Bhat58b8b242014-01-02 16:52:41 +000042 return (jlong)new BackupDataReader(fd);
Joe Onorato1cf58742009-06-12 11:06:24 -070043}
44
45static void
Ashok Bhat58b8b242014-01-02 16:52:41 +000046dtor_native(JNIEnv* env, jobject clazz, jlong r)
Joe Onorato1cf58742009-06-12 11:06:24 -070047{
48 delete (BackupDataReader*)r;
49}
50
51static jint
Ashok Bhat58b8b242014-01-02 16:52:41 +000052readNextHeader_native(JNIEnv* env, jobject clazz, jlong r, jobject entity)
Joe Onorato1cf58742009-06-12 11:06:24 -070053{
54 int err;
Joe Onorato5f15d152009-06-16 16:31:35 -040055 bool done;
Joe Onorato1cf58742009-06-12 11:06:24 -070056 BackupDataReader* reader = (BackupDataReader*)r;
57
Christopher Tate2fdd4282009-06-12 15:20:04 -070058 int type = 0;
Joe Onorato1cf58742009-06-12 11:06:24 -070059
Joe Onorato5f15d152009-06-16 16:31:35 -040060 err = reader->ReadNextHeader(&done, &type);
61 if (done) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070062 return 1;
63 }
64
65 if (err != 0) {
66 return err < 0 ? err : -1;
67 }
68
69 switch (type) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070070 case BACKUP_HEADER_ENTITY_V1:
71 {
72 String8 key;
73 size_t dataSize;
74 err = reader->ReadEntityHeader(&key, &dataSize);
75 if (err != 0) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070076 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 Tate2fdd4282009-06-12 15:20:04 -070084 default:
Steve Block5baa3a62011-12-20 16:23:08 +000085 ALOGD("Unknown header type: 0x%08x\n", type);
Christopher Tate2fdd4282009-06-12 15:20:04 -070086 return -1;
87 }
88
Joe Onorato1cf58742009-06-12 11:06:24 -070089 // done
90 return 1;
91}
92
93static jint
Ashok Bhat58b8b242014-01-02 16:52:41 +000094readEntityData_native(JNIEnv* env, jobject clazz, jlong r, jbyteArray data, jint offset, jint size)
Joe Onorato1cf58742009-06-12 11:06:24 -070095{
96 int err;
97 BackupDataReader* reader = (BackupDataReader*)r;
98
Joe Onorato5f15d152009-06-16 16:31:35 -040099 if (env->GetArrayLength(data) < (size+offset)) {
Joe Onorato1cf58742009-06-12 11:06:24 -0700100 // size mismatch
101 return -1;
102 }
103
104 jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
105 if (dataBytes == NULL) {
Christopher Tate2fdd4282009-06-12 15:20:04 -0700106 return -2;
Joe Onorato1cf58742009-06-12 11:06:24 -0700107 }
108
Joe Onorato5f15d152009-06-16 16:31:35 -0400109 err = reader->ReadEntityData(dataBytes+offset, size);
Joe Onorato1cf58742009-06-12 11:06:24 -0700110
111 env->ReleaseByteArrayElements(data, dataBytes, 0);
112
113 return err;
114}
115
Joe Onorato5f15d152009-06-16 16:31:35 -0400116static jint
Ashok Bhat58b8b242014-01-02 16:52:41 +0000117skipEntityData_native(JNIEnv* env, jobject clazz, jlong r)
Joe Onorato5f15d152009-06-16 16:31:35 -0400118{
119 int err;
120 BackupDataReader* reader = (BackupDataReader*)r;
121
122 err = reader->SkipEntityData();
123
124 return err;
125}
126
Joe Onorato1cf58742009-06-12 11:06:24 -0700127static const JNINativeMethod g_methods[] = {
Ashok Bhat58b8b242014-01-02 16:52:41 +0000128 { "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 Onorato1cf58742009-06-12 11:06:24 -0700131 (void*)readNextHeader_native },
Ashok Bhat58b8b242014-01-02 16:52:41 +0000132 { "readEntityData_native", "(J[BII)I", (void*)readEntityData_native },
133 { "skipEntityData_native", "(J)I", (void*)skipEntityData_native },
Joe Onorato1cf58742009-06-12 11:06:24 -0700134};
135
136int register_android_backup_BackupDataInput(JNIEnv* env)
137{
Steve Block5baa3a62011-12-20 16:23:08 +0000138 //ALOGD("register_android_backup_BackupDataInput");
Joe Onorato1cf58742009-06-12 11:06:24 -0700139
Andreas Gampe987f79f2014-11-18 17:29:46 -0800140 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 Onorato1cf58742009-06-12 11:06:24 -0700143
Andreas Gampe987f79f2014-11-18 17:29:46 -0800144 return RegisterMethodsOrDie(env, "android/app/backup/BackupDataInput", g_methods,
145 NELEM(g_methods));
Joe Onorato1cf58742009-06-12 11:06:24 -0700146}
147
148}