diff options
author | Christopher Tate <ctate@google.com> | 2011-04-01 14:43:32 -0700 |
---|---|---|
committer | Christopher Tate <ctate@google.com> | 2011-05-10 17:52:51 -0700 |
commit | 4a627c71ff53a4fca1f961f4b1dcc0461df18a06 (patch) | |
tree | 270190b1e030424210b6375ca886c45db10c4fb6 /core/jni/android_app_backup_FullBackup.cpp | |
parent | 2bb51bb203c117649db10ad8bd497f199ca797b0 (diff) | |
download | frameworks_base-4a627c71ff53a4fca1f961f4b1dcc0461df18a06.zip frameworks_base-4a627c71ff53a4fca1f961f4b1dcc0461df18a06.tar.gz frameworks_base-4a627c71ff53a4fca1f961f4b1dcc0461df18a06.tar.bz2 |
Full local backup infrastructure
This is the basic infrastructure for pulling a full(*) backup of the
device's data over an adb(**) connection to the local device. The
basic process consists of these interacting pieces:
1. The framework's BackupManagerService, which coordinates the
collection of app data and routing to the destination.
2. A new framework-provided BackupAgent implementation called
FullBackupAgent, which is instantiated in the target applications'
processes in turn, and knows how to emit a datastream that contains
all of the app's saved data files.
3. A new shell-level program called "bu" that is used to bridge from
adb to the framework's Backup Manager.
4. adb itself, which now knows how to use 'bu' to kick off a backup
operation and pull the resulting data stream to the desktop host.
5. A system-provided application that verifies with the user that
an attempted backup/restore operation is in fact expected and to
be allowed.
The full agent implementation is not used during normal operation of
the delta-based app-customized remote backup process. Instead it's
used during user-confirmed *full* backup of applications and all their
data to a local destination, e.g. via the adb connection.
The output format is 'tar'. This makes it very easy for the end
user to examine the resulting dataset, e.g. for purpose of extracting
files for debug purposes; as well as making it easy to contemplate
adding things like a direct gzip stage to the data pipeline during
backup/restore. It also makes it convenient to construct and maintain
synthetic backup datasets for testing purposes.
Within the tar format, certain artificial conventions are used.
All files are stored within top-level directories according to
their semantic origin:
apps/pkgname/a/ : Application .apk file itself
apps/pkgname/obb/: The application's associated .obb containers
apps/pkgname/f/ : The subtree rooted at the getFilesDir() location
apps/pkgname/db/ : The subtree rooted at the getDatabasePath() parent
apps/pkgname/sp/ : The subtree rooted at the getSharedPrefsFile() parent
apps/pkgname/r/ : Files stored relative to the root of the app's file tree
apps/pkgname/c/ : Reserved for the app's getCacheDir() tree; not stored.
For each package, the first entry in the tar stream is a file called
"_manifest", nominally rooted at apps/pkgname. This file contains some
metadata about the package whose data is stored in the archive.
The contents of shared storage can optionally be included in the tar
stream. It is placed in the synthetic location:
shared/...
uid/gid are ignored; app uids are assigned at install time, and the
app's data is handled from within its own execution environment, so
will automatically have the app's correct uid.
Forward-locked .apk files are never backed up. System-partition
.apk files are not backed up unless they have been overridden by a
post-factory upgrade, in which case the current .apk *is* backed up --
i.e. the .apk that matches the on-disk data. The manifest preceding
each application's portion of the tar stream provides version numbers
and signature blocks for version checking, as well as an indication
of whether the restore logic should expect to install the .apk before
extracting the data.
System packages can designate their own full backup agents. This is
to manage things like the settings provider which (a) cannot be shut
down on the fly in order to do a clean snapshot of their file trees,
and (b) manage data that is not only irrelevant but actively hostile
to non-identical devices -- CDMA telephony settings would seriously
mess up a GSM device if emplaced there blind, for example.
When a full backup or restore is initiated from adb, the system will
present a confirmation UI that the user must explicitly respond to
within a short [~ 30 seconds] timeout. This is to avoid the
possibility of malicious desktop-side software secretly grabbing a copy
of all the user's data for nefarious purposes.
(*) The backup is not strictly a full mirror. In particular, the
settings database is not cloned; it is handled the same way that
it is in cloud backup/restore. This is because some settings
are actively destructive if cloned onto a different (or
especially a different-model) device: telephony settings and
AndroidID are good examples of this.
(**) On the framework side it doesn't care that it's adb; it just
sends the tar stream to a file descriptor. This can easily be
retargeted around whatever transport we might decide to use
in the future.
KNOWN ISSUES:
* the security UI is desperately ugly; no proper designs have yet
been done for it
* restore is not yet implemented
* shared storage backup is not yet implemented
* symlinks aren't yet handled, though some infrastructure for
dealing with them has been put in place.
Change-Id: Ia8347611e23b398af36ea22c36dff0a276b1ce91
Diffstat (limited to 'core/jni/android_app_backup_FullBackup.cpp')
-rw-r--r-- | core/jni/android_app_backup_FullBackup.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/core/jni/android_app_backup_FullBackup.cpp b/core/jni/android_app_backup_FullBackup.cpp new file mode 100644 index 0000000..ecfe5ff --- /dev/null +++ b/core/jni/android_app_backup_FullBackup.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2011 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 "FullBackup_native" +#include <utils/Log.h> +#include <utils/String8.h> + +#include "JNIHelp.h" +#include <android_runtime/AndroidRuntime.h> + +#include <utils/BackupHelpers.h> + +#include <string.h> + +namespace android +{ + +// android.app.backup.BackupDataOutput +static struct { + // This is actually a native pointer to the underlying BackupDataWriter instance + jfieldID mBackupWriter; +} sBackupDataOutput; + +/* + * Write files to the given output. This implementation does *not* create + * a standalone archive suitable for restore on its own. In particular, the identification of + * the application's name etc is not in-band here; it's assumed that the calling code has + * taken care of supplying that information previously in the output stream. + * + * The file format is 'tar's, with special semantics applied by use of a "fake" directory + * hierarchy within the tar stream: + * + * apps/packagename/a/Filename.apk - this is an actual application binary, which will be + * installed on the target device at restore time. These need to appear first in the tar + * stream. + * apps/packagename/obb/[relpath] - OBB containers belonging the app + * apps/packagename/r/[relpath] - these are files at the root of the app's data tree + * apps/packagename/f/[relpath] - this is a file within the app's getFilesDir() tree, stored + * at [relpath] relative to the top of that tree. + * apps/packagename/db/[relpath] - as with "files" but for the getDatabasePath() tree + * apps/packagename/sp/[relpath] - as with "files" but for the getSharedPrefsFile() tree + * apps/packagename/c/[relpath] - as with "files" but for the getCacheDir() tree + * + * and for the shared storage hierarchy: + * + * shared/[relpaths] - files belonging in the device's shared storage location. This will + * *not* include .obb files; those are saved with their owning apps. + * + * This method writes one file data block. 'domain' is the name of the appropriate pseudo- + * directory to be applied for this file; 'linkdomain' is the pseudo-dir for a relative + * symlink's antecedent. + * + * packagename: the package name to use as the top level directory tag + * domain: which semantic name the file is to be stored under (a, r, f, db, etc) + * linkdomain: where a symlink points for purposes of rewriting; current unused + * rootpath: prefix to be snipped from full path when encoding in tar + * path: absolute path to the file to be saved + * dataOutput: the BackupDataOutput object that we're saving into + */ +static int backupToTar(JNIEnv* env, jobject clazz, jstring packageNameObj, + jstring domainObj, jstring linkdomain, + jstring rootpathObj, jstring pathObj, jobject dataOutputObj) { + // Extract the various strings, allowing for null object pointers + const char* packagenamechars = env->GetStringUTFChars(packageNameObj, NULL); + const char* rootchars = env->GetStringUTFChars(rootpathObj, NULL); + const char* pathchars = env->GetStringUTFChars(pathObj, NULL); + const char* domainchars = env->GetStringUTFChars(domainObj, NULL); + + String8 packageName(packagenamechars ? packagenamechars : ""); + String8 rootpath(rootchars ? rootchars : ""); + String8 path(pathchars ? pathchars : ""); + String8 domain(domainchars ? domainchars : ""); + + if (domainchars) env->ReleaseStringUTFChars(domainObj, domainchars); + if (pathchars) env->ReleaseStringUTFChars(pathObj, pathchars); + if (rootchars) env->ReleaseStringUTFChars(rootpathObj, rootchars); + if (packagenamechars) env->ReleaseStringUTFChars(packageNameObj, packagenamechars); + + // Extract the data output fd + BackupDataWriter* writer = (BackupDataWriter*) env->GetIntField(dataOutputObj, + sBackupDataOutput.mBackupWriter); + + // Validate + if (!writer) { + LOGE("No output stream provided [%s]", path.string()); + return -1; + } + + if (path.length() < rootpath.length()) { + LOGE("file path [%s] shorter than root path [%s]", + path.string(), rootpath.string()); + return -1; + } + + return write_tarfile(packageName, domain, rootpath, path, writer); +} + +static const JNINativeMethod g_methods[] = { + { "backupToTar", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/app/backup/BackupDataOutput;)I", + (void*)backupToTar }, +}; + +int register_android_app_backup_FullBackup(JNIEnv* env) +{ + jclass clazz = env->FindClass("android/app/backup/BackupDataOutput"); + LOG_FATAL_IF(clazz == NULL, "Unable to find class android.app.backup.BackupDataOutput"); + + sBackupDataOutput.mBackupWriter = env->GetFieldID(clazz, "mBackupWriter", "I"); + LOG_FATAL_IF(sBackupDataOutput.mBackupwriter == NULL, + "Unable to find mBackupWriter field in android.app.backup.BackupDataOutput"); + + return AndroidRuntime::registerNativeMethods(env, "android/app/backup/FullBackup", + g_methods, NELEM(g_methods)); +} + +} |