Joe Onorato | d2110db | 2009-05-19 13:41: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 | |
| 17 | #define LOG_TAG "FileBackupHelper_native" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 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 | d2110db | 2009-05-19 13:41: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 | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 29 | ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 30 | { |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 31 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 32 | if (fd == -1) { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 33 | return (jlong)NULL; |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 36 | return (jlong)new BackupDataWriter(fd); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 40 | dtor_native(JNIEnv* env, jobject clazz, jlong w) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 41 | { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 42 | delete (BackupDataWriter*)w; |
| 43 | } |
| 44 | |
| 45 | static jint |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 46 | writeEntityHeader_native(JNIEnv* env, jobject clazz, jlong w, jstring key, jint dataSize) |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 47 | { |
| 48 | int err; |
| 49 | BackupDataWriter* writer = (BackupDataWriter*)w; |
| 50 | |
| 51 | const char* keyUTF = env->GetStringUTFChars(key, NULL); |
| 52 | if (keyUTF == NULL) { |
| 53 | return -1; |
| 54 | } |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 55 | err = writer->WriteEntityHeader(String8(keyUTF), dataSize); |
| 56 | |
| 57 | env->ReleaseStringUTFChars(key, keyUTF); |
| 58 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 59 | return (jint)err; |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static jint |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 63 | writeEntityData_native(JNIEnv* env, jobject clazz, jlong w, jbyteArray data, jint size) |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 64 | { |
| 65 | int err; |
| 66 | BackupDataWriter* writer = (BackupDataWriter*)w; |
| 67 | |
Dan Egnor | 6d87738 | 2009-07-09 13:48:36 -0700 | [diff] [blame] | 68 | if (env->GetArrayLength(data) < size) { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 69 | // size mismatch |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | jbyte* dataBytes = env->GetByteArrayElements(data, NULL); |
| 74 | if (dataBytes == NULL) { |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | err = writer->WriteEntityData(dataBytes, size); |
| 79 | |
| 80 | env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT); |
| 81 | |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 82 | return (jint)err; |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 85 | static void |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 86 | setKeyPrefix_native(JNIEnv* env, jobject clazz, jlong w, jstring keyPrefixObj) |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 87 | { |
| 88 | int err; |
| 89 | BackupDataWriter* writer = (BackupDataWriter*)w; |
| 90 | |
| 91 | const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL); |
| 92 | String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : ""); |
| 93 | |
| 94 | writer->SetKeyPrefix(keyPrefix); |
| 95 | |
| 96 | env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF); |
| 97 | } |
| 98 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 99 | static const JNINativeMethod g_methods[] = { |
Ashok Bhat | 58b8b24 | 2014-01-02 16:52:41 +0000 | [diff] [blame] | 100 | { "ctor", "(Ljava/io/FileDescriptor;)J", (void*)ctor_native }, |
| 101 | { "dtor", "(J)V", (void*)dtor_native }, |
| 102 | { "writeEntityHeader_native", "(JLjava/lang/String;I)I", (void*)writeEntityHeader_native }, |
| 103 | { "writeEntityData_native", "(J[BI)I", (void*)writeEntityData_native }, |
| 104 | { "setKeyPrefix_native", "(JLjava/lang/String;)V", (void*)setKeyPrefix_native }, |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | int register_android_backup_BackupDataOutput(JNIEnv* env) |
| 108 | { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 109 | //ALOGD("register_android_backup_BackupDataOutput"); |
Christopher Tate | 7adc274 | 2010-03-05 18:03:22 -0800 | [diff] [blame] | 110 | return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataOutput", |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 111 | g_methods, NELEM(g_methods)); |
| 112 | } |
| 113 | |
| 114 | } |