summaryrefslogtreecommitdiffstats
path: root/core/java/android/app/backup
diff options
context:
space:
mode:
authorChristopher Tate <ctate@google.com>2011-05-18 16:28:19 -0700
committerChristopher Tate <ctate@google.com>2011-06-01 15:09:55 -0700
commit75a99709accef8cf221fd436d646727e7c8dd1f1 (patch)
tree9ce16dbf95890e8dad57d63724a6cdb3d36d6fb9 /core/java/android/app/backup
parent2978cef0a77550ea3a364ffbf42fc43f2029070e (diff)
downloadframeworks_base-75a99709accef8cf221fd436d646727e7c8dd1f1.zip
frameworks_base-75a99709accef8cf221fd436d646727e7c8dd1f1.tar.gz
frameworks_base-75a99709accef8cf221fd436d646727e7c8dd1f1.tar.bz2
Restore from a previous full backup's tarfile
Usage: adb restore [tarfilename] Restores app data [and installs the apps if necessary from the backup file] captured in a previous invocation of 'adb backup'. The user must explicitly acknowledge the action on-device before it is allowed to proceed; this prevents any "invisible" pushes of content from the host to the device. Known issues: * The settings databases and wallpaper are saved/restored, but lots of other system state is not yet captured in the full backup. This means that for practical purposes this is usable for 3rd party apps at present but not for full-system cloning/imaging. Change-Id: I0c748b645845e7c9178e30bf142857861a64efd3
Diffstat (limited to 'core/java/android/app/backup')
-rw-r--r--core/java/android/app/backup/BackupAgent.java32
-rw-r--r--core/java/android/app/backup/FullBackup.java110
-rw-r--r--core/java/android/app/backup/FullBackupAgent.java42
-rw-r--r--core/java/android/app/backup/IBackupManager.aidl8
4 files changed, 179 insertions, 13 deletions
diff --git a/core/java/android/app/backup/BackupAgent.java b/core/java/android/app/backup/BackupAgent.java
index dc60e24..17f8adb 100644
--- a/core/java/android/app/backup/BackupAgent.java
+++ b/core/java/android/app/backup/BackupAgent.java
@@ -179,10 +179,18 @@ public abstract class BackupAgent extends ContextWrapper {
throws IOException;
/**
+ * @hide
+ */
+ public void onRestoreFile(ParcelFileDescriptor data, long size,
+ int type, String domain, String path, long mode, long mtime)
+ throws IOException {
+ // empty stub implementation
+ }
+
+ /**
* Package-private, used only for dispatching an extra step during full backup
*/
void onSaveApk(BackupDataOutput data) {
- if (DEBUG) Log.v(TAG, "--- base onSaveApk() ---");
}
// ----- Core implementation -----
@@ -203,6 +211,7 @@ public abstract class BackupAgent extends ContextWrapper {
private class BackupServiceBinder extends IBackupAgent.Stub {
private static final String TAG = "BackupServiceBinder";
+ @Override
public void doBackup(ParcelFileDescriptor oldState,
ParcelFileDescriptor data,
ParcelFileDescriptor newState,
@@ -236,6 +245,7 @@ public abstract class BackupAgent extends ContextWrapper {
}
}
+ @Override
public void doRestore(ParcelFileDescriptor data, int appVersionCode,
ParcelFileDescriptor newState,
int token, IBackupManager callbackBinder) throws RemoteException {
@@ -261,5 +271,25 @@ public abstract class BackupAgent extends ContextWrapper {
}
}
}
+
+ @Override
+ public void doRestoreFile(ParcelFileDescriptor data, long size,
+ int type, String domain, String path, long mode, long mtime,
+ int token, IBackupManager callbackBinder) throws RemoteException {
+ long ident = Binder.clearCallingIdentity();
+ try {
+Log.d(TAG, "doRestoreFile() => onRestoreFile()");
+ BackupAgent.this.onRestoreFile(data, size, type, domain, path, mode, mtime);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ } finally {
+ Binder.restoreCallingIdentity(ident);
+ try {
+ callbackBinder.opComplete(token);
+ } catch (RemoteException e) {
+ // we'll time out anyway, so we're safe
+ }
+ }
+ }
}
}
diff --git a/core/java/android/app/backup/FullBackup.java b/core/java/android/app/backup/FullBackup.java
index 9850566..dfb0dd7 100644
--- a/core/java/android/app/backup/FullBackup.java
+++ b/core/java/android/app/backup/FullBackup.java
@@ -16,6 +16,17 @@
package android.app.backup;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import libcore.io.ErrnoException;
+import libcore.io.Libcore;
+
/**
* Global constant definitions et cetera related to the full-backup-to-fd
* binary format.
@@ -23,18 +34,95 @@ package android.app.backup;
* @hide
*/
public class FullBackup {
- public static String APK_TREE_TOKEN = "a";
- public static String OBB_TREE_TOKEN = "obb";
- public static String ROOT_TREE_TOKEN = "r";
- public static String DATA_TREE_TOKEN = "f";
- public static String DATABASE_TREE_TOKEN = "db";
- public static String SHAREDPREFS_TREE_TOKEN = "sp";
- public static String CACHE_TREE_TOKEN = "c";
-
- public static String FULL_BACKUP_INTENT_ACTION = "fullback";
- public static String FULL_RESTORE_INTENT_ACTION = "fullrest";
- public static String CONF_TOKEN_INTENT_EXTRA = "conftoken";
+ static final String TAG = "FullBackup";
+
+ public static final String APK_TREE_TOKEN = "a";
+ public static final String OBB_TREE_TOKEN = "obb";
+ public static final String ROOT_TREE_TOKEN = "r";
+ public static final String DATA_TREE_TOKEN = "f";
+ public static final String DATABASE_TREE_TOKEN = "db";
+ public static final String SHAREDPREFS_TREE_TOKEN = "sp";
+ public static final String CACHE_TREE_TOKEN = "c";
+ public static final String SHARED_STORAGE_TOKEN = "shared";
+
+ public static final String APPS_PREFIX = "apps/";
+ public static final String SHARED_PREFIX = "shared/";
+
+ public static final String FULL_BACKUP_INTENT_ACTION = "fullback";
+ public static final String FULL_RESTORE_INTENT_ACTION = "fullrest";
+ public static final String CONF_TOKEN_INTENT_EXTRA = "conftoken";
+
+ public static final int TYPE_EOF = 0;
+ public static final int TYPE_FILE = 1;
+ public static final int TYPE_DIRECTORY = 2;
+ public static final int TYPE_SYMLINK = 3;
static public native int backupToTar(String packageName, String domain,
String linkdomain, String rootpath, String path, BackupDataOutput output);
+
+ static public void restoreToFile(ParcelFileDescriptor data,
+ long size, int type, long mode, long mtime, File outFile) throws IOException {
+ if (type == FullBackup.TYPE_DIRECTORY) {
+ // Canonically a directory has no associated content, so we don't need to read
+ // anything from the pipe in this case. Just create the directory here and
+ // drop down to the final metadata adjustment.
+ if (outFile != null) outFile.mkdirs();
+ } else {
+ FileOutputStream out = null;
+
+ // Pull the data from the pipe, copying it to the output file, until we're done
+ try {
+ if (outFile != null) {
+ File parent = outFile.getParentFile();
+ if (!parent.exists()) {
+ // in practice this will only be for the default semantic directories,
+ // and using the default mode for those is appropriate.
+ // TODO: support the edge case of apps that have adjusted the
+ // permissions on these core directories
+ parent.mkdirs();
+ }
+ out = new FileOutputStream(outFile);
+ }
+ } catch (IOException e) {
+ Log.e(TAG, "Unable to create/open file " + outFile.getPath(), e);
+ }
+
+ byte[] buffer = new byte[32 * 1024];
+ final long origSize = size;
+ FileInputStream in = new FileInputStream(data.getFileDescriptor());
+ while (size > 0) {
+ int toRead = (size > buffer.length) ? buffer.length : (int)size;
+ int got = in.read(buffer, 0, toRead);
+ if (got <= 0) {
+ Log.w(TAG, "Incomplete read: expected " + size + " but got "
+ + (origSize - size));
+ break;
+ }
+ if (out != null) {
+ try {
+ out.write(buffer, 0, got);
+ } catch (IOException e) {
+ // Problem writing to the file. Quit copying data and delete
+ // the file, but of course keep consuming the input stream.
+ Log.e(TAG, "Unable to write to file " + outFile.getPath(), e);
+ out.close();
+ out = null;
+ outFile.delete();
+ }
+ }
+ size -= got;
+ }
+ if (out != null) out.close();
+ }
+
+ // Now twiddle the state to match the backup, assuming all went well
+ if (outFile != null) {
+ try {
+ Libcore.os.chmod(outFile.getPath(), (int)mode);
+ } catch (ErrnoException e) {
+ e.rethrowAsIOException();
+ }
+ outFile.setLastModified(mtime);
+ }
+ }
}
diff --git a/core/java/android/app/backup/FullBackupAgent.java b/core/java/android/app/backup/FullBackupAgent.java
index f0a1f2a..4dca593 100644
--- a/core/java/android/app/backup/FullBackupAgent.java
+++ b/core/java/android/app/backup/FullBackupAgent.java
@@ -28,6 +28,9 @@ import libcore.io.OsConstants;
import libcore.io.StructStat;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
@@ -53,8 +56,12 @@ public class FullBackupAgent extends BackupAgent {
private String mCacheDir;
private String mLibDir;
+ private File NULL_FILE;
+
@Override
public void onCreate() {
+ NULL_FILE = new File("/dev/null");
+
mPm = getPackageManager();
try {
ApplicationInfo appInfo = mPm.getApplicationInfo(getPackageName(), 0);
@@ -177,7 +184,40 @@ public class FullBackupAgent extends BackupAgent {
}
}
+ /**
+ * Dummy -- We're never used for restore of an incremental dataset
+ */
+ @Override
+ public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
+ throws IOException {
+ }
+
+ /**
+ * Restore the described file from the given pipe.
+ */
@Override
- public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) {
+ public void onRestoreFile(ParcelFileDescriptor data, long size,
+ int type, String domain, String relpath, long mode, long mtime)
+ throws IOException {
+ String basePath = null;
+ File outFile = null;
+
+ if (DEBUG) Log.d(TAG, "onRestoreFile() size=" + size + " type=" + type
+ + " domain=" + domain + " relpath=" + relpath + " mode=" + mode
+ + " mtime=" + mtime);
+
+ // Parse out the semantic domains into the correct physical location
+ if (domain.equals(FullBackup.DATA_TREE_TOKEN)) basePath = mFilesDir;
+ else if (domain.equals(FullBackup.DATABASE_TREE_TOKEN)) basePath = mDatabaseDir;
+ else if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) basePath = mMainDir;
+ else if (domain.equals(FullBackup.SHAREDPREFS_TREE_TOKEN)) basePath = mSharedPrefsDir;
+
+ // Not a supported output location? We need to consume the data
+ // anyway, so send it to /dev/null
+ outFile = (basePath != null) ? new File(basePath, relpath) : null;
+ if (DEBUG) Log.i(TAG, "[" + domain + " : " + relpath + "] mapped to " + outFile.getPath());
+
+ // Now that we've figured out where the data goes, send it on its way
+ FullBackup.restoreToFile(data, size, type, mode, mtime, outFile);
}
}
diff --git a/core/java/android/app/backup/IBackupManager.aidl b/core/java/android/app/backup/IBackupManager.aidl
index 94e31a8..bac874e 100644
--- a/core/java/android/app/backup/IBackupManager.aidl
+++ b/core/java/android/app/backup/IBackupManager.aidl
@@ -147,6 +147,14 @@ interface IBackupManager {
boolean allApps, in String[] packageNames);
/**
+ * Restore device content from the data stream passed through the given socket. The
+ * data stream must be in the format emitted by fullBackup().
+ *
+ * <p>Callers must hold the android.permission.BACKUP permission to use this method.
+ */
+ void fullRestore(in ParcelFileDescriptor fd);
+
+ /**
* Confirm that the requested full backup/restore operation can proceed. The system will
* not actually perform the operation described to fullBackup() / fullRestore() unless the
* UI calls back into the Backup Manager to confirm, passing the correct token. At