blob: c6de3a52f69b0a3308c337fb48bf7ce330dfbe37 [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 Onorato290bb012009-05-13 18:57:29 -040031performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldState, jobject data,
32 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 Onoratob1a7ffe2009-05-06 18:06:21 -070040 int dataFD = env->GetIntField(data, s_descriptorField);
41
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 Onorato290bb012009-05-13 18:57:29 -040050 err = back_up_files(oldStateFD, dataFD, 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",
63 "(Ljava/lang/String;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;"
64 "Ljava/io/FileDescriptor;[Ljava/lang/String;)I",
65 (void*)performBackup_native },
66};
67
68int register_android_backup_FileBackupHelper(JNIEnv* env)
69{
Joe Onorato290bb012009-05-13 18:57:29 -040070 LOGD("register_android_backup_FileBackupHelper");
71
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070072 jclass clazz;
73
74 clazz = env->FindClass("java/io/FileDescriptor");
75 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
76 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
77 LOG_FATAL_IF(s_descriptorField == NULL,
78 "Unable to find descriptor field in java.io.FileDescriptor");
79
80 return AndroidRuntime::registerNativeMethods(env, "android/backup/FileBackupHelper",
81 g_methods, NELEM(g_methods));
82}
83
84}