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 | 8ae2335 | 2009-06-04 13:53:57 -0700 | [diff] [blame^] | 23 | #include <utils/BackupHelpers.h> |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android |
| 26 | { |
| 27 | |
Joe Onorato | 290bb01 | 2009-05-13 18:57:29 -0400 | [diff] [blame] | 28 | static jfieldID s_descriptorField = 0; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 29 | |
| 30 | static int |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 31 | performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldState, int data, |
Joe Onorato | 290bb01 | 2009-05-13 18:57:29 -0400 | [diff] [blame] | 32 | jobject newState, jobjectArray files) |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 33 | { |
| 34 | int err; |
| 35 | |
| 36 | // all parameters have already been checked against null |
Joe Onorato | 290bb01 | 2009-05-13 18:57:29 -0400 | [diff] [blame] | 37 | LOGD("oldState=%p newState=%p data=%p\n", oldState, newState, data); |
| 38 | int oldStateFD = oldState != NULL ? env->GetIntField(oldState, s_descriptorField) : -1; |
| 39 | int newStateFD = env->GetIntField(newState, s_descriptorField); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 40 | BackupDataWriter* dataStream = (BackupDataWriter*)data; |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 41 | |
| 42 | char const* basePathUTF = env->GetStringUTFChars(basePath, NULL); |
Joe Onorato | 290bb01 | 2009-05-13 18:57:29 -0400 | [diff] [blame] | 43 | LOGD("basePathUTF=\"%s\"\n", basePathUTF); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 44 | const int fileCount = env->GetArrayLength(files); |
| 45 | char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount); |
| 46 | for (int i=0; i<fileCount; i++) { |
| 47 | filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL); |
| 48 | } |
| 49 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 50 | err = back_up_files(oldStateFD, dataStream, newStateFD, basePathUTF, filesUTF, fileCount); |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 51 | |
| 52 | for (int i=0; i<fileCount; i++) { |
| 53 | env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]); |
| 54 | } |
| 55 | free(filesUTF); |
| 56 | env->ReleaseStringUTFChars(basePath, basePathUTF); |
| 57 | |
| 58 | return err; |
| 59 | } |
| 60 | |
| 61 | static const JNINativeMethod g_methods[] = { |
| 62 | { "performBackup_native", |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 63 | "(Ljava/lang/String;Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;)I", |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 64 | (void*)performBackup_native }, |
| 65 | }; |
| 66 | |
| 67 | int register_android_backup_FileBackupHelper(JNIEnv* env) |
| 68 | { |
Joe Onorato | 290bb01 | 2009-05-13 18:57:29 -0400 | [diff] [blame] | 69 | LOGD("register_android_backup_FileBackupHelper"); |
| 70 | |
Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 71 | jclass clazz; |
| 72 | |
| 73 | clazz = env->FindClass("java/io/FileDescriptor"); |
| 74 | LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); |
| 75 | s_descriptorField = env->GetFieldID(clazz, "descriptor", "I"); |
| 76 | LOG_FATAL_IF(s_descriptorField == NULL, |
| 77 | "Unable to find descriptor field in java.io.FileDescriptor"); |
| 78 | |
| 79 | return AndroidRuntime::registerNativeMethods(env, "android/backup/FileBackupHelper", |
| 80 | g_methods, NELEM(g_methods)); |
| 81 | } |
| 82 | |
| 83 | } |