diff options
author | Christopher Tate <ctate@google.com> | 2014-07-11 17:25:57 -0700 |
---|---|---|
committer | Christopher Tate <ctate@android.com> | 2014-07-14 22:56:04 +0000 |
commit | a7835b6b6b00923b608a6bc3194e7840f67de7a8 (patch) | |
tree | 17f914d3a2e608714d3833c90d6c0cf2db6ffc7b /core/java/android/app/backup | |
parent | 4ad58a4738048ade35f4bee91554dd84aeab3f88 (diff) | |
download | frameworks_base-a7835b6b6b00923b608a6bc3194e7840f67de7a8.zip frameworks_base-a7835b6b6b00923b608a6bc3194e7840f67de7a8.tar.gz frameworks_base-a7835b6b6b00923b608a6bc3194e7840f67de7a8.tar.bz2 |
Add Context.getNoBackupFilesDir()
This is an app-private filesystem space exactly like the one
reported by Context.getFilesDir(), with one exception: files
placed here are never backed up by the full-backup infrastructure.
If an app attempts to back up any of its contents via the normal
API it's immediately ignored with a logged warning.
The restriction is also enforced on the restore side, because
apps using support libraries might wind up creating full backup
archives containing no_backup subdirs on pre-L devices (via
adb backup, Helium, &c.). We check for this before passing the
restore data to the app, and drop it if we detect the situation
so that the app never sees the bits.
Bug 16240573
Change-Id: I11216a391f1d32117ec7ce15aafc9cd93d0337de
Diffstat (limited to 'core/java/android/app/backup')
-rw-r--r-- | core/java/android/app/backup/BackupAgent.java | 46 | ||||
-rw-r--r-- | core/java/android/app/backup/FullBackup.java | 1 |
2 files changed, 43 insertions, 4 deletions
diff --git a/core/java/android/app/backup/BackupAgent.java b/core/java/android/app/backup/BackupAgent.java index 886f1a6..e2a86e8 100644 --- a/core/java/android/app/backup/BackupAgent.java +++ b/core/java/android/app/backup/BackupAgent.java @@ -247,12 +247,40 @@ public abstract class BackupAgent extends ContextWrapper { throws IOException; /** - * The default implementation backs up the entirety of the application's "owned" - * file system trees to the output. + * The application is having its entire file system contents backed up. {@code data} + * points to the backup destination, and the app has the opportunity to choose which + * files are to be stored. To commit a file as part of the backup, call the + * {@link #fullBackupFile(File, FullBackupDataOutput)} helper method. After all file + * data is written to the output, the agent returns from this method and the backup + * operation concludes. + * + * <p>Certain parts of the app's data are never backed up even if the app explicitly + * sends them to the output: + * + * <ul> + * <li>The contents of the {@link #getCacheDir()} directory</li> + * <li>The contents of the {@link #getNoBackupFilesDir()} directory</li> + * <li>The contents of the app's shared library directory</li> + * </ul> + * + * <p>The default implementation of this method backs up the entirety of the + * application's "owned" file system trees to the output other than the few exceptions + * listed above. Apps only need to override this method if they need to impose special + * limitations on which files are being stored beyond the control that + * {@link #getNoBackupFilesDir()} offers. + * + * @param data A structured wrapper pointing to the backup destination. + * @throws IOException + * + * @see Context#getNoBackupFilesDir() + * @see #fullBackupFile(File, FullBackupDataOutput) + * @see #onRestoreFile(ParcelFileDescriptor, long, File, int, long, long) */ public void onFullBackup(FullBackupDataOutput data) throws IOException { ApplicationInfo appInfo = getApplicationInfo(); + // Note that we don't need to think about the no_backup dir because it's outside + // all of the ones we will be traversing String rootDir = new File(appInfo.dataDir).getCanonicalPath(); String filesDir = getFilesDir().getCanonicalPath(); String databaseDir = getDatabasePath("foo").getParentFile().getCanonicalPath(); @@ -311,6 +339,10 @@ public abstract class BackupAgent extends ContextWrapper { * to place it with the proper location and permissions on the device where the * data is restored. * + * <p class="note">It is safe to explicitly back up files underneath your application's + * {@link #getNoBackupFilesDir()} directory, and they will be restored to that + * location correctly. + * * @param file The file to be backed up. The file must exist and be readable by * the caller. * @param output The destination to which the backed-up file data will be sent. @@ -319,6 +351,7 @@ public abstract class BackupAgent extends ContextWrapper { // Look up where all of our various well-defined dir trees live on this device String mainDir; String filesDir; + String nbFilesDir; String dbDir; String spDir; String cacheDir; @@ -331,6 +364,7 @@ public abstract class BackupAgent extends ContextWrapper { try { mainDir = new File(appInfo.dataDir).getCanonicalPath(); filesDir = getFilesDir().getCanonicalPath(); + nbFilesDir = getNoBackupFilesDir().getCanonicalPath(); dbDir = getDatabasePath("foo").getParentFile().getCanonicalPath(); spDir = getSharedPrefsFile("foo").getParentFile().getCanonicalPath(); cacheDir = getCacheDir().getCanonicalPath(); @@ -354,8 +388,10 @@ public abstract class BackupAgent extends ContextWrapper { return; } - if (filePath.startsWith(cacheDir) || filePath.startsWith(libDir)) { - Log.w(TAG, "lib and cache files are not backed up"); + if (filePath.startsWith(cacheDir) + || filePath.startsWith(libDir) + || filePath.startsWith(nbFilesDir)) { + Log.w(TAG, "lib, cache, and no_backup files are not backed up"); return; } @@ -508,6 +544,8 @@ public abstract class BackupAgent extends ContextWrapper { mode = -1; // < 0 is a token to skip attempting a chmod() } } + } else if (domain.equals(FullBackup.NO_BACKUP_TREE_TOKEN)) { + basePath = getNoBackupFilesDir().getCanonicalPath(); } else { // Not a supported location Log.i(TAG, "Unrecognized domain " + domain); diff --git a/core/java/android/app/backup/FullBackup.java b/core/java/android/app/backup/FullBackup.java index 6ebb6c4..e5b47c6 100644 --- a/core/java/android/app/backup/FullBackup.java +++ b/core/java/android/app/backup/FullBackup.java @@ -40,6 +40,7 @@ public class FullBackup { 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 NO_BACKUP_TREE_TOKEN = "nb"; public static final String DATABASE_TREE_TOKEN = "db"; public static final String SHAREDPREFS_TREE_TOKEN = "sp"; public static final String MANAGED_EXTERNAL_TREE_TOKEN = "ef"; |