blob: 2ee064bd21f736d7ea64c730315d63e2fe51409f [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 Onorato290bb012009-05-13 18:57:29 -040028static jfieldID s_descriptorField = 0;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070029
30static int
Joe Onorato23ecae32009-06-10 17:07:15 -070031performBackup_native(JNIEnv* env, jobject clazz, jobject oldState, int data,
32 jobject newState, jobjectArray files, jobjectArray keys)
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070033{
34 int err;
35
36 // all parameters have already been checked against null
Joe Onorato290bb012009-05-13 18:57:29 -040037 LOGD("oldState=%p newState=%p data=%p\n", oldState, newState, data);
38 int oldStateFD = oldState != NULL ? env->GetIntField(oldState, s_descriptorField) : -1;
39 int newStateFD = env->GetIntField(newState, s_descriptorField);
Joe Onoratod2110db2009-05-19 13:41:21 -070040 BackupDataWriter* dataStream = (BackupDataWriter*)data;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070041
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070042 const int fileCount = env->GetArrayLength(files);
43 char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount);
44 for (int i=0; i<fileCount; i++) {
45 filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL);
46 }
47
Joe Onorato23ecae32009-06-10 17:07:15 -070048 const int keyCount = env->GetArrayLength(keys);
49 char const** keysUTF = (char const**)malloc(sizeof(char*)*keyCount);
50 for (int i=0; i<keyCount; i++) {
51 keysUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), NULL);
52 }
53
54 err = back_up_files(oldStateFD, dataStream, newStateFD, filesUTF, keysUTF, fileCount);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070055
56 for (int i=0; i<fileCount; i++) {
57 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
58 }
59 free(filesUTF);
Joe Onorato23ecae32009-06-10 17:07:15 -070060
61 for (int i=0; i<keyCount; i++) {
62 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), keysUTF[i]);
63 }
64 free(keysUTF);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070065
66 return err;
67}
68
69static const JNINativeMethod g_methods[] = {
70 { "performBackup_native",
Joe Onorato23ecae32009-06-10 17:07:15 -070071 "(Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;[Ljava/lang/String;)I",
72 (void*)performBackup_native },
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070073};
74
75int register_android_backup_FileBackupHelper(JNIEnv* env)
76{
Joe Onorato290bb012009-05-13 18:57:29 -040077 LOGD("register_android_backup_FileBackupHelper");
78
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070079 jclass clazz;
80
81 clazz = env->FindClass("java/io/FileDescriptor");
82 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
83 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
84 LOG_FATAL_IF(s_descriptorField == NULL,
85 "Unable to find descriptor field in java.io.FileDescriptor");
86
87 return AndroidRuntime::registerNativeMethods(env, "android/backup/FileBackupHelper",
88 g_methods, NELEM(g_methods));
89}
90
91}