Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Onorato | 290bb01 | 2009-05-13 18:57:29 -0400 | [diff] [blame] | 17 | #define LOG_TAG "FileBackupHelper_native" |
| 18 | #include <utils/Log.h> |
| 19 | |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 20 | #include "JNIHelp.h" |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 23 | #include <androidfw/BackupHelpers.h> |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android |
| 26 | { |
| 27 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 28 | static jlong |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 29 | ctor(JNIEnv* env, jobject clazz) |
| 30 | { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 31 | return (jlong)new RestoreHelperBase(); |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 35 | dtor(JNIEnv* env, jobject clazz, jlong ptr) |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 36 | { |
| 37 | delete (RestoreHelperBase*)ptr; |
| 38 | } |
| 39 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 40 | static jint |
| 41 | performBackup_native(JNIEnv* env, jobject clazz, jobject oldState, jlong data, |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 42 | jobject newState, jobjectArray files, jobjectArray keys) |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 43 | { |
| 44 | int err; |
| 45 | |
| 46 | // all parameters have already been checked against null |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 47 | int oldStateFD = oldState != NULL ? jniGetFDFromFileDescriptor(env, oldState) : -1; |
| 48 | int newStateFD = jniGetFDFromFileDescriptor(env, newState); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 49 | BackupDataWriter* dataStream = (BackupDataWriter*)data; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 50 | |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 51 | 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 Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 57 | 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 Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 64 | |
| 65 | for (int i=0; i<fileCount; i++) { |
| 66 | env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]); |
| 67 | } |
| 68 | free(filesUTF); |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 69 | |
| 70 | for (int i=0; i<keyCount; i++) { |
| 71 | env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), keysUTF[i]); |
| 72 | } |
| 73 | free(keysUTF); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 74 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 75 | return (jint) err; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 78 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 79 | static jint |
| 80 | writeFile_native(JNIEnv* env, jobject clazz, jlong ptr, jstring filenameObj, jlong backupReaderPtr) |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 81 | { |
| 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 Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 93 | return (jint) err; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 96 | static jint |
| 97 | writeSnapshot_native(JNIEnv* env, jobject clazz, jlong ptr, jobject fileDescriptor) |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 98 | { |
| 99 | int err; |
| 100 | |
| 101 | RestoreHelperBase* restore = (RestoreHelperBase*)ptr; |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 102 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 103 | |
| 104 | err = restore->WriteSnapshot(fd); |
| 105 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 106 | return (jint) err; |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 109 | static const JNINativeMethod g_methods[] = { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 110 | { "ctor", "()J", (void*)ctor }, |
| 111 | { "dtor", "(J)V", (void*)dtor }, |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 112 | { "performBackup_native", |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 113 | "(Ljava/io/FileDescriptor;JLjava/io/FileDescriptor;[Ljava/lang/String;[Ljava/lang/String;)I", |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 114 | (void*)performBackup_native }, |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 115 | { "writeFile_native", "(JLjava/lang/String;J)I", (void*)writeFile_native }, |
| 116 | { "writeSnapshot_native", "(JLjava/io/FileDescriptor;)I", (void*)writeSnapshot_native }, |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 119 | int register_android_backup_FileBackupHelperBase(JNIEnv* env) |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 120 | { |
Christopher Tate | 7adc274 | 2010-03-05 18:03:22 -0800 | [diff] [blame] | 121 | return AndroidRuntime::registerNativeMethods(env, "android/app/backup/FileBackupHelperBase", |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 122 | g_methods, NELEM(g_methods)); |
| 123 | } |
| 124 | |
| 125 | } |