blob: a05d812307c8fc20faaba72a7e18b702cfee9e7c [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
23#include <utils/backup_helpers.h>
24
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 Onoratod2110db2009-05-19 13:41:21 -070031performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldState, int data,
Joe Onorato290bb012009-05-13 18:57:29 -040032 jobject newState, jobjectArray files)
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
42 char const* basePathUTF = env->GetStringUTFChars(basePath, NULL);
Joe Onorato290bb012009-05-13 18:57:29 -040043 LOGD("basePathUTF=\"%s\"\n", basePathUTF);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070044 const int fileCount = env->GetArrayLength(files);
45 char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount);
46 for (int i=0; i<fileCount; i++) {
47 filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL);
48 }
49
Joe Onoratod2110db2009-05-19 13:41:21 -070050 err = back_up_files(oldStateFD, dataStream, newStateFD, basePathUTF, filesUTF, fileCount);
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070051
52 for (int i=0; i<fileCount; i++) {
53 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
54 }
55 free(filesUTF);
56 env->ReleaseStringUTFChars(basePath, basePathUTF);
57
58 return err;
59}
60
61static const JNINativeMethod g_methods[] = {
62 { "performBackup_native",
Joe Onoratod2110db2009-05-19 13:41:21 -070063 "(Ljava/lang/String;Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;)I",
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070064 (void*)performBackup_native },
65};
66
67int register_android_backup_FileBackupHelper(JNIEnv* env)
68{
Joe Onorato290bb012009-05-13 18:57:29 -040069 LOGD("register_android_backup_FileBackupHelper");
70
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070071 jclass clazz;
72
73 clazz = env->FindClass("java/io/FileDescriptor");
74 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
75 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
76 LOG_FATAL_IF(s_descriptorField == NULL,
77 "Unable to find descriptor field in java.io.FileDescriptor");
78
79 return AndroidRuntime::registerNativeMethods(env, "android/backup/FileBackupHelper",
80 g_methods, NELEM(g_methods));
81}
82
83}