blob: 0dfd8dbfb29d2f6a8bc9323ab4f1c35353911482 [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 Onoratob1a7ffe2009-05-06 18:06:21 -070028static int
Joe Onorato06290a42009-06-18 20:10:37 -070029ctor(JNIEnv* env, jobject clazz)
30{
31 return (int)new RestoreHelperBase();
32}
33
34static void
35dtor(JNIEnv* env, jobject clazz, jint ptr)
36{
37 delete (RestoreHelperBase*)ptr;
38}
39
40static int
Joe Onorato23ecae32009-06-10 17:07:15 -070041performBackup_native(JNIEnv* env, jobject clazz, jobject oldState, int data,
42 jobject newState, jobjectArray files, jobjectArray keys)
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070043{
44 int err;
45
46 // all parameters have already been checked against null
Elliott Hughesa3804cf2011-04-11 16:50:19 -070047 int oldStateFD = oldState != NULL ? jniGetFDFromFileDescriptor(env, oldState) : -1;
48 int newStateFD = jniGetFDFromFileDescriptor(env, newState);
Joe Onoratod2110db2009-05-19 13:41:21 -070049 BackupDataWriter* dataStream = (BackupDataWriter*)data;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070050
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070051 const int fileCount = env->GetArrayLength(files);
52 char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount);
53 for (int i=0; i<fileCount; i++) {
54 filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL);
55 }
56
Joe Onorato23ecae32009-06-10 17:07:15 -070057 const int keyCount = env->GetArrayLength(keys);
58 char const** keysUTF = (char const**)malloc(sizeof(char*)*keyCount);
59 for (int i=0; i<keyCount; i++) {
60 keysUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), NULL);
61 }
62
63 err = back_up_files(oldStateFD, dataStream, newStateFD, filesUTF, keysUTF, fileCount);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070064
65 for (int i=0; i<fileCount; i++) {
66 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
67 }
68 free(filesUTF);
Joe Onorato23ecae32009-06-10 17:07:15 -070069
70 for (int i=0; i<keyCount; i++) {
71 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), keysUTF[i]);
72 }
73 free(keysUTF);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070074
75 return err;
76}
77
Joe Onorato06290a42009-06-18 20:10:37 -070078
79static int
80writeFile_native(JNIEnv* env, jobject clazz, jint ptr, jstring filenameObj, int backupReaderPtr)
81{
82 int err;
83 RestoreHelperBase* restore = (RestoreHelperBase*)ptr;
84 BackupDataReader* reader = (BackupDataReader*)backupReaderPtr;
85 char const* filename;
86
87 filename = env->GetStringUTFChars(filenameObj, NULL);
88
89 err = restore->WriteFile(String8(filename), reader);
90
91 env->ReleaseStringUTFChars(filenameObj, filename);
92
93 return err;
94}
95
96static int
97writeSnapshot_native(JNIEnv* env, jobject clazz, jint ptr, jobject fileDescriptor)
98{
99 int err;
100
101 RestoreHelperBase* restore = (RestoreHelperBase*)ptr;
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700102 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
Joe Onorato06290a42009-06-18 20:10:37 -0700103
104 err = restore->WriteSnapshot(fd);
105
106 return err;
107}
108
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700109static const JNINativeMethod g_methods[] = {
Joe Onorato06290a42009-06-18 20:10:37 -0700110 { "ctor", "()I", (void*)ctor },
111 { "dtor", "(I)V", (void*)dtor },
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700112 { "performBackup_native",
Joe Onorato23ecae32009-06-10 17:07:15 -0700113 "(Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;[Ljava/lang/String;)I",
114 (void*)performBackup_native },
Joe Onorato06290a42009-06-18 20:10:37 -0700115 { "writeFile_native", "(ILjava/lang/String;I)I", (void*)writeFile_native },
116 { "writeSnapshot_native", "(ILjava/io/FileDescriptor;)I", (void*)writeSnapshot_native },
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700117};
118
Joe Onorato06290a42009-06-18 20:10:37 -0700119int register_android_backup_FileBackupHelperBase(JNIEnv* env)
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700120{
Christopher Tate7adc2742010-03-05 18:03:22 -0800121 return AndroidRuntime::registerNativeMethods(env, "android/app/backup/FileBackupHelperBase",
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700122 g_methods, NELEM(g_methods));
123}
124
125}