Home | History | Annotate | Download | only in jni
      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 
     23 #include <utils/BackupHelpers.h>
     24 
     25 namespace android
     26 {
     27 
     28 // java.io.FileDescriptor
     29 static jfieldID s_descriptorField = 0;
     30 
     31 static int
     32 ctor(JNIEnv* env, jobject clazz)
     33 {
     34     return (int)new RestoreHelperBase();
     35 }
     36 
     37 static void
     38 dtor(JNIEnv* env, jobject clazz, jint ptr)
     39 {
     40     delete (RestoreHelperBase*)ptr;
     41 }
     42 
     43 static int
     44 performBackup_native(JNIEnv* env, jobject clazz, jobject oldState, int data,
     45         jobject newState, jobjectArray files, jobjectArray keys)
     46 {
     47     int err;
     48 
     49     // all parameters have already been checked against null
     50     int oldStateFD = oldState != NULL ? env->GetIntField(oldState, s_descriptorField) : -1;
     51     int newStateFD = env->GetIntField(newState, s_descriptorField);
     52     BackupDataWriter* dataStream = (BackupDataWriter*)data;
     53 
     54     const int fileCount = env->GetArrayLength(files);
     55     char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount);
     56     for (int i=0; i<fileCount; i++) {
     57         filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL);
     58     }
     59 
     60     const int keyCount = env->GetArrayLength(keys);
     61     char const** keysUTF = (char const**)malloc(sizeof(char*)*keyCount);
     62     for (int i=0; i<keyCount; i++) {
     63         keysUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), NULL);
     64     }
     65 
     66     err = back_up_files(oldStateFD, dataStream, newStateFD, filesUTF, keysUTF, fileCount);
     67 
     68     for (int i=0; i<fileCount; i++) {
     69         env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
     70     }
     71     free(filesUTF);
     72 
     73     for (int i=0; i<keyCount; i++) {
     74         env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), keysUTF[i]);
     75     }
     76     free(keysUTF);
     77 
     78     return err;
     79 }
     80 
     81 
     82 static int
     83 writeFile_native(JNIEnv* env, jobject clazz, jint ptr, jstring filenameObj, int backupReaderPtr)
     84 {
     85     int err;
     86     RestoreHelperBase* restore = (RestoreHelperBase*)ptr;
     87     BackupDataReader* reader = (BackupDataReader*)backupReaderPtr;
     88     char const* filename;
     89 
     90     filename = env->GetStringUTFChars(filenameObj, NULL);
     91 
     92     err = restore->WriteFile(String8(filename), reader);
     93 
     94     env->ReleaseStringUTFChars(filenameObj, filename);
     95 
     96     return err;
     97 }
     98 
     99 static int
    100 writeSnapshot_native(JNIEnv* env, jobject clazz, jint ptr, jobject fileDescriptor)
    101 {
    102     int err;
    103 
    104     RestoreHelperBase* restore = (RestoreHelperBase*)ptr;
    105     int fd = env->GetIntField(fileDescriptor, s_descriptorField);
    106 
    107     err = restore->WriteSnapshot(fd);
    108 
    109     return err;
    110 }
    111 
    112 static const JNINativeMethod g_methods[] = {
    113     { "ctor", "()I", (void*)ctor },
    114     { "dtor", "(I)V", (void*)dtor },
    115     { "performBackup_native",
    116        "(Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;[Ljava/lang/String;)I",
    117        (void*)performBackup_native },
    118     { "writeFile_native", "(ILjava/lang/String;I)I", (void*)writeFile_native },
    119     { "writeSnapshot_native", "(ILjava/io/FileDescriptor;)I", (void*)writeSnapshot_native },
    120 };
    121 
    122 int register_android_backup_FileBackupHelperBase(JNIEnv* env)
    123 {
    124     jclass clazz;
    125 
    126     clazz = env->FindClass("java/io/FileDescriptor");
    127     LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
    128     s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
    129     LOG_FATAL_IF(s_descriptorField == NULL,
    130             "Unable to find descriptor field in java.io.FileDescriptor");
    131 
    132     return AndroidRuntime::registerNativeMethods(env, "android/app/backup/FileBackupHelperBase",
    133             g_methods, NELEM(g_methods));
    134 }
    135 
    136 }
    137