blob: ce30aaa89006855ec84e73eeee70e90ae6e4661a [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
28static jfieldID s_descriptorField = 0;
29
30static int
Joe Onorato1cf58742009-06-12 11:06:24 -070031ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor)
Joe Onoratod2110db2009-05-19 13:41:21 -070032{
33 int err;
34
35 int fd = env->GetIntField(fileDescriptor, s_descriptorField);
36 if (fd == -1) {
37 return NULL;
38 }
39
40 return (int)new BackupDataWriter(fd);
41}
42
43static void
Joe Onorato1cf58742009-06-12 11:06:24 -070044dtor_native(JNIEnv* env, jobject clazz, int w)
Joe Onoratod2110db2009-05-19 13:41:21 -070045{
Joe Onorato1cf58742009-06-12 11:06:24 -070046 delete (BackupDataWriter*)w;
47}
48
49static jint
50writeEntityHeader_native(JNIEnv* env, jobject clazz, int w, jstring key, int dataSize)
51{
52 int err;
53 BackupDataWriter* writer = (BackupDataWriter*)w;
54
55 const char* keyUTF = env->GetStringUTFChars(key, NULL);
56 if (keyUTF == NULL) {
57 return -1;
58 }
59
60 err = writer->WriteEntityHeader(String8(keyUTF), dataSize);
61
62 env->ReleaseStringUTFChars(key, keyUTF);
63
64 return err;
65}
66
67static jint
68writeEntityData_native(JNIEnv* env, jobject clazz, int w, jbyteArray data, int size)
69{
70 int err;
71 BackupDataWriter* writer = (BackupDataWriter*)w;
72
Dan Egnor6d877382009-07-09 13:48:36 -070073 if (env->GetArrayLength(data) < size) {
Joe Onorato1cf58742009-06-12 11:06:24 -070074 // size mismatch
75 return -1;
76 }
77
78 jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
79 if (dataBytes == NULL) {
80 return -1;
81 }
82
83 err = writer->WriteEntityData(dataBytes, size);
84
85 env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT);
86
87 return err;
Joe Onoratod2110db2009-05-19 13:41:21 -070088}
89
Joe Onorato06290a42009-06-18 20:10:37 -070090static void
91setKeyPrefix_native(JNIEnv* env, jobject clazz, int w, jstring keyPrefixObj)
92{
93 int err;
94 BackupDataWriter* writer = (BackupDataWriter*)w;
95
96 const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL);
97 String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : "");
98
99 writer->SetKeyPrefix(keyPrefix);
100
101 env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF);
102}
103
Joe Onoratod2110db2009-05-19 13:41:21 -0700104static const JNINativeMethod g_methods[] = {
105 { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native },
106 { "dtor", "(I)V", (void*)dtor_native },
Joe Onorato1cf58742009-06-12 11:06:24 -0700107 { "writeEntityHeader_native", "(ILjava/lang/String;I)I", (void*)writeEntityHeader_native },
108 { "writeEntityData_native", "(I[BI)I", (void*)writeEntityData_native },
Joe Onorato06290a42009-06-18 20:10:37 -0700109 { "setKeyPrefix_native", "(ILjava/lang/String;)V", (void*)setKeyPrefix_native },
Joe Onoratod2110db2009-05-19 13:41:21 -0700110};
111
112int register_android_backup_BackupDataOutput(JNIEnv* env)
113{
Christopher Tate2fdd4282009-06-12 15:20:04 -0700114 //LOGD("register_android_backup_BackupDataOutput");
Joe Onoratod2110db2009-05-19 13:41:21 -0700115
116 jclass clazz;
117
118 clazz = env->FindClass("java/io/FileDescriptor");
119 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
120 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
121 LOG_FATAL_IF(s_descriptorField == NULL,
122 "Unable to find descriptor field in java.io.FileDescriptor");
123
124 return AndroidRuntime::registerNativeMethods(env, "android/backup/BackupDataOutput",
125 g_methods, NELEM(g_methods));
126}
127
128}