blob: 144a10ced67ca0a5f0657ed4b86b09b71591dae6 [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 Agopian8ae23352009-06-04 13:53:57 -070023#include <utils/BackupHelpers.h>
Joe Onoratod2110db2009-05-19 13:41:21 -070024
25namespace android
26{
27
Joe Onoratod2110db2009-05-19 13:41:21 -070028static int
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) {
33 return NULL;
34 }
35
36 return (int)new BackupDataWriter(fd);
37}
38
39static void
Joe Onorato1cf58742009-06-12 11:06:24 -070040dtor_native(JNIEnv* env, jobject clazz, int w)
Joe Onoratod2110db2009-05-19 13:41:21 -070041{
Joe Onorato1cf58742009-06-12 11:06:24 -070042 delete (BackupDataWriter*)w;
43}
44
45static jint
46writeEntityHeader_native(JNIEnv* env, jobject clazz, int w, jstring key, int dataSize)
47{
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 }
55
56 err = writer->WriteEntityHeader(String8(keyUTF), dataSize);
57
58 env->ReleaseStringUTFChars(key, keyUTF);
59
60 return err;
61}
62
63static jint
64writeEntityData_native(JNIEnv* env, jobject clazz, int w, jbyteArray data, int size)
65{
66 int err;
67 BackupDataWriter* writer = (BackupDataWriter*)w;
68
Dan Egnor6d877382009-07-09 13:48:36 -070069 if (env->GetArrayLength(data) < size) {
Joe Onorato1cf58742009-06-12 11:06:24 -070070 // size mismatch
71 return -1;
72 }
73
74 jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
75 if (dataBytes == NULL) {
76 return -1;
77 }
78
79 err = writer->WriteEntityData(dataBytes, size);
80
81 env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT);
82
83 return err;
Joe Onoratod2110db2009-05-19 13:41:21 -070084}
85
Joe Onorato06290a42009-06-18 20:10:37 -070086static void
87setKeyPrefix_native(JNIEnv* env, jobject clazz, int w, jstring keyPrefixObj)
88{
89 int err;
90 BackupDataWriter* writer = (BackupDataWriter*)w;
91
92 const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL);
93 String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : "");
94
95 writer->SetKeyPrefix(keyPrefix);
96
97 env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF);
98}
99
Joe Onoratod2110db2009-05-19 13:41:21 -0700100static const JNINativeMethod g_methods[] = {
101 { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native },
102 { "dtor", "(I)V", (void*)dtor_native },
Joe Onorato1cf58742009-06-12 11:06:24 -0700103 { "writeEntityHeader_native", "(ILjava/lang/String;I)I", (void*)writeEntityHeader_native },
104 { "writeEntityData_native", "(I[BI)I", (void*)writeEntityData_native },
Joe Onorato06290a42009-06-18 20:10:37 -0700105 { "setKeyPrefix_native", "(ILjava/lang/String;)V", (void*)setKeyPrefix_native },
Joe Onoratod2110db2009-05-19 13:41:21 -0700106};
107
108int register_android_backup_BackupDataOutput(JNIEnv* env)
109{
Christopher Tate2fdd4282009-06-12 15:20:04 -0700110 //LOGD("register_android_backup_BackupDataOutput");
Christopher Tate7adc2742010-03-05 18:03:22 -0800111 return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataOutput",
Joe Onoratod2110db2009-05-19 13:41:21 -0700112 g_methods, NELEM(g_methods));
113}
114
115}