summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cmds/backup/Android.mk15
-rw-r--r--cmds/backup/NOTICE190
-rw-r--r--cmds/backup/backup.cpp122
-rw-r--r--core/java/android/backup/BackupDataOutput.java28
-rw-r--r--core/java/android/backup/FileBackupHelper.java7
-rw-r--r--core/jni/Android.mk1
-rw-r--r--core/jni/AndroidRuntime.cpp2
-rw-r--r--core/jni/android_backup_BackupDataOutput.cpp70
-rw-r--r--core/jni/android_backup_FileBackupHelper.cpp9
-rw-r--r--include/utils/backup_helpers.h24
-rw-r--r--libs/utils/backup_data.cpp53
-rw-r--r--libs/utils/backup_helper_file.cpp152
-rw-r--r--services/java/com/android/server/BackupManagerService.java21
-rw-r--r--tests/backup/Android.mk4
14 files changed, 605 insertions, 93 deletions
diff --git a/cmds/backup/Android.mk b/cmds/backup/Android.mk
new file mode 100644
index 0000000..a37a80e
--- /dev/null
+++ b/cmds/backup/Android.mk
@@ -0,0 +1,15 @@
+# Copyright 2009 The Android Open Source Project
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= backup.cpp
+
+LOCAL_SHARED_LIBRARIES := libcutils libc libutils
+
+LOCAL_MODULE:= btool
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_EXECUTABLE)
diff --git a/cmds/backup/NOTICE b/cmds/backup/NOTICE
new file mode 100644
index 0000000..c5b1efa
--- /dev/null
+++ b/cmds/backup/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2005-2008, 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.
+
+ 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.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
diff --git a/cmds/backup/backup.cpp b/cmds/backup/backup.cpp
new file mode 100644
index 0000000..51ac943
--- /dev/null
+++ b/cmds/backup/backup.cpp
@@ -0,0 +1,122 @@
+
+#include <utils/backup_helpers.h>
+#include <utils/String8.h>
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+
+using namespace android;
+
+#include <unistd.h>
+
+int
+usage(int argc, const char** argv)
+{
+ const char* p = argv[0];
+
+ fprintf(stderr, "%s: Backs up your data.\n"
+ "\n"
+ "usage: %s\n"
+ " Prints all of the data that can be backed up to stdout.\n"
+ "\n"
+ "usage: %s list FILE\n"
+ " Lists the backup entities in the file.\n"
+ "\n"
+ "usage: %s print NAME FILE\n"
+ " Prints the entity named NAME in FILE.\n",
+ p, p, p, p);
+ return 1;
+}
+
+int
+perform_full_backup()
+{
+ printf("this would have written all of your data to stdout\n");
+ return 0;
+}
+
+int
+perform_list(const char* filename)
+{
+ int err;
+ int fd;
+
+ fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "Error opening: %s\n", filename);
+ return 1;
+ }
+
+ BackupDataReader reader(fd);
+ int type;
+
+ while (reader.ReadNextHeader(&type) == 0) {
+ switch (type) {
+ case BACKUP_HEADER_APP_V1:
+ {
+ String8 packageName;
+ int cookie;
+ err = reader.ReadAppHeader(&packageName, &cookie);
+ if (err == 0) {
+ printf("App header: %s 0x%08x (%d)\n", packageName.string(), cookie, cookie);
+ } else {
+ printf("Error reading app header\n");
+ }
+ break;
+ }
+ case BACKUP_HEADER_ENTITY_V1:
+ {
+ String8 key;
+ size_t dataSize;
+ err = reader.ReadEntityHeader(&key, &dataSize);
+ if (err == 0) {
+ printf(" entity: %s (%d bytes)\n", key.string(), dataSize);
+ } else {
+ printf(" Error reading entity header\n");
+ }
+ break;
+ }
+ case BACKUP_FOOTER_APP_V1:
+ {
+ int cookie;
+ err = reader.ReadAppFooter(&cookie);
+ if (err == 0) {
+ printf(" App footer: 0x%08x (%d)\n", cookie, cookie);
+ } else {
+ printf(" Error reading entity header\n");
+ }
+ break;
+ }
+ default:
+ {
+ printf("Unknown chunk type: 0x%08x\n", type);
+ break;
+ }
+ }
+ }
+
+ return 0;
+}
+
+int perform_print(const char* entityname, const char* filename)
+{
+ printf("perform_print(%s, %s);", entityname, filename);
+ return 0;
+}
+
+int
+main(int argc, const char** argv)
+{
+ if (argc <= 1) {
+ return perform_full_backup();
+ }
+ if (argc == 3 && 0 == strcmp(argv[1], "list")) {
+ return perform_list(argv[2]);
+ }
+ if (argc == 4 && 0 == strcmp(argv[1], "print")) {
+ return perform_print(argv[2], argv[3]);
+ }
+ return usage(argc, argv);
+}
+
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 },
};
diff --git a/include/utils/backup_helpers.h b/include/utils/backup_helpers.h
index 0c59fec..24b6c9e 100644
--- a/include/utils/backup_helpers.h
+++ b/include/utils/backup_helpers.h
@@ -22,24 +22,27 @@
namespace android {
-int back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
- char const* fileBase, char const* const* files, int fileCount);
+enum {
+ BACKUP_HEADER_APP_V1 = 0x31707041, // App1 (little endian)
+ BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
+ BACKUP_FOOTER_APP_V1 = 0x746f6f46, // Foot (little endian)
+};
// the sizes of all of these match.
typedef struct {
- int type; // == APP_MAGIC_V1
+ int type; // == BACKUP_HEADER_APP_V1
int packageLen; // length of the name of the package that follows, not including the null.
int cookie;
} app_header_v1;
typedef struct {
- int type; // ENTITY_MAGIC_V1
+ int type; // BACKUP_HEADER_ENTITY_V1
int keyLen; // length of the key name, not including the null terminator
- int dataSize; // size of the data, not including the padding
+ int dataSize; // size of the data, not including the padding, -1 means delete
} entity_header_v1;
typedef struct {
- int type; // FOOTER_MAGIC_V1
+ int type; // BACKUP_FOOTER_APP_V1
int entityCount; // the number of entities that were written
int cookie;
} app_footer_v1;
@@ -89,11 +92,12 @@ public:
~BackupDataReader();
status_t Status();
- status_t ReadNextHeader();
+ status_t ReadNextHeader(int* type = NULL);
status_t ReadAppHeader(String8* packageName, int* cookie);
bool HasEntities();
status_t ReadEntityHeader(String8* key, size_t* dataSize);
+ status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
status_t ReadEntityData(void* data, size_t size);
status_t ReadAppFooter(int* cookie);
@@ -113,7 +117,11 @@ private:
} m_header;
};
-#define TEST_BACKUP_HELPERS 0
+int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
+ char const* fileBase, char const* const* files, int fileCount);
+
+
+#define TEST_BACKUP_HELPERS 1
#if TEST_BACKUP_HELPERS
int backup_helper_test_empty();
diff --git a/libs/utils/backup_data.cpp b/libs/utils/backup_data.cpp
index dd04449..95c05b7 100644
--- a/libs/utils/backup_data.cpp
+++ b/libs/utils/backup_data.cpp
@@ -39,10 +39,6 @@ namespace android {
* - The value, padded to 4 byte boundary
*/
-#define APP_MAGIC_V1 0x31707041 // App1 (little endian)
-#define ENTITY_MAGIC_V1 0x61746144 // Data (little endian)
-#define FOOTER_MAGIC_V1 0x746f6f46 // Foot (little endian)
-
const static int ROUND_UP[4] = { 0, 3, 2, 1 };
static inline size_t
@@ -108,7 +104,7 @@ BackupDataWriter::WriteAppHeader(const String8& packageName, int cookie)
nameLen = packageName.length();
- header.type = tolel(APP_MAGIC_V1);
+ header.type = tolel(BACKUP_HEADER_APP_V1);
header.packageLen = tolel(nameLen);
header.cookie = cookie;
@@ -148,7 +144,7 @@ BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
keyLen = key.length();
- header.type = tolel(ENTITY_MAGIC_V1);
+ header.type = tolel(BACKUP_HEADER_ENTITY_V1);
header.keyLen = tolel(keyLen);
header.dataSize = tolel(dataSize);
@@ -209,7 +205,7 @@ BackupDataWriter::WriteAppFooter(int cookie)
app_footer_v1 footer;
ssize_t nameLen;
- footer.type = tolel(FOOTER_MAGIC_V1);
+ footer.type = tolel(BACKUP_FOOTER_APP_V1);
footer.entityCount = tolel(m_entityCount);
footer.cookie = cookie;
@@ -264,7 +260,7 @@ BackupDataReader::Status()
} while(0)
status_t
-BackupDataReader::ReadNextHeader()
+BackupDataReader::ReadNextHeader(int* type)
{
if (m_status != NO_ERROR) {
return m_status;
@@ -280,7 +276,7 @@ BackupDataReader::ReadNextHeader()
m_header.type = fromlel(m_header.type);
switch (m_header.type)
{
- case APP_MAGIC_V1:
+ case BACKUP_HEADER_APP_V1:
m_header.app.packageLen = fromlel(m_header.app.packageLen);
if (m_header.app.packageLen < 0) {
LOGD("App header at %d has packageLen<0: 0x%08x\n", (int)m_pos,
@@ -289,7 +285,7 @@ BackupDataReader::ReadNextHeader()
}
m_header.app.cookie = m_header.app.cookie;
break;
- case ENTITY_MAGIC_V1:
+ case BACKUP_HEADER_ENTITY_V1:
m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
if (m_header.entity.keyLen <= 0) {
LOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
@@ -297,14 +293,9 @@ BackupDataReader::ReadNextHeader()
m_status = EINVAL;
}
m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
- if (m_header.entity.dataSize < 0) {
- LOGD("Entity header at %d has dataSize<0: 0x%08x\n", (int)m_pos,
- (int)m_header.entity.dataSize);
- m_status = EINVAL;
- }
m_entityCount++;
break;
- case FOOTER_MAGIC_V1:
+ case BACKUP_FOOTER_APP_V1:
m_header.footer.entityCount = fromlel(m_header.footer.entityCount);
if (m_header.footer.entityCount < 0) {
LOGD("Entity header at %d has entityCount<0: 0x%08x\n", (int)m_pos,
@@ -318,6 +309,9 @@ BackupDataReader::ReadNextHeader()
m_status = EINVAL;
}
m_pos += sizeof(m_header);
+ if (type) {
+ *type = m_header.type;
+ }
return m_status;
}
@@ -328,7 +322,7 @@ BackupDataReader::ReadAppHeader(String8* packageName, int* cookie)
if (m_status != NO_ERROR) {
return m_status;
}
- if (m_header.type != APP_MAGIC_V1) {
+ if (m_header.type != BACKUP_HEADER_APP_V1) {
return EINVAL;
}
size_t size = m_header.app.packageLen;
@@ -349,7 +343,7 @@ BackupDataReader::ReadAppHeader(String8* packageName, int* cookie)
bool
BackupDataReader::HasEntities()
{
- return m_status == NO_ERROR && m_header.type == ENTITY_MAGIC_V1;
+ return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
}
status_t
@@ -358,10 +352,10 @@ BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
if (m_status != NO_ERROR) {
return m_status;
}
- if (m_header.type != ENTITY_MAGIC_V1) {
+ if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
return EINVAL;
}
- size_t size = m_header.app.packageLen;
+ size_t size = m_header.entity.keyLen;
char* buf = key->lockBuffer(size);
if (key == NULL) {
key->unlockBuffer();
@@ -378,6 +372,23 @@ BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
}
status_t
+BackupDataReader::SkipEntityData()
+{
+ if (m_status != NO_ERROR) {
+ return m_status;
+ }
+ if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
+ return EINVAL;
+ }
+ if (m_header.entity.dataSize > 0) {
+ int pos = lseek(m_fd, m_header.entity.dataSize, SEEK_CUR);
+ return pos == -1 ? (int)errno : (int)NO_ERROR;
+ } else {
+ return NO_ERROR;
+ }
+}
+
+status_t
BackupDataReader::ReadEntityData(void* data, size_t size)
{
if (m_status != NO_ERROR) {
@@ -395,7 +406,7 @@ BackupDataReader::ReadAppFooter(int* cookie)
if (m_status != NO_ERROR) {
return m_status;
}
- if (m_header.type != FOOTER_MAGIC_V1) {
+ if (m_header.type != BACKUP_FOOTER_APP_V1) {
return EINVAL;
}
if (m_header.footer.entityCount != m_entityCount) {
diff --git a/libs/utils/backup_helper_file.cpp b/libs/utils/backup_helper_file.cpp
index bf56945..8efb3eb 100644
--- a/libs/utils/backup_helper_file.cpp
+++ b/libs/utils/backup_helper_file.cpp
@@ -40,7 +40,7 @@ namespace android {
#define MAGIC0 0x70616e53 // Snap
#define MAGIC1 0x656c6946 // File
-#if TEST_BACKUP_HELPERS
+#if 0 // TEST_BACKUP_HELPERS
#define LOGP(x...) printf(x)
#else
#define LOGP(x...) LOGD(x)
@@ -181,45 +181,105 @@ write_snapshot_file(int fd, const KeyedVector<String8,FileState>& snapshot)
}
static int
-write_delete_file(const String8& key)
+write_delete_file(BackupDataWriter* dataStream, const String8& key)
{
LOGP("write_delete_file %s\n", key.string());
- return 0;
+ return dataStream->WriteEntityHeader(key, -1);
}
static int
-write_update_file(const String8& realFilename, const String8& key)
+write_update_file(BackupDataWriter* dataStream, int fd, const String8& key,
+ const String8& realFilename)
{
LOGP("write_update_file %s (%s)\n", realFilename.string(), key.string());
- return 0;
-}
-static int
-compute_crc32(const String8& filename)
-{
const int bufsize = 4*1024;
+ int err;
int amt;
+ int fileSize;
+ int bytesLeft;
+
+ char* buf = (char*)malloc(bufsize);
+ int crc = crc32(0L, Z_NULL, 0);
+
+
+ bytesLeft = fileSize = lseek(fd, 0, SEEK_END);
+ lseek(fd, 0, SEEK_SET);
+
+ err = dataStream->WriteEntityHeader(key, bytesLeft);
+ if (err != 0) {
+ return err;
+ }
+
+ while ((amt = read(fd, buf, bufsize)) != 0 && bytesLeft > 0) {
+ bytesLeft -= amt;
+ if (bytesLeft < 0) {
+ amt += bytesLeft; // Plus a negative is minus. Don't write more than we promised.
+ }
+ err = dataStream->WriteEntityData(buf, amt);
+ if (err != 0) {
+ return err;
+ }
+ }
+ if (bytesLeft != 0) {
+ if (bytesLeft > 0) {
+ // Pad out the space we promised in the buffer. We can't corrupt the buffer,
+ // even though the data we're sending is probably bad.
+ memset(buf, 0, bufsize);
+ while (bytesLeft > 0) {
+ amt = bytesLeft < bufsize ? bytesLeft : bufsize;
+ bytesLeft -= amt;
+ err = dataStream->WriteEntityData(buf, amt);
+ if (err != 0) {
+ return err;
+ }
+ }
+ }
+ LOGE("write_update_file size mismatch for %s. expected=%d actual=%d."
+ " You aren't doing proper locking!",
+ realFilename.string(), fileSize, fileSize-bytesLeft);
+ }
+
+ free(buf);
- int fd = open(filename.string(), O_RDONLY);
+ return NO_ERROR;
+}
+
+static int
+write_update_file(BackupDataWriter* dataStream, const String8& key, const String8& realFilename)
+{
+ int err;
+ int fd = open(realFilename.string(), O_RDONLY);
if (fd == -1) {
- return -1;
+ return errno;
}
+ err = write_update_file(dataStream, fd, key, realFilename);
+ close(fd);
+ return err;
+}
+
+static int
+compute_crc32(int fd)
+{
+ const int bufsize = 4*1024;
+ int amt;
char* buf = (char*)malloc(bufsize);
int crc = crc32(0L, Z_NULL, 0);
+ lseek(fd, 0, SEEK_SET);
+
while ((amt = read(fd, buf, bufsize)) != 0) {
crc = crc32(crc, (Bytef*)buf, amt);
}
- close(fd);
free(buf);
return crc;
}
int
-back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
+back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
char const* fileBase, char const* const* files, int fileCount)
{
int err;
@@ -252,7 +312,8 @@ back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
s.modTime_nsec = 0; // workaround sim breakage
//s.modTime_nsec = st.st_mtime_nsec;
s.size = st.st_size;
- s.crc32 = compute_crc32(realFilename);
+
+ // we compute the crc32 later down below, when we already have the file open.
newSnapshot.add(name, s);
}
@@ -270,30 +331,42 @@ back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
String8 realFilename(base);
realFilename.appendPath(q);
LOGP("file added: %s\n", realFilename.string());
- write_update_file(realFilename, q);
+ write_update_file(dataStream, q, realFilename);
m++;
}
else if (cmp < 0) {
// file removed
LOGP("file removed: %s\n", p.string());
- write_delete_file(p);
+ dataStream->WriteEntityHeader(p, -1);
n++;
}
else {
+
// both files exist, check them
String8 realFilename(base);
realFilename.appendPath(q);
const FileState& f = oldSnapshot.valueAt(n);
- const FileState& g = newSnapshot.valueAt(m);
-
- LOGP("%s\n", q.string());
- LOGP(" new: modTime=%d,%d size=%-3d crc32=0x%08x\n",
- f.modTime_sec, f.modTime_nsec, f.size, f.crc32);
- LOGP(" old: modTime=%d,%d size=%-3d crc32=0x%08x\n",
- g.modTime_sec, g.modTime_nsec, g.size, g.crc32);
- if (f.modTime_sec != g.modTime_sec || f.modTime_nsec != g.modTime_nsec
- || f.size != g.size || f.crc32 != g.crc32) {
- write_update_file(realFilename, p);
+ FileState& g = newSnapshot.editValueAt(m);
+
+ int fd = open(realFilename.string(), O_RDONLY);
+ if (fd != -1) {
+ // We can't open the file. Don't report it as a delete either. Let the
+ // server keep the old version. Maybe they'll be able to deal with it
+ // on restore.
+ } else {
+ g.crc32 = compute_crc32(fd);
+
+ LOGP("%s\n", q.string());
+ LOGP(" new: modTime=%d,%d size=%-3d crc32=0x%08x\n",
+ f.modTime_sec, f.modTime_nsec, f.size, f.crc32);
+ LOGP(" old: modTime=%d,%d size=%-3d crc32=0x%08x\n",
+ g.modTime_sec, g.modTime_nsec, g.size, g.crc32);
+ if (f.modTime_sec != g.modTime_sec || f.modTime_nsec != g.modTime_nsec
+ || f.size != g.size || f.crc32 != g.crc32) {
+ write_update_file(dataStream, fd, p, realFilename);
+ }
+
+ close(fd);
}
n++;
m++;
@@ -302,7 +375,7 @@ back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
// these were deleted
while (n<N) {
- write_delete_file(oldSnapshot.keyAt(n));
+ dataStream->WriteEntityHeader(oldSnapshot.keyAt(n), -1);
n++;
}
@@ -311,7 +384,7 @@ back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
const String8& q = newSnapshot.keyAt(m);
String8 realFilename(base);
realFilename.appendPath(q);
- write_update_file(realFilename, q);
+ write_update_file(dataStream, q, realFilename);
m++;
}
@@ -911,10 +984,14 @@ backup_helper_test_files()
fprintf(stderr, "error creating: %s\n", strerror(errno));
return errno;
}
+
+ {
+ BackupDataWriter dataStream(dataStreamFD);
- err = back_up_files(-1, dataStreamFD, newSnapshotFD, SCRATCH_DIR, files_before, 5);
- if (err != 0) {
- return err;
+ err = back_up_files(-1, &dataStream, newSnapshotFD, SCRATCH_DIR, files_before, 5);
+ if (err != 0) {
+ return err;
+ }
}
close(dataStreamFD);
@@ -968,10 +1045,15 @@ backup_helper_test_files()
return errno;
}
- err = back_up_files(oldSnapshotFD, dataStreamFD, newSnapshotFD, SCRATCH_DIR, files_after, 6);
- if (err != 0) {
- return err;
- }
+ {
+ BackupDataWriter dataStream(dataStreamFD);
+
+ err = back_up_files(oldSnapshotFD, &dataStream, newSnapshotFD, SCRATCH_DIR,
+ files_after, 6);
+ if (err != 0) {
+ return err;
+ }
+}
close(oldSnapshotFD);
close(dataStreamFD);
diff --git a/services/java/com/android/server/BackupManagerService.java b/services/java/com/android/server/BackupManagerService.java
index 983329b..e3fff81 100644
--- a/services/java/com/android/server/BackupManagerService.java
+++ b/services/java/com/android/server/BackupManagerService.java
@@ -158,11 +158,13 @@ class BackupManagerService extends IBackupManager.Stub {
case MSG_RUN_BACKUP:
// snapshot the pending-backup set and work on that
synchronized (mQueueLock) {
- mBackupQueue = new ArrayList();
- for (BackupRequest b: mPendingBackups.values()) {
- mBackupQueue.add(b);
+ if (mBackupQueue == null) {
+ mBackupQueue = new ArrayList();
+ for (BackupRequest b: mPendingBackups.values()) {
+ mBackupQueue.add(b);
+ }
+ mPendingBackups = new HashMap<ComponentName,BackupRequest>();
}
- mPendingBackups = new HashMap<ComponentName,BackupRequest>();
// !!! TODO: start a new backup-queue journal file too
// WARNING: If we crash after this line, anything in mPendingBackups will
// be lost. FIX THIS.
@@ -190,9 +192,13 @@ class BackupManagerService extends IBackupManager.Stub {
BackupRequest request;
synchronized (mQueueLock) {
int queueSize = mBackupQueue.size();
+ Log.d(TAG, "mBackupQueue.size=" + queueSize);
if (queueSize == 0) {
mBackupQueue = null;
- // TODO: Anything else to do here?
+ // if there are pending backups, start those after a short delay
+ if (mPendingBackups.size() > 0) {
+ mBackupHandler.sendEmptyMessageDelayed(MSG_RUN_BACKUP, COLLECTION_INTERVAL);
+ }
return;
}
request = mBackupQueue.get(0);
@@ -267,7 +273,7 @@ class BackupManagerService extends IBackupManager.Stub {
// !!! TODO: After successful transport, delete the now-stale data
// and juggle the files so that next time the new state is passed
- backupDataName.delete();
+ //backupDataName.delete();
newStateName.renameTo(savedStateName);
} catch (FileNotFoundException fnf) {
@@ -284,6 +290,9 @@ class BackupManagerService extends IBackupManager.Stub {
mBackupQueue.remove(0);
}
mContext.unbindService(mBackupHandler);
+
+ // start the next one
+ startOneService();
}
// Add the backup services in the given package to our set of known backup participants.
diff --git a/tests/backup/Android.mk b/tests/backup/Android.mk
index 2e3385f..0813c35 100644
--- a/tests/backup/Android.mk
+++ b/tests/backup/Android.mk
@@ -21,7 +21,7 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
backup_helper_test.cpp
-LOCAL_MODULE_TAGS := user
+LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := backup_helper_test
LOCAL_SHARED_LIBRARIES := libutils
@@ -31,7 +31,7 @@ include $(BUILD_EXECUTABLE)
# ========================================
include $(CLEAR_VARS)
-LOCAL_MODULE_TAGS := user
+LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)