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 | 8ae2335 | 2009-06-04 13:53:57 -0700 | [diff] [blame] | 23 | #include <utils/BackupHelpers.h> |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android |
| 26 | { |
| 27 | |
| 28 | static jfieldID s_descriptorField = 0; |
| 29 | |
| 30 | static int |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 31 | ctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 32 | { |
| 33 | int err; |
| 34 | |
| 35 | int fd = env->GetIntField(fileDescriptor, s_descriptorField); |
| 36 | if (fd == -1) { |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | return (int)new BackupDataWriter(fd); |
| 41 | } |
| 42 | |
| 43 | static void |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 44 | dtor_native(JNIEnv* env, jobject clazz, int w) |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 45 | { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 46 | delete (BackupDataWriter*)w; |
| 47 | } |
| 48 | |
| 49 | static jint |
| 50 | writeEntityHeader_native(JNIEnv* env, jobject clazz, int w, jstring key, int dataSize) |
| 51 | { |
| 52 | int err; |
| 53 | BackupDataWriter* writer = (BackupDataWriter*)w; |
| 54 | |
| 55 | const char* keyUTF = env->GetStringUTFChars(key, NULL); |
| 56 | if (keyUTF == NULL) { |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | err = writer->WriteEntityHeader(String8(keyUTF), dataSize); |
| 61 | |
| 62 | env->ReleaseStringUTFChars(key, keyUTF); |
| 63 | |
| 64 | return err; |
| 65 | } |
| 66 | |
| 67 | static jint |
| 68 | writeEntityData_native(JNIEnv* env, jobject clazz, int w, jbyteArray data, int size) |
| 69 | { |
| 70 | int err; |
| 71 | BackupDataWriter* writer = (BackupDataWriter*)w; |
| 72 | |
Dan Egnor | 6d87738 | 2009-07-09 13:48:36 -0700 | [diff] [blame^] | 73 | if (env->GetArrayLength(data) < size) { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 74 | // size mismatch |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | jbyte* dataBytes = env->GetByteArrayElements(data, NULL); |
| 79 | if (dataBytes == NULL) { |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | err = writer->WriteEntityData(dataBytes, size); |
| 84 | |
| 85 | env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT); |
| 86 | |
| 87 | return err; |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 90 | static void |
| 91 | setKeyPrefix_native(JNIEnv* env, jobject clazz, int w, jstring keyPrefixObj) |
| 92 | { |
| 93 | int err; |
| 94 | BackupDataWriter* writer = (BackupDataWriter*)w; |
| 95 | |
| 96 | const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL); |
| 97 | String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : ""); |
| 98 | |
| 99 | writer->SetKeyPrefix(keyPrefix); |
| 100 | |
| 101 | env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF); |
| 102 | } |
| 103 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 104 | static const JNINativeMethod g_methods[] = { |
| 105 | { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native }, |
| 106 | { "dtor", "(I)V", (void*)dtor_native }, |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 107 | { "writeEntityHeader_native", "(ILjava/lang/String;I)I", (void*)writeEntityHeader_native }, |
| 108 | { "writeEntityData_native", "(I[BI)I", (void*)writeEntityData_native }, |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 109 | { "setKeyPrefix_native", "(ILjava/lang/String;)V", (void*)setKeyPrefix_native }, |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | int register_android_backup_BackupDataOutput(JNIEnv* env) |
| 113 | { |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 114 | //LOGD("register_android_backup_BackupDataOutput"); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 115 | |
| 116 | jclass clazz; |
| 117 | |
| 118 | clazz = env->FindClass("java/io/FileDescriptor"); |
| 119 | LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); |
| 120 | s_descriptorField = env->GetFieldID(clazz, "descriptor", "I"); |
| 121 | LOG_FATAL_IF(s_descriptorField == NULL, |
| 122 | "Unable to find descriptor field in java.io.FileDescriptor"); |
| 123 | |
| 124 | return AndroidRuntime::registerNativeMethods(env, "android/backup/BackupDataOutput", |
| 125 | g_methods, NELEM(g_methods)); |
| 126 | } |
| 127 | |
| 128 | } |