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 <androidfw/BackupHelpers.h>
     24 
     25 namespace android
     26 {
     27 
     28 static int
     29 ctor(JNIEnv* env, jobject clazz)
     30 {
     31     return (int)new RestoreHelperBase();
     32 }
     33 
     34 static void
     35 dtor(JNIEnv* env, jobject clazz, jint ptr)
     36 {
     37     delete (RestoreHelperBase*)ptr;
     38 }
     39 
     40 static int
     41 performBackup_native(JNIEnv* env, jobject clazz, jobject oldState, int data,
     42         jobject newState, jobjectArray files, jobjectArray keys)
     43 {
     44     int err;
     45 
     46     // all parameters have already been checked against null
     47     int oldStateFD = oldState != NULL ? jniGetFDFromFileDescriptor(env, oldState) : -1;
     48     int newStateFD = jniGetFDFromFileDescriptor(env, newState);
     49     BackupDataWriter* dataStream = (BackupDataWriter*)data;
     50 
     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 
     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);
     64 
     65     for (int i=0; i<fileCount; i++) {
     66         env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
     67     }
     68     free(filesUTF);
     69 
     70     for (int i=0; i<keyCount; i++) {
     71         env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(keys, i), keysUTF[i]);
     72     }
     73     free(keysUTF);
     74 
     75     return err;
     76 }
     77 
     78 
     79 static int
     80 writeFile_native(JNIEnv* env, jobject clazz, jint ptr, jstring filenameObj, int backupReaderPtr)
     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 
     93     return err;
     94 }
     95 
     96 static int
     97 writeSnapshot_native(JNIEnv* env, jobject clazz, jint ptr, jobject fileDescriptor)
     98 {
     99     int err;
    100 
    101     RestoreHelperBase* restore = (RestoreHelperBase*)ptr;
    102     int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
    103 
    104     err = restore->WriteSnapshot(fd);
    105 
    106     return err;
    107 }
    108 
    109 static const JNINativeMethod g_methods[] = {
    110     { "ctor", "()I", (void*)ctor },
    111     { "dtor", "(I)V", (void*)dtor },
    112     { "performBackup_native",
    113        "(Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;[Ljava/lang/String;)I",
    114        (void*)performBackup_native },
    115     { "writeFile_native", "(ILjava/lang/String;I)I", (void*)writeFile_native },
    116     { "writeSnapshot_native", "(ILjava/io/FileDescriptor;)I", (void*)writeSnapshot_native },
    117 };
    118 
    119 int register_android_backup_FileBackupHelperBase(JNIEnv* env)
    120 {
    121     return AndroidRuntime::registerNativeMethods(env, "android/app/backup/FileBackupHelperBase",
    122             g_methods, NELEM(g_methods));
    123 }
    124 
    125 }
    126