11cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato/*
21cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * Copyright (C) 2009 The Android Open Source Project
31cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *
41cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
51cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * you may not use this file except in compliance with the License.
61cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * You may obtain a copy of the License at
71cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *
81cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
91cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *
101cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * Unless required by applicable law or agreed to in writing, software
111cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
121cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * See the License for the specific language governing permissions and
141cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * limitations under the License.
151cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato */
161cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
171cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato#define LOG_TAG "FileBackupHelper_native"
181cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato#include <utils/Log.h>
191cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
201cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato#include "JNIHelp.h"
211cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato#include <android_runtime/AndroidRuntime.h>
221cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
23b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/BackupHelpers.h>
241cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
25987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe#include "core_jni_helpers.h"
26987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe
271cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratonamespace android
281cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
291cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
304528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tate// android.app.backup.BackupDataInput$EntityHeader
311cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic jfieldID s_keyField = 0;
321cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic jfieldID s_dataSizeField = 0;
331cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
3458b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhatstatic jlong
351cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratoctor_native(JNIEnv* env, jobject clazz, jobject fileDescriptor)
361cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
37a3804cf77f0edd93f6247a055cdafb856b117eecElliott Hughes    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
381cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    if (fd == -1) {
3958b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat        return (jlong)NULL;
401cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
411cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
4258b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    return (jlong)new BackupDataReader(fd);
431cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
441cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
451cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic void
4658b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhatdtor_native(JNIEnv* env, jobject clazz, jlong r)
471cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
481cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    delete (BackupDataReader*)r;
491cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
501cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
511cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic jint
5258b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatreadNextHeader_native(JNIEnv* env, jobject clazz, jlong r, jobject entity)
531cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
541cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    int err;
555f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    bool done;
561cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    BackupDataReader* reader = (BackupDataReader*)r;
571cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
582fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    int type = 0;
591cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
605f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    err = reader->ReadNextHeader(&done, &type);
615f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    if (done) {
622fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        return 1;
632fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    }
642fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate
652fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    if (err != 0) {
662fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        return err < 0 ? err : -1;
672fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    }
682fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate
692fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    switch (type) {
702fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    case BACKUP_HEADER_ENTITY_V1:
712fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    {
722fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        String8 key;
732fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        size_t dataSize;
742fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        err = reader->ReadEntityHeader(&key, &dataSize);
752fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        if (err != 0) {
762fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate            return err < 0 ? err : -1;
771cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        }
782fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        // TODO: Set the fields in the entity object
792fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        jstring keyStr = env->NewStringUTF(key.string());
802fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        env->SetObjectField(entity, s_keyField, keyStr);
812fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        env->SetIntField(entity, s_dataSizeField, dataSize);
822fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        return 0;
832fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    }
842fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    default:
855baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("Unknown header type: 0x%08x\n", type);
862fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        return -1;
872fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate    }
882fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate
891cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    // done
901cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    return 1;
911cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
921cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
931cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic jint
9458b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatreadEntityData_native(JNIEnv* env, jobject clazz, jlong r, jbyteArray data, jint offset, jint size)
951cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
961cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    int err;
971cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    BackupDataReader* reader = (BackupDataReader*)r;
981cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
995f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    if (env->GetArrayLength(data) < (size+offset)) {
1001cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        // size mismatch
1011cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        return -1;
1021cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
1031cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1041cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    jbyte* dataBytes = env->GetByteArrayElements(data, NULL);
1051cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    if (dataBytes == NULL) {
1062fdd428e0f18384160f7c38ce3a2cd9ba7e7b2c2Christopher Tate        return -2;
1071cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
1081cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1095f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    err = reader->ReadEntityData(dataBytes+offset, size);
1101cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1111cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    env->ReleaseByteArrayElements(data, dataBytes, 0);
1121cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1131cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    return err;
1141cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
1151cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1165f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onoratostatic jint
11758b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok BhatskipEntityData_native(JNIEnv* env, jobject clazz, jlong r)
1185f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato{
1195f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    int err;
1205f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    BackupDataReader* reader = (BackupDataReader*)r;
1215f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato
1225f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    err = reader->SkipEntityData();
1235f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato
1245f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    return err;
1255f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato}
1265f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato
1271cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratostatic const JNINativeMethod g_methods[] = {
12858b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "ctor", "(Ljava/io/FileDescriptor;)J", (void*)ctor_native },
12958b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "dtor", "(J)V", (void*)dtor_native },
13058b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "readNextHeader_native", "(JLandroid/app/backup/BackupDataInput$EntityHeader;)I",
1311cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            (void*)readNextHeader_native },
13258b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "readEntityData_native", "(J[BII)I", (void*)readEntityData_native },
13358b8b24256bdc2b613b7fda9151845ed9898a4c7Ashok Bhat    { "skipEntityData_native", "(J)I", (void*)skipEntityData_native },
1341cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato};
1351cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1361cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratoint register_android_backup_BackupDataInput(JNIEnv* env)
1371cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato{
1385baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    //ALOGD("register_android_backup_BackupDataInput");
1391cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
140987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe    jclass clazz = FindClassOrDie(env, "android/app/backup/BackupDataInput$EntityHeader");
141987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe    s_keyField = GetFieldIDOrDie(env, clazz, "key", "Ljava/lang/String;");
142987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe    s_dataSizeField = GetFieldIDOrDie(env, clazz, "dataSize", "I");
143987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe
144987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe    return RegisterMethodsOrDie(env, "android/app/backup/BackupDataInput", g_methods,
145987f79f60bb1f0a4bcd3ef22e57301c743f0b94fAndreas Gampe                                NELEM(g_methods));
1461cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
1471cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1481cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
149