Scan at boot time to detect newly-present full backup candidates

OTA or similar might have caused an app to appear without the usual
notifications being sent.  Make sure we pick up those apps as
appropriate for full-data backup.

Bug 19462310

Change-Id: Ic17bc72b14cc7599ae8ea540548fda932606b8f2
This commit is contained in:
Christopher Tate
2015-05-20 14:49:38 -07:00
committed by Chris Tate
parent 770e98953e
commit 62d1e1ef7e

View File

@@ -88,13 +88,13 @@ import android.util.Slog;
import android.util.SparseArray; import android.util.SparseArray;
import android.util.StringBuilderPrinter; import android.util.StringBuilderPrinter;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.backup.IBackupTransport; import com.android.internal.backup.IBackupTransport;
import com.android.internal.backup.IObbBackupService; import com.android.internal.backup.IObbBackupService;
import com.android.server.AppWidgetBackupBridge; import com.android.server.AppWidgetBackupBridge;
import com.android.server.EventLogTags; import com.android.server.EventLogTags;
import com.android.server.SystemService; import com.android.server.SystemService;
import com.android.server.backup.PackageManagerBackupAgent.Metadata; import com.android.server.backup.PackageManagerBackupAgent.Metadata;
import com.android.server.pm.PackageManagerService;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
@@ -589,8 +589,12 @@ public class BackupManagerService {
File mFullBackupScheduleFile; File mFullBackupScheduleFile;
// If we're running a schedule-driven full backup, this is the task instance doing it // If we're running a schedule-driven full backup, this is the task instance doing it
PerformFullTransportBackupTask mRunningFullBackupTask; // inside mQueueLock
ArrayList<FullBackupEntry> mFullBackupQueue; // inside mQueueLock @GuardedBy("mQueueLock")
PerformFullTransportBackupTask mRunningFullBackupTask;
@GuardedBy("mQueueLock")
ArrayList<FullBackupEntry> mFullBackupQueue;
// Utility: build a new random integer token // Utility: build a new random integer token
int generateToken() { int generateToken() {
@@ -1229,8 +1233,10 @@ public class BackupManagerService {
} }
} }
// Resume the full-data backup queue synchronized (mQueueLock) {
mFullBackupQueue = readFullBackupSchedule(); // Resume the full-data backup queue
mFullBackupQueue = readFullBackupSchedule();
}
// Register for broadcasts about package install, etc., so we can // Register for broadcasts about package install, etc., so we can
// update the provider list. // update the provider list.
@@ -1248,74 +1254,98 @@ public class BackupManagerService {
} }
private ArrayList<FullBackupEntry> readFullBackupSchedule() { private ArrayList<FullBackupEntry> readFullBackupSchedule() {
boolean changed = false;
ArrayList<FullBackupEntry> schedule = null; ArrayList<FullBackupEntry> schedule = null;
synchronized (mQueueLock) { List<PackageInfo> apps =
if (mFullBackupScheduleFile.exists()) { PackageManagerBackupAgent.getStorableApplications(mPackageManager);
FileInputStream fstream = null;
BufferedInputStream bufStream = null;
DataInputStream in = null;
try {
fstream = new FileInputStream(mFullBackupScheduleFile);
bufStream = new BufferedInputStream(fstream);
in = new DataInputStream(bufStream);
int version = in.readInt(); if (mFullBackupScheduleFile.exists()) {
if (version != SCHEDULE_FILE_VERSION) { FileInputStream fstream = null;
Slog.e(TAG, "Unknown backup schedule version " + version); BufferedInputStream bufStream = null;
return null; DataInputStream in = null;
} try {
fstream = new FileInputStream(mFullBackupScheduleFile);
bufStream = new BufferedInputStream(fstream);
in = new DataInputStream(bufStream);
int N = in.readInt(); int version = in.readInt();
schedule = new ArrayList<FullBackupEntry>(N); if (version != SCHEDULE_FILE_VERSION) {
for (int i = 0; i < N; i++) { Slog.e(TAG, "Unknown backup schedule version " + version);
String pkgName = in.readUTF(); return null;
long lastBackup = in.readLong(); }
try {
PackageInfo pkg = mPackageManager.getPackageInfo(pkgName, 0); final int N = in.readInt();
if (appGetsFullBackup(pkg) schedule = new ArrayList<FullBackupEntry>(N);
&& appIsEligibleForBackup(pkg.applicationInfo)) {
schedule.add(new FullBackupEntry(pkgName, lastBackup)); // HashSet instead of ArraySet specifically because we want the eventual
} else { // lookups against O(hundreds) of entries to be as fast as possible, and
if (DEBUG) { // we discard the set immediately after the scan so the extra memory
Slog.i(TAG, "Package " + pkgName // overhead is transient.
+ " no longer eligible for full backup"); HashSet<String> foundApps = new HashSet<String>(N);
}
} for (int i = 0; i < N; i++) {
} catch (NameNotFoundException e) { String pkgName = in.readUTF();
long lastBackup = in.readLong();
foundApps.add(pkgName); // all apps that we've addressed already
try {
PackageInfo pkg = mPackageManager.getPackageInfo(pkgName, 0);
if (appGetsFullBackup(pkg) && appIsEligibleForBackup(pkg.applicationInfo)) {
schedule.add(new FullBackupEntry(pkgName, lastBackup));
} else {
if (DEBUG) { if (DEBUG) {
Slog.i(TAG, "Package " + pkgName Slog.i(TAG, "Package " + pkgName
+ " not installed; dropping from full backup"); + " no longer eligible for full backup");
} }
} }
} catch (NameNotFoundException e) {
if (DEBUG) {
Slog.i(TAG, "Package " + pkgName
+ " not installed; dropping from full backup");
}
} }
Collections.sort(schedule);
} catch (Exception e) {
Slog.e(TAG, "Unable to read backup schedule", e);
mFullBackupScheduleFile.delete();
schedule = null;
} finally {
IoUtils.closeQuietly(in);
IoUtils.closeQuietly(bufStream);
IoUtils.closeQuietly(fstream);
} }
}
if (schedule == null) { // New apps can arrive "out of band" via OTA and similar, so we also need to
// no prior queue record, or unable to read it. Set up the queue // scan to make sure that we're tracking all full-backup candidates properly
// from scratch. for (PackageInfo app : apps) {
List<PackageInfo> apps = if (appGetsFullBackup(app) && appIsEligibleForBackup(app.applicationInfo)) {
PackageManagerBackupAgent.getStorableApplications(mPackageManager); if (!foundApps.contains(app.packageName)) {
final int N = apps.size(); if (DEBUG) {
schedule = new ArrayList<FullBackupEntry>(N); Slog.i(TAG, "New full backup app " + app.packageName + " found");
for (int i = 0; i < N; i++) { }
PackageInfo info = apps.get(i); schedule.add(new FullBackupEntry(app.packageName, 0));
if (appGetsFullBackup(info) && appIsEligibleForBackup(info.applicationInfo)) { changed = true;
schedule.add(new FullBackupEntry(info.packageName, 0)); }
} }
} }
writeFullBackupScheduleAsync();
Collections.sort(schedule);
} catch (Exception e) {
Slog.e(TAG, "Unable to read backup schedule", e);
mFullBackupScheduleFile.delete();
schedule = null;
} finally {
IoUtils.closeQuietly(in);
IoUtils.closeQuietly(bufStream);
IoUtils.closeQuietly(fstream);
} }
} }
if (schedule == null) {
// no prior queue record, or unable to read it. Set up the queue
// from scratch.
changed = true;
schedule = new ArrayList<FullBackupEntry>(apps.size());
for (PackageInfo info : apps) {
if (appGetsFullBackup(info) && appIsEligibleForBackup(info.applicationInfo)) {
schedule.add(new FullBackupEntry(info.packageName, 0));
}
}
}
if (changed) {
writeFullBackupScheduleAsync();
}
return schedule; return schedule;
} }
@@ -4313,13 +4343,16 @@ public class BackupManagerService {
// This is also slow but easy for modest numbers of apps: work backwards // This is also slow but easy for modest numbers of apps: work backwards
// from the end of the queue until we find an item whose last backup // from the end of the queue until we find an item whose last backup
// time was before this one, then insert this new entry after it. // time was before this one, then insert this new entry after it. If we're
int which; // adding something new we don't bother scanning, and just prepend.
for (which = mFullBackupQueue.size() - 1; which >= 0; which--) { int which = -1;
final FullBackupEntry entry = mFullBackupQueue.get(which); if (lastBackedUp > 0) {
if (entry.lastBackup <= lastBackedUp) { for (which = mFullBackupQueue.size() - 1; which >= 0; which--) {
mFullBackupQueue.add(which + 1, newEntry); final FullBackupEntry entry = mFullBackupQueue.get(which);
break; if (entry.lastBackup <= lastBackedUp) {
mFullBackupQueue.add(which + 1, newEntry);
break;
}
} }
} }
if (which < 0) { if (which < 0) {