diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/backup/BackupDataOutput.java | 28 | ||||
-rw-r--r-- | core/java/android/backup/FileBackupHelper.java | 7 | ||||
-rw-r--r-- | core/jni/Android.mk | 1 | ||||
-rw-r--r-- | core/jni/AndroidRuntime.cpp | 2 | ||||
-rw-r--r-- | core/jni/android_backup_BackupDataOutput.cpp | 70 | ||||
-rw-r--r-- | core/jni/android_backup_FileBackupHelper.cpp | 9 |
6 files changed, 96 insertions, 21 deletions
diff --git a/core/java/android/backup/BackupDataOutput.java b/core/java/android/backup/BackupDataOutput.java index 555494e..25ae15b 100644 --- a/core/java/android/backup/BackupDataOutput.java +++ b/core/java/android/backup/BackupDataOutput.java @@ -22,24 +22,30 @@ import java.io.FileDescriptor; /** @hide */ public class BackupDataOutput { - /* package */ FileDescriptor fd; + int mBackupWriter; + private Context mContext; public static final int OP_UPDATE = 1; public static final int OP_DELETE = 2; public BackupDataOutput(Context context, FileDescriptor fd) { - this.fd = fd; + mContext = context; + if (fd == null) throw new NullPointerException(); + mBackupWriter = ctor(fd); + if (mBackupWriter == 0) { + throw new RuntimeException("Native initialization failed with fd=" + fd); + } } - public void close() { - // do we close the fd? + protected void finalize() throws Throwable { + try { + dtor(mBackupWriter); + } finally { + super.finalize(); + } } - public native void flush(); - public native void write(byte[] buffer); - public native void write(int oneByte); - public native void write(byte[] buffer, int offset, int count); - - public native void writeOperation(int op); - public native void writeKey(String key); + + private native static int ctor(FileDescriptor fd); + private native static void dtor(int mBackupWriter); } diff --git a/core/java/android/backup/FileBackupHelper.java b/core/java/android/backup/FileBackupHelper.java index 05159dc..ec16eb1 100644 --- a/core/java/android/backup/FileBackupHelper.java +++ b/core/java/android/backup/FileBackupHelper.java @@ -53,15 +53,12 @@ public class FileBackupHelper { } // oldStateFd can be null FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null; - if (data.fd == null) { - throw new NullPointerException(); - } FileDescriptor newStateFd = newState.getFileDescriptor(); if (newStateFd == null) { throw new NullPointerException(); } - int err = performBackup_native(basePath, oldStateFd, data.fd, newStateFd, files); + int err = performBackup_native(basePath, oldStateFd, data.mBackupWriter, newStateFd, files); if (err != 0) { throw new RuntimeException("Backup failed"); // TODO: more here @@ -69,5 +66,5 @@ public class FileBackupHelper { } native private static int performBackup_native(String basePath, FileDescriptor oldState, - FileDescriptor data, FileDescriptor newState, String[] files); + int data, FileDescriptor newState, String[] files); } diff --git a/core/jni/Android.mk b/core/jni/Android.mk index 4839b6f..d46dd80 100644 --- a/core/jni/Android.mk +++ b/core/jni/Android.mk @@ -117,6 +117,7 @@ LOCAL_SRC_FILES:= \ android_location_GpsLocationProvider.cpp \ com_android_internal_os_ZygoteInit.cpp \ com_android_internal_graphics_NativeUtils.cpp \ + android_backup_BackupDataOutput.cpp \ android_backup_FileBackupHelper.cpp LOCAL_C_INCLUDES += \ diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp index aa6450d..302f39e 100644 --- a/core/jni/AndroidRuntime.cpp +++ b/core/jni/AndroidRuntime.cpp @@ -155,6 +155,7 @@ extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env); extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env); extern int register_android_util_Base64(JNIEnv* env); extern int register_android_location_GpsLocationProvider(JNIEnv* env); +extern int register_android_backup_BackupDataOutput(JNIEnv *env); extern int register_android_backup_FileBackupHelper(JNIEnv *env); static AndroidRuntime* gCurRuntime = NULL; @@ -1126,6 +1127,7 @@ static const RegJNIRec gRegJNI[] = { REG_JNI(register_android_ddm_DdmHandleNativeHeap), REG_JNI(register_android_util_Base64), REG_JNI(register_android_location_GpsLocationProvider), + REG_JNI(register_android_backup_BackupDataOutput), REG_JNI(register_android_backup_FileBackupHelper), }; diff --git a/core/jni/android_backup_BackupDataOutput.cpp b/core/jni/android_backup_BackupDataOutput.cpp new file mode 100644 index 0000000..8fce2a2 --- /dev/null +++ b/core/jni/android_backup_BackupDataOutput.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "FileBackupHelper_native" +#include <utils/Log.h> + +#include "JNIHelp.h" +#include <android_runtime/AndroidRuntime.h> + +#include <utils/backup_helpers.h> + +namespace android +{ + +static jfieldID s_descriptorField = 0; + +static int +ctor_native(JNIEnv* env, jobject This, jobject fileDescriptor) +{ + int err; + + int fd = env->GetIntField(fileDescriptor, s_descriptorField); + if (fd == -1) { + return NULL; + } + + return (int)new BackupDataWriter(fd); +} + +static void +dtor_native(JNIEnv* env, jobject This, int fd) +{ + delete (BackupDataWriter*)fd; +} + +static const JNINativeMethod g_methods[] = { + { "ctor", "(Ljava/io/FileDescriptor;)I", (void*)ctor_native }, + { "dtor", "(I)V", (void*)dtor_native }, +}; + +int register_android_backup_BackupDataOutput(JNIEnv* env) +{ + LOGD("register_android_backup_BackupDataOutput"); + + jclass clazz; + + clazz = env->FindClass("java/io/FileDescriptor"); + LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); + s_descriptorField = env->GetFieldID(clazz, "descriptor", "I"); + LOG_FATAL_IF(s_descriptorField == NULL, + "Unable to find descriptor field in java.io.FileDescriptor"); + + return AndroidRuntime::registerNativeMethods(env, "android/backup/BackupDataOutput", + g_methods, NELEM(g_methods)); +} + +} diff --git a/core/jni/android_backup_FileBackupHelper.cpp b/core/jni/android_backup_FileBackupHelper.cpp index c6de3a5..a05d812 100644 --- a/core/jni/android_backup_FileBackupHelper.cpp +++ b/core/jni/android_backup_FileBackupHelper.cpp @@ -28,7 +28,7 @@ namespace android static jfieldID s_descriptorField = 0; static int -performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldState, jobject data, +performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldState, int data, jobject newState, jobjectArray files) { int err; @@ -37,7 +37,7 @@ performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldSt LOGD("oldState=%p newState=%p data=%p\n", oldState, newState, data); int oldStateFD = oldState != NULL ? env->GetIntField(oldState, s_descriptorField) : -1; int newStateFD = env->GetIntField(newState, s_descriptorField); - int dataFD = env->GetIntField(data, s_descriptorField); + BackupDataWriter* dataStream = (BackupDataWriter*)data; char const* basePathUTF = env->GetStringUTFChars(basePath, NULL); LOGD("basePathUTF=\"%s\"\n", basePathUTF); @@ -47,7 +47,7 @@ performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldSt filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL); } - err = back_up_files(oldStateFD, dataFD, newStateFD, basePathUTF, filesUTF, fileCount); + err = back_up_files(oldStateFD, dataStream, newStateFD, basePathUTF, filesUTF, fileCount); for (int i=0; i<fileCount; i++) { env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]); @@ -60,8 +60,7 @@ performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldSt static const JNINativeMethod g_methods[] = { { "performBackup_native", - "(Ljava/lang/String;Ljava/io/FileDescriptor;Ljava/io/FileDescriptor;" - "Ljava/io/FileDescriptor;[Ljava/lang/String;)I", + "(Ljava/lang/String;Ljava/io/FileDescriptor;ILjava/io/FileDescriptor;[Ljava/lang/String;)I", (void*)performBackup_native }, }; |