blob: 8244e1bfdbf15d3b7ccad71083c584ebb5598c94 [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
20#include "JNIHelp.h"
21#include <android_runtime/AndroidRuntime.h>
22
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{
88 int err;
89 BackupDataWriter* writer = (BackupDataWriter*)w;
90
91 const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL);
92 String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : "");
93
94 writer->SetKeyPrefix(keyPrefix);
95
96 env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF);
97}
98
Joe Onoratod2110db2009-05-19 13:41:21 -070099static const JNINativeMethod g_methods[] = {
Ashok Bhat58b8b242014-01-02 16:52:41 +0000100 { "ctor", "(Ljava/io/FileDescriptor;)J", (void*)ctor_native },
101 { "dtor", "(J)V", (void*)dtor_native },
102 { "writeEntityHeader_native", "(JLjava/lang/String;I)I", (void*)writeEntityHeader_native },
103 { "writeEntityData_native", "(J[BI)I", (void*)writeEntityData_native },
104 { "setKeyPrefix_native", "(JLjava/lang/String;)V", (void*)setKeyPrefix_native },
Joe Onoratod2110db2009-05-19 13:41:21 -0700105};
106
107int register_android_backup_BackupDataOutput(JNIEnv* env)
108{
Steve Block5baa3a62011-12-20 16:23:08 +0000109 //ALOGD("register_android_backup_BackupDataOutput");
Christopher Tate7adc2742010-03-05 18:03:22 -0800110 return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataOutput",
Joe Onoratod2110db2009-05-19 13:41:21 -0700111 g_methods, NELEM(g_methods));
112}
113
114}