blob: 65840ee2c0166a520478d8e63361159c4fb3b8ab [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
Steven Moreland2279b252017-07-19 09:50:45 -070020#include <nativehelper/JNIHelp.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080021#include "core_jni_helpers.h"
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070022
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080023#include <androidfw/BackupHelpers.h>
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070024
25namespace android
26{
27
Ashok Bhat58b8b242014-01-02 16:52:41 +000028static jlong
Joe Onorato06290a42009-06-18 20:10:37 -070029ctor(JNIEnv* env, jobject clazz)
30{
Ashok Bhat58b8b242014-01-02 16:52:41 +000031 return (jlong)new RestoreHelperBase();
Joe Onorato06290a42009-06-18 20:10:37 -070032}
33
34static void
Ashok Bhat58b8b242014-01-02 16:52:41 +000035dtor(JNIEnv* env, jobject clazz, jlong ptr)
Joe Onorato06290a42009-06-18 20:10:37 -070036{
37 delete (RestoreHelperBase*)ptr;
38}
39
Ashok Bhat58b8b242014-01-02 16:52:41 +000040static jint
41performBackup_native(JNIEnv* env, jobject clazz, jobject oldState, jlong data,
Joe Onorato23ecae32009-06-10 17:07:15 -070042 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
Ashok Bhat58b8b242014-01-02 16:52:41 +000075 return (jint) err;
Joe Onoratob1a7ffe2009-05-06 18:06:21 -070076}
77
Joe Onorato06290a42009-06-18 20:10:37 -070078
Ashok Bhat58b8b242014-01-02 16:52:41 +000079static jint
80writeFile_native(JNIEnv* env, jobject clazz, jlong ptr, jstring filenameObj, jlong backupReaderPtr)
Joe Onorato06290a42009-06-18 20:10:37 -070081{
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
Ashok Bhat58b8b242014-01-02 16:52:41 +000093 return (jint) err;
Joe Onorato06290a42009-06-18 20:10:37 -070094}
95
Ashok Bhat58b8b242014-01-02 16:52:41 +000096static jint
97writeSnapshot_native(JNIEnv* env, jobject clazz, jlong ptr, jobject fileDescriptor)
Joe Onorato06290a42009-06-18 20:10:37 -070098{
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
Ashok Bhat58b8b242014-01-02 16:52:41 +0000106 return (jint) err;
Joe Onorato06290a42009-06-18 20:10:37 -0700107}
108
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700109static const JNINativeMethod g_methods[] = {
Ashok Bhat58b8b242014-01-02 16:52:41 +0000110 { "ctor", "()J", (void*)ctor },
111 { "dtor", "(J)V", (void*)dtor },
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700112 { "performBackup_native",
Ashok Bhat58b8b242014-01-02 16:52:41 +0000113 "(Ljava/io/FileDescriptor;JLjava/io/FileDescriptor;[Ljava/lang/String;[Ljava/lang/String;)I",
Joe Onorato23ecae32009-06-10 17:07:15 -0700114 (void*)performBackup_native },
Ashok Bhat58b8b242014-01-02 16:52:41 +0000115 { "writeFile_native", "(JLjava/lang/String;J)I", (void*)writeFile_native },
116 { "writeSnapshot_native", "(JLjava/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{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800121 return RegisterMethodsOrDie(env, "android/app/backup/FileBackupHelperBase",
Joe Onoratob1a7ffe2009-05-06 18:06:21 -0700122 g_methods, NELEM(g_methods));
123}
124
125}