blob: 8225a36b4164813f2ff71dbf08b53e7c267723a4 [file] [log] [blame]
Joe Onoratob1a7ffe2009-05-06 18:06: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
Joe Onorato290bb012009-05-13 18:57:29 -040017#define LOG_TAG "FileBackupHelper_native"
18#include <utils/Log.h>
19
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070020#include "JNIHelp.h"
21#include <android_runtime/AndroidRuntime.h>
22
Mathias Agopian8ae23352009-06-04 13:53:57 -070023#include <utils/BackupHelpers.h>
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070024
25namespace android
26{
27
Joe Onorato1cf58742009-06-12 11:06:24 -070028// java.io.FileDescriptor
Joe Onorato290bb012009-05-13 18:57:29 -040029static jfieldID s_descriptorField = 0;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070030
31static int
Joe Onorato06290a42009-06-18 20:10:37 -070032ctor(JNIEnv* env, jobject clazz)
33{
34 return (int)new RestoreHelperBase();
35}
36
37static void
38dtor(JNIEnv* env, jobject clazz, jint ptr)
39{
40 delete (RestoreHelperBase*)ptr;
41}
42
43static int
Joe Onorato23ecae32009-06-10 17:07:15 -070044performBackup_native(JNIEnv* env, jobject clazz, jobject oldState, int data,
45 jobject newState, jobjectArray files, jobjectArray keys)
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070046{
47 int err;
48
49 // all parameters have already been checked against null
Joe Onorato290bb012009-05-13 18:57:29 -040050 int oldStateFD = oldState != NULL ? env->GetIntField(oldState, s_descriptorField) : -1;
51 int newStateFD = env->GetIntField(newState, s_descriptorField);
Joe Onoratod2110db2009-05-19 13:41:21 -070052 BackupDataWriter* dataStream = (BackupDataWriter*)data;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070053
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070054 const int fileCount = env->GetArrayLength(files);
55 char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount);
56 for (int i=0; i<fileCount; i++) {
57 filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL);
58 }
59
Joe Onorato23ecae32009-06-10 17:07:15 -070060 const int keyCount = env->GetArrayLength(keys);
61 char const** keysUTF = (char const**)malloc(sizeof(char*)*keyCount);
62 for (int i=0; i<keyCount; i++) {
63 keysUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), NULL);
64 }
65
66 err = back_up_files(oldStateFD, dataStream, newStateFD, filesUTF, keysUTF, fileCount);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070067
68 for (int i=0; i<fileCount; i++) {
69 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
70 }
71 free(filesUTF);
Joe Onorato23ecae32009-06-10 17:07:15 -070072
73 for (int i=0; i<keyCount; i++) {
74 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), keysUTF[i]);
75 }
76 free(keysUTF);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070077
78 return err;
79}
80
Joe Onorato06290a42009-06-18 20:10:37 -070081
82static int
83writeFile_native(JNIEnv* env, jobject clazz, jint ptr, jstring filenameObj, int backupReaderPtr)
84{
85 int err;
86 RestoreHelperBase* restore = (RestoreHelperBase*)ptr;
87 BackupDataReader* reader = (BackupDataReader*)backupReaderPtr;
88 char const* filename;
89
90 filename = env->GetStringUTFChars(filenameObj, NULL);
91
92 err = restore->WriteFile(String8(filename), reader);
93
94 env->ReleaseStringUTFChars(filenameObj, filename);
95
96 return err;
97}
98
99static int
100writeSnapshot_native(JNIEnv* env, jobject clazz, jint ptr, jobject fileDescriptor)
101{
102 int err;
103
104 RestoreHelperBase* restore = (RestoreHelperBase*)ptr;
105 int fd = env->GetIntField(fileDescriptor, s_descriptorField);
106
107 err = restore->WriteSnapshot(fd);
108
109 return err;
110}
111
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700112static const JNINativeMethod g_methods[] = {
Joe Onorato06290a42009-06-18 20:10:37 -0700113 { "ctor", "()I", (void*)ctor },
114 { "dtor", "(I)V", (void*)dtor },
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700115 { "performBackup_native",
Joe Onorato23ecae32009-06-10 17:07:15 -0700116 "(Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;[Ljava/lang/String;)I",
117 (void*)performBackup_native },
Joe Onorato06290a42009-06-18 20:10:37 -0700118 { "writeFile_native", "(ILjava/lang/String;I)I", (void*)writeFile_native },
119 { "writeSnapshot_native", "(ILjava/io/FileDescriptor;)I", (void*)writeSnapshot_native },
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700120};
121
Joe Onorato06290a42009-06-18 20:10:37 -0700122int register_android_backup_FileBackupHelperBase(JNIEnv* env)
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700123{
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700124 jclass clazz;
125
126 clazz = env->FindClass("java/io/FileDescriptor");
127 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
128 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
129 LOG_FATAL_IF(s_descriptorField == NULL,
130 "Unable to find descriptor field in java.io.FileDescriptor");
131
Joe Onorato06290a42009-06-18 20:10:37 -0700132 return AndroidRuntime::registerNativeMethods(env, "android/backup/FileBackupHelperBase",
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700133 g_methods, NELEM(g_methods));
134}
135
136}