blob: 4f5d1f80dffcda5bf429f4088a1547b7b6453558 [file] [log] [blame]
Joe Onoratod2110db2009-05-19 13:41:21 -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>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080021#include "core_jni_helpers.h"
Joe Onoratod2110db2009-05-19 13:41:21 -070022
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080023#include <androidfw/BackupHelpers.h>
Joe Onoratod2110db2009-05-19 13:41:21 -070024
25namespace android
26{
27
Ashok Bhat58b8b242014-01-02 16:52:41 +000028static jlong
Joe Onorato1cf58742009-06-12 11:06:24 -070029ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor)
Joe Onoratod2110db2009-05-19 13:41:21 -070030{
Elliott Hughesa3804cf2011-04-11 16:50:19 -070031 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
Joe Onoratod2110db2009-05-19 13:41:21 -070032 if (fd == -1) {
Ashok Bhat58b8b242014-01-02 16:52:41 +000033 return (jlong)NULL;
Joe Onoratod2110db2009-05-19 13:41:21 -070034 }
35
Ashok Bhat58b8b242014-01-02 16:52:41 +000036 return (jlong)new BackupDataWriter(fd);
Joe Onoratod2110db2009-05-19 13:41:21 -070037}
38
39static void
Ashok Bhat58b8b242014-01-02 16:52:41 +000040dtor_native(JNIEnv* env, jobject clazz, jlong w)
Joe Onoratod2110db2009-05-19 13:41:21 -070041{
Joe Onorato1cf58742009-06-12 11:06:24 -070042 delete (BackupDataWriter*)w;
43}
44
45static jint
Ashok Bhat58b8b242014-01-02 16:52:41 +000046writeEntityHeader_native(JNIEnv* env, jobject clazz, jlong w, jstring key, jint dataSize)
Joe Onorato1cf58742009-06-12 11:06:24 -070047{
48 int err;
49 BackupDataWriter* writer = (BackupDataWriter*)w;
50
51 const char* keyUTF = env->GetStringUTFChars(key, NULL);
52 if (keyUTF == NULL) {
53 return -1;
54 }
Joe Onorato1cf58742009-06-12 11:06:24 -070055 err = writer->WriteEntityHeader(String8(keyUTF), dataSize);
56
57 env->ReleaseStringUTFChars(key, keyUTF);
58
Ashok Bhat58b8b242014-01-02 16:52:41 +000059 return (jint)err;
Joe Onorato1cf58742009-06-12 11:06:24 -070060}
61
62static jint
Ashok Bhat58b8b242014-01-02 16:52:41 +000063writeEntityData_native(JNIEnv* env, jobject clazz, jlong w, jbyteArray data, jint size)
Joe Onorato1cf58742009-06-12 11:06:24 -070064{
65 int err;
66 BackupDataWriter* writer = (BackupDataWriter*)w;
67
Dan Egnor6d877382009-07-09 13:48:36 -070068 if (env->GetArrayLength(data) < size) {
Joe Onorato1cf58742009-06-12 11:06:24 -070069 // size mismatch
70 return -1;
71 }
72
73 jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
74 if (dataBytes == NULL) {
75 return -1;
76 }
77
78 err = writer->WriteEntityData(dataBytes, size);
79
80 env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT);
81
Ashok Bhat58b8b242014-01-02 16:52:41 +000082 return (jint)err;
Joe Onoratod2110db2009-05-19 13:41:21 -070083}
84
Joe Onorato06290a42009-06-18 20:10:37 -070085static void
Ashok Bhat58b8b242014-01-02 16:52:41 +000086setKeyPrefix_native(JNIEnv* env, jobject clazz, jlong w, jstring keyPrefixObj)
Joe Onorato06290a42009-06-18 20:10:37 -070087{
Joe Onorato06290a42009-06-18 20:10:37 -070088 BackupDataWriter* writer = (BackupDataWriter*)w;
89
90 const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL);
91 String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : "");
92
93 writer->SetKeyPrefix(keyPrefix);
94
95 env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF);
96}
97
Joe Onoratod2110db2009-05-19 13:41:21 -070098static const JNINativeMethod g_methods[] = {
Ashok Bhat58b8b242014-01-02 16:52:41 +000099 { "ctor", "(Ljava/io/FileDescriptor;)J", (void*)ctor_native },
100 { "dtor", "(J)V", (void*)dtor_native },
101 { "writeEntityHeader_native", "(JLjava/lang/String;I)I", (void*)writeEntityHeader_native },
102 { "writeEntityData_native", "(J[BI)I", (void*)writeEntityData_native },
103 { "setKeyPrefix_native", "(JLjava/lang/String;)V", (void*)setKeyPrefix_native },
Joe Onoratod2110db2009-05-19 13:41:21 -0700104};
105
106int register_android_backup_BackupDataOutput(JNIEnv* env)
107{
Steve Block5baa3a62011-12-20 16:23:08 +0000108 //ALOGD("register_android_backup_BackupDataOutput");
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800109 return RegisterMethodsOrDie(env, "android/app/backup/BackupDataOutput",
Joe Onoratod2110db2009-05-19 13:41:21 -0700110 g_methods, NELEM(g_methods));
111}
112
113}