blob: e8d60a029749e3d1744bd5dbaec95e36799629ab [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
17#include "JNIHelp.h"
18#include <android_runtime/AndroidRuntime.h>
19
20#include <utils/backup_helpers.h>
21
22namespace android
23{
24
25static jfieldID s_descriptorField;
26
27static int
28performBackup_native(JNIEnv* env, jstring basePath,
29 jobject oldSnapshot, jobject newSnapshot,
30 jobject data, jobjectArray files)
31{
32 int err;
33
34 // all parameters have already been checked against null
35
36 int oldSnapshotFD = env->GetIntField(oldSnapshot, s_descriptorField);
37 int newSnapshotFD = env->GetIntField(newSnapshot, s_descriptorField);
38 int dataFD = env->GetIntField(data, s_descriptorField);
39
40 char const* basePathUTF = env->GetStringUTFChars(basePath, NULL);
41 const int fileCount = env->GetArrayLength(files);
42 char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount);
43 for (int i=0; i<fileCount; i++) {
44 filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL);
45 }
46
47 err = back_up_files(oldSnapshotFD, newSnapshotFD, dataFD, basePathUTF, filesUTF, fileCount);
48
49 for (int i=0; i<fileCount; i++) {
50 env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
51 }
52 free(filesUTF);
53 env->ReleaseStringUTFChars(basePath, basePathUTF);
54
55 return err;
56}
57
58static const JNINativeMethod g_methods[] = {
59 { "performBackup_native",
60 "(Ljava/lang/String;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;"
61 "Ljava/io/FileDescriptor;[Ljava/lang/String;)I",
62 (void*)performBackup_native },
63};
64
65int register_android_backup_FileBackupHelper(JNIEnv* env)
66{
67 jclass clazz;
68
69 clazz = env->FindClass("java/io/FileDescriptor");
70 LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
71 s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
72 LOG_FATAL_IF(s_descriptorField == NULL,
73 "Unable to find descriptor field in java.io.FileDescriptor");
74
75 return AndroidRuntime::registerNativeMethods(env, "android/backup/FileBackupHelper",
76 g_methods, NELEM(g_methods));
77}
78
79}