1d2110dbce071a236b6176de344ca797b737542ebJoe Onorato/*
2d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * Copyright (C) 2009 The Android Open Source Project
3d2110dbce071a236b6176de344ca797b737542ebJoe Onorato *
4d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
5d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * you may not use this file except in compliance with the License.
6d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * You may obtain a copy of the License at
7d2110dbce071a236b6176de344ca797b737542ebJoe Onorato *
8d2110dbce071a236b6176de344ca797b737542ebJoe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
9d2110dbce071a236b6176de344ca797b737542ebJoe Onorato *
10d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * Unless required by applicable law or agreed to in writing, software
11d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
12d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * See the License for the specific language governing permissions and
14d2110dbce071a236b6176de344ca797b737542ebJoe Onorato * limitations under the License.
15d2110dbce071a236b6176de344ca797b737542ebJoe Onorato */
16d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
17d2110dbce071a236b6176de344ca797b737542ebJoe Onorato#define LOG_TAG "FileBackupHelper_native"
18d2110dbce071a236b6176de344ca797b737542ebJoe Onorato#include <utils/Log.h>
19d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
20d2110dbce071a236b6176de344ca797b737542ebJoe Onorato#include "JNIHelp.h"
21d2110dbce071a236b6176de344ca797b737542ebJoe Onorato#include <android_runtime/AndroidRuntime.h>
22d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
23b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/BackupHelpers.h>
24d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
25d2110dbce071a236b6176de344ca797b737542ebJoe Onoratonamespace android
26d2110dbce071a236b6176de344ca797b737542ebJoe Onorato{
27d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
2858b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhatstatic jlong
291cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratoctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor)
30d2110dbce071a236b6176de344ca797b737542ebJoe Onorato{
31a3804cf77f0edd93f6247a055cdafb856b117eecElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
32d2110dbce071a236b6176de344ca797b737542ebJoe Onorato    if (fd == -1) {
3358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat        return (jlong)NULL;
34d2110dbce071a236b6176de344ca797b737542ebJoe Onorato    }
35d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
3658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    return (jlong)new BackupDataWriter(fd);
37d2110dbce071a236b6176de344ca797b737542ebJoe Onorato}
38d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
39d2110dbce071a236b6176de344ca797b737542ebJoe Onoratostatic void
4058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhatdtor_native(JNIEnv* env, jobject clazz, jlong w)
41d2110dbce071a236b6176de344ca797b737542ebJoe Onorato{
421cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    delete (BackupDataWriter*)w;
431cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
441cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
451cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic jint
4658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatwriteEntityHeader_native(JNIEnv* env, jobject clazz, jlong w, jstring key, jint dataSize)
471cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
481cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    int err;
491cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    BackupDataWriter* writer = (BackupDataWriter*)w;
501cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
511cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    const char* keyUTF = env->GetStringUTFChars(key, NULL);
521cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    if (keyUTF == NULL) {
531cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        return -1;
541cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
551cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    err = writer->WriteEntityHeader(String8(keyUTF), dataSize);
561cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
571cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    env->ReleaseStringUTFChars(key, keyUTF);
581cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
5958b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    return (jint)err;
601cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
611cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
621cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic jint
6358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatwriteEntityData_native(JNIEnv* env, jobject clazz, jlong w, jbyteArray data, jint size)
641cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
651cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    int err;
661cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    BackupDataWriter* writer = (BackupDataWriter*)w;
671cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
686d877383bc2e2952cad48780c410ed452870a5a4Dan Egnor    if (env->GetArrayLength(data) < size) {
691cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        // size mismatch
701cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        return -1;
711cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
721cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
731cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
741cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    if (dataBytes == NULL) {
751cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        return -1;
761cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
771cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
781cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    err = writer->WriteEntityData(dataBytes, size);
791cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
801cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT);
811cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
8258b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    return (jint)err;
83d2110dbce071a236b6176de344ca797b737542ebJoe Onorato}
84d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
8506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onoratostatic void
8658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatsetKeyPrefix_native(JNIEnv* env, jobject clazz, jlong w, jstring keyPrefixObj)
8706290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato{
8806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    int err;
8906290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    BackupDataWriter* writer = (BackupDataWriter*)w;
9006290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
9106290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    const char* keyPrefixUTF = env->GetStringUTFChars(keyPrefixObj, NULL);
9206290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    String8 keyPrefix(keyPrefixUTF ? keyPrefixUTF : "");
9306290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
9406290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    writer->SetKeyPrefix(keyPrefix);
9506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
9606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    env->ReleaseStringUTFChars(keyPrefixObj, keyPrefixUTF);
9706290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato}
9806290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
99d2110dbce071a236b6176de344ca797b737542ebJoe Onoratostatic const JNINativeMethod g_methods[] = {
10058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "ctor", "(Ljava/io/FileDescriptor;)J", (void*)ctor_native },
10158b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "dtor", "(J)V", (void*)dtor_native },
10258b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "writeEntityHeader_native", "(JLjava/lang/String;I)I", (void*)writeEntityHeader_native },
10358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "writeEntityData_native", "(J[BI)I", (void*)writeEntityData_native },
10458b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "setKeyPrefix_native", "(JLjava/lang/String;)V", (void*)setKeyPrefix_native },
105d2110dbce071a236b6176de344ca797b737542ebJoe Onorato};
106d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
107d2110dbce071a236b6176de344ca797b737542ebJoe Onoratoint register_android_backup_BackupDataOutput(JNIEnv* env)
108d2110dbce071a236b6176de344ca797b737542ebJoe Onorato{
1095baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    //ALOGD("register_android_backup_BackupDataOutput");
1107adc274abd9c0c361b798c2348251358d7adeb18Christopher Tate    return AndroidRuntime::registerNativeMethods(env, "android/app/backup/BackupDataOutput",
111d2110dbce071a236b6176de344ca797b737542ebJoe Onorato            g_methods, NELEM(g_methods));
112d2110dbce071a236b6176de344ca797b737542ebJoe Onorato}
113d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
114d2110dbce071a236b6176de344ca797b737542ebJoe Onorato}
115