| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 19 | import android.app.ActivityManagerNative; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 20 | import android.app.AlarmManager; |
| Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 21 | import android.app.AppGlobals; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 22 | import android.app.IActivityManager; |
| 23 | import android.app.IApplicationThread; |
| 24 | import android.app.IBackupAgent; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 25 | import android.app.PendingIntent; |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 26 | import android.app.backup.BackupAgent; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 27 | import android.app.backup.BackupDataOutput; |
| 28 | import android.app.backup.FullBackup; |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 29 | import android.app.backup.RestoreSet; |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 30 | import android.app.backup.IBackupManager; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 31 | import android.app.backup.IFullBackupRestoreObserver; |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 32 | import android.app.backup.IRestoreObserver; |
| 33 | import android.app.backup.IRestoreSession; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 34 | import android.content.ActivityNotFoundException; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 36 | import android.content.ComponentName; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 39 | import android.content.IntentFilter; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 40 | import android.content.ServiceConnection; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 41 | import android.content.pm.ApplicationInfo; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 42 | import android.content.pm.IPackageDataObserver; |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 43 | import android.content.pm.IPackageDeleteObserver; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 44 | import android.content.pm.IPackageInstallObserver; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 45 | import android.content.pm.IPackageManager; |
| Christopher Tate | 7b88128 | 2009-06-07 13:52:37 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageInfo; |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 47 | import android.content.pm.PackageManager; |
| Jason parks | 1125d78 | 2011-01-12 09:47:26 -0600 | [diff] [blame] | 48 | import android.content.pm.Signature; |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 49 | import android.content.pm.PackageManager.NameNotFoundException; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 50 | import android.net.Uri; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 51 | import android.os.Binder; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 52 | import android.os.Build; |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 53 | import android.os.Bundle; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 54 | import android.os.Environment; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 55 | import android.os.Handler; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 56 | import android.os.HandlerThread; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 57 | import android.os.IBinder; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 58 | import android.os.Looper; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 59 | import android.os.Message; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 60 | import android.os.ParcelFileDescriptor; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 61 | import android.os.PowerManager; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 62 | import android.os.Process; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 63 | import android.os.RemoteException; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 64 | import android.os.SystemClock; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 65 | import android.os.WorkSource; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 66 | import android.provider.Settings; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 67 | import android.util.EventLog; |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 68 | import android.util.Log; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 69 | import android.util.Slog; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 70 | import android.util.SparseArray; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 71 | import android.util.SparseIntArray; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 72 | import android.util.StringBuilderPrinter; |
| 73 | |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 74 | import com.android.internal.backup.BackupConstants; |
| 75 | import com.android.internal.backup.IBackupTransport; |
| 76 | import com.android.internal.backup.LocalTransport; |
| 77 | import com.android.server.PackageManagerBackupAgent.Metadata; |
| 78 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 79 | import java.io.BufferedInputStream; |
| 80 | import java.io.BufferedOutputStream; |
| 81 | import java.io.ByteArrayOutputStream; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 82 | import java.io.DataInputStream; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 83 | import java.io.DataOutputStream; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 84 | import java.io.EOFException; |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 85 | import java.io.File; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 86 | import java.io.FileDescriptor; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 87 | import java.io.FileInputStream; |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 88 | import java.io.FileNotFoundException; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 89 | import java.io.FileOutputStream; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 90 | import java.io.IOException; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 91 | import java.io.InputStream; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 92 | import java.io.OutputStream; |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 93 | import java.io.PrintWriter; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 94 | import java.io.RandomAccessFile; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 95 | import java.security.InvalidAlgorithmParameterException; |
| 96 | import java.security.InvalidKeyException; |
| 97 | import java.security.Key; |
| 98 | import java.security.NoSuchAlgorithmException; |
| 99 | import java.security.SecureRandom; |
| 100 | import java.security.spec.InvalidKeySpecException; |
| 101 | import java.security.spec.KeySpec; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 102 | import java.text.SimpleDateFormat; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 103 | import java.util.ArrayList; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 104 | import java.util.Arrays; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 105 | import java.util.Date; |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 106 | import java.util.HashMap; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 107 | import java.util.HashSet; |
| 108 | import java.util.List; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 109 | import java.util.Map; |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 110 | import java.util.Random; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 111 | import java.util.Set; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 112 | import java.util.concurrent.atomic.AtomicBoolean; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 113 | import java.util.zip.Deflater; |
| 114 | import java.util.zip.DeflaterOutputStream; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 115 | import java.util.zip.InflaterInputStream; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 116 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 117 | import javax.crypto.BadPaddingException; |
| 118 | import javax.crypto.Cipher; |
| 119 | import javax.crypto.CipherInputStream; |
| 120 | import javax.crypto.CipherOutputStream; |
| 121 | import javax.crypto.IllegalBlockSizeException; |
| 122 | import javax.crypto.NoSuchPaddingException; |
| 123 | import javax.crypto.SecretKey; |
| 124 | import javax.crypto.SecretKeyFactory; |
| 125 | import javax.crypto.spec.IvParameterSpec; |
| 126 | import javax.crypto.spec.PBEKeySpec; |
| 127 | import javax.crypto.spec.SecretKeySpec; |
| 128 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 129 | class BackupManagerService extends IBackupManager.Stub { |
| 130 | private static final String TAG = "BackupManagerService"; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 131 | private static final boolean DEBUG = true; |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 132 | private static final boolean MORE_DEBUG = false; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 133 | |
| 134 | // Name and current contents version of the full-backup manifest file |
| 135 | static final String BACKUP_MANIFEST_FILENAME = "_manifest"; |
| 136 | static final int BACKUP_MANIFEST_VERSION = 1; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 137 | static final String BACKUP_FILE_HEADER_MAGIC = "ANDROID BACKUP\n"; |
| 138 | static final int BACKUP_FILE_VERSION = 1; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 139 | static final boolean COMPRESS_FULL_BACKUPS = true; // should be true in production |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 140 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 141 | // How often we perform a backup pass. Privileged external callers can |
| 142 | // trigger an immediate pass. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 143 | private static final long BACKUP_INTERVAL = AlarmManager.INTERVAL_HOUR; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 144 | |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 145 | // Random variation in backup scheduling time to avoid server load spikes |
| 146 | private static final int FUZZ_MILLIS = 5 * 60 * 1000; |
| 147 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 148 | // The amount of time between the initial provisioning of the device and |
| 149 | // the first backup pass. |
| 150 | private static final long FIRST_BACKUP_INTERVAL = 12 * AlarmManager.INTERVAL_HOUR; |
| 151 | |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 152 | private static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN"; |
| 153 | private static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT"; |
| 154 | private static final String RUN_CLEAR_ACTION = "android.app.backup.intent.CLEAR"; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 155 | private static final int MSG_RUN_BACKUP = 1; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 156 | private static final int MSG_RUN_FULL_BACKUP = 2; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 157 | private static final int MSG_RUN_RESTORE = 3; |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 158 | private static final int MSG_RUN_CLEAR = 4; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 159 | private static final int MSG_RUN_INITIALIZE = 5; |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 160 | private static final int MSG_RUN_GET_RESTORE_SETS = 6; |
| 161 | private static final int MSG_TIMEOUT = 7; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 162 | private static final int MSG_RESTORE_TIMEOUT = 8; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 163 | private static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9; |
| 164 | private static final int MSG_RUN_FULL_RESTORE = 10; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 165 | |
| 166 | // Timeout interval for deciding that a bind or clear-data has taken too long |
| 167 | static final long TIMEOUT_INTERVAL = 10 * 1000; |
| 168 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 169 | // Timeout intervals for agent backup & restore operations |
| 170 | static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 171 | static final long TIMEOUT_FULL_BACKUP_INTERVAL = 5 * 60 * 1000; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 172 | static final long TIMEOUT_SHARED_BACKUP_INTERVAL = 30 * 60 * 1000; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 173 | static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000; |
| 174 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 175 | // User confirmation timeout for a full backup/restore operation. It's this long in |
| 176 | // order to give them time to enter the backup password. |
| 177 | static final long TIMEOUT_FULL_CONFIRMATION = 60 * 1000; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 178 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 179 | private Context mContext; |
| 180 | private PackageManager mPackageManager; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 181 | IPackageManager mPackageManagerBinder; |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 182 | private IActivityManager mActivityManager; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 183 | private PowerManager mPowerManager; |
| 184 | private AlarmManager mAlarmManager; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 185 | IBackupManager mBackupManagerBinder; |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 186 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 187 | boolean mEnabled; // access to this is synchronized on 'this' |
| 188 | boolean mProvisioned; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 189 | boolean mAutoRestore; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 190 | PowerManager.WakeLock mWakelock; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 191 | HandlerThread mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 192 | BackupHandler mBackupHandler; |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 193 | PendingIntent mRunBackupIntent, mRunInitIntent; |
| 194 | BroadcastReceiver mRunBackupReceiver, mRunInitReceiver; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 195 | // map UIDs to the set of backup client services within that UID's app set |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 196 | final SparseArray<HashSet<ApplicationInfo>> mBackupParticipants |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 197 | = new SparseArray<HashSet<ApplicationInfo>>(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 198 | // set of backup services that have pending changes |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 199 | class BackupRequest { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 200 | public String packageName; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 201 | |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 202 | BackupRequest(String pkgName) { |
| 203 | packageName = pkgName; |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 204 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 205 | |
| 206 | public String toString() { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 207 | return "BackupRequest{pkg=" + packageName + "}"; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 208 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 209 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 210 | // Backups that we haven't started yet. Keys are package names. |
| 211 | HashMap<String,BackupRequest> mPendingBackups |
| 212 | = new HashMap<String,BackupRequest>(); |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 213 | |
| 214 | // Pseudoname that we use for the Package Manager metadata "package" |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 215 | static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 216 | |
| 217 | // locking around the pending-backup management |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 218 | final Object mQueueLock = new Object(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 219 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 220 | // The thread performing the sequence of queued backups binds to each app's agent |
| 221 | // in succession. Bind notifications are asynchronously delivered through the |
| 222 | // Activity Manager; use this lock object to signal when a requested binding has |
| 223 | // completed. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 224 | final Object mAgentConnectLock = new Object(); |
| 225 | IBackupAgent mConnectedAgent; |
| 226 | volatile boolean mConnecting; |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 227 | volatile long mLastBackupPass; |
| 228 | volatile long mNextBackupPass; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 229 | |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 230 | // A similar synchronization mechanism around clearing apps' data for restore |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 231 | final Object mClearDataLock = new Object(); |
| 232 | volatile boolean mClearingData; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 233 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 234 | // Transport bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 235 | final HashMap<String,IBackupTransport> mTransports |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 236 | = new HashMap<String,IBackupTransport>(); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 237 | String mCurrentTransport; |
| 238 | IBackupTransport mLocalTransport, mGoogleTransport; |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 239 | ActiveRestoreSession mActiveRestoreSession; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 240 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 241 | class RestoreGetSetsParams { |
| 242 | public IBackupTransport transport; |
| 243 | public ActiveRestoreSession session; |
| 244 | public IRestoreObserver observer; |
| 245 | |
| 246 | RestoreGetSetsParams(IBackupTransport _transport, ActiveRestoreSession _session, |
| 247 | IRestoreObserver _observer) { |
| 248 | transport = _transport; |
| 249 | session = _session; |
| 250 | observer = _observer; |
| 251 | } |
| 252 | } |
| 253 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 254 | class RestoreParams { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 255 | public IBackupTransport transport; |
| 256 | public IRestoreObserver observer; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 257 | public long token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 258 | public PackageInfo pkgInfo; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 259 | public int pmToken; // in post-install restore, the PM's token for this transaction |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 260 | public boolean needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 261 | public String[] filterSet; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 262 | |
| 263 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 264 | long _token, PackageInfo _pkg, int _pmToken, boolean _needFullBackup) { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 265 | transport = _transport; |
| 266 | observer = _obs; |
| 267 | token = _token; |
| 268 | pkgInfo = _pkg; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 269 | pmToken = _pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 270 | needFullBackup = _needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 271 | filterSet = null; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 272 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 273 | |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 274 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token, |
| 275 | boolean _needFullBackup) { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 276 | transport = _transport; |
| 277 | observer = _obs; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 278 | token = _token; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 279 | pkgInfo = null; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 280 | pmToken = 0; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 281 | needFullBackup = _needFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 282 | filterSet = null; |
| 283 | } |
| 284 | |
| 285 | RestoreParams(IBackupTransport _transport, IRestoreObserver _obs, long _token, |
| 286 | String[] _filterSet, boolean _needFullBackup) { |
| 287 | transport = _transport; |
| 288 | observer = _obs; |
| 289 | token = _token; |
| 290 | pkgInfo = null; |
| 291 | pmToken = 0; |
| 292 | needFullBackup = _needFullBackup; |
| 293 | filterSet = _filterSet; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 297 | class ClearParams { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 298 | public IBackupTransport transport; |
| 299 | public PackageInfo packageInfo; |
| 300 | |
| 301 | ClearParams(IBackupTransport _transport, PackageInfo _info) { |
| 302 | transport = _transport; |
| 303 | packageInfo = _info; |
| 304 | } |
| 305 | } |
| 306 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 307 | class FullParams { |
| 308 | public ParcelFileDescriptor fd; |
| 309 | public final AtomicBoolean latch; |
| 310 | public IFullBackupRestoreObserver observer; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 311 | public String curPassword; // filled in by the confirmation step |
| 312 | public String encryptPassword; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 313 | |
| 314 | FullParams() { |
| 315 | latch = new AtomicBoolean(false); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | class FullBackupParams extends FullParams { |
| 320 | public boolean includeApks; |
| 321 | public boolean includeShared; |
| 322 | public boolean allApps; |
| 323 | public String[] packages; |
| 324 | |
| 325 | FullBackupParams(ParcelFileDescriptor output, boolean saveApks, boolean saveShared, |
| 326 | boolean doAllApps, String[] pkgList) { |
| 327 | fd = output; |
| 328 | includeApks = saveApks; |
| 329 | includeShared = saveShared; |
| 330 | allApps = doAllApps; |
| 331 | packages = pkgList; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | class FullRestoreParams extends FullParams { |
| 336 | FullRestoreParams(ParcelFileDescriptor input) { |
| 337 | fd = input; |
| 338 | } |
| 339 | } |
| 340 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 341 | // Bookkeeping of in-flight operations for timeout etc. purposes. The operation |
| 342 | // token is the index of the entry in the pending-operations list. |
| 343 | static final int OP_PENDING = 0; |
| 344 | static final int OP_ACKNOWLEDGED = 1; |
| 345 | static final int OP_TIMEOUT = -1; |
| 346 | |
| 347 | final SparseIntArray mCurrentOperations = new SparseIntArray(); |
| 348 | final Object mCurrentOpLock = new Object(); |
| 349 | final Random mTokenGenerator = new Random(); |
| 350 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 351 | final SparseArray<FullParams> mFullConfirmations = new SparseArray<FullParams>(); |
| 352 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 353 | // Where we keep our journal files and other bookkeeping |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 354 | File mBaseStateDir; |
| 355 | File mDataDir; |
| 356 | File mJournalDir; |
| 357 | File mJournal; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 358 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 359 | // Backup password, if any, and the file where it's saved. What is stored is not the |
| 360 | // password text itself; it's the result of a PBKDF2 hash with a randomly chosen (but |
| 361 | // persisted) salt. Validation is performed by running the challenge text through the |
| 362 | // same PBKDF2 cycle with the persisted salt; if the resulting derived key string matches |
| 363 | // the saved hash string, then the challenge text matches the originally supplied |
| 364 | // password text. |
| 365 | private final SecureRandom mRng = new SecureRandom(); |
| 366 | private String mPasswordHash; |
| 367 | private File mPasswordHashFile; |
| 368 | private byte[] mPasswordSalt; |
| 369 | |
| 370 | // Configuration of PBKDF2 that we use for generating pw hashes and intermediate keys |
| 371 | static final int PBKDF2_HASH_ROUNDS = 10000; |
| 372 | static final int PBKDF2_KEY_SIZE = 256; // bits |
| 373 | static final int PBKDF2_SALT_SIZE = 512; // bits |
| 374 | static final String ENCRYPTION_ALGORITHM_NAME = "AES-256"; |
| 375 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 376 | // Keep a log of all the apps we've ever backed up, and what the |
| 377 | // dataset tokens are for both the current backup dataset and |
| 378 | // the ancestral dataset. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 379 | private File mEverStored; |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 380 | HashSet<String> mEverStoredApps = new HashSet<String>(); |
| 381 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 382 | static final int CURRENT_ANCESTRAL_RECORD_VERSION = 1; // increment when the schema changes |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 383 | File mTokenFile; |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 384 | Set<String> mAncestralPackages = null; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 385 | long mAncestralToken = 0; |
| 386 | long mCurrentToken = 0; |
| 387 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 388 | // Persistently track the need to do a full init |
| 389 | static final String INIT_SENTINEL_FILE_NAME = "_need_init_"; |
| 390 | HashSet<String> mPendingInits = new HashSet<String>(); // transport names |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 391 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 392 | // Utility: build a new random integer token |
| 393 | int generateToken() { |
| 394 | int token; |
| 395 | do { |
| 396 | synchronized (mTokenGenerator) { |
| 397 | token = mTokenGenerator.nextInt(); |
| 398 | } |
| 399 | } while (token < 0); |
| 400 | return token; |
| 401 | } |
| 402 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 403 | // ----- Asynchronous backup/restore handler thread ----- |
| 404 | |
| 405 | private class BackupHandler extends Handler { |
| 406 | public BackupHandler(Looper looper) { |
| 407 | super(looper); |
| 408 | } |
| 409 | |
| 410 | public void handleMessage(Message msg) { |
| 411 | |
| 412 | switch (msg.what) { |
| 413 | case MSG_RUN_BACKUP: |
| 414 | { |
| 415 | mLastBackupPass = System.currentTimeMillis(); |
| 416 | mNextBackupPass = mLastBackupPass + BACKUP_INTERVAL; |
| 417 | |
| 418 | IBackupTransport transport = getTransport(mCurrentTransport); |
| 419 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 420 | Slog.v(TAG, "Backup requested but no transport available"); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 421 | mWakelock.release(); |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | // snapshot the pending-backup set and work on that |
| 426 | ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>(); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 427 | File oldJournal = mJournal; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 428 | synchronized (mQueueLock) { |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 429 | // Do we have any work to do? Construct the work queue |
| 430 | // then release the synchronization lock to actually run |
| 431 | // the backup. |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 432 | if (mPendingBackups.size() > 0) { |
| 433 | for (BackupRequest b: mPendingBackups.values()) { |
| 434 | queue.add(b); |
| 435 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 436 | if (DEBUG) Slog.v(TAG, "clearing pending backups"); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 437 | mPendingBackups.clear(); |
| 438 | |
| 439 | // Start a new backup-queue journal file too |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 440 | mJournal = null; |
| 441 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 442 | } |
| 443 | } |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 444 | |
| 445 | if (queue.size() > 0) { |
| 446 | // At this point, we have started a new journal file, and the old |
| 447 | // file identity is being passed to the backup processing thread. |
| 448 | // When it completes successfully, that old journal file will be |
| 449 | // deleted. If we crash prior to that, the old journal is parsed |
| 450 | // at next boot and the journaled requests fulfilled. |
| 451 | (new PerformBackupTask(transport, queue, oldJournal)).run(); |
| 452 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 453 | Slog.v(TAG, "Backup requested but nothing pending"); |
| Christopher Tate | c61da31 | 2010-02-05 10:41:27 -0800 | [diff] [blame] | 454 | mWakelock.release(); |
| 455 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | |
| 459 | case MSG_RUN_FULL_BACKUP: |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 460 | { |
| 461 | FullBackupParams params = (FullBackupParams)msg.obj; |
| 462 | (new PerformFullBackupTask(params.fd, params.observer, params.includeApks, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 463 | params.includeShared, params.curPassword, params.encryptPassword, |
| 464 | params.allApps, params.packages, params.latch)).run(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 465 | break; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 466 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 467 | |
| 468 | case MSG_RUN_RESTORE: |
| 469 | { |
| 470 | RestoreParams params = (RestoreParams)msg.obj; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 471 | Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 472 | (new PerformRestoreTask(params.transport, params.observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 473 | params.token, params.pkgInfo, params.pmToken, |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 474 | params.needFullBackup, params.filterSet)).run(); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 478 | case MSG_RUN_FULL_RESTORE: |
| 479 | { |
| 480 | FullRestoreParams params = (FullRestoreParams)msg.obj; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 481 | (new PerformFullRestoreTask(params.fd, params.curPassword, params.encryptPassword, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 482 | params.observer, params.latch)).run(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 483 | break; |
| 484 | } |
| 485 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 486 | case MSG_RUN_CLEAR: |
| 487 | { |
| 488 | ClearParams params = (ClearParams)msg.obj; |
| 489 | (new PerformClearTask(params.transport, params.packageInfo)).run(); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | case MSG_RUN_INITIALIZE: |
| 494 | { |
| 495 | HashSet<String> queue; |
| 496 | |
| 497 | // Snapshot the pending-init queue and work on that |
| 498 | synchronized (mQueueLock) { |
| 499 | queue = new HashSet<String>(mPendingInits); |
| 500 | mPendingInits.clear(); |
| 501 | } |
| 502 | |
| 503 | (new PerformInitializeTask(queue)).run(); |
| 504 | break; |
| 505 | } |
| 506 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 507 | case MSG_RUN_GET_RESTORE_SETS: |
| 508 | { |
| 509 | // Like other async operations, this is entered with the wakelock held |
| 510 | RestoreSet[] sets = null; |
| 511 | RestoreGetSetsParams params = (RestoreGetSetsParams)msg.obj; |
| 512 | try { |
| 513 | sets = params.transport.getAvailableRestoreSets(); |
| 514 | // cache the result in the active session |
| 515 | synchronized (params.session) { |
| 516 | params.session.mRestoreSets = sets; |
| 517 | } |
| 518 | if (sets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| 519 | } catch (Exception e) { |
| 520 | Slog.e(TAG, "Error from transport getting set list"); |
| 521 | } finally { |
| 522 | if (params.observer != null) { |
| 523 | try { |
| 524 | params.observer.restoreSetsAvailable(sets); |
| 525 | } catch (RemoteException re) { |
| 526 | Slog.e(TAG, "Unable to report listing to observer"); |
| 527 | } catch (Exception e) { |
| 528 | Slog.e(TAG, "Restore observer threw", e); |
| 529 | } |
| 530 | } |
| 531 | |
| Christopher Tate | 2a93509 | 2011-03-03 17:30:32 -0800 | [diff] [blame] | 532 | // Done: reset the session timeout clock |
| 533 | removeMessages(MSG_RESTORE_TIMEOUT); |
| 534 | sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| 535 | |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 536 | mWakelock.release(); |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 541 | case MSG_TIMEOUT: |
| 542 | { |
| 543 | synchronized (mCurrentOpLock) { |
| 544 | final int token = msg.arg1; |
| 545 | int state = mCurrentOperations.get(token, OP_TIMEOUT); |
| 546 | if (state == OP_PENDING) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 547 | if (DEBUG) Slog.v(TAG, "TIMEOUT: token=" + token); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 548 | mCurrentOperations.put(token, OP_TIMEOUT); |
| 549 | } |
| 550 | mCurrentOpLock.notifyAll(); |
| 551 | } |
| 552 | break; |
| 553 | } |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 554 | |
| 555 | case MSG_RESTORE_TIMEOUT: |
| 556 | { |
| 557 | synchronized (BackupManagerService.this) { |
| 558 | if (mActiveRestoreSession != null) { |
| 559 | // Client app left the restore session dangling. We know that it |
| 560 | // can't be in the middle of an actual restore operation because |
| 561 | // those are executed serially on this same handler thread. Clean |
| 562 | // up now. |
| 563 | Slog.w(TAG, "Restore session timed out; aborting"); |
| 564 | post(mActiveRestoreSession.new EndRestoreRunnable( |
| 565 | BackupManagerService.this, mActiveRestoreSession)); |
| 566 | } |
| 567 | } |
| 568 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 569 | |
| 570 | case MSG_FULL_CONFIRMATION_TIMEOUT: |
| 571 | { |
| 572 | synchronized (mFullConfirmations) { |
| 573 | FullParams params = mFullConfirmations.get(msg.arg1); |
| 574 | if (params != null) { |
| 575 | Slog.i(TAG, "Full backup/restore timed out waiting for user confirmation"); |
| 576 | |
| 577 | // Release the waiter; timeout == completion |
| 578 | signalFullBackupRestoreCompletion(params); |
| 579 | |
| 580 | // Remove the token from the set |
| 581 | mFullConfirmations.delete(msg.arg1); |
| 582 | |
| 583 | // Report a timeout to the observer, if any |
| 584 | if (params.observer != null) { |
| 585 | try { |
| 586 | params.observer.onTimeout(); |
| 587 | } catch (RemoteException e) { |
| 588 | /* don't care if the app has gone away */ |
| 589 | } |
| 590 | } |
| 591 | } else { |
| 592 | Slog.d(TAG, "couldn't find params for token " + msg.arg1); |
| 593 | } |
| 594 | } |
| 595 | break; |
| 596 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // ----- Main service implementation ----- |
| 602 | |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 603 | public BackupManagerService(Context context) { |
| 604 | mContext = context; |
| 605 | mPackageManager = context.getPackageManager(); |
| Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 606 | mPackageManagerBinder = AppGlobals.getPackageManager(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 607 | mActivityManager = ActivityManagerNative.getDefault(); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 608 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 609 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 610 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 611 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 612 | mBackupManagerBinder = asInterface(asBinder()); |
| 613 | |
| 614 | // spin up the backup/restore handler thread |
| 615 | mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND); |
| 616 | mHandlerThread.start(); |
| 617 | mBackupHandler = new BackupHandler(mHandlerThread.getLooper()); |
| 618 | |
| Christopher Tate | 22b8787 | 2009-05-04 16:41:53 -0700 | [diff] [blame] | 619 | // Set up our bookkeeping |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 620 | boolean areEnabled = Settings.Secure.getInt(context.getContentResolver(), |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 621 | Settings.Secure.BACKUP_ENABLED, 0) != 0; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 622 | mProvisioned = Settings.Secure.getInt(context.getContentResolver(), |
| Joe Onorato | ab9a2a5 | 2009-07-27 08:56:39 -0700 | [diff] [blame] | 623 | Settings.Secure.BACKUP_PROVISIONED, 0) != 0; |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 624 | mAutoRestore = Settings.Secure.getInt(context.getContentResolver(), |
| Christopher Tate | 5035fda | 2010-02-25 18:01:14 -0800 | [diff] [blame] | 625 | Settings.Secure.BACKUP_AUTO_RESTORE, 1) != 0; |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 626 | // If Encrypted file systems is enabled or disabled, this call will return the |
| 627 | // correct directory. |
| Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 628 | mBaseStateDir = new File(Environment.getSecureDataDirectory(), "backup"); |
| Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 629 | mBaseStateDir.mkdirs(); |
| Christopher Tate | f417247 | 2009-05-05 15:50:03 -0700 | [diff] [blame] | 630 | mDataDir = Environment.getDownloadCacheDirectory(); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 631 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 632 | mPasswordHashFile = new File(mBaseStateDir, "pwhash"); |
| 633 | if (mPasswordHashFile.exists()) { |
| 634 | FileInputStream fin = null; |
| 635 | DataInputStream in = null; |
| 636 | try { |
| 637 | fin = new FileInputStream(mPasswordHashFile); |
| 638 | in = new DataInputStream(new BufferedInputStream(fin)); |
| 639 | // integer length of the salt array, followed by the salt, |
| 640 | // then the hex pw hash string |
| 641 | int saltLen = in.readInt(); |
| 642 | byte[] salt = new byte[saltLen]; |
| 643 | in.readFully(salt); |
| 644 | mPasswordHash = in.readUTF(); |
| 645 | mPasswordSalt = salt; |
| 646 | } catch (IOException e) { |
| 647 | Slog.e(TAG, "Unable to read saved backup pw hash"); |
| 648 | } finally { |
| 649 | try { |
| 650 | if (in != null) in.close(); |
| 651 | if (fin != null) fin.close(); |
| 652 | } catch (IOException e) { |
| 653 | Slog.w(TAG, "Unable to close streams"); |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 658 | // Alarm receivers for scheduled backups & initialization operations |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 659 | mRunBackupReceiver = new RunBackupReceiver(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 660 | IntentFilter filter = new IntentFilter(); |
| 661 | filter.addAction(RUN_BACKUP_ACTION); |
| 662 | context.registerReceiver(mRunBackupReceiver, filter, |
| 663 | android.Manifest.permission.BACKUP, null); |
| 664 | |
| 665 | mRunInitReceiver = new RunInitializeReceiver(); |
| 666 | filter = new IntentFilter(); |
| 667 | filter.addAction(RUN_INITIALIZE_ACTION); |
| 668 | context.registerReceiver(mRunInitReceiver, filter, |
| 669 | android.Manifest.permission.BACKUP, null); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 670 | |
| 671 | Intent backupIntent = new Intent(RUN_BACKUP_ACTION); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 672 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 673 | mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0); |
| 674 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 675 | Intent initIntent = new Intent(RUN_INITIALIZE_ACTION); |
| 676 | backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 677 | mRunInitIntent = PendingIntent.getBroadcast(context, MSG_RUN_INITIALIZE, initIntent, 0); |
| 678 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 679 | // Set up the backup-request journaling |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 680 | mJournalDir = new File(mBaseStateDir, "pending"); |
| 681 | mJournalDir.mkdirs(); // creates mBaseStateDir along the way |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 682 | mJournal = null; // will be created on first use |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 683 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 684 | // Set up the various sorts of package tracking we do |
| 685 | initPackageTracking(); |
| 686 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 687 | // Build our mapping of uid to backup client services. This implicitly |
| 688 | // schedules a backup pass on the Package Manager metadata the first |
| 689 | // time anything needs to be backed up. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 690 | synchronized (mBackupParticipants) { |
| 691 | addPackageParticipantsLocked(null); |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 694 | // Set up our transport options and initialize the default transport |
| 695 | // TODO: Have transports register themselves somehow? |
| 696 | // TODO: Don't create transports that we don't need to? |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 697 | mLocalTransport = new LocalTransport(context); // This is actually pretty cheap |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 698 | ComponentName localName = new ComponentName(context, LocalTransport.class); |
| 699 | registerTransport(localName.flattenToShortString(), mLocalTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 700 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 701 | mGoogleTransport = null; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 702 | mCurrentTransport = Settings.Secure.getString(context.getContentResolver(), |
| 703 | Settings.Secure.BACKUP_TRANSPORT); |
| 704 | if ("".equals(mCurrentTransport)) { |
| 705 | mCurrentTransport = null; |
| Christopher Tate | ce0bf06 | 2009-07-01 11:43:53 -0700 | [diff] [blame] | 706 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 707 | if (DEBUG) Slog.v(TAG, "Starting with transport " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 708 | |
| 709 | // Attach to the Google backup transport. When this comes up, it will set |
| 710 | // itself as the current transport because we explicitly reset mCurrentTransport |
| 711 | // to null. |
| Christopher Tate | a32504f | 2010-04-21 17:58:07 -0700 | [diff] [blame] | 712 | ComponentName transportComponent = new ComponentName("com.google.android.backup", |
| 713 | "com.google.android.backup.BackupTransportService"); |
| 714 | try { |
| 715 | // If there's something out there that is supposed to be the Google |
| 716 | // backup transport, make sure it's legitimately part of the OS build |
| 717 | // and not an app lying about its package name. |
| 718 | ApplicationInfo info = mPackageManager.getApplicationInfo( |
| 719 | transportComponent.getPackageName(), 0); |
| 720 | if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 721 | if (DEBUG) Slog.v(TAG, "Binding to Google transport"); |
| 722 | Intent intent = new Intent().setComponent(transportComponent); |
| 723 | context.bindService(intent, mGoogleConnection, Context.BIND_AUTO_CREATE); |
| 724 | } else { |
| 725 | Slog.w(TAG, "Possible Google transport spoof: ignoring " + info); |
| 726 | } |
| 727 | } catch (PackageManager.NameNotFoundException nnf) { |
| 728 | // No such package? No binding. |
| 729 | if (DEBUG) Slog.v(TAG, "Google transport not present"); |
| 730 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 731 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 732 | // Now that we know about valid backup participants, parse any |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 733 | // leftover journal files into the pending backup set |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 734 | parseLeftoverJournals(); |
| 735 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 736 | // Power management |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 737 | mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 738 | |
| 739 | // Start the backup passes going |
| 740 | setBackupEnabled(areEnabled); |
| 741 | } |
| 742 | |
| 743 | private class RunBackupReceiver extends BroadcastReceiver { |
| 744 | public void onReceive(Context context, Intent intent) { |
| 745 | if (RUN_BACKUP_ACTION.equals(intent.getAction())) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 746 | synchronized (mQueueLock) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 747 | if (mPendingInits.size() > 0) { |
| 748 | // If there are pending init operations, we process those |
| 749 | // and then settle into the usual periodic backup schedule. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 750 | if (DEBUG) Slog.v(TAG, "Init pending at scheduled backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 751 | try { |
| 752 | mAlarmManager.cancel(mRunInitIntent); |
| 753 | mRunInitIntent.send(); |
| 754 | } catch (PendingIntent.CanceledException ce) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 755 | Slog.e(TAG, "Run init intent cancelled"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 756 | // can't really do more than bail here |
| 757 | } |
| 758 | } else { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 759 | // Don't run backups now if we're disabled or not yet |
| 760 | // fully set up. |
| 761 | if (mEnabled && mProvisioned) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 762 | if (DEBUG) Slog.v(TAG, "Running a backup pass"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 763 | |
| 764 | // Acquire the wakelock and pass it to the backup thread. it will |
| 765 | // be released once backup concludes. |
| 766 | mWakelock.acquire(); |
| 767 | |
| 768 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP); |
| 769 | mBackupHandler.sendMessage(msg); |
| 770 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 771 | Slog.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | private class RunInitializeReceiver extends BroadcastReceiver { |
| 780 | public void onReceive(Context context, Intent intent) { |
| 781 | if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) { |
| 782 | synchronized (mQueueLock) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 783 | if (DEBUG) Slog.v(TAG, "Running a device init"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 784 | |
| 785 | // Acquire the wakelock and pass it to the init thread. it will |
| 786 | // be released once init concludes. |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 787 | mWakelock.acquire(); |
| 788 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 789 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 790 | mBackupHandler.sendMessage(msg); |
| 791 | } |
| 792 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 793 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 794 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 795 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 796 | private void initPackageTracking() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 797 | if (DEBUG) Slog.v(TAG, "Initializing package tracking"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 798 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 799 | // Remember our ancestral dataset |
| 800 | mTokenFile = new File(mBaseStateDir, "ancestral"); |
| 801 | try { |
| 802 | RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 803 | int version = tf.readInt(); |
| 804 | if (version == CURRENT_ANCESTRAL_RECORD_VERSION) { |
| 805 | mAncestralToken = tf.readLong(); |
| 806 | mCurrentToken = tf.readLong(); |
| 807 | |
| 808 | int numPackages = tf.readInt(); |
| 809 | if (numPackages >= 0) { |
| 810 | mAncestralPackages = new HashSet<String>(); |
| 811 | for (int i = 0; i < numPackages; i++) { |
| 812 | String pkgName = tf.readUTF(); |
| 813 | mAncestralPackages.add(pkgName); |
| 814 | } |
| 815 | } |
| 816 | } |
| Brad Fitzpatrick | 725d8f0 | 2010-11-15 11:12:42 -0800 | [diff] [blame] | 817 | tf.close(); |
| Christopher Tate | 1168baa | 2010-02-17 13:03:40 -0800 | [diff] [blame] | 818 | } catch (FileNotFoundException fnf) { |
| 819 | // Probably innocuous |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 820 | Slog.v(TAG, "No ancestral data"); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 821 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 822 | Slog.w(TAG, "Unable to read token file", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 823 | } |
| 824 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 825 | // Keep a log of what apps we've ever backed up. Because we might have |
| 826 | // rebooted in the middle of an operation that was removing something from |
| 827 | // this log, we sanity-check its contents here and reconstruct it. |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 828 | mEverStored = new File(mBaseStateDir, "processed"); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 829 | File tempProcessedFile = new File(mBaseStateDir, "processed.new"); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 830 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 831 | // If we were in the middle of removing something from the ever-backed-up |
| 832 | // file, there might be a transient "processed.new" file still present. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 833 | // Ignore it -- we'll validate "processed" against the current package set. |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 834 | if (tempProcessedFile.exists()) { |
| 835 | tempProcessedFile.delete(); |
| 836 | } |
| 837 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 838 | // If there are previous contents, parse them out then start a new |
| 839 | // file to continue the recordkeeping. |
| 840 | if (mEverStored.exists()) { |
| 841 | RandomAccessFile temp = null; |
| 842 | RandomAccessFile in = null; |
| 843 | |
| 844 | try { |
| 845 | temp = new RandomAccessFile(tempProcessedFile, "rws"); |
| 846 | in = new RandomAccessFile(mEverStored, "r"); |
| 847 | |
| 848 | while (true) { |
| 849 | PackageInfo info; |
| 850 | String pkg = in.readUTF(); |
| 851 | try { |
| 852 | info = mPackageManager.getPackageInfo(pkg, 0); |
| 853 | mEverStoredApps.add(pkg); |
| 854 | temp.writeUTF(pkg); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 855 | if (MORE_DEBUG) Slog.v(TAG, " + " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 856 | } catch (NameNotFoundException e) { |
| 857 | // nope, this package was uninstalled; don't include it |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 858 | if (MORE_DEBUG) Slog.v(TAG, " - " + pkg); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | } catch (EOFException e) { |
| 862 | // Once we've rewritten the backup history log, atomically replace the |
| 863 | // old one with the new one then reopen the file for continuing use. |
| 864 | if (!tempProcessedFile.renameTo(mEverStored)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 865 | Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 866 | } |
| 867 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 868 | Slog.e(TAG, "Error in processed file", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 869 | } finally { |
| 870 | try { if (temp != null) temp.close(); } catch (IOException e) {} |
| 871 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 872 | } |
| 873 | } |
| 874 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 875 | // Register for broadcasts about package install, etc., so we can |
| 876 | // update the provider list. |
| 877 | IntentFilter filter = new IntentFilter(); |
| 878 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 879 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 880 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 881 | filter.addDataScheme("package"); |
| 882 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 883 | // Register for events related to sdcard installation. |
| 884 | IntentFilter sdFilter = new IntentFilter(); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 885 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 886 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 887 | mContext.registerReceiver(mBroadcastReceiver, sdFilter); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 890 | private void parseLeftoverJournals() { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 891 | for (File f : mJournalDir.listFiles()) { |
| 892 | if (mJournal == null || f.compareTo(mJournal) != 0) { |
| 893 | // This isn't the current journal, so it must be a leftover. Read |
| 894 | // out the package names mentioned there and schedule them for |
| 895 | // backup. |
| 896 | RandomAccessFile in = null; |
| 897 | try { |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 898 | Slog.i(TAG, "Found stale backup journal, scheduling"); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 899 | in = new RandomAccessFile(f, "r"); |
| 900 | while (true) { |
| 901 | String packageName = in.readUTF(); |
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 902 | Slog.i(TAG, " " + packageName); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 903 | dataChangedImpl(packageName); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 904 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 905 | } catch (EOFException e) { |
| 906 | // no more data; we're done |
| 907 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 908 | Slog.e(TAG, "Can't read " + f, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 909 | } finally { |
| 910 | // close/delete the file |
| 911 | try { if (in != null) in.close(); } catch (IOException e) {} |
| 912 | f.delete(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 918 | private SecretKey buildPasswordKey(String pw, byte[] salt, int rounds) { |
| 919 | return buildCharArrayKey(pw.toCharArray(), salt, rounds); |
| 920 | } |
| 921 | |
| 922 | private SecretKey buildCharArrayKey(char[] pwArray, byte[] salt, int rounds) { |
| 923 | try { |
| 924 | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
| 925 | KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE); |
| 926 | return keyFactory.generateSecret(ks); |
| 927 | } catch (InvalidKeySpecException e) { |
| 928 | Slog.e(TAG, "Invalid key spec for PBKDF2!"); |
| 929 | } catch (NoSuchAlgorithmException e) { |
| 930 | Slog.e(TAG, "PBKDF2 unavailable!"); |
| 931 | } |
| 932 | return null; |
| 933 | } |
| 934 | |
| 935 | private String buildPasswordHash(String pw, byte[] salt, int rounds) { |
| 936 | SecretKey key = buildPasswordKey(pw, salt, rounds); |
| 937 | if (key != null) { |
| 938 | return byteArrayToHex(key.getEncoded()); |
| 939 | } |
| 940 | return null; |
| 941 | } |
| 942 | |
| 943 | private String byteArrayToHex(byte[] data) { |
| 944 | StringBuilder buf = new StringBuilder(data.length * 2); |
| 945 | for (int i = 0; i < data.length; i++) { |
| 946 | buf.append(Byte.toHexString(data[i], true)); |
| 947 | } |
| 948 | return buf.toString(); |
| 949 | } |
| 950 | |
| 951 | private byte[] hexToByteArray(String digits) { |
| 952 | final int bytes = digits.length() / 2; |
| 953 | if (2*bytes != digits.length()) { |
| 954 | throw new IllegalArgumentException("Hex string must have an even number of digits"); |
| 955 | } |
| 956 | |
| 957 | byte[] result = new byte[bytes]; |
| 958 | for (int i = 0; i < digits.length(); i += 2) { |
| 959 | result[i/2] = (byte) Integer.parseInt(digits.substring(i, i+2), 16); |
| 960 | } |
| 961 | return result; |
| 962 | } |
| 963 | |
| 964 | private byte[] makeKeyChecksum(byte[] pwBytes, byte[] salt, int rounds) { |
| 965 | char[] mkAsChar = new char[pwBytes.length]; |
| 966 | for (int i = 0; i < pwBytes.length; i++) { |
| 967 | mkAsChar[i] = (char) pwBytes[i]; |
| 968 | } |
| 969 | |
| 970 | Key checksum = buildCharArrayKey(mkAsChar, salt, rounds); |
| 971 | return checksum.getEncoded(); |
| 972 | } |
| 973 | |
| 974 | // Used for generating random salts or passwords |
| 975 | private byte[] randomBytes(int bits) { |
| 976 | byte[] array = new byte[bits / 8]; |
| 977 | mRng.nextBytes(array); |
| 978 | return array; |
| 979 | } |
| 980 | |
| 981 | // Backup password management |
| 982 | boolean passwordMatchesSaved(String candidatePw, int rounds) { |
| 983 | if (mPasswordHash == null) { |
| 984 | // no current password case -- require that 'currentPw' be null or empty |
| 985 | if (candidatePw == null || "".equals(candidatePw)) { |
| 986 | return true; |
| 987 | } // else the non-empty candidate does not match the empty stored pw |
| 988 | } else { |
| 989 | // hash the stated current pw and compare to the stored one |
| 990 | if (candidatePw != null && candidatePw.length() > 0) { |
| 991 | String currentPwHash = buildPasswordHash(candidatePw, mPasswordSalt, rounds); |
| 992 | if (mPasswordHash.equalsIgnoreCase(currentPwHash)) { |
| 993 | // candidate hash matches the stored hash -- the password matches |
| 994 | return true; |
| 995 | } |
| 996 | } // else the stored pw is nonempty but the candidate is empty; no match |
| 997 | } |
| 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | @Override |
| 1002 | public boolean setBackupPassword(String currentPw, String newPw) { |
| 1003 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 1004 | "setBackupPassword"); |
| 1005 | |
| 1006 | // If the supplied pw doesn't hash to the the saved one, fail |
| 1007 | if (!passwordMatchesSaved(currentPw, PBKDF2_HASH_ROUNDS)) { |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | // Clearing the password is okay |
| 1012 | if (newPw == null || newPw.isEmpty()) { |
| 1013 | if (mPasswordHashFile.exists()) { |
| 1014 | if (!mPasswordHashFile.delete()) { |
| 1015 | // Unable to delete the old pw file, so fail |
| 1016 | Slog.e(TAG, "Unable to clear backup password"); |
| 1017 | return false; |
| 1018 | } |
| 1019 | } |
| 1020 | mPasswordHash = null; |
| 1021 | mPasswordSalt = null; |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
| 1025 | try { |
| 1026 | // Okay, build the hash of the new backup password |
| 1027 | byte[] salt = randomBytes(PBKDF2_SALT_SIZE); |
| 1028 | String newPwHash = buildPasswordHash(newPw, salt, PBKDF2_HASH_ROUNDS); |
| 1029 | |
| 1030 | OutputStream pwf = null, buffer = null; |
| 1031 | DataOutputStream out = null; |
| 1032 | try { |
| 1033 | pwf = new FileOutputStream(mPasswordHashFile); |
| 1034 | buffer = new BufferedOutputStream(pwf); |
| 1035 | out = new DataOutputStream(buffer); |
| 1036 | // integer length of the salt array, followed by the salt, |
| 1037 | // then the hex pw hash string |
| 1038 | out.writeInt(salt.length); |
| 1039 | out.write(salt); |
| 1040 | out.writeUTF(newPwHash); |
| 1041 | out.flush(); |
| 1042 | mPasswordHash = newPwHash; |
| 1043 | mPasswordSalt = salt; |
| 1044 | return true; |
| 1045 | } finally { |
| 1046 | if (out != null) out.close(); |
| 1047 | if (buffer != null) buffer.close(); |
| 1048 | if (pwf != null) pwf.close(); |
| 1049 | } |
| 1050 | } catch (IOException e) { |
| 1051 | Slog.e(TAG, "Unable to set backup password"); |
| 1052 | } |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | @Override |
| 1057 | public boolean hasBackupPassword() { |
| 1058 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 1059 | "hasBackupPassword"); |
| 1060 | return (mPasswordHash != null && mPasswordHash.length() > 0); |
| 1061 | } |
| 1062 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1063 | // Maintain persistent state around whether need to do an initialize operation. |
| 1064 | // Must be called with the queue lock held. |
| 1065 | void recordInitPendingLocked(boolean isPending, String transportName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1066 | if (DEBUG) Slog.i(TAG, "recordInitPendingLocked: " + isPending |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1067 | + " on transport " + transportName); |
| 1068 | try { |
| 1069 | IBackupTransport transport = getTransport(transportName); |
| 1070 | String transportDirName = transport.transportDirName(); |
| 1071 | File stateDir = new File(mBaseStateDir, transportDirName); |
| 1072 | File initPendingFile = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 1073 | |
| 1074 | if (isPending) { |
| 1075 | // We need an init before we can proceed with sending backup data. |
| 1076 | // Record that with an entry in our set of pending inits, as well as |
| 1077 | // journaling it via creation of a sentinel file. |
| 1078 | mPendingInits.add(transportName); |
| 1079 | try { |
| 1080 | (new FileOutputStream(initPendingFile)).close(); |
| 1081 | } catch (IOException ioe) { |
| 1082 | // Something is badly wrong with our permissions; just try to move on |
| 1083 | } |
| 1084 | } else { |
| 1085 | // No more initialization needed; wipe the journal and reset our state. |
| 1086 | initPendingFile.delete(); |
| 1087 | mPendingInits.remove(transportName); |
| 1088 | } |
| 1089 | } catch (RemoteException e) { |
| 1090 | // can't happen; the transport is local |
| 1091 | } |
| 1092 | } |
| 1093 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1094 | // Reset all of our bookkeeping, in response to having been told that |
| 1095 | // the backend data has been wiped [due to idle expiry, for example], |
| 1096 | // so we must re-upload all saved settings. |
| 1097 | void resetBackupState(File stateFileDir) { |
| 1098 | synchronized (mQueueLock) { |
| 1099 | // Wipe the "what we've ever backed up" tracking |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1100 | mEverStoredApps.clear(); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1101 | mEverStored.delete(); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1102 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1103 | mCurrentToken = 0; |
| 1104 | writeRestoreTokens(); |
| 1105 | |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1106 | // Remove all the state files |
| 1107 | for (File sf : stateFileDir.listFiles()) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1108 | // ... but don't touch the needs-init sentinel |
| 1109 | if (!sf.getName().equals(INIT_SENTINEL_FILE_NAME)) { |
| 1110 | sf.delete(); |
| 1111 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1112 | } |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 1113 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1114 | |
| Christopher Tate | 4559764 | 2011-04-04 16:59:21 -0700 | [diff] [blame] | 1115 | // Enqueue a new backup of every participant |
| 1116 | int N = mBackupParticipants.size(); |
| 1117 | for (int i=0; i<N; i++) { |
| 1118 | int uid = mBackupParticipants.keyAt(i); |
| 1119 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 1120 | for (ApplicationInfo app: participants) { |
| 1121 | dataChangedImpl(app.packageName); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1126 | // Add a transport to our set of available backends. If 'transport' is null, this |
| 1127 | // is an unregistration, and the transport's entry is removed from our bookkeeping. |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1128 | private void registerTransport(String name, IBackupTransport transport) { |
| 1129 | synchronized (mTransports) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1130 | if (DEBUG) Slog.v(TAG, "Registering transport " + name + " = " + transport); |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1131 | if (transport != null) { |
| 1132 | mTransports.put(name, transport); |
| 1133 | } else { |
| 1134 | mTransports.remove(name); |
| Christopher Tate | b0dcaaf | 2010-01-29 16:27:04 -0800 | [diff] [blame] | 1135 | if ((mCurrentTransport != null) && mCurrentTransport.equals(name)) { |
| Christopher Tate | dfa47b56e | 2009-12-22 16:01:32 -0800 | [diff] [blame] | 1136 | mCurrentTransport = null; |
| 1137 | } |
| 1138 | // Nothing further to do in the unregistration case |
| 1139 | return; |
| 1140 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1141 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 1142 | |
| 1143 | // If the init sentinel file exists, we need to be sure to perform the init |
| 1144 | // as soon as practical. We also create the state directory at registration |
| 1145 | // time to ensure it's present from the outset. |
| 1146 | try { |
| 1147 | String transportName = transport.transportDirName(); |
| 1148 | File stateDir = new File(mBaseStateDir, transportName); |
| 1149 | stateDir.mkdirs(); |
| 1150 | |
| 1151 | File initSentinel = new File(stateDir, INIT_SENTINEL_FILE_NAME); |
| 1152 | if (initSentinel.exists()) { |
| 1153 | synchronized (mQueueLock) { |
| 1154 | mPendingInits.add(transportName); |
| 1155 | |
| 1156 | // TODO: pick a better starting time than now + 1 minute |
| 1157 | long delay = 1000 * 60; // one minute, in milliseconds |
| 1158 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 1159 | System.currentTimeMillis() + delay, mRunInitIntent); |
| 1160 | } |
| 1161 | } |
| 1162 | } catch (RemoteException e) { |
| 1163 | // can't happen, the transport is local |
| 1164 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1167 | // ----- Track installation/removal of packages ----- |
| 1168 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1169 | public void onReceive(Context context, Intent intent) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1170 | if (DEBUG) Slog.d(TAG, "Received broadcast " + intent); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1171 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1172 | String action = intent.getAction(); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1173 | boolean replacing = false; |
| 1174 | boolean added = false; |
| 1175 | Bundle extras = intent.getExtras(); |
| 1176 | String pkgList[] = null; |
| 1177 | if (Intent.ACTION_PACKAGE_ADDED.equals(action) || |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1178 | Intent.ACTION_PACKAGE_REMOVED.equals(action) || |
| 1179 | Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1180 | Uri uri = intent.getData(); |
| 1181 | if (uri == null) { |
| 1182 | return; |
| 1183 | } |
| 1184 | String pkgName = uri.getSchemeSpecificPart(); |
| 1185 | if (pkgName != null) { |
| 1186 | pkgList = new String[] { pkgName }; |
| 1187 | } |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1188 | if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 1189 | // use the existing "add with replacement" logic |
| 1190 | if (MORE_DEBUG) Slog.d(TAG, "PACKAGE_REPLACED, updating package " + pkgName); |
| 1191 | added = replacing = true; |
| 1192 | } else { |
| 1193 | added = Intent.ACTION_PACKAGE_ADDED.equals(action); |
| 1194 | replacing = extras.getBoolean(Intent.EXTRA_REPLACING, false); |
| 1195 | } |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1196 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1197 | added = true; |
| 1198 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1199 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1200 | added = false; |
| 1201 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1202 | } |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1203 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1204 | if (pkgList == null || pkgList.length == 0) { |
| 1205 | return; |
| 1206 | } |
| 1207 | if (added) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1208 | synchronized (mBackupParticipants) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1209 | for (String pkgName : pkgList) { |
| 1210 | if (replacing) { |
| 1211 | // The package was just upgraded |
| 1212 | updatePackageParticipantsLocked(pkgName); |
| 1213 | } else { |
| 1214 | // The package was just added |
| 1215 | addPackageParticipantsLocked(pkgName); |
| 1216 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1217 | } |
| 1218 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1219 | } else { |
| 1220 | if (replacing) { |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1221 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 1222 | } else { |
| 1223 | synchronized (mBackupParticipants) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1224 | for (String pkgName : pkgList) { |
| 1225 | removePackageParticipantsLocked(pkgName); |
| 1226 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | }; |
| 1232 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1233 | // ----- Track connection to GoogleBackupTransport service ----- |
| 1234 | ServiceConnection mGoogleConnection = new ServiceConnection() { |
| 1235 | public void onServiceConnected(ComponentName name, IBinder service) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1236 | if (DEBUG) Slog.v(TAG, "Connected to Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1237 | mGoogleTransport = IBackupTransport.Stub.asInterface(service); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1238 | registerTransport(name.flattenToShortString(), mGoogleTransport); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | public void onServiceDisconnected(ComponentName name) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1242 | if (DEBUG) Slog.v(TAG, "Disconnected from Google transport"); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1243 | mGoogleTransport = null; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1244 | registerTransport(name.flattenToShortString(), null); |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1245 | } |
| 1246 | }; |
| 1247 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1248 | // Add the backup agents in the given package to our set of known backup participants. |
| 1249 | // If 'packageName' is null, adds all backup agents in the whole system. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1250 | void addPackageParticipantsLocked(String packageName) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1251 | // Look for apps that define the android:backupAgent attribute |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1252 | if (DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: " + packageName); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1253 | List<PackageInfo> targetApps = allAgentPackages(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1254 | addPackageParticipantsLockedInner(packageName, targetApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1257 | private void addPackageParticipantsLockedInner(String packageName, |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1258 | List<PackageInfo> targetPkgs) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1259 | if (MORE_DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1260 | Slog.v(TAG, "Adding " + targetPkgs.size() + " backup participants:"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1261 | for (PackageInfo p : targetPkgs) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1262 | Slog.v(TAG, " " + p + " agent=" + p.applicationInfo.backupAgentName |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1263 | + " uid=" + p.applicationInfo.uid |
| 1264 | + " killAfterRestore=" |
| 1265 | + (((p.applicationInfo.flags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) ? "true" : "false") |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 1266 | ); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1270 | for (PackageInfo pkg : targetPkgs) { |
| 1271 | if (packageName == null || pkg.packageName.equals(packageName)) { |
| 1272 | int uid = pkg.applicationInfo.uid; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1273 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1274 | if (set == null) { |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1275 | set = new HashSet<ApplicationInfo>(); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1276 | mBackupParticipants.put(uid, set); |
| 1277 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1278 | set.add(pkg.applicationInfo); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1279 | |
| 1280 | // If we've never seen this app before, schedule a backup for it |
| 1281 | if (!mEverStoredApps.contains(pkg.packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1282 | if (DEBUG) Slog.i(TAG, "New app " + pkg.packageName |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1283 | + " never backed up; scheduling"); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 1284 | dataChangedImpl(pkg.packageName); |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1285 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1286 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1290 | // Remove the given package's entry from our known active set. If |
| 1291 | // 'packageName' is null, *all* participating apps will be removed. |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1292 | void removePackageParticipantsLocked(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1293 | if (DEBUG) Slog.v(TAG, "removePackageParticipantsLocked: " + packageName); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1294 | List<String> allApps = new ArrayList<String>(); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1295 | if (packageName != null) { |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1296 | allApps.add(packageName); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1297 | } else { |
| 1298 | // all apps with agents |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1299 | List<PackageInfo> knownPackages = allAgentPackages(); |
| 1300 | for (PackageInfo pkg : knownPackages) { |
| 1301 | allApps.add(pkg.packageName); |
| 1302 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1303 | } |
| 1304 | removePackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
| Joe Onorato | 8ad0281 | 2009-05-13 01:41:44 -0400 | [diff] [blame] | 1307 | private void removePackageParticipantsLockedInner(String packageName, |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1308 | List<String> allPackageNames) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1309 | if (MORE_DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1310 | Slog.v(TAG, "removePackageParticipantsLockedInner (" + packageName |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1311 | + ") removing " + allPackageNames.size() + " entries"); |
| 1312 | for (String p : allPackageNames) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1313 | Slog.v(TAG, " - " + p); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1314 | } |
| 1315 | } |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1316 | for (String pkg : allPackageNames) { |
| 1317 | if (packageName == null || pkg.equals(packageName)) { |
| 1318 | int uid = -1; |
| 1319 | try { |
| 1320 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1321 | uid = info.applicationInfo.uid; |
| 1322 | } catch (NameNotFoundException e) { |
| 1323 | // we don't know this package name, so just skip it for now |
| 1324 | continue; |
| 1325 | } |
| 1326 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1327 | HashSet<ApplicationInfo> set = mBackupParticipants.get(uid); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1328 | if (set != null) { |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1329 | // Find the existing entry with the same package name, and remove it. |
| 1330 | // We can't just remove(app) because the instances are different. |
| 1331 | for (ApplicationInfo entry: set) { |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1332 | if (entry.packageName.equals(pkg)) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1333 | if (MORE_DEBUG) Slog.v(TAG, " removing participant " + pkg); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1334 | set.remove(entry); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1335 | removeEverBackedUp(pkg); |
| Christopher Tate | cd4ff2e | 2009-06-05 13:57:54 -0700 | [diff] [blame] | 1336 | break; |
| 1337 | } |
| 1338 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1339 | if (set.size() == 0) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1340 | mBackupParticipants.delete(uid); |
| 1341 | } |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1347 | // Returns the set of all applications that define an android:backupAgent attribute |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1348 | List<PackageInfo> allAgentPackages() { |
| Christopher Tate | 6785dd8 | 2009-06-18 15:58:25 -0700 | [diff] [blame] | 1349 | // !!! TODO: cache this and regenerate only when necessary |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1350 | int flags = PackageManager.GET_SIGNATURES; |
| 1351 | List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags); |
| 1352 | int N = packages.size(); |
| 1353 | for (int a = N-1; a >= 0; a--) { |
| Christopher Tate | 0749dcd | 2009-08-13 15:13:03 -0700 | [diff] [blame] | 1354 | PackageInfo pkg = packages.get(a); |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1355 | try { |
| 1356 | ApplicationInfo app = pkg.applicationInfo; |
| 1357 | if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) |
| Christopher Tate | a87240c | 2010-02-12 14:12:34 -0800 | [diff] [blame] | 1358 | || app.backupAgentName == null) { |
| Christopher Tate | b8eb1cb | 2009-09-16 10:57:21 -0700 | [diff] [blame] | 1359 | packages.remove(a); |
| 1360 | } |
| 1361 | else { |
| 1362 | // we will need the shared library path, so look that up and store it here |
| 1363 | app = mPackageManager.getApplicationInfo(pkg.packageName, |
| 1364 | PackageManager.GET_SHARED_LIBRARY_FILES); |
| 1365 | pkg.applicationInfo.sharedLibraryFiles = app.sharedLibraryFiles; |
| 1366 | } |
| 1367 | } catch (NameNotFoundException e) { |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1368 | packages.remove(a); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1369 | } |
| 1370 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1371 | return packages; |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1372 | } |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1373 | |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1374 | // Reset the given package's known backup participants. Unlike add/remove, the update |
| 1375 | // action cannot be passed a null package name. |
| 1376 | void updatePackageParticipantsLocked(String packageName) { |
| 1377 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1378 | Slog.e(TAG, "updatePackageParticipants called with null package name"); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1379 | return; |
| 1380 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1381 | if (DEBUG) Slog.v(TAG, "updatePackageParticipantsLocked: " + packageName); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1382 | |
| 1383 | // brute force but small code size |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 1384 | List<PackageInfo> allApps = allAgentPackages(); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1385 | List<String> allAppNames = new ArrayList<String>(); |
| 1386 | for (PackageInfo pkg : allApps) { |
| 1387 | allAppNames.add(pkg.packageName); |
| 1388 | } |
| 1389 | removePackageParticipantsLockedInner(packageName, allAppNames); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 1390 | addPackageParticipantsLockedInner(packageName, allApps); |
| Christopher Tate | 3799bc2 | 2009-05-06 16:13:56 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1393 | // Called from the backup task: record that the given app has been successfully |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1394 | // backed up at least once |
| 1395 | void logBackupComplete(String packageName) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1396 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return; |
| 1397 | |
| 1398 | synchronized (mEverStoredApps) { |
| 1399 | if (!mEverStoredApps.add(packageName)) return; |
| 1400 | |
| 1401 | RandomAccessFile out = null; |
| 1402 | try { |
| 1403 | out = new RandomAccessFile(mEverStored, "rws"); |
| 1404 | out.seek(out.length()); |
| 1405 | out.writeUTF(packageName); |
| 1406 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1407 | Slog.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1408 | } finally { |
| 1409 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1414 | // Remove our awareness of having ever backed up the given package |
| 1415 | void removeEverBackedUp(String packageName) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1416 | if (DEBUG) Slog.v(TAG, "Removing backed-up knowledge of " + packageName); |
| 1417 | if (MORE_DEBUG) Slog.v(TAG, "New set:"); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1418 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1419 | synchronized (mEverStoredApps) { |
| 1420 | // Rewrite the file and rename to overwrite. If we reboot in the middle, |
| 1421 | // we'll recognize on initialization time that the package no longer |
| 1422 | // exists and fix it up then. |
| 1423 | File tempKnownFile = new File(mBaseStateDir, "processed.new"); |
| 1424 | RandomAccessFile known = null; |
| 1425 | try { |
| 1426 | known = new RandomAccessFile(tempKnownFile, "rws"); |
| 1427 | mEverStoredApps.remove(packageName); |
| 1428 | for (String s : mEverStoredApps) { |
| 1429 | known.writeUTF(s); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1430 | if (MORE_DEBUG) Slog.v(TAG, " " + s); |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1431 | } |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1432 | known.close(); |
| 1433 | known = null; |
| 1434 | if (!tempKnownFile.renameTo(mEverStored)) { |
| 1435 | throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored); |
| 1436 | } |
| 1437 | } catch (IOException e) { |
| 1438 | // Bad: we couldn't create the new copy. For safety's sake we |
| 1439 | // abandon the whole process and remove all what's-backed-up |
| 1440 | // state entirely, meaning we'll force a backup pass for every |
| 1441 | // participant on the next boot or [re]install. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1442 | Slog.w(TAG, "Error rewriting " + mEverStored, e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1443 | mEverStoredApps.clear(); |
| 1444 | tempKnownFile.delete(); |
| 1445 | mEverStored.delete(); |
| 1446 | } finally { |
| 1447 | try { if (known != null) known.close(); } catch (IOException e) {} |
| Christopher Tate | e97e807 | 2009-07-15 16:45:50 -0700 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1452 | // Persistently record the current and ancestral backup tokens as well |
| 1453 | // as the set of packages with data [supposedly] available in the |
| 1454 | // ancestral dataset. |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1455 | void writeRestoreTokens() { |
| 1456 | try { |
| 1457 | RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd"); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1458 | |
| 1459 | // First, the version number of this record, for futureproofing |
| 1460 | af.writeInt(CURRENT_ANCESTRAL_RECORD_VERSION); |
| 1461 | |
| 1462 | // Write the ancestral and current tokens |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1463 | af.writeLong(mAncestralToken); |
| 1464 | af.writeLong(mCurrentToken); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1465 | |
| 1466 | // Now write the set of ancestral packages |
| 1467 | if (mAncestralPackages == null) { |
| 1468 | af.writeInt(-1); |
| 1469 | } else { |
| 1470 | af.writeInt(mAncestralPackages.size()); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1471 | if (DEBUG) Slog.v(TAG, "Ancestral packages: " + mAncestralPackages.size()); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1472 | for (String pkgName : mAncestralPackages) { |
| 1473 | af.writeUTF(pkgName); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1474 | if (MORE_DEBUG) Slog.v(TAG, " " + pkgName); |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 1475 | } |
| 1476 | } |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1477 | af.close(); |
| 1478 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1479 | Slog.w(TAG, "Unable to write token file:", e); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | |
| Dan Egnor | 87a02bc | 2009-06-17 02:30:10 -0700 | [diff] [blame] | 1483 | // Return the given transport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1484 | private IBackupTransport getTransport(String transportName) { |
| 1485 | synchronized (mTransports) { |
| 1486 | IBackupTransport transport = mTransports.get(transportName); |
| 1487 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1488 | Slog.w(TAG, "Requested unavailable transport: " + transportName); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 1489 | } |
| 1490 | return transport; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1491 | } |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1494 | // fire off a backup agent, blocking until it attaches or times out |
| 1495 | IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) { |
| 1496 | IBackupAgent agent = null; |
| 1497 | synchronized(mAgentConnectLock) { |
| 1498 | mConnecting = true; |
| 1499 | mConnectedAgent = null; |
| 1500 | try { |
| 1501 | if (mActivityManager.bindBackupAgent(app, mode)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1502 | Slog.d(TAG, "awaiting agent for " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1503 | |
| 1504 | // success; wait for the agent to arrive |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 1505 | // only wait 10 seconds for the bind to happen |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1506 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1507 | while (mConnecting && mConnectedAgent == null |
| 1508 | && (System.currentTimeMillis() < timeoutMark)) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1509 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1510 | mAgentConnectLock.wait(5000); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1511 | } catch (InterruptedException e) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1512 | // just bail |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1513 | return null; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | // if we timed out with no connect, abort and move on |
| 1518 | if (mConnecting == true) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1519 | Slog.w(TAG, "Timeout waiting for agent " + app); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1520 | return null; |
| 1521 | } |
| 1522 | agent = mConnectedAgent; |
| 1523 | } |
| 1524 | } catch (RemoteException e) { |
| 1525 | // can't happen |
| 1526 | } |
| 1527 | } |
| 1528 | return agent; |
| 1529 | } |
| 1530 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1531 | // clear an application's data, blocking until the operation completes or times out |
| 1532 | void clearApplicationDataSynchronous(String packageName) { |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1533 | // Don't wipe packages marked allowClearUserData=false |
| 1534 | try { |
| 1535 | PackageInfo info = mPackageManager.getPackageInfo(packageName, 0); |
| 1536 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1537 | if (MORE_DEBUG) Slog.i(TAG, "allowClearUserData=false so not wiping " |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1538 | + packageName); |
| 1539 | return; |
| 1540 | } |
| 1541 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1542 | Slog.w(TAG, "Tried to clear data for " + packageName + " but not found"); |
| Christopher Tate | f7c886b | 2009-06-26 15:34:09 -0700 | [diff] [blame] | 1543 | return; |
| 1544 | } |
| 1545 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1546 | ClearDataObserver observer = new ClearDataObserver(); |
| 1547 | |
| 1548 | synchronized(mClearDataLock) { |
| 1549 | mClearingData = true; |
| Christopher Tate | 9dfdac5 | 2009-08-06 14:57:53 -0700 | [diff] [blame] | 1550 | try { |
| 1551 | mActivityManager.clearApplicationUserData(packageName, observer); |
| 1552 | } catch (RemoteException e) { |
| 1553 | // can't happen because the activity manager is in this process |
| 1554 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1555 | |
| 1556 | // only wait 10 seconds for the clear data to happen |
| 1557 | long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL; |
| 1558 | while (mClearingData && (System.currentTimeMillis() < timeoutMark)) { |
| 1559 | try { |
| 1560 | mClearDataLock.wait(5000); |
| 1561 | } catch (InterruptedException e) { |
| 1562 | // won't happen, but still. |
| 1563 | mClearingData = false; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | class ClearDataObserver extends IPackageDataObserver.Stub { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1570 | public void onRemoveCompleted(String packageName, boolean succeeded) { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1571 | synchronized(mClearDataLock) { |
| 1572 | mClearingData = false; |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 1573 | mClearDataLock.notifyAll(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1578 | // Get the restore-set token for the best-available restore set for this package: |
| 1579 | // the active set if possible, else the ancestral one. Returns zero if none available. |
| 1580 | long getAvailableRestoreToken(String packageName) { |
| 1581 | long token = mAncestralToken; |
| 1582 | synchronized (mQueueLock) { |
| 1583 | if (mEverStoredApps.contains(packageName)) { |
| 1584 | token = mCurrentToken; |
| 1585 | } |
| 1586 | } |
| 1587 | return token; |
| 1588 | } |
| 1589 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1590 | // ----- |
| 1591 | // Utility methods used by the asynchronous-with-timeout backup/restore operations |
| 1592 | boolean waitUntilOperationComplete(int token) { |
| 1593 | int finalState = OP_PENDING; |
| 1594 | synchronized (mCurrentOpLock) { |
| 1595 | try { |
| 1596 | while ((finalState = mCurrentOperations.get(token, OP_TIMEOUT)) == OP_PENDING) { |
| 1597 | try { |
| 1598 | mCurrentOpLock.wait(); |
| 1599 | } catch (InterruptedException e) {} |
| 1600 | } |
| 1601 | } catch (IndexOutOfBoundsException e) { |
| 1602 | // the operation has been mysteriously cleared from our |
| 1603 | // bookkeeping -- consider this a success and ignore it. |
| 1604 | } |
| 1605 | } |
| 1606 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1607 | if (MORE_DEBUG) Slog.v(TAG, "operation " + Integer.toHexString(token) |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1608 | + " complete: finalState=" + finalState); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1609 | return finalState == OP_ACKNOWLEDGED; |
| 1610 | } |
| 1611 | |
| 1612 | void prepareOperationTimeout(int token, long interval) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1613 | if (MORE_DEBUG) Slog.v(TAG, "starting timeout: token=" + Integer.toHexString(token) |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 1614 | + " interval=" + interval); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1615 | synchronized (mCurrentOpLock) { |
| 1616 | mCurrentOperations.put(token, OP_PENDING); |
| 1617 | Message msg = mBackupHandler.obtainMessage(MSG_TIMEOUT, token, 0); |
| 1618 | mBackupHandler.sendMessageDelayed(msg, interval); |
| 1619 | } |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1620 | } |
| 1621 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1622 | // ----- Back up a set of applications via a worker thread ----- |
| 1623 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1624 | class PerformBackupTask implements Runnable { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1625 | private static final String TAG = "PerformBackupThread"; |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1626 | IBackupTransport mTransport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1627 | ArrayList<BackupRequest> mQueue; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1628 | File mStateDir; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1629 | File mJournal; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1630 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1631 | public PerformBackupTask(IBackupTransport transport, ArrayList<BackupRequest> queue, |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1632 | File journal) { |
| Christopher Tate | aa08844 | 2009-06-16 18:25:46 -0700 | [diff] [blame] | 1633 | mTransport = transport; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1634 | mQueue = queue; |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1635 | mJournal = journal; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 1636 | |
| 1637 | try { |
| 1638 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 1639 | } catch (RemoteException e) { |
| 1640 | // can't happen; the transport is local |
| 1641 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1644 | public void run() { |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1645 | int status = BackupConstants.TRANSPORT_OK; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1646 | long startRealtime = SystemClock.elapsedRealtime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1647 | if (DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1648 | |
| Christopher Tate | 7958834 | 2009-06-30 16:11:49 -0700 | [diff] [blame] | 1649 | // Backups run at background priority |
| 1650 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 1651 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1652 | try { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1653 | EventLog.writeEvent(EventLogTags.BACKUP_START, mTransport.transportDirName()); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1654 | |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1655 | // If we haven't stored package manager metadata yet, we must init the transport. |
| 1656 | File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL); |
| 1657 | if (status == BackupConstants.TRANSPORT_OK && pmState.length() <= 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1658 | Slog.i(TAG, "Initializing (wiping) backup state and transport storage"); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1659 | resetBackupState(mStateDir); // Just to make sure. |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1660 | status = mTransport.initializeDevice(); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1661 | if (status == BackupConstants.TRANSPORT_OK) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1662 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1663 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1664 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1665 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 1666 | } |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1667 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1668 | |
| 1669 | // The package manager doesn't have a proper <application> etc, but since |
| 1670 | // it's running here in the system process we can just set up its agent |
| 1671 | // directly and use a synthetic BackupRequest. We always run this pass |
| 1672 | // because it's cheap and this way we guarantee that we don't get out of |
| 1673 | // step even if we're selecting among various transports at run time. |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1674 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1675 | PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent( |
| 1676 | mPackageManager, allAgentPackages()); |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1677 | status = processOneBackup(PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1678 | IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport); |
| 1679 | } |
| Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 1680 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1681 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1682 | // Now run all the backups in our queue |
| 1683 | status = doQueuedBackups(mTransport); |
| 1684 | } |
| 1685 | |
| 1686 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1687 | // Tell the transport to finish everything it has buffered |
| 1688 | status = mTransport.finishBackup(); |
| 1689 | if (status == BackupConstants.TRANSPORT_OK) { |
| 1690 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1691 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, mQueue.size(), millis); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1692 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1693 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(finish)"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1694 | Slog.e(TAG, "Transport error in finishBackup()"); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1698 | if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1699 | // The backend reports that our dataset has been wiped. We need to |
| 1700 | // reset all of our bookkeeping and instead run a new backup pass for |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 1701 | // everything. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1702 | EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName()); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1703 | resetBackupState(mStateDir); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1704 | } |
| 1705 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1706 | Slog.e(TAG, "Error in backup thread", e); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1707 | status = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1708 | } finally { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1709 | // If everything actually went through and this is the first time we've |
| 1710 | // done a backup, we can now record what the current backup dataset token |
| 1711 | // is. |
| Christopher Tate | 2950555 | 2010-06-24 15:58:01 -0700 | [diff] [blame] | 1712 | if ((mCurrentToken == 0) && (status == BackupConstants.TRANSPORT_OK)) { |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 1713 | try { |
| 1714 | mCurrentToken = mTransport.getCurrentRestoreSet(); |
| 1715 | } catch (RemoteException e) { /* cannot happen */ } |
| 1716 | writeRestoreTokens(); |
| 1717 | } |
| 1718 | |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1719 | // If things went wrong, we need to re-stage the apps we had expected |
| 1720 | // to be backing up in this pass. This journals the package names in |
| 1721 | // the current active pending-backup file, not in the we are holding |
| 1722 | // here in mJournal. |
| 1723 | if (status != BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1724 | Slog.w(TAG, "Backup pass unsuccessful, restaging"); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1725 | for (BackupRequest req : mQueue) { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1726 | dataChangedImpl(req.packageName); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1727 | } |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 1728 | |
| 1729 | // We also want to reset the backup schedule based on whatever |
| 1730 | // the transport suggests by way of retry/backoff time. |
| 1731 | try { |
| 1732 | startBackupAlarmsLocked(mTransport.requestBackupTime()); |
| 1733 | } catch (RemoteException e) { /* cannot happen */ } |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | // Either backup was successful, in which case we of course do not need |
| 1737 | // this pass's journal any more; or it failed, in which case we just |
| 1738 | // re-enqueued all of these packages in the current active journal. |
| 1739 | // Either way, we no longer need this pass's journal. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1740 | if (mJournal != null && !mJournal.delete()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1741 | Slog.e(TAG, "Unable to remove backup journal file " + mJournal); |
| Christopher Tate | b03b3bb | 2009-09-22 11:14:17 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 1744 | // Only once we're entirely finished do we release the wakelock |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1745 | if (status == BackupConstants.TRANSPORT_NOT_INITIALIZED) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 1746 | backupNow(); |
| 1747 | } |
| 1748 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1749 | mWakelock.release(); |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 1750 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1753 | private int doQueuedBackups(IBackupTransport transport) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1754 | for (BackupRequest request : mQueue) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1755 | Slog.d(TAG, "starting agent for backup of " + request); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1756 | |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1757 | // Verify that the requested app exists; it might be something that |
| 1758 | // requested a backup but was then uninstalled. The request was |
| 1759 | // journalled and rather than tamper with the journal it's safer |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1760 | // to sanity-check here. This also gives us the classname of the |
| 1761 | // package's backup agent. |
| 1762 | PackageInfo pkg; |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1763 | try { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1764 | pkg = mPackageManager.getPackageInfo(request.packageName, 0); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 1765 | } catch (NameNotFoundException e) { |
| 1766 | Slog.d(TAG, "Package does not exist; skipping"); |
| 1767 | continue; |
| 1768 | } |
| 1769 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1770 | IBackupAgent agent = null; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1771 | try { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1772 | mWakelock.setWorkSource(new WorkSource(pkg.applicationInfo.uid)); |
| 1773 | agent = bindToAgentSynchronous(pkg.applicationInfo, |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1774 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1775 | if (agent != null) { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1776 | int result = processOneBackup(request.packageName, agent, transport); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1777 | if (result != BackupConstants.TRANSPORT_OK) return result; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1778 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1779 | } catch (SecurityException ex) { |
| 1780 | // Try for the next one. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1781 | Slog.d(TAG, "error in bind/backup", ex); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1782 | } finally { |
| 1783 | try { // unbind even on timeout, just in case |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1784 | mActivityManager.unbindBackupAgent(pkg.applicationInfo); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1785 | } catch (RemoteException e) {} |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1786 | } |
| 1787 | } |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1788 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1789 | mWakelock.setWorkSource(null); |
| 1790 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1791 | return BackupConstants.TRANSPORT_OK; |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1792 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1793 | |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 1794 | private int processOneBackup(String packageName, IBackupAgent agent, |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1795 | IBackupTransport transport) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1796 | if (DEBUG) Slog.d(TAG, "processOneBackup doBackup() on " + packageName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1797 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1798 | File savedStateName = new File(mStateDir, packageName); |
| 1799 | File backupDataName = new File(mDataDir, packageName + ".data"); |
| 1800 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 1801 | |
| 1802 | ParcelFileDescriptor savedState = null; |
| 1803 | ParcelFileDescriptor backupData = null; |
| 1804 | ParcelFileDescriptor newState = null; |
| 1805 | |
| 1806 | PackageInfo packInfo; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1807 | final int token = generateToken(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1808 | try { |
| 1809 | // Look up the package info & signatures. This is first so that if it |
| 1810 | // throws an exception, there's no file setup yet that would need to |
| 1811 | // be unraveled. |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1812 | if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| 1813 | // The metadata 'package' is synthetic |
| 1814 | packInfo = new PackageInfo(); |
| 1815 | packInfo.packageName = packageName; |
| 1816 | } else { |
| 1817 | packInfo = mPackageManager.getPackageInfo(packageName, |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1818 | PackageManager.GET_SIGNATURES); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 1819 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1820 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1821 | // In a full backup, we pass a null ParcelFileDescriptor as |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1822 | // the saved-state "file". This is by definition an incremental, |
| 1823 | // so we build a saved state file to pass. |
| 1824 | savedState = ParcelFileDescriptor.open(savedStateName, |
| 1825 | ParcelFileDescriptor.MODE_READ_ONLY | |
| 1826 | ParcelFileDescriptor.MODE_CREATE); // Make an empty file if necessary |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1827 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1828 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1829 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1830 | ParcelFileDescriptor.MODE_CREATE | |
| 1831 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1832 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1833 | newState = ParcelFileDescriptor.open(newStateName, |
| 1834 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 1835 | ParcelFileDescriptor.MODE_CREATE | |
| 1836 | ParcelFileDescriptor.MODE_TRUNCATE); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1837 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1838 | // Initiate the target's backup pass |
| 1839 | prepareOperationTimeout(token, TIMEOUT_BACKUP_INTERVAL); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 1840 | agent.doBackup(savedState, backupData, newState, token, mBackupManagerBinder); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1841 | boolean success = waitUntilOperationComplete(token); |
| 1842 | |
| 1843 | if (!success) { |
| 1844 | // timeout -- bail out into the failed-transaction logic |
| 1845 | throw new RuntimeException("Backup timeout"); |
| 1846 | } |
| 1847 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1848 | logBackupComplete(packageName); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1849 | if (DEBUG) Slog.v(TAG, "doBackup() success"); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1850 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1851 | Slog.e(TAG, "Error backing up " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1852 | EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName, e.toString()); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1853 | backupDataName.delete(); |
| 1854 | newStateName.delete(); |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1855 | return BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1856 | } finally { |
| 1857 | try { if (savedState != null) savedState.close(); } catch (IOException e) {} |
| 1858 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| 1859 | try { if (newState != null) newState.close(); } catch (IOException e) {} |
| 1860 | savedState = backupData = newState = null; |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 1861 | synchronized (mCurrentOpLock) { |
| 1862 | mCurrentOperations.clear(); |
| 1863 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | // Now propagate the newly-backed-up data to the transport |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1867 | int result = BackupConstants.TRANSPORT_OK; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1868 | try { |
| 1869 | int size = (int) backupDataName.length(); |
| 1870 | if (size > 0) { |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1871 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1872 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 1873 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 1874 | result = transport.performBackup(packInfo, backupData); |
| 1875 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1876 | |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 1877 | // TODO - We call finishBackup() for each application backed up, because |
| 1878 | // we need to know now whether it succeeded or failed. Instead, we should |
| 1879 | // hold off on finishBackup() until the end, which implies holding off on |
| 1880 | // renaming *all* the output state files (see below) until that happens. |
| 1881 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1882 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1883 | result = transport.finishBackup(); |
| Dan Egnor | 83861e7 | 2009-09-17 16:17:55 -0700 | [diff] [blame] | 1884 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1885 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1886 | if (DEBUG) Slog.i(TAG, "no backup data written; not calling transport"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | // After successful transport, delete the now-stale data |
| 1890 | // and juggle the files so that next time we supply the agent |
| 1891 | // with the new state file it just created. |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1892 | if (result == BackupConstants.TRANSPORT_OK) { |
| 1893 | backupDataName.delete(); |
| 1894 | newStateName.renameTo(savedStateName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1895 | EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, packageName, size); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1896 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1897 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1898 | } |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1899 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1900 | Slog.e(TAG, "Transport error backing up " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1901 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, packageName); |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1902 | result = BackupConstants.TRANSPORT_ERROR; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 1903 | } finally { |
| 1904 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1905 | } |
| Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 1906 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 1907 | return result; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 1908 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 1911 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1912 | // ----- Full backup to a file/socket ----- |
| 1913 | |
| 1914 | class PerformFullBackupTask implements Runnable { |
| 1915 | ParcelFileDescriptor mOutputFile; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 1916 | DeflaterOutputStream mDeflater; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1917 | IFullBackupRestoreObserver mObserver; |
| 1918 | boolean mIncludeApks; |
| 1919 | boolean mIncludeShared; |
| 1920 | boolean mAllApps; |
| 1921 | String[] mPackages; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 1922 | String mCurrentPassword; |
| 1923 | String mEncryptPassword; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1924 | AtomicBoolean mLatchObject; |
| 1925 | File mFilesDir; |
| 1926 | File mManifestFile; |
| 1927 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 1928 | class FullBackupRunner implements Runnable { |
| 1929 | PackageInfo mPackage; |
| 1930 | IBackupAgent mAgent; |
| 1931 | ParcelFileDescriptor mPipe; |
| 1932 | int mToken; |
| 1933 | boolean mSendApk; |
| 1934 | |
| 1935 | FullBackupRunner(PackageInfo pack, IBackupAgent agent, ParcelFileDescriptor pipe, |
| 1936 | int token, boolean sendApk) throws IOException { |
| 1937 | mPackage = pack; |
| 1938 | mAgent = agent; |
| 1939 | mPipe = ParcelFileDescriptor.dup(pipe.getFileDescriptor()); |
| 1940 | mToken = token; |
| 1941 | mSendApk = sendApk; |
| 1942 | } |
| 1943 | |
| 1944 | @Override |
| 1945 | public void run() { |
| 1946 | try { |
| 1947 | BackupDataOutput output = new BackupDataOutput( |
| 1948 | mPipe.getFileDescriptor()); |
| 1949 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1950 | if (MORE_DEBUG) Slog.d(TAG, "Writing manifest for " + mPackage.packageName); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 1951 | writeAppManifest(mPackage, mManifestFile, mSendApk); |
| 1952 | FullBackup.backupToTar(mPackage.packageName, null, null, |
| 1953 | mFilesDir.getAbsolutePath(), |
| 1954 | mManifestFile.getAbsolutePath(), |
| 1955 | output); |
| 1956 | |
| 1957 | if (mSendApk) { |
| 1958 | writeApkToBackup(mPackage, output); |
| 1959 | } |
| 1960 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 1961 | if (DEBUG) Slog.d(TAG, "Calling doFullBackup() on " + mPackage.packageName); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 1962 | prepareOperationTimeout(mToken, TIMEOUT_FULL_BACKUP_INTERVAL); |
| 1963 | mAgent.doFullBackup(mPipe, mToken, mBackupManagerBinder); |
| 1964 | } catch (IOException e) { |
| 1965 | Slog.e(TAG, "Error running full backup for " + mPackage.packageName); |
| 1966 | } catch (RemoteException e) { |
| 1967 | Slog.e(TAG, "Remote agent vanished during full backup of " |
| 1968 | + mPackage.packageName); |
| 1969 | } finally { |
| 1970 | try { |
| 1971 | mPipe.close(); |
| 1972 | } catch (IOException e) {} |
| 1973 | } |
| 1974 | } |
| 1975 | } |
| 1976 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1977 | PerformFullBackupTask(ParcelFileDescriptor fd, IFullBackupRestoreObserver observer, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 1978 | boolean includeApks, boolean includeShared, String curPassword, |
| 1979 | String encryptPassword, boolean doAllApps, String[] packages, |
| 1980 | AtomicBoolean latch) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1981 | mOutputFile = fd; |
| 1982 | mObserver = observer; |
| 1983 | mIncludeApks = includeApks; |
| 1984 | mIncludeShared = includeShared; |
| 1985 | mAllApps = doAllApps; |
| 1986 | mPackages = packages; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 1987 | mCurrentPassword = curPassword; |
| 1988 | // when backing up, if there is a current backup password, we require that |
| 1989 | // the user use a nonempty encryption password as well. if one is supplied |
| 1990 | // in the UI we use that, but if the UI was left empty we fall back to the |
| 1991 | // current backup password (which was supplied by the user as well). |
| 1992 | if (encryptPassword == null || "".equals(encryptPassword)) { |
| 1993 | mEncryptPassword = curPassword; |
| 1994 | } else { |
| 1995 | mEncryptPassword = encryptPassword; |
| 1996 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1997 | mLatchObject = latch; |
| 1998 | |
| 1999 | mFilesDir = new File("/data/system"); |
| 2000 | mManifestFile = new File(mFilesDir, BACKUP_MANIFEST_FILENAME); |
| 2001 | } |
| 2002 | |
| 2003 | @Override |
| 2004 | public void run() { |
| 2005 | final List<PackageInfo> packagesToBackup; |
| 2006 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2007 | Slog.i(TAG, "--- Performing full-dataset backup ---"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2008 | sendStartBackup(); |
| 2009 | |
| 2010 | // doAllApps supersedes the package set if any |
| 2011 | if (mAllApps) { |
| 2012 | packagesToBackup = mPackageManager.getInstalledPackages( |
| 2013 | PackageManager.GET_SIGNATURES); |
| 2014 | } else { |
| 2015 | packagesToBackup = new ArrayList<PackageInfo>(); |
| 2016 | for (String pkgName : mPackages) { |
| 2017 | try { |
| 2018 | packagesToBackup.add(mPackageManager.getPackageInfo(pkgName, |
| 2019 | PackageManager.GET_SIGNATURES)); |
| 2020 | } catch (NameNotFoundException e) { |
| 2021 | Slog.w(TAG, "Unknown package " + pkgName + ", skipping"); |
| 2022 | } |
| 2023 | } |
| 2024 | } |
| 2025 | |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 2026 | // Cull any packages that have indicated that backups are not permitted. |
| 2027 | for (int i = 0; i < packagesToBackup.size(); ) { |
| 2028 | PackageInfo info = packagesToBackup.get(i); |
| 2029 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) { |
| 2030 | packagesToBackup.remove(i); |
| 2031 | } else { |
| 2032 | i++; |
| 2033 | } |
| 2034 | } |
| 2035 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2036 | FileOutputStream ofstream = new FileOutputStream(mOutputFile.getFileDescriptor()); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2037 | OutputStream out = null; |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2038 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2039 | PackageInfo pkg = null; |
| 2040 | try { |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2041 | boolean encrypting = (mEncryptPassword != null && mEncryptPassword.length() > 0); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2042 | boolean compressing = COMPRESS_FULL_BACKUPS; |
| 2043 | OutputStream finalOutput = ofstream; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2044 | |
| Christopher Tate | eef4ae4 | 2011-08-05 13:15:53 -0700 | [diff] [blame] | 2045 | // Verify that the given password matches the currently-active |
| 2046 | // backup password, if any |
| 2047 | if (hasBackupPassword()) { |
| 2048 | if (!passwordMatchesSaved(mCurrentPassword, PBKDF2_HASH_ROUNDS)) { |
| 2049 | if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting"); |
| 2050 | return; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2054 | // Write the global file header. All strings are UTF-8 encoded; lines end |
| 2055 | // with a '\n' byte. Actual backup data begins immediately following the |
| 2056 | // final '\n'. |
| 2057 | // |
| 2058 | // line 1: "ANDROID BACKUP" |
| 2059 | // line 2: backup file format version, currently "1" |
| 2060 | // line 3: compressed? "0" if not compressed, "1" if compressed. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2061 | // line 4: name of encryption algorithm [currently only "none" or "AES-256"] |
| 2062 | // |
| 2063 | // When line 4 is not "none", then additional header data follows: |
| 2064 | // |
| 2065 | // line 5: user password salt [hex] |
| 2066 | // line 6: master key checksum salt [hex] |
| 2067 | // line 7: number of PBKDF2 rounds to use (same for user & master) [decimal] |
| 2068 | // line 8: IV of the user key [hex] |
| 2069 | // line 9: master key blob [hex] |
| 2070 | // IV of the master key, master key itself, master key checksum hash |
| 2071 | // |
| 2072 | // The master key checksum is the master key plus its checksum salt, run through |
| 2073 | // 10k rounds of PBKDF2. This is used to verify that the user has supplied the |
| 2074 | // correct password for decrypting the archive: the master key decrypted from |
| 2075 | // the archive using the user-supplied password is also run through PBKDF2 in |
| 2076 | // this way, and if the result does not match the checksum as stored in the |
| 2077 | // archive, then we know that the user-supplied password does not match the |
| 2078 | // archive's. |
| 2079 | StringBuilder headerbuf = new StringBuilder(1024); |
| 2080 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2081 | headerbuf.append(BACKUP_FILE_HEADER_MAGIC); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2082 | headerbuf.append(BACKUP_FILE_VERSION); // integer, no trailing \n |
| 2083 | headerbuf.append(compressing ? "\n1\n" : "\n0\n"); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2084 | |
| 2085 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2086 | // Set up the encryption stage if appropriate, and emit the correct header |
| 2087 | if (encrypting) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2088 | finalOutput = emitAesBackupHeader(headerbuf, finalOutput); |
| 2089 | } else { |
| 2090 | headerbuf.append("none\n"); |
| 2091 | } |
| 2092 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2093 | byte[] header = headerbuf.toString().getBytes("UTF-8"); |
| 2094 | ofstream.write(header); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2095 | |
| 2096 | // Set up the compression stage feeding into the encryption stage (if any) |
| 2097 | if (compressing) { |
| 2098 | Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); |
| 2099 | finalOutput = new DeflaterOutputStream(finalOutput, deflater, true); |
| 2100 | } |
| 2101 | |
| 2102 | out = finalOutput; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2103 | } catch (Exception e) { |
| 2104 | // Should never happen! |
| 2105 | Slog.e(TAG, "Unable to emit archive header", e); |
| 2106 | return; |
| 2107 | } |
| 2108 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2109 | // Now back up the app data via the agent mechanism |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2110 | int N = packagesToBackup.size(); |
| 2111 | for (int i = 0; i < N; i++) { |
| 2112 | pkg = packagesToBackup.get(i); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2113 | backupOnePackage(pkg, out); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2114 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2115 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2116 | // Shared storage if requested |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2117 | if (mIncludeShared) { |
| 2118 | backupSharedStorage(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2119 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2120 | |
| 2121 | // Done! |
| 2122 | finalizeBackup(out); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2123 | } catch (RemoteException e) { |
| 2124 | Slog.e(TAG, "App died during full backup"); |
| 2125 | } finally { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2126 | tearDown(pkg); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2127 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2128 | if (out != null) out.close(); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2129 | mOutputFile.close(); |
| 2130 | } catch (IOException e) { |
| 2131 | /* nothing we can do about this */ |
| 2132 | } |
| 2133 | synchronized (mCurrentOpLock) { |
| 2134 | mCurrentOperations.clear(); |
| 2135 | } |
| 2136 | synchronized (mLatchObject) { |
| 2137 | mLatchObject.set(true); |
| 2138 | mLatchObject.notifyAll(); |
| 2139 | } |
| 2140 | sendEndBackup(); |
| 2141 | mWakelock.release(); |
| 2142 | if (DEBUG) Slog.d(TAG, "Full backup pass complete."); |
| 2143 | } |
| 2144 | } |
| 2145 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2146 | private OutputStream emitAesBackupHeader(StringBuilder headerbuf, |
| 2147 | OutputStream ofstream) throws Exception { |
| 2148 | // User key will be used to encrypt the master key. |
| 2149 | byte[] newUserSalt = randomBytes(PBKDF2_SALT_SIZE); |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2150 | SecretKey userKey = buildPasswordKey(mEncryptPassword, newUserSalt, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2151 | PBKDF2_HASH_ROUNDS); |
| 2152 | |
| 2153 | // the master key is random for each backup |
| 2154 | byte[] masterPw = new byte[256 / 8]; |
| 2155 | mRng.nextBytes(masterPw); |
| 2156 | byte[] checksumSalt = randomBytes(PBKDF2_SALT_SIZE); |
| 2157 | |
| 2158 | // primary encryption of the datastream with the random key |
| 2159 | Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 2160 | SecretKeySpec masterKeySpec = new SecretKeySpec(masterPw, "AES"); |
| 2161 | c.init(Cipher.ENCRYPT_MODE, masterKeySpec); |
| 2162 | OutputStream finalOutput = new CipherOutputStream(ofstream, c); |
| 2163 | |
| 2164 | // line 4: name of encryption algorithm |
| 2165 | headerbuf.append(ENCRYPTION_ALGORITHM_NAME); |
| 2166 | headerbuf.append('\n'); |
| 2167 | // line 5: user password salt [hex] |
| 2168 | headerbuf.append(byteArrayToHex(newUserSalt)); |
| 2169 | headerbuf.append('\n'); |
| 2170 | // line 6: master key checksum salt [hex] |
| 2171 | headerbuf.append(byteArrayToHex(checksumSalt)); |
| 2172 | headerbuf.append('\n'); |
| 2173 | // line 7: number of PBKDF2 rounds used [decimal] |
| 2174 | headerbuf.append(PBKDF2_HASH_ROUNDS); |
| 2175 | headerbuf.append('\n'); |
| 2176 | |
| 2177 | // line 8: IV of the user key [hex] |
| 2178 | Cipher mkC = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 2179 | mkC.init(Cipher.ENCRYPT_MODE, userKey); |
| 2180 | |
| 2181 | byte[] IV = mkC.getIV(); |
| 2182 | headerbuf.append(byteArrayToHex(IV)); |
| 2183 | headerbuf.append('\n'); |
| 2184 | |
| 2185 | // line 9: master IV + key blob, encrypted by the user key [hex]. Blob format: |
| 2186 | // [byte] IV length = Niv |
| 2187 | // [array of Niv bytes] IV itself |
| 2188 | // [byte] master key length = Nmk |
| 2189 | // [array of Nmk bytes] master key itself |
| 2190 | // [byte] MK checksum hash length = Nck |
| 2191 | // [array of Nck bytes] master key checksum hash |
| 2192 | // |
| 2193 | // The checksum is the (master key + checksum salt), run through the |
| 2194 | // stated number of PBKDF2 rounds |
| 2195 | IV = c.getIV(); |
| 2196 | byte[] mk = masterKeySpec.getEncoded(); |
| 2197 | byte[] checksum = makeKeyChecksum(masterKeySpec.getEncoded(), |
| 2198 | checksumSalt, PBKDF2_HASH_ROUNDS); |
| 2199 | |
| 2200 | ByteArrayOutputStream blob = new ByteArrayOutputStream(IV.length + mk.length |
| 2201 | + checksum.length + 3); |
| 2202 | DataOutputStream mkOut = new DataOutputStream(blob); |
| 2203 | mkOut.writeByte(IV.length); |
| 2204 | mkOut.write(IV); |
| 2205 | mkOut.writeByte(mk.length); |
| 2206 | mkOut.write(mk); |
| 2207 | mkOut.writeByte(checksum.length); |
| 2208 | mkOut.write(checksum); |
| 2209 | mkOut.flush(); |
| 2210 | byte[] encryptedMk = mkC.doFinal(blob.toByteArray()); |
| 2211 | headerbuf.append(byteArrayToHex(encryptedMk)); |
| 2212 | headerbuf.append('\n'); |
| 2213 | |
| 2214 | return finalOutput; |
| 2215 | } |
| 2216 | |
| 2217 | private void backupOnePackage(PackageInfo pkg, OutputStream out) |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2218 | throws RemoteException { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2219 | Slog.d(TAG, "Binding to full backup agent : " + pkg.packageName); |
| 2220 | |
| 2221 | IBackupAgent agent = bindToAgentSynchronous(pkg.applicationInfo, |
| 2222 | IApplicationThread.BACKUP_MODE_FULL); |
| 2223 | if (agent != null) { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2224 | ParcelFileDescriptor[] pipes = null; |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2225 | try { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2226 | pipes = ParcelFileDescriptor.createPipe(); |
| 2227 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2228 | ApplicationInfo app = pkg.applicationInfo; |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2229 | final boolean sendApk = mIncludeApks |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2230 | && ((app.flags & ApplicationInfo.FLAG_FORWARD_LOCK) == 0) |
| 2231 | && ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 || |
| 2232 | (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); |
| 2233 | |
| 2234 | sendOnBackupPackage(pkg.packageName); |
| 2235 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2236 | final int token = generateToken(); |
| 2237 | FullBackupRunner runner = new FullBackupRunner(pkg, agent, pipes[1], |
| 2238 | token, sendApk); |
| 2239 | pipes[1].close(); // the runner has dup'd it |
| 2240 | pipes[1] = null; |
| 2241 | Thread t = new Thread(runner); |
| 2242 | t.start(); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2243 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2244 | // Now pull data from the app and stuff it into the compressor |
| 2245 | try { |
| 2246 | FileInputStream raw = new FileInputStream(pipes[0].getFileDescriptor()); |
| 2247 | DataInputStream in = new DataInputStream(raw); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2248 | |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2249 | byte[] buffer = new byte[16 * 1024]; |
| 2250 | int chunkTotal; |
| 2251 | while ((chunkTotal = in.readInt()) > 0) { |
| 2252 | while (chunkTotal > 0) { |
| 2253 | int toRead = (chunkTotal > buffer.length) |
| 2254 | ? buffer.length : chunkTotal; |
| 2255 | int nRead = in.read(buffer, 0, toRead); |
| 2256 | out.write(buffer, 0, nRead); |
| 2257 | chunkTotal -= nRead; |
| 2258 | } |
| 2259 | } |
| 2260 | } catch (IOException e) { |
| 2261 | Slog.i(TAG, "Caught exception reading from agent", e); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2264 | if (!waitUntilOperationComplete(token)) { |
| 2265 | Slog.e(TAG, "Full backup failed on package " + pkg.packageName); |
| 2266 | } else { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2267 | if (DEBUG) Slog.d(TAG, "Full package backup success: " + pkg.packageName); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2268 | } |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2269 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2270 | } catch (IOException e) { |
| 2271 | Slog.e(TAG, "Error backing up " + pkg.packageName, e); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2272 | } finally { |
| 2273 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2274 | // flush after every package |
| 2275 | out.flush(); |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2276 | if (pipes != null) { |
| 2277 | if (pipes[0] != null) pipes[0].close(); |
| 2278 | if (pipes[1] != null) pipes[1].close(); |
| 2279 | } |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2280 | } catch (IOException e) { |
| 2281 | Slog.w(TAG, "Error bringing down backup stack"); |
| 2282 | } |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2283 | } |
| 2284 | } else { |
| 2285 | Slog.w(TAG, "Unable to bind to full agent for " + pkg.packageName); |
| 2286 | } |
| 2287 | tearDown(pkg); |
| 2288 | } |
| 2289 | |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2290 | private void writeApkToBackup(PackageInfo pkg, BackupDataOutput output) { |
| 2291 | // Forward-locked apps, system-bundled .apks, etc are filtered out before we get here |
| 2292 | final String appSourceDir = pkg.applicationInfo.sourceDir; |
| 2293 | final String apkDir = new File(appSourceDir).getParent(); |
| 2294 | FullBackup.backupToTar(pkg.packageName, FullBackup.APK_TREE_TOKEN, null, |
| 2295 | apkDir, appSourceDir, output); |
| 2296 | |
| 2297 | // Save associated .obb content if it exists and we did save the apk |
| 2298 | // check for .obb and save those too |
| 2299 | final File obbDir = Environment.getExternalStorageAppObbDirectory(pkg.packageName); |
| 2300 | if (obbDir != null) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2301 | if (MORE_DEBUG) Log.i(TAG, "obb dir: " + obbDir.getAbsolutePath()); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2302 | File[] obbFiles = obbDir.listFiles(); |
| 2303 | if (obbFiles != null) { |
| 2304 | final String obbDirName = obbDir.getAbsolutePath(); |
| 2305 | for (File obb : obbFiles) { |
| 2306 | FullBackup.backupToTar(pkg.packageName, FullBackup.OBB_TREE_TOKEN, null, |
| 2307 | obbDirName, obb.getAbsolutePath(), output); |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2313 | private void backupSharedStorage() throws RemoteException { |
| 2314 | PackageInfo pkg = null; |
| 2315 | try { |
| 2316 | pkg = mPackageManager.getPackageInfo("com.android.sharedstoragebackup", 0); |
| 2317 | IBackupAgent agent = bindToAgentSynchronous(pkg.applicationInfo, |
| 2318 | IApplicationThread.BACKUP_MODE_FULL); |
| 2319 | if (agent != null) { |
| 2320 | sendOnBackupPackage("Shared storage"); |
| 2321 | |
| 2322 | final int token = generateToken(); |
| 2323 | prepareOperationTimeout(token, TIMEOUT_SHARED_BACKUP_INTERVAL); |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2324 | agent.doFullBackup(mOutputFile, token, mBackupManagerBinder); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2325 | if (!waitUntilOperationComplete(token)) { |
| 2326 | Slog.e(TAG, "Full backup failed on shared storage"); |
| 2327 | } else { |
| 2328 | if (DEBUG) Slog.d(TAG, "Full shared storage backup success"); |
| 2329 | } |
| 2330 | } else { |
| 2331 | Slog.e(TAG, "Could not bind to shared storage backup agent"); |
| 2332 | } |
| 2333 | } catch (NameNotFoundException e) { |
| 2334 | Slog.e(TAG, "Shared storage backup package not found"); |
| 2335 | } finally { |
| 2336 | tearDown(pkg); |
| 2337 | } |
| 2338 | } |
| 2339 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 2340 | private void finalizeBackup(OutputStream out) { |
| 2341 | try { |
| 2342 | // A standard 'tar' EOF sequence: two 512-byte blocks of all zeroes. |
| 2343 | byte[] eof = new byte[512 * 2]; // newly allocated == zero filled |
| 2344 | out.write(eof); |
| 2345 | } catch (IOException e) { |
| 2346 | Slog.w(TAG, "Error attempting to finalize backup stream"); |
| 2347 | } |
| 2348 | } |
| 2349 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2350 | private void writeAppManifest(PackageInfo pkg, File manifestFile, boolean withApk) |
| 2351 | throws IOException { |
| 2352 | // Manifest format. All data are strings ending in LF: |
| 2353 | // BACKUP_MANIFEST_VERSION, currently 1 |
| 2354 | // |
| 2355 | // Version 1: |
| 2356 | // package name |
| 2357 | // package's versionCode |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2358 | // platform versionCode |
| 2359 | // getInstallerPackageName() for this package (maybe empty) |
| 2360 | // boolean: "1" if archive includes .apk; any other string means not |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2361 | // number of signatures == N |
| 2362 | // N*: signature byte array in ascii format per Signature.toCharsString() |
| 2363 | StringBuilder builder = new StringBuilder(4096); |
| 2364 | StringBuilderPrinter printer = new StringBuilderPrinter(builder); |
| 2365 | |
| 2366 | printer.println(Integer.toString(BACKUP_MANIFEST_VERSION)); |
| 2367 | printer.println(pkg.packageName); |
| 2368 | printer.println(Integer.toString(pkg.versionCode)); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2369 | printer.println(Integer.toString(Build.VERSION.SDK_INT)); |
| 2370 | |
| 2371 | String installerName = mPackageManager.getInstallerPackageName(pkg.packageName); |
| 2372 | printer.println((installerName != null) ? installerName : ""); |
| 2373 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2374 | printer.println(withApk ? "1" : "0"); |
| 2375 | if (pkg.signatures == null) { |
| 2376 | printer.println("0"); |
| 2377 | } else { |
| 2378 | printer.println(Integer.toString(pkg.signatures.length)); |
| 2379 | for (Signature sig : pkg.signatures) { |
| 2380 | printer.println(sig.toCharsString()); |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | FileOutputStream outstream = new FileOutputStream(manifestFile); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2385 | outstream.write(builder.toString().getBytes()); |
| 2386 | outstream.close(); |
| 2387 | } |
| 2388 | |
| 2389 | private void tearDown(PackageInfo pkg) { |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2390 | if (pkg != null) { |
| 2391 | final ApplicationInfo app = pkg.applicationInfo; |
| 2392 | if (app != null) { |
| 2393 | try { |
| 2394 | // unbind and tidy up even on timeout or failure, just in case |
| 2395 | mActivityManager.unbindBackupAgent(app); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2396 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2397 | // The agent was running with a stub Application object, so shut it down. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2398 | if (app.uid != Process.SYSTEM_UID |
| 2399 | && app.uid != Process.PHONE_UID) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2400 | if (MORE_DEBUG) Slog.d(TAG, "Backup complete, killing host process"); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2401 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 2402 | } else { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2403 | if (MORE_DEBUG) Slog.d(TAG, "Not killing after restore: " + app.processName); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2404 | } |
| 2405 | } catch (RemoteException e) { |
| 2406 | Slog.d(TAG, "Lost app trying to shut down"); |
| 2407 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2408 | } |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | // wrappers for observer use |
| 2413 | void sendStartBackup() { |
| 2414 | if (mObserver != null) { |
| 2415 | try { |
| 2416 | mObserver.onStartBackup(); |
| 2417 | } catch (RemoteException e) { |
| 2418 | Slog.w(TAG, "full backup observer went away: startBackup"); |
| 2419 | mObserver = null; |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | void sendOnBackupPackage(String name) { |
| 2425 | if (mObserver != null) { |
| 2426 | try { |
| 2427 | // TODO: use a more user-friendly name string |
| 2428 | mObserver.onBackupPackage(name); |
| 2429 | } catch (RemoteException e) { |
| 2430 | Slog.w(TAG, "full backup observer went away: backupPackage"); |
| 2431 | mObserver = null; |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | void sendEndBackup() { |
| 2437 | if (mObserver != null) { |
| 2438 | try { |
| 2439 | mObserver.onEndBackup(); |
| 2440 | } catch (RemoteException e) { |
| 2441 | Slog.w(TAG, "full backup observer went away: endBackup"); |
| 2442 | mObserver = null; |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2449 | // ----- Full restore from a file/socket ----- |
| 2450 | |
| 2451 | // Description of a file in the restore datastream |
| 2452 | static class FileMetadata { |
| 2453 | String packageName; // name of the owning app |
| 2454 | String installerPackageName; // name of the market-type app that installed the owner |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2455 | int type; // e.g. BackupAgent.TYPE_DIRECTORY |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2456 | String domain; // e.g. FullBackup.DATABASE_TREE_TOKEN |
| 2457 | String path; // subpath within the semantic domain |
| 2458 | long mode; // e.g. 0666 (actually int) |
| 2459 | long mtime; // last mod time, UTC time_t (actually int) |
| 2460 | long size; // bytes of content |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2461 | |
| 2462 | @Override |
| 2463 | public String toString() { |
| 2464 | StringBuilder sb = new StringBuilder(128); |
| 2465 | sb.append("FileMetadata{"); |
| 2466 | sb.append(packageName); sb.append(','); |
| 2467 | sb.append(type); sb.append(','); |
| 2468 | sb.append(domain); sb.append(':'); sb.append(path); sb.append(','); |
| 2469 | sb.append(size); |
| 2470 | sb.append('}'); |
| 2471 | return sb.toString(); |
| 2472 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | enum RestorePolicy { |
| 2476 | IGNORE, |
| 2477 | ACCEPT, |
| 2478 | ACCEPT_IF_APK |
| 2479 | } |
| 2480 | |
| 2481 | class PerformFullRestoreTask implements Runnable { |
| 2482 | ParcelFileDescriptor mInputFile; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2483 | String mCurrentPassword; |
| 2484 | String mDecryptPassword; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2485 | IFullBackupRestoreObserver mObserver; |
| 2486 | AtomicBoolean mLatchObject; |
| 2487 | IBackupAgent mAgent; |
| 2488 | String mAgentPackage; |
| 2489 | ApplicationInfo mTargetApp; |
| 2490 | ParcelFileDescriptor[] mPipes = null; |
| 2491 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2492 | long mBytes; |
| 2493 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2494 | // possible handling states for a given package in the restore dataset |
| 2495 | final HashMap<String, RestorePolicy> mPackagePolicies |
| 2496 | = new HashMap<String, RestorePolicy>(); |
| 2497 | |
| 2498 | // installer package names for each encountered app, derived from the manifests |
| 2499 | final HashMap<String, String> mPackageInstallers = new HashMap<String, String>(); |
| 2500 | |
| 2501 | // Signatures for a given package found in its manifest file |
| 2502 | final HashMap<String, Signature[]> mManifestSignatures |
| 2503 | = new HashMap<String, Signature[]>(); |
| 2504 | |
| 2505 | // Packages we've already wiped data on when restoring their first file |
| 2506 | final HashSet<String> mClearedPackages = new HashSet<String>(); |
| 2507 | |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2508 | PerformFullRestoreTask(ParcelFileDescriptor fd, String curPassword, String decryptPassword, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2509 | IFullBackupRestoreObserver observer, AtomicBoolean latch) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2510 | mInputFile = fd; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2511 | mCurrentPassword = curPassword; |
| 2512 | mDecryptPassword = decryptPassword; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2513 | mObserver = observer; |
| 2514 | mLatchObject = latch; |
| 2515 | mAgent = null; |
| 2516 | mAgentPackage = null; |
| 2517 | mTargetApp = null; |
| 2518 | |
| 2519 | // Which packages we've already wiped data on. We prepopulate this |
| 2520 | // with a whitelist of packages known to be unclearable. |
| 2521 | mClearedPackages.add("android"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2522 | mClearedPackages.add("com.android.providers.settings"); |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2523 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | class RestoreFileRunnable implements Runnable { |
| 2527 | IBackupAgent mAgent; |
| 2528 | FileMetadata mInfo; |
| 2529 | ParcelFileDescriptor mSocket; |
| 2530 | int mToken; |
| 2531 | |
| 2532 | RestoreFileRunnable(IBackupAgent agent, FileMetadata info, |
| 2533 | ParcelFileDescriptor socket, int token) throws IOException { |
| 2534 | mAgent = agent; |
| 2535 | mInfo = info; |
| 2536 | mToken = token; |
| 2537 | |
| 2538 | // This class is used strictly for process-local binder invocations. The |
| 2539 | // semantics of ParcelFileDescriptor differ in this case; in particular, we |
| 2540 | // do not automatically get a 'dup'ed descriptor that we can can continue |
| 2541 | // to use asynchronously from the caller. So, we make sure to dup it ourselves |
| 2542 | // before proceeding to do the restore. |
| 2543 | mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 2544 | } |
| 2545 | |
| 2546 | @Override |
| 2547 | public void run() { |
| 2548 | try { |
| 2549 | mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type, |
| 2550 | mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime, |
| 2551 | mToken, mBackupManagerBinder); |
| 2552 | } catch (RemoteException e) { |
| 2553 | // never happens; this is used strictly for local binder calls |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | @Override |
| 2559 | public void run() { |
| 2560 | Slog.i(TAG, "--- Performing full-dataset restore ---"); |
| 2561 | sendStartRestore(); |
| 2562 | |
| Christopher Tate | b0628bf | 2011-06-02 15:08:13 -0700 | [diff] [blame] | 2563 | // Are we able to restore shared-storage data? |
| 2564 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| 2565 | mPackagePolicies.put("com.android.sharedstoragebackup", RestorePolicy.ACCEPT); |
| 2566 | } |
| 2567 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2568 | FileInputStream rawInStream = null; |
| 2569 | DataInputStream rawDataIn = null; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2570 | try { |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2571 | if (hasBackupPassword()) { |
| 2572 | if (!passwordMatchesSaved(mCurrentPassword, PBKDF2_HASH_ROUNDS)) { |
| 2573 | if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting"); |
| 2574 | return; |
| 2575 | } |
| 2576 | } |
| 2577 | |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2578 | mBytes = 0; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2579 | byte[] buffer = new byte[32 * 1024]; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2580 | rawInStream = new FileInputStream(mInputFile.getFileDescriptor()); |
| 2581 | rawDataIn = new DataInputStream(rawInStream); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2582 | |
| 2583 | // First, parse out the unencrypted/uncompressed header |
| 2584 | boolean compressed = false; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2585 | InputStream preCompressStream = rawInStream; |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2586 | final InputStream in; |
| 2587 | |
| 2588 | boolean okay = false; |
| 2589 | final int headerLen = BACKUP_FILE_HEADER_MAGIC.length(); |
| 2590 | byte[] streamHeader = new byte[headerLen]; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2591 | rawDataIn.readFully(streamHeader); |
| 2592 | byte[] magicBytes = BACKUP_FILE_HEADER_MAGIC.getBytes("UTF-8"); |
| 2593 | if (Arrays.equals(magicBytes, streamHeader)) { |
| 2594 | // okay, header looks good. now parse out the rest of the fields. |
| 2595 | String s = readHeaderLine(rawInStream); |
| 2596 | if (Integer.parseInt(s) == BACKUP_FILE_VERSION) { |
| 2597 | // okay, it's a version we recognize |
| 2598 | s = readHeaderLine(rawInStream); |
| 2599 | compressed = (Integer.parseInt(s) != 0); |
| 2600 | s = readHeaderLine(rawInStream); |
| 2601 | if (s.equals("none")) { |
| 2602 | // no more header to parse; we're good to go |
| 2603 | okay = true; |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2604 | } else if (mDecryptPassword != null && mDecryptPassword.length() > 0) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2605 | preCompressStream = decodeAesHeaderAndInitialize(s, rawInStream); |
| 2606 | if (preCompressStream != null) { |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2607 | okay = true; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2608 | } |
| 2609 | } else Slog.w(TAG, "Archive is encrypted but no password given"); |
| 2610 | } else Slog.w(TAG, "Wrong header version: " + s); |
| 2611 | } else Slog.w(TAG, "Didn't read the right header magic"); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2612 | |
| 2613 | if (!okay) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2614 | Slog.w(TAG, "Invalid restore data; aborting."); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2615 | return; |
| 2616 | } |
| 2617 | |
| 2618 | // okay, use the right stream layer based on compression |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2619 | in = (compressed) ? new InflaterInputStream(preCompressStream) : preCompressStream; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2620 | |
| 2621 | boolean didRestore; |
| 2622 | do { |
| Christopher Tate | 7926a69 | 2011-07-11 11:31:57 -0700 | [diff] [blame] | 2623 | didRestore = restoreOneFile(in, buffer); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2624 | } while (didRestore); |
| 2625 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2626 | if (MORE_DEBUG) Slog.v(TAG, "Done consuming input tarfile, total bytes=" + mBytes); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2627 | } catch (IOException e) { |
| 2628 | Slog.e(TAG, "Unable to read restore input"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2629 | } finally { |
| 2630 | tearDownPipes(); |
| 2631 | tearDownAgent(mTargetApp); |
| 2632 | |
| 2633 | try { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2634 | if (rawDataIn != null) rawDataIn.close(); |
| 2635 | if (rawInStream != null) rawInStream.close(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2636 | mInputFile.close(); |
| 2637 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2638 | Slog.w(TAG, "Close of restore data pipe threw", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2639 | /* nothing we can do about this */ |
| 2640 | } |
| 2641 | synchronized (mCurrentOpLock) { |
| 2642 | mCurrentOperations.clear(); |
| 2643 | } |
| 2644 | synchronized (mLatchObject) { |
| 2645 | mLatchObject.set(true); |
| 2646 | mLatchObject.notifyAll(); |
| 2647 | } |
| 2648 | sendEndRestore(); |
| 2649 | mWakelock.release(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2650 | Slog.d(TAG, "Full restore pass complete."); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2654 | String readHeaderLine(InputStream in) throws IOException { |
| 2655 | int c; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2656 | StringBuilder buffer = new StringBuilder(80); |
| Christopher Tate | 7bdb096 | 2011-07-13 19:30:21 -0700 | [diff] [blame] | 2657 | while ((c = in.read()) >= 0) { |
| 2658 | if (c == '\n') break; // consume and discard the newlines |
| 2659 | buffer.append((char)c); |
| 2660 | } |
| 2661 | return buffer.toString(); |
| 2662 | } |
| 2663 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2664 | InputStream decodeAesHeaderAndInitialize(String encryptionName, InputStream rawInStream) { |
| 2665 | InputStream result = null; |
| 2666 | try { |
| 2667 | if (encryptionName.equals(ENCRYPTION_ALGORITHM_NAME)) { |
| 2668 | |
| 2669 | String userSaltHex = readHeaderLine(rawInStream); // 5 |
| 2670 | byte[] userSalt = hexToByteArray(userSaltHex); |
| 2671 | |
| 2672 | String ckSaltHex = readHeaderLine(rawInStream); // 6 |
| 2673 | byte[] ckSalt = hexToByteArray(ckSaltHex); |
| 2674 | |
| 2675 | int rounds = Integer.parseInt(readHeaderLine(rawInStream)); // 7 |
| 2676 | String userIvHex = readHeaderLine(rawInStream); // 8 |
| 2677 | |
| 2678 | String masterKeyBlobHex = readHeaderLine(rawInStream); // 9 |
| 2679 | |
| 2680 | // decrypt the master key blob |
| 2681 | Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 2682 | SecretKey userKey = buildPasswordKey(mDecryptPassword, userSalt, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2683 | rounds); |
| 2684 | byte[] IV = hexToByteArray(userIvHex); |
| 2685 | IvParameterSpec ivSpec = new IvParameterSpec(IV); |
| 2686 | c.init(Cipher.DECRYPT_MODE, |
| 2687 | new SecretKeySpec(userKey.getEncoded(), "AES"), |
| 2688 | ivSpec); |
| 2689 | byte[] mkCipher = hexToByteArray(masterKeyBlobHex); |
| 2690 | byte[] mkBlob = c.doFinal(mkCipher); |
| 2691 | |
| 2692 | // first, the master key IV |
| 2693 | int offset = 0; |
| 2694 | int len = mkBlob[offset++]; |
| 2695 | IV = Arrays.copyOfRange(mkBlob, offset, offset + len); |
| 2696 | offset += len; |
| 2697 | // then the master key itself |
| 2698 | len = mkBlob[offset++]; |
| 2699 | byte[] mk = Arrays.copyOfRange(mkBlob, |
| 2700 | offset, offset + len); |
| 2701 | offset += len; |
| 2702 | // and finally the master key checksum hash |
| 2703 | len = mkBlob[offset++]; |
| 2704 | byte[] mkChecksum = Arrays.copyOfRange(mkBlob, |
| 2705 | offset, offset + len); |
| 2706 | |
| 2707 | // now validate the decrypted master key against the checksum |
| 2708 | byte[] calculatedCk = makeKeyChecksum(mk, ckSalt, rounds); |
| 2709 | if (Arrays.equals(calculatedCk, mkChecksum)) { |
| 2710 | ivSpec = new IvParameterSpec(IV); |
| 2711 | c.init(Cipher.DECRYPT_MODE, |
| 2712 | new SecretKeySpec(mk, "AES"), |
| 2713 | ivSpec); |
| 2714 | // Only if all of the above worked properly will 'result' be assigned |
| 2715 | result = new CipherInputStream(rawInStream, c); |
| 2716 | } else Slog.w(TAG, "Incorrect password"); |
| 2717 | } else Slog.w(TAG, "Unsupported encryption method: " + encryptionName); |
| 2718 | } catch (InvalidAlgorithmParameterException e) { |
| 2719 | Slog.e(TAG, "Needed parameter spec unavailable!", e); |
| 2720 | } catch (BadPaddingException e) { |
| 2721 | // This case frequently occurs when the wrong password is used to decrypt |
| 2722 | // the master key. Use the identical "incorrect password" log text as is |
| 2723 | // used in the checksum failure log in order to avoid providing additional |
| 2724 | // information to an attacker. |
| 2725 | Slog.w(TAG, "Incorrect password"); |
| 2726 | } catch (IllegalBlockSizeException e) { |
| 2727 | Slog.w(TAG, "Invalid block size in master key"); |
| 2728 | } catch (NoSuchAlgorithmException e) { |
| 2729 | Slog.e(TAG, "Needed decryption algorithm unavailable!"); |
| 2730 | } catch (NoSuchPaddingException e) { |
| 2731 | Slog.e(TAG, "Needed padding mechanism unavailable!"); |
| 2732 | } catch (InvalidKeyException e) { |
| 2733 | Slog.w(TAG, "Illegal password; aborting"); |
| 2734 | } catch (NumberFormatException e) { |
| 2735 | Slog.w(TAG, "Can't parse restore data header"); |
| 2736 | } catch (IOException e) { |
| 2737 | Slog.w(TAG, "Can't read input header"); |
| 2738 | } |
| 2739 | |
| 2740 | return result; |
| 2741 | } |
| 2742 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2743 | boolean restoreOneFile(InputStream instream, byte[] buffer) { |
| 2744 | FileMetadata info; |
| 2745 | try { |
| 2746 | info = readTarHeaders(instream); |
| 2747 | if (info != null) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 2748 | if (MORE_DEBUG) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2749 | dumpFileMetadata(info); |
| 2750 | } |
| 2751 | |
| 2752 | final String pkg = info.packageName; |
| 2753 | if (!pkg.equals(mAgentPackage)) { |
| 2754 | // okay, change in package; set up our various |
| 2755 | // bookkeeping if we haven't seen it yet |
| 2756 | if (!mPackagePolicies.containsKey(pkg)) { |
| 2757 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2758 | } |
| 2759 | |
| 2760 | // Clean up the previous agent relationship if necessary, |
| 2761 | // and let the observer know we're considering a new app. |
| 2762 | if (mAgent != null) { |
| 2763 | if (DEBUG) Slog.d(TAG, "Saw new package; tearing down old one"); |
| 2764 | tearDownPipes(); |
| 2765 | tearDownAgent(mTargetApp); |
| 2766 | mTargetApp = null; |
| 2767 | mAgentPackage = null; |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | if (info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 2772 | mPackagePolicies.put(pkg, readAppManifest(info, instream)); |
| 2773 | mPackageInstallers.put(pkg, info.installerPackageName); |
| 2774 | // We've read only the manifest content itself at this point, |
| 2775 | // so consume the footer before looping around to the next |
| 2776 | // input file |
| 2777 | skipTarPadding(info.size, instream); |
| 2778 | sendOnRestorePackage(pkg); |
| 2779 | } else { |
| 2780 | // Non-manifest, so it's actual file data. Is this a package |
| 2781 | // we're ignoring? |
| 2782 | boolean okay = true; |
| 2783 | RestorePolicy policy = mPackagePolicies.get(pkg); |
| 2784 | switch (policy) { |
| 2785 | case IGNORE: |
| 2786 | okay = false; |
| 2787 | break; |
| 2788 | |
| 2789 | case ACCEPT_IF_APK: |
| 2790 | // If we're in accept-if-apk state, then the first file we |
| 2791 | // see MUST be the apk. |
| 2792 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 2793 | if (DEBUG) Slog.d(TAG, "APK file; installing"); |
| 2794 | // Try to install the app. |
| 2795 | String installerName = mPackageInstallers.get(pkg); |
| 2796 | okay = installApk(info, installerName, instream); |
| 2797 | // good to go; promote to ACCEPT |
| 2798 | mPackagePolicies.put(pkg, (okay) |
| 2799 | ? RestorePolicy.ACCEPT |
| 2800 | : RestorePolicy.IGNORE); |
| 2801 | // At this point we've consumed this file entry |
| 2802 | // ourselves, so just strip the tar footer and |
| 2803 | // go on to the next file in the input stream |
| 2804 | skipTarPadding(info.size, instream); |
| 2805 | return true; |
| 2806 | } else { |
| 2807 | // File data before (or without) the apk. We can't |
| 2808 | // handle it coherently in this case so ignore it. |
| 2809 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2810 | okay = false; |
| 2811 | } |
| 2812 | break; |
| 2813 | |
| 2814 | case ACCEPT: |
| 2815 | if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) { |
| 2816 | if (DEBUG) Slog.d(TAG, "apk present but ACCEPT"); |
| 2817 | // we can take the data without the apk, so we |
| 2818 | // *want* to do so. skip the apk by declaring this |
| 2819 | // one file not-okay without changing the restore |
| 2820 | // policy for the package. |
| 2821 | okay = false; |
| 2822 | } |
| 2823 | break; |
| 2824 | |
| 2825 | default: |
| 2826 | // Something has gone dreadfully wrong when determining |
| 2827 | // the restore policy from the manifest. Ignore the |
| 2828 | // rest of this package's data. |
| 2829 | Slog.e(TAG, "Invalid policy from manifest"); |
| 2830 | okay = false; |
| 2831 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2832 | break; |
| 2833 | } |
| 2834 | |
| 2835 | // If the policy is satisfied, go ahead and set up to pipe the |
| 2836 | // data to the agent. |
| 2837 | if (DEBUG && okay && mAgent != null) { |
| 2838 | Slog.i(TAG, "Reusing existing agent instance"); |
| 2839 | } |
| 2840 | if (okay && mAgent == null) { |
| 2841 | if (DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg); |
| 2842 | |
| 2843 | try { |
| 2844 | mTargetApp = mPackageManager.getApplicationInfo(pkg, 0); |
| 2845 | |
| 2846 | // If we haven't sent any data to this app yet, we probably |
| 2847 | // need to clear it first. Check that. |
| 2848 | if (!mClearedPackages.contains(pkg)) { |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2849 | // apps with their own backup agents are |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2850 | // responsible for coherently managing a full |
| 2851 | // restore. |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2852 | if (mTargetApp.backupAgentName == null) { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2853 | if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore"); |
| 2854 | clearApplicationDataSynchronous(pkg); |
| 2855 | } else { |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 2856 | if (DEBUG) Slog.d(TAG, "backup agent (" |
| 2857 | + mTargetApp.backupAgentName + ") => no clear"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2858 | } |
| 2859 | mClearedPackages.add(pkg); |
| 2860 | } else { |
| 2861 | if (DEBUG) Slog.d(TAG, "We've initialized this app already; no clear required"); |
| 2862 | } |
| 2863 | |
| 2864 | // All set; now set up the IPC and launch the agent |
| 2865 | setUpPipes(); |
| 2866 | mAgent = bindToAgentSynchronous(mTargetApp, |
| 2867 | IApplicationThread.BACKUP_MODE_RESTORE_FULL); |
| 2868 | mAgentPackage = pkg; |
| 2869 | } catch (IOException e) { |
| 2870 | // fall through to error handling |
| 2871 | } catch (NameNotFoundException e) { |
| 2872 | // fall through to error handling |
| 2873 | } |
| 2874 | |
| 2875 | if (mAgent == null) { |
| 2876 | if (DEBUG) Slog.d(TAG, "Unable to create agent for " + pkg); |
| 2877 | okay = false; |
| 2878 | tearDownPipes(); |
| 2879 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | // Sanity check: make sure we never give data to the wrong app. This |
| 2884 | // should never happen but a little paranoia here won't go amiss. |
| 2885 | if (okay && !pkg.equals(mAgentPackage)) { |
| 2886 | Slog.e(TAG, "Restoring data for " + pkg |
| 2887 | + " but agent is for " + mAgentPackage); |
| 2888 | okay = false; |
| 2889 | } |
| 2890 | |
| 2891 | // At this point we have an agent ready to handle the full |
| 2892 | // restore data as well as a pipe for sending data to |
| 2893 | // that agent. Tell the agent to start reading from the |
| 2894 | // pipe. |
| 2895 | if (okay) { |
| 2896 | boolean agentSuccess = true; |
| 2897 | long toCopy = info.size; |
| 2898 | final int token = generateToken(); |
| 2899 | try { |
| 2900 | if (DEBUG) Slog.d(TAG, "Invoking agent to restore file " |
| 2901 | + info.path); |
| 2902 | prepareOperationTimeout(token, |
| 2903 | TIMEOUT_FULL_BACKUP_INTERVAL); |
| 2904 | // fire up the app's agent listening on the socket. If |
| 2905 | // the agent is running in the system process we can't |
| 2906 | // just invoke it asynchronously, so we provide a thread |
| 2907 | // for it here. |
| 2908 | if (mTargetApp.processName.equals("system")) { |
| 2909 | Slog.d(TAG, "system process agent - spinning a thread"); |
| 2910 | RestoreFileRunnable runner = new RestoreFileRunnable( |
| 2911 | mAgent, info, mPipes[0], token); |
| 2912 | new Thread(runner).start(); |
| 2913 | } else { |
| 2914 | mAgent.doRestoreFile(mPipes[0], info.size, info.type, |
| 2915 | info.domain, info.path, info.mode, info.mtime, |
| 2916 | token, mBackupManagerBinder); |
| 2917 | } |
| 2918 | } catch (IOException e) { |
| 2919 | // couldn't dup the socket for a process-local restore |
| 2920 | Slog.d(TAG, "Couldn't establish restore"); |
| 2921 | agentSuccess = false; |
| 2922 | okay = false; |
| 2923 | } catch (RemoteException e) { |
| 2924 | // whoops, remote agent went away. We'll eat the content |
| 2925 | // ourselves, then, and not copy it over. |
| 2926 | Slog.e(TAG, "Agent crashed during full restore"); |
| 2927 | agentSuccess = false; |
| 2928 | okay = false; |
| 2929 | } |
| 2930 | |
| 2931 | // Copy over the data if the agent is still good |
| 2932 | if (okay) { |
| 2933 | boolean pipeOkay = true; |
| 2934 | FileOutputStream pipe = new FileOutputStream( |
| 2935 | mPipes[1].getFileDescriptor()); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2936 | while (toCopy > 0) { |
| 2937 | int toRead = (toCopy > buffer.length) |
| 2938 | ? buffer.length : (int)toCopy; |
| 2939 | int nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2940 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2941 | if (nRead <= 0) break; |
| 2942 | toCopy -= nRead; |
| 2943 | |
| 2944 | // send it to the output pipe as long as things |
| 2945 | // are still good |
| 2946 | if (pipeOkay) { |
| 2947 | try { |
| 2948 | pipe.write(buffer, 0, nRead); |
| 2949 | } catch (IOException e) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2950 | Slog.e(TAG, "Failed to write to restore pipe", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2951 | pipeOkay = false; |
| 2952 | } |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | // done sending that file! Now we just need to consume |
| 2957 | // the delta from info.size to the end of block. |
| 2958 | skipTarPadding(info.size, instream); |
| 2959 | |
| 2960 | // and now that we've sent it all, wait for the remote |
| 2961 | // side to acknowledge receipt |
| 2962 | agentSuccess = waitUntilOperationComplete(token); |
| 2963 | } |
| 2964 | |
| 2965 | // okay, if the remote end failed at any point, deal with |
| 2966 | // it by ignoring the rest of the restore on it |
| 2967 | if (!agentSuccess) { |
| 2968 | mBackupHandler.removeMessages(MSG_TIMEOUT); |
| 2969 | tearDownPipes(); |
| 2970 | tearDownAgent(mTargetApp); |
| 2971 | mAgent = null; |
| 2972 | mPackagePolicies.put(pkg, RestorePolicy.IGNORE); |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | // Problems setting up the agent communication, or an already- |
| 2977 | // ignored package: skip to the next tar stream entry by |
| 2978 | // reading and discarding this file. |
| 2979 | if (!okay) { |
| 2980 | if (DEBUG) Slog.d(TAG, "[discarding file content]"); |
| 2981 | long bytesToConsume = (info.size + 511) & ~511; |
| 2982 | while (bytesToConsume > 0) { |
| 2983 | int toRead = (bytesToConsume > buffer.length) |
| 2984 | ? buffer.length : (int)bytesToConsume; |
| 2985 | long nRead = instream.read(buffer, 0, toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 2986 | if (nRead >= 0) mBytes += nRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2987 | if (nRead <= 0) break; |
| 2988 | bytesToConsume -= nRead; |
| 2989 | } |
| 2990 | } |
| 2991 | } |
| 2992 | } |
| 2993 | } catch (IOException e) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 2994 | if (DEBUG) Slog.w(TAG, "io exception on restore socket read", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 2995 | // treat as EOF |
| 2996 | info = null; |
| 2997 | } |
| 2998 | |
| 2999 | return (info != null); |
| 3000 | } |
| 3001 | |
| 3002 | void setUpPipes() throws IOException { |
| 3003 | mPipes = ParcelFileDescriptor.createPipe(); |
| 3004 | } |
| 3005 | |
| 3006 | void tearDownPipes() { |
| 3007 | if (mPipes != null) { |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3008 | try { |
| 3009 | mPipes[0].close(); |
| 3010 | mPipes[0] = null; |
| 3011 | mPipes[1].close(); |
| 3012 | mPipes[1] = null; |
| 3013 | } catch (IOException e) { |
| 3014 | Slog.w(TAG, "Couldn't close agent pipes", e); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3015 | } |
| 3016 | mPipes = null; |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | void tearDownAgent(ApplicationInfo app) { |
| 3021 | if (mAgent != null) { |
| 3022 | try { |
| 3023 | // unbind and tidy up even on timeout or failure, just in case |
| 3024 | mActivityManager.unbindBackupAgent(app); |
| 3025 | |
| 3026 | // The agent was running with a stub Application object, so shut it down. |
| 3027 | // !!! We hardcode the confirmation UI's package name here rather than use a |
| 3028 | // manifest flag! TODO something less direct. |
| 3029 | if (app.uid != Process.SYSTEM_UID |
| 3030 | && !app.packageName.equals("com.android.backupconfirm")) { |
| 3031 | if (DEBUG) Slog.d(TAG, "Killing host process"); |
| 3032 | mActivityManager.killApplicationProcess(app.processName, app.uid); |
| 3033 | } else { |
| 3034 | if (DEBUG) Slog.d(TAG, "Not killing after full restore"); |
| 3035 | } |
| 3036 | } catch (RemoteException e) { |
| 3037 | Slog.d(TAG, "Lost app trying to shut down"); |
| 3038 | } |
| 3039 | mAgent = null; |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | class RestoreInstallObserver extends IPackageInstallObserver.Stub { |
| 3044 | final AtomicBoolean mDone = new AtomicBoolean(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3045 | String mPackageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3046 | int mResult; |
| 3047 | |
| 3048 | public void reset() { |
| 3049 | synchronized (mDone) { |
| 3050 | mDone.set(false); |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | public void waitForCompletion() { |
| 3055 | synchronized (mDone) { |
| 3056 | while (mDone.get() == false) { |
| 3057 | try { |
| 3058 | mDone.wait(); |
| 3059 | } catch (InterruptedException e) { } |
| 3060 | } |
| 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | int getResult() { |
| 3065 | return mResult; |
| 3066 | } |
| 3067 | |
| 3068 | @Override |
| 3069 | public void packageInstalled(String packageName, int returnCode) |
| 3070 | throws RemoteException { |
| 3071 | synchronized (mDone) { |
| 3072 | mResult = returnCode; |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3073 | mPackageName = packageName; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3074 | mDone.set(true); |
| 3075 | mDone.notifyAll(); |
| 3076 | } |
| 3077 | } |
| 3078 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3079 | |
| 3080 | class RestoreDeleteObserver extends IPackageDeleteObserver.Stub { |
| 3081 | final AtomicBoolean mDone = new AtomicBoolean(); |
| 3082 | int mResult; |
| 3083 | |
| 3084 | public void reset() { |
| 3085 | synchronized (mDone) { |
| 3086 | mDone.set(false); |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | public void waitForCompletion() { |
| 3091 | synchronized (mDone) { |
| 3092 | while (mDone.get() == false) { |
| 3093 | try { |
| 3094 | mDone.wait(); |
| 3095 | } catch (InterruptedException e) { } |
| 3096 | } |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | @Override |
| 3101 | public void packageDeleted(String packageName, int returnCode) throws RemoteException { |
| 3102 | synchronized (mDone) { |
| 3103 | mResult = returnCode; |
| 3104 | mDone.set(true); |
| 3105 | mDone.notifyAll(); |
| 3106 | } |
| 3107 | } |
| 3108 | } |
| 3109 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3110 | final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver(); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3111 | final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver(); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3112 | |
| 3113 | boolean installApk(FileMetadata info, String installerPackage, InputStream instream) { |
| 3114 | boolean okay = true; |
| 3115 | |
| 3116 | if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName); |
| 3117 | |
| 3118 | // The file content is an .apk file. Copy it out to a staging location and |
| 3119 | // attempt to install it. |
| 3120 | File apkFile = new File(mDataDir, info.packageName); |
| 3121 | try { |
| 3122 | FileOutputStream apkStream = new FileOutputStream(apkFile); |
| 3123 | byte[] buffer = new byte[32 * 1024]; |
| 3124 | long size = info.size; |
| 3125 | while (size > 0) { |
| 3126 | long toRead = (buffer.length < size) ? buffer.length : size; |
| 3127 | int didRead = instream.read(buffer, 0, (int)toRead); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3128 | if (didRead >= 0) mBytes += didRead; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3129 | apkStream.write(buffer, 0, didRead); |
| 3130 | size -= didRead; |
| 3131 | } |
| 3132 | apkStream.close(); |
| 3133 | |
| 3134 | // make sure the installer can read it |
| 3135 | apkFile.setReadable(true, false); |
| 3136 | |
| 3137 | // Now install it |
| 3138 | Uri packageUri = Uri.fromFile(apkFile); |
| 3139 | mInstallObserver.reset(); |
| 3140 | mPackageManager.installPackage(packageUri, mInstallObserver, |
| 3141 | PackageManager.INSTALL_REPLACE_EXISTING, installerPackage); |
| 3142 | mInstallObserver.waitForCompletion(); |
| 3143 | |
| 3144 | if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) { |
| 3145 | // The only time we continue to accept install of data even if the |
| 3146 | // apk install failed is if we had already determined that we could |
| 3147 | // accept the data regardless. |
| 3148 | if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) { |
| 3149 | okay = false; |
| 3150 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3151 | } else { |
| 3152 | // Okay, the install succeeded. Make sure it was the right app. |
| 3153 | boolean uninstall = false; |
| 3154 | if (!mInstallObserver.mPackageName.equals(info.packageName)) { |
| 3155 | Slog.w(TAG, "Restore stream claimed to include apk for " |
| 3156 | + info.packageName + " but apk was really " |
| 3157 | + mInstallObserver.mPackageName); |
| 3158 | // delete the package we just put in place; it might be fraudulent |
| 3159 | okay = false; |
| 3160 | uninstall = true; |
| 3161 | } else { |
| 3162 | try { |
| 3163 | PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName, |
| 3164 | PackageManager.GET_SIGNATURES); |
| 3165 | if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) { |
| 3166 | Slog.w(TAG, "Restore stream contains apk of package " |
| 3167 | + info.packageName + " but it disallows backup/restore"); |
| 3168 | okay = false; |
| 3169 | } else { |
| 3170 | // So far so good -- do the signatures match the manifest? |
| 3171 | Signature[] sigs = mManifestSignatures.get(info.packageName); |
| 3172 | if (!signaturesMatch(sigs, pkg)) { |
| 3173 | Slog.w(TAG, "Installed app " + info.packageName |
| 3174 | + " signatures do not match restore manifest"); |
| 3175 | okay = false; |
| 3176 | uninstall = true; |
| 3177 | } |
| 3178 | } |
| 3179 | } catch (NameNotFoundException e) { |
| 3180 | Slog.w(TAG, "Install of package " + info.packageName |
| 3181 | + " succeeded but now not found"); |
| 3182 | okay = false; |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | // If we're not okay at this point, we need to delete the package |
| 3187 | // that we just installed. |
| 3188 | if (uninstall) { |
| 3189 | mDeleteObserver.reset(); |
| 3190 | mPackageManager.deletePackage(mInstallObserver.mPackageName, |
| 3191 | mDeleteObserver, 0); |
| 3192 | mDeleteObserver.waitForCompletion(); |
| 3193 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3194 | } |
| 3195 | } catch (IOException e) { |
| 3196 | Slog.e(TAG, "Unable to transcribe restored apk for install"); |
| 3197 | okay = false; |
| 3198 | } finally { |
| 3199 | apkFile.delete(); |
| 3200 | } |
| 3201 | |
| 3202 | return okay; |
| 3203 | } |
| 3204 | |
| 3205 | // Given an actual file content size, consume the post-content padding mandated |
| 3206 | // by the tar format. |
| 3207 | void skipTarPadding(long size, InputStream instream) throws IOException { |
| 3208 | long partial = (size + 512) % 512; |
| 3209 | if (partial > 0) { |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3210 | final int needed = 512 - (int)partial; |
| 3211 | byte[] buffer = new byte[needed]; |
| 3212 | if (readExactly(instream, buffer, 0, needed) == needed) { |
| 3213 | mBytes += needed; |
| 3214 | } else throw new IOException("Unexpected EOF in padding"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | // Returns a policy constant; takes a buffer arg to reduce memory churn |
| 3219 | RestorePolicy readAppManifest(FileMetadata info, InputStream instream) |
| 3220 | throws IOException { |
| 3221 | // Fail on suspiciously large manifest files |
| 3222 | if (info.size > 64 * 1024) { |
| 3223 | throw new IOException("Restore manifest too big; corrupt? size=" + info.size); |
| 3224 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3225 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3226 | byte[] buffer = new byte[(int) info.size]; |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3227 | if (readExactly(instream, buffer, 0, (int)info.size) == info.size) { |
| 3228 | mBytes += info.size; |
| 3229 | } else throw new IOException("Unexpected EOF in manifest"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3230 | |
| 3231 | RestorePolicy policy = RestorePolicy.IGNORE; |
| 3232 | String[] str = new String[1]; |
| 3233 | int offset = 0; |
| 3234 | |
| 3235 | try { |
| 3236 | offset = extractLine(buffer, offset, str); |
| 3237 | int version = Integer.parseInt(str[0]); |
| 3238 | if (version == BACKUP_MANIFEST_VERSION) { |
| 3239 | offset = extractLine(buffer, offset, str); |
| 3240 | String manifestPackage = str[0]; |
| 3241 | // TODO: handle <original-package> |
| 3242 | if (manifestPackage.equals(info.packageName)) { |
| 3243 | offset = extractLine(buffer, offset, str); |
| 3244 | version = Integer.parseInt(str[0]); // app version |
| 3245 | offset = extractLine(buffer, offset, str); |
| 3246 | int platformVersion = Integer.parseInt(str[0]); |
| 3247 | offset = extractLine(buffer, offset, str); |
| 3248 | info.installerPackageName = (str[0].length() > 0) ? str[0] : null; |
| 3249 | offset = extractLine(buffer, offset, str); |
| 3250 | boolean hasApk = str[0].equals("1"); |
| 3251 | offset = extractLine(buffer, offset, str); |
| 3252 | int numSigs = Integer.parseInt(str[0]); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3253 | if (numSigs > 0) { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3254 | Signature[] sigs = new Signature[numSigs]; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3255 | for (int i = 0; i < numSigs; i++) { |
| 3256 | offset = extractLine(buffer, offset, str); |
| 3257 | sigs[i] = new Signature(str[0]); |
| 3258 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3259 | mManifestSignatures.put(info.packageName, sigs); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3260 | |
| 3261 | // Okay, got the manifest info we need... |
| 3262 | try { |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3263 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 3264 | info.packageName, PackageManager.GET_SIGNATURES); |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3265 | // Fall through to IGNORE if the app explicitly disallows backup |
| 3266 | final int flags = pkgInfo.applicationInfo.flags; |
| 3267 | if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) { |
| 3268 | // Verify signatures against any installed version; if they |
| 3269 | // don't match, then we fall though and ignore the data. The |
| 3270 | // signatureMatch() method explicitly ignores the signature |
| 3271 | // check for packages installed on the system partition, because |
| 3272 | // such packages are signed with the platform cert instead of |
| 3273 | // the app developer's cert, so they're different on every |
| 3274 | // device. |
| 3275 | if (signaturesMatch(sigs, pkgInfo)) { |
| 3276 | if (pkgInfo.versionCode >= version) { |
| 3277 | Slog.i(TAG, "Sig + version match; taking data"); |
| 3278 | policy = RestorePolicy.ACCEPT; |
| 3279 | } else { |
| 3280 | // The data is from a newer version of the app than |
| 3281 | // is presently installed. That means we can only |
| 3282 | // use it if the matching apk is also supplied. |
| 3283 | Slog.d(TAG, "Data version " + version |
| 3284 | + " is newer than installed version " |
| 3285 | + pkgInfo.versionCode + " - requiring apk"); |
| 3286 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 3287 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3288 | } else { |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3289 | Slog.w(TAG, "Restore manifest signatures do not match " |
| 3290 | + "installed application for " + info.packageName); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3291 | } |
| Christopher Tate | a858cb0 | 2011-06-03 12:27:51 -0700 | [diff] [blame] | 3292 | } else { |
| 3293 | if (DEBUG) Slog.i(TAG, "Restore manifest from " |
| 3294 | + info.packageName + " but allowBackup=false"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3295 | } |
| 3296 | } catch (NameNotFoundException e) { |
| 3297 | // Okay, the target app isn't installed. We can process |
| 3298 | // the restore properly only if the dataset provides the |
| 3299 | // apk file and we can successfully install it. |
| 3300 | if (DEBUG) Slog.i(TAG, "Package " + info.packageName |
| 3301 | + " not installed; requiring apk in dataset"); |
| 3302 | policy = RestorePolicy.ACCEPT_IF_APK; |
| 3303 | } |
| 3304 | |
| 3305 | if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) { |
| 3306 | Slog.i(TAG, "Cannot restore package " + info.packageName |
| 3307 | + " without the matching .apk"); |
| 3308 | } |
| 3309 | } else { |
| 3310 | Slog.i(TAG, "Missing signature on backed-up package " |
| 3311 | + info.packageName); |
| 3312 | } |
| 3313 | } else { |
| 3314 | Slog.i(TAG, "Expected package " + info.packageName |
| 3315 | + " but restore manifest claims " + manifestPackage); |
| 3316 | } |
| 3317 | } else { |
| 3318 | Slog.i(TAG, "Unknown restore manifest version " + version |
| 3319 | + " for package " + info.packageName); |
| 3320 | } |
| 3321 | } catch (NumberFormatException e) { |
| 3322 | Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName); |
| Kenny Root | 1137341 | 2011-07-28 15:13:33 -0700 | [diff] [blame] | 3323 | } catch (IllegalArgumentException e) { |
| 3324 | Slog.w(TAG, e.getMessage()); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | return policy; |
| 3328 | } |
| 3329 | |
| 3330 | // Builds a line from a byte buffer starting at 'offset', and returns |
| 3331 | // the index of the next unconsumed data in the buffer. |
| 3332 | int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException { |
| 3333 | final int end = buffer.length; |
| 3334 | if (offset >= end) throw new IOException("Incomplete data"); |
| 3335 | |
| 3336 | int pos; |
| 3337 | for (pos = offset; pos < end; pos++) { |
| 3338 | byte c = buffer[pos]; |
| 3339 | // at LF we declare end of line, and return the next char as the |
| 3340 | // starting point for the next time through |
| 3341 | if (c == '\n') { |
| 3342 | break; |
| 3343 | } |
| 3344 | } |
| 3345 | outStr[0] = new String(buffer, offset, pos - offset); |
| 3346 | pos++; // may be pointing an extra byte past the end but that's okay |
| 3347 | return pos; |
| 3348 | } |
| 3349 | |
| 3350 | void dumpFileMetadata(FileMetadata info) { |
| 3351 | if (DEBUG) { |
| 3352 | StringBuilder b = new StringBuilder(128); |
| 3353 | |
| 3354 | // mode string |
| Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 3355 | b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-'); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3356 | b.append(((info.mode & 0400) != 0) ? 'r' : '-'); |
| 3357 | b.append(((info.mode & 0200) != 0) ? 'w' : '-'); |
| 3358 | b.append(((info.mode & 0100) != 0) ? 'x' : '-'); |
| 3359 | b.append(((info.mode & 0040) != 0) ? 'r' : '-'); |
| 3360 | b.append(((info.mode & 0020) != 0) ? 'w' : '-'); |
| 3361 | b.append(((info.mode & 0010) != 0) ? 'x' : '-'); |
| 3362 | b.append(((info.mode & 0004) != 0) ? 'r' : '-'); |
| 3363 | b.append(((info.mode & 0002) != 0) ? 'w' : '-'); |
| 3364 | b.append(((info.mode & 0001) != 0) ? 'x' : '-'); |
| 3365 | b.append(String.format(" %9d ", info.size)); |
| 3366 | |
| 3367 | Date stamp = new Date(info.mtime); |
| 3368 | b.append(new SimpleDateFormat("MMM dd kk:mm:ss ").format(stamp)); |
| 3369 | |
| 3370 | b.append(info.packageName); |
| 3371 | b.append(" :: "); |
| 3372 | b.append(info.domain); |
| 3373 | b.append(" :: "); |
| 3374 | b.append(info.path); |
| 3375 | |
| 3376 | Slog.i(TAG, b.toString()); |
| 3377 | } |
| 3378 | } |
| 3379 | // Consume a tar file header block [sequence] and accumulate the relevant metadata |
| 3380 | FileMetadata readTarHeaders(InputStream instream) throws IOException { |
| 3381 | byte[] block = new byte[512]; |
| 3382 | FileMetadata info = null; |
| 3383 | |
| 3384 | boolean gotHeader = readTarHeader(instream, block); |
| 3385 | if (gotHeader) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3386 | try { |
| 3387 | // okay, presume we're okay, and extract the various metadata |
| 3388 | info = new FileMetadata(); |
| 3389 | info.size = extractRadix(block, 124, 12, 8); |
| 3390 | info.mtime = extractRadix(block, 136, 12, 8); |
| 3391 | info.mode = extractRadix(block, 100, 8, 8); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3392 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3393 | info.path = extractString(block, 345, 155); // prefix |
| 3394 | String path = extractString(block, 0, 100); |
| 3395 | if (path.length() > 0) { |
| 3396 | if (info.path.length() > 0) info.path += '/'; |
| 3397 | info.path += path; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3398 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3399 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3400 | // tar link indicator field: 1 byte at offset 156 in the header. |
| 3401 | int typeChar = block[156]; |
| 3402 | if (typeChar == 'x') { |
| 3403 | // pax extended header, so we need to read that |
| 3404 | gotHeader = readPaxExtendedHeader(instream, info); |
| 3405 | if (gotHeader) { |
| 3406 | // and after a pax extended header comes another real header -- read |
| 3407 | // that to find the real file type |
| 3408 | gotHeader = readTarHeader(instream, block); |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3409 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3410 | if (!gotHeader) throw new IOException("Bad or missing pax header"); |
| 3411 | |
| 3412 | typeChar = block[156]; |
| Christopher Tate | e9e78ec | 2011-06-08 20:09:31 -0700 | [diff] [blame] | 3413 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3414 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3415 | switch (typeChar) { |
| 3416 | case '0': info.type = BackupAgent.TYPE_FILE; break; |
| 3417 | case '5': { |
| 3418 | info.type = BackupAgent.TYPE_DIRECTORY; |
| 3419 | if (info.size != 0) { |
| 3420 | Slog.w(TAG, "Directory entry with nonzero size in header"); |
| 3421 | info.size = 0; |
| 3422 | } |
| 3423 | break; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3424 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3425 | case 0: { |
| 3426 | // presume EOF |
| 3427 | if (DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info); |
| 3428 | return null; |
| 3429 | } |
| 3430 | default: { |
| 3431 | Slog.e(TAG, "Unknown tar entity type: " + typeChar); |
| 3432 | throw new IOException("Unknown entity type " + typeChar); |
| 3433 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3434 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3435 | |
| 3436 | // Parse out the path |
| 3437 | // |
| 3438 | // first: apps/shared/unrecognized |
| 3439 | if (FullBackup.SHARED_PREFIX.regionMatches(0, |
| 3440 | info.path, 0, FullBackup.SHARED_PREFIX.length())) { |
| 3441 | // File in shared storage. !!! TODO: implement this. |
| 3442 | info.path = info.path.substring(FullBackup.SHARED_PREFIX.length()); |
| 3443 | info.packageName = "com.android.sharedstoragebackup"; |
| 3444 | info.domain = FullBackup.SHARED_STORAGE_TOKEN; |
| 3445 | if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path); |
| 3446 | } else if (FullBackup.APPS_PREFIX.regionMatches(0, |
| 3447 | info.path, 0, FullBackup.APPS_PREFIX.length())) { |
| 3448 | // App content! Parse out the package name and domain |
| 3449 | |
| 3450 | // strip the apps/ prefix |
| 3451 | info.path = info.path.substring(FullBackup.APPS_PREFIX.length()); |
| 3452 | |
| 3453 | // extract the package name |
| 3454 | int slash = info.path.indexOf('/'); |
| 3455 | if (slash < 0) throw new IOException("Illegal semantic path in " + info.path); |
| 3456 | info.packageName = info.path.substring(0, slash); |
| 3457 | info.path = info.path.substring(slash+1); |
| 3458 | |
| 3459 | // if it's a manifest we're done, otherwise parse out the domains |
| 3460 | if (!info.path.equals(BACKUP_MANIFEST_FILENAME)) { |
| 3461 | slash = info.path.indexOf('/'); |
| 3462 | if (slash < 0) throw new IOException("Illegal semantic path in non-manifest " + info.path); |
| 3463 | info.domain = info.path.substring(0, slash); |
| 3464 | // validate that it's one of the domains we understand |
| 3465 | if (!info.domain.equals(FullBackup.APK_TREE_TOKEN) |
| 3466 | && !info.domain.equals(FullBackup.DATA_TREE_TOKEN) |
| 3467 | && !info.domain.equals(FullBackup.DATABASE_TREE_TOKEN) |
| 3468 | && !info.domain.equals(FullBackup.ROOT_TREE_TOKEN) |
| 3469 | && !info.domain.equals(FullBackup.SHAREDPREFS_TREE_TOKEN) |
| 3470 | && !info.domain.equals(FullBackup.OBB_TREE_TOKEN) |
| 3471 | && !info.domain.equals(FullBackup.CACHE_TREE_TOKEN)) { |
| 3472 | throw new IOException("Unrecognized domain " + info.domain); |
| 3473 | } |
| 3474 | |
| 3475 | info.path = info.path.substring(slash + 1); |
| 3476 | } |
| 3477 | } |
| 3478 | } catch (IOException e) { |
| 3479 | if (DEBUG) { |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3480 | Slog.e(TAG, "Parse error in header: " + e.getMessage()); |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3481 | HEXLOG(block); |
| 3482 | } |
| 3483 | throw e; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3484 | } |
| 3485 | } |
| 3486 | return info; |
| 3487 | } |
| 3488 | |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3489 | private void HEXLOG(byte[] block) { |
| 3490 | int offset = 0; |
| 3491 | int todo = block.length; |
| 3492 | StringBuilder buf = new StringBuilder(64); |
| 3493 | while (todo > 0) { |
| 3494 | buf.append(String.format("%04x ", offset)); |
| 3495 | int numThisLine = (todo > 16) ? 16 : todo; |
| 3496 | for (int i = 0; i < numThisLine; i++) { |
| 3497 | buf.append(String.format("%02x ", block[offset+i])); |
| 3498 | } |
| 3499 | Slog.i("hexdump", buf.toString()); |
| 3500 | buf.setLength(0); |
| 3501 | todo -= numThisLine; |
| 3502 | offset += numThisLine; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3503 | } |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3506 | // Read exactly the given number of bytes into a buffer at the stated offset. |
| 3507 | // Returns false if EOF is encountered before the requested number of bytes |
| 3508 | // could be read. |
| 3509 | int readExactly(InputStream in, byte[] buffer, int offset, int size) |
| 3510 | throws IOException { |
| 3511 | if (size <= 0) throw new IllegalArgumentException("size must be > 0"); |
| 3512 | |
| 3513 | int soFar = 0; |
| 3514 | while (soFar < size) { |
| 3515 | int nRead = in.read(buffer, offset + soFar, size - soFar); |
| 3516 | if (nRead <= 0) { |
| 3517 | if (MORE_DEBUG) Slog.w(TAG, "- wanted exactly " + size + " but got only " + soFar); |
| 3518 | break; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3519 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3520 | soFar += nRead; |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3521 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3522 | return soFar; |
| 3523 | } |
| 3524 | |
| 3525 | boolean readTarHeader(InputStream instream, byte[] block) throws IOException { |
| 3526 | final int got = readExactly(instream, block, 0, 512); |
| 3527 | if (got == 0) return false; // Clean EOF |
| 3528 | if (got < 512) throw new IOException("Unable to read full block header"); |
| 3529 | mBytes += 512; |
| 3530 | return true; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | // overwrites 'info' fields based on the pax extended header |
| 3534 | boolean readPaxExtendedHeader(InputStream instream, FileMetadata info) |
| 3535 | throws IOException { |
| 3536 | // We should never see a pax extended header larger than this |
| 3537 | if (info.size > 32*1024) { |
| 3538 | Slog.w(TAG, "Suspiciously large pax header size " + info.size |
| 3539 | + " - aborting"); |
| 3540 | throw new IOException("Sanity failure: pax header size " + info.size); |
| 3541 | } |
| 3542 | |
| 3543 | // read whole blocks, not just the content size |
| 3544 | int numBlocks = (int)((info.size + 511) >> 9); |
| 3545 | byte[] data = new byte[numBlocks * 512]; |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3546 | if (readExactly(instream, data, 0, data.length) < data.length) { |
| 3547 | throw new IOException("Unable to read full pax header"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3548 | } |
| Christopher Tate | 6853fcf | 2011-08-10 17:52:21 -0700 | [diff] [blame] | 3549 | mBytes += data.length; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3550 | |
| 3551 | final int contentSize = (int) info.size; |
| 3552 | int offset = 0; |
| 3553 | do { |
| 3554 | // extract the line at 'offset' |
| 3555 | int eol = offset+1; |
| 3556 | while (eol < contentSize && data[eol] != ' ') eol++; |
| 3557 | if (eol >= contentSize) { |
| 3558 | // error: we just hit EOD looking for the end of the size field |
| 3559 | throw new IOException("Invalid pax data"); |
| 3560 | } |
| 3561 | // eol points to the space between the count and the key |
| 3562 | int linelen = (int) extractRadix(data, offset, eol - offset, 10); |
| 3563 | int key = eol + 1; // start of key=value |
| 3564 | eol = offset + linelen - 1; // trailing LF |
| 3565 | int value; |
| 3566 | for (value = key+1; data[value] != '=' && value <= eol; value++); |
| 3567 | if (value > eol) { |
| 3568 | throw new IOException("Invalid pax declaration"); |
| 3569 | } |
| 3570 | |
| 3571 | // pax requires that key/value strings be in UTF-8 |
| 3572 | String keyStr = new String(data, key, value-key, "UTF-8"); |
| 3573 | // -1 to strip the trailing LF |
| 3574 | String valStr = new String(data, value+1, eol-value-1, "UTF-8"); |
| 3575 | |
| 3576 | if ("path".equals(keyStr)) { |
| 3577 | info.path = valStr; |
| 3578 | } else if ("size".equals(keyStr)) { |
| 3579 | info.size = Long.parseLong(valStr); |
| 3580 | } else { |
| 3581 | if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key); |
| 3582 | } |
| 3583 | |
| 3584 | offset += linelen; |
| 3585 | } while (offset < contentSize); |
| 3586 | |
| 3587 | return true; |
| 3588 | } |
| 3589 | |
| 3590 | long extractRadix(byte[] data, int offset, int maxChars, int radix) |
| 3591 | throws IOException { |
| 3592 | long value = 0; |
| 3593 | final int end = offset + maxChars; |
| 3594 | for (int i = offset; i < end; i++) { |
| 3595 | final byte b = data[i]; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 3596 | // Numeric fields in tar can terminate with either NUL or SPC |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3597 | if (b == 0 || b == ' ') break; |
| 3598 | if (b < '0' || b > ('0' + radix - 1)) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 3599 | throw new IOException("Invalid number in header: '" + (char)b + "' for radix " + radix); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3600 | } |
| 3601 | value = radix * value + (b - '0'); |
| 3602 | } |
| 3603 | return value; |
| 3604 | } |
| 3605 | |
| 3606 | String extractString(byte[] data, int offset, int maxChars) throws IOException { |
| 3607 | final int end = offset + maxChars; |
| 3608 | int eos = offset; |
| Christopher Tate | 3f6c77b | 2011-06-07 13:17:17 -0700 | [diff] [blame] | 3609 | // tar string fields terminate early with a NUL |
| 3610 | while (eos < end && data[eos] != 0) eos++; |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3611 | return new String(data, offset, eos-offset, "US-ASCII"); |
| 3612 | } |
| 3613 | |
| 3614 | void sendStartRestore() { |
| 3615 | if (mObserver != null) { |
| 3616 | try { |
| 3617 | mObserver.onStartRestore(); |
| 3618 | } catch (RemoteException e) { |
| 3619 | Slog.w(TAG, "full restore observer went away: startRestore"); |
| 3620 | mObserver = null; |
| 3621 | } |
| 3622 | } |
| 3623 | } |
| 3624 | |
| 3625 | void sendOnRestorePackage(String name) { |
| 3626 | if (mObserver != null) { |
| 3627 | try { |
| 3628 | // TODO: use a more user-friendly name string |
| 3629 | mObserver.onRestorePackage(name); |
| 3630 | } catch (RemoteException e) { |
| 3631 | Slog.w(TAG, "full restore observer went away: restorePackage"); |
| 3632 | mObserver = null; |
| 3633 | } |
| 3634 | } |
| 3635 | } |
| 3636 | |
| 3637 | void sendEndRestore() { |
| 3638 | if (mObserver != null) { |
| 3639 | try { |
| 3640 | mObserver.onEndRestore(); |
| 3641 | } catch (RemoteException e) { |
| 3642 | Slog.w(TAG, "full restore observer went away: endRestore"); |
| 3643 | mObserver = null; |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | } |
| 3648 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3649 | // ----- Restore handling ----- |
| 3650 | |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3651 | private boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) { |
| 3652 | // If the target resides on the system partition, we allow it to restore |
| 3653 | // data from the like-named package in a restore set even if the signatures |
| 3654 | // do not match. (Unlike general applications, those flashed to the system |
| 3655 | // partition will be signed with the device's platform certificate, so on |
| 3656 | // different phones the same system app will have different signatures.) |
| 3657 | if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3658 | if (DEBUG) Slog.v(TAG, "System app " + target.packageName + " - skipping sig check"); |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3659 | return true; |
| 3660 | } |
| 3661 | |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 3662 | // Allow unsigned apps, but not signed on one device and unsigned on the other |
| 3663 | // !!! TODO: is this the right policy? |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3664 | Signature[] deviceSigs = target.signatures; |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 3665 | if (MORE_DEBUG) Slog.v(TAG, "signaturesMatch(): stored=" + storedSigs |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 3666 | + " device=" + deviceSigs); |
| Christopher Tate | 20efdf6b | 2009-06-18 19:41:36 -0700 | [diff] [blame] | 3667 | if ((storedSigs == null || storedSigs.length == 0) |
| 3668 | && (deviceSigs == null || deviceSigs.length == 0)) { |
| 3669 | return true; |
| 3670 | } |
| 3671 | if (storedSigs == null || deviceSigs == null) { |
| 3672 | return false; |
| 3673 | } |
| 3674 | |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 3675 | // !!! TODO: this demands that every stored signature match one |
| 3676 | // that is present on device, and does not demand the converse. |
| 3677 | // Is this this right policy? |
| 3678 | int nStored = storedSigs.length; |
| 3679 | int nDevice = deviceSigs.length; |
| 3680 | |
| 3681 | for (int i=0; i < nStored; i++) { |
| 3682 | boolean match = false; |
| 3683 | for (int j=0; j < nDevice; j++) { |
| 3684 | if (storedSigs[i].equals(deviceSigs[j])) { |
| 3685 | match = true; |
| 3686 | break; |
| 3687 | } |
| 3688 | } |
| 3689 | if (!match) { |
| 3690 | return false; |
| 3691 | } |
| 3692 | } |
| 3693 | return true; |
| 3694 | } |
| 3695 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3696 | class PerformRestoreTask implements Runnable { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3697 | private IBackupTransport mTransport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3698 | private IRestoreObserver mObserver; |
| Dan Egnor | 156411d | 2009-06-26 13:20:02 -0700 | [diff] [blame] | 3699 | private long mToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3700 | private PackageInfo mTargetPackage; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 3701 | private File mStateDir; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3702 | private int mPmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3703 | private boolean mNeedFullBackup; |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3704 | private HashSet<String> mFilterSet; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3705 | |
| Christopher Tate | 5cbbf56 | 2009-06-22 16:44:51 -0700 | [diff] [blame] | 3706 | class RestoreRequest { |
| 3707 | public PackageInfo app; |
| 3708 | public int storedAppVersion; |
| 3709 | |
| 3710 | RestoreRequest(PackageInfo _app, int _version) { |
| 3711 | app = _app; |
| 3712 | storedAppVersion = _version; |
| 3713 | } |
| 3714 | } |
| 3715 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 3716 | PerformRestoreTask(IBackupTransport transport, IRestoreObserver observer, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3717 | long restoreSetToken, PackageInfo targetPackage, int pmToken, |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3718 | boolean needFullBackup, String[] filterSet) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3719 | mTransport = transport; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3720 | mObserver = observer; |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 3721 | mToken = restoreSetToken; |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3722 | mTargetPackage = targetPackage; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3723 | mPmToken = pmToken; |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3724 | mNeedFullBackup = needFullBackup; |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 3725 | |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3726 | if (filterSet != null) { |
| 3727 | mFilterSet = new HashSet<String>(); |
| 3728 | for (String pkg : filterSet) { |
| 3729 | mFilterSet.add(pkg); |
| 3730 | } |
| 3731 | } else { |
| 3732 | mFilterSet = null; |
| 3733 | } |
| 3734 | |
| Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 3735 | try { |
| 3736 | mStateDir = new File(mBaseStateDir, transport.transportDirName()); |
| 3737 | } catch (RemoteException e) { |
| 3738 | // can't happen; the transport is local |
| 3739 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3740 | } |
| 3741 | |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3742 | public void run() { |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3743 | long startRealtime = SystemClock.elapsedRealtime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3744 | if (DEBUG) Slog.v(TAG, "Beginning restore process mTransport=" + mTransport |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3745 | + " mObserver=" + mObserver + " mToken=" + Long.toHexString(mToken) |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3746 | + " mTargetPackage=" + mTargetPackage + " mFilterSet=" + mFilterSet |
| 3747 | + " mPmToken=" + mPmToken); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3748 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 3749 | PackageManagerBackupAgent pmAgent = null; |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3750 | int error = -1; // assume error |
| 3751 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3752 | // build the set of apps to restore |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3753 | try { |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3754 | // TODO: Log this before getAvailableRestoreSets, somehow |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3755 | EventLog.writeEvent(EventLogTags.RESTORE_START, mTransport.transportDirName(), mToken); |
| Christopher Tate | abce4e8 | 2009-06-18 18:35:32 -0700 | [diff] [blame] | 3756 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3757 | // Get the list of all packages which have backup enabled. |
| 3758 | // (Include the Package Manager metadata pseudo-package first.) |
| 3759 | ArrayList<PackageInfo> restorePackages = new ArrayList<PackageInfo>(); |
| 3760 | PackageInfo omPackage = new PackageInfo(); |
| 3761 | omPackage.packageName = PACKAGE_MANAGER_SENTINEL; |
| 3762 | restorePackages.add(omPackage); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3763 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3764 | List<PackageInfo> agentPackages = allAgentPackages(); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3765 | if (mTargetPackage == null) { |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 3766 | // if there's a filter set, strip out anything that isn't |
| 3767 | // present before proceeding |
| 3768 | if (mFilterSet != null) { |
| 3769 | for (int i = agentPackages.size() - 1; i >= 0; i--) { |
| 3770 | final PackageInfo pkg = agentPackages.get(i); |
| 3771 | if (! mFilterSet.contains(pkg.packageName)) { |
| 3772 | agentPackages.remove(i); |
| 3773 | } |
| 3774 | } |
| 3775 | if (DEBUG) { |
| 3776 | Slog.i(TAG, "Post-filter package set for restore:"); |
| 3777 | for (PackageInfo p : agentPackages) { |
| 3778 | Slog.i(TAG, " " + p); |
| 3779 | } |
| 3780 | } |
| 3781 | } |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3782 | restorePackages.addAll(agentPackages); |
| 3783 | } else { |
| 3784 | // Just one package to attempt restore of |
| 3785 | restorePackages.add(mTargetPackage); |
| 3786 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3787 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3788 | // let the observer know that we're running |
| 3789 | if (mObserver != null) { |
| 3790 | try { |
| 3791 | // !!! TODO: get an actual count from the transport after |
| 3792 | // its startRestore() runs? |
| 3793 | mObserver.restoreStarting(restorePackages.size()); |
| 3794 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3795 | Slog.d(TAG, "Restore observer died at restoreStarting"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3796 | mObserver = null; |
| 3797 | } |
| 3798 | } |
| 3799 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 3800 | if (mTransport.startRestore(mToken, restorePackages.toArray(new PackageInfo[0])) != |
| 3801 | BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3802 | Slog.e(TAG, "Error starting restore operation"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3803 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3804 | return; |
| 3805 | } |
| 3806 | |
| 3807 | String packageName = mTransport.nextRestorePackage(); |
| 3808 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3809 | Slog.e(TAG, "Error getting first restore package"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3810 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3811 | return; |
| 3812 | } else if (packageName.equals("")) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3813 | Slog.i(TAG, "No restore data available"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3814 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3815 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, 0, millis); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3816 | return; |
| 3817 | } else if (!packageName.equals(PACKAGE_MANAGER_SENTINEL)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3818 | Slog.e(TAG, "Expected restore data for \"" + PACKAGE_MANAGER_SENTINEL |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3819 | + "\", found only \"" + packageName + "\""); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3820 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3821 | "Package manager data missing"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3822 | return; |
| 3823 | } |
| 3824 | |
| 3825 | // Pull the Package Manager metadata from the restore set first |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 3826 | pmAgent = new PackageManagerBackupAgent( |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3827 | mPackageManager, agentPackages); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3828 | processOneRestore(omPackage, 0, IBackupAgent.Stub.asInterface(pmAgent.onBind()), |
| 3829 | mNeedFullBackup); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3830 | |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 3831 | // Verify that the backup set includes metadata. If not, we can't do |
| 3832 | // signature/version verification etc, so we simply do not proceed with |
| 3833 | // the restore operation. |
| Christopher Tate | 3d7cd13 | 2009-07-07 14:23:07 -0700 | [diff] [blame] | 3834 | if (!pmAgent.hasMetadata()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3835 | Slog.e(TAG, "No restore metadata available, so not restoring settings"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3836 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, PACKAGE_MANAGER_SENTINEL, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3837 | "Package manager restore metadata missing"); |
| Christopher Tate | 8c03247 | 2009-07-02 14:28:47 -0700 | [diff] [blame] | 3838 | return; |
| 3839 | } |
| 3840 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3841 | int count = 0; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3842 | for (;;) { |
| 3843 | packageName = mTransport.nextRestorePackage(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3844 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3845 | if (packageName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3846 | Slog.e(TAG, "Error getting next restore package"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3847 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3848 | return; |
| 3849 | } else if (packageName.equals("")) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3850 | if (DEBUG) Slog.v(TAG, "No next package, finishing restore"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3851 | break; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3852 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3853 | |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3854 | if (mObserver != null) { |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3855 | try { |
| Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 3856 | mObserver.onUpdate(count, packageName); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3857 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3858 | Slog.d(TAG, "Restore observer died in onUpdate"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3859 | mObserver = null; |
| 3860 | } |
| 3861 | } |
| 3862 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3863 | Metadata metaInfo = pmAgent.getRestoredMetadata(packageName); |
| 3864 | if (metaInfo == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3865 | Slog.e(TAG, "Missing metadata for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3866 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3867 | "Package metadata missing"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3868 | continue; |
| 3869 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3870 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3871 | PackageInfo packageInfo; |
| 3872 | try { |
| 3873 | int flags = PackageManager.GET_SIGNATURES; |
| 3874 | packageInfo = mPackageManager.getPackageInfo(packageName, flags); |
| 3875 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3876 | Slog.e(TAG, "Invalid package restoring data", e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3877 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3878 | "Package missing on device"); |
| 3879 | continue; |
| 3880 | } |
| 3881 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3882 | if (metaInfo.versionCode > packageInfo.versionCode) { |
| Christopher Tate | 3dda518 | 2010-02-24 16:06:18 -0800 | [diff] [blame] | 3883 | // Data is from a "newer" version of the app than we have currently |
| 3884 | // installed. If the app has not declared that it is prepared to |
| 3885 | // handle this case, we do not attempt the restore. |
| 3886 | if ((packageInfo.applicationInfo.flags |
| 3887 | & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) { |
| 3888 | String message = "Version " + metaInfo.versionCode |
| 3889 | + " > installed version " + packageInfo.versionCode; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3890 | Slog.w(TAG, "Package " + packageName + ": " + message); |
| Christopher Tate | 3dda518 | 2010-02-24 16:06:18 -0800 | [diff] [blame] | 3891 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, |
| 3892 | packageName, message); |
| 3893 | continue; |
| 3894 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3895 | if (DEBUG) Slog.v(TAG, "Version " + metaInfo.versionCode |
| Christopher Tate | 3dda518 | 2010-02-24 16:06:18 -0800 | [diff] [blame] | 3896 | + " > installed " + packageInfo.versionCode |
| 3897 | + " but restoreAnyVersion"); |
| 3898 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3899 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3900 | |
| Christopher Tate | 78dd4a7 | 2009-11-04 11:49:08 -0800 | [diff] [blame] | 3901 | if (!signaturesMatch(metaInfo.signatures, packageInfo)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3902 | Slog.w(TAG, "Signature mismatch restoring " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3903 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3904 | "Signature mismatch"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3905 | continue; |
| 3906 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3907 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3908 | if (DEBUG) Slog.v(TAG, "Package " + packageName |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3909 | + " restore version [" + metaInfo.versionCode |
| 3910 | + "] is compatible with installed version [" |
| 3911 | + packageInfo.versionCode + "]"); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 3912 | |
| Christopher Tate | 3de55bc | 2010-03-12 17:28:08 -0800 | [diff] [blame] | 3913 | // Then set up and bind the agent |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3914 | IBackupAgent agent = bindToAgentSynchronous( |
| 3915 | packageInfo.applicationInfo, |
| Christopher Tate | 3de55bc | 2010-03-12 17:28:08 -0800 | [diff] [blame] | 3916 | IApplicationThread.BACKUP_MODE_INCREMENTAL); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3917 | if (agent == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3918 | Slog.w(TAG, "Can't find backup agent for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3919 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3920 | "Restore agent missing"); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3921 | continue; |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3922 | } |
| 3923 | |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3924 | // And then finally run the restore on this agent |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3925 | try { |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 3926 | processOneRestore(packageInfo, metaInfo.versionCode, agent, |
| 3927 | mNeedFullBackup); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3928 | ++count; |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3929 | } finally { |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3930 | // unbind and tidy up even on timeout or failure, just in case |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3931 | mActivityManager.unbindBackupAgent(packageInfo.applicationInfo); |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3932 | |
| 3933 | // The agent was probably running with a stub Application object, |
| 3934 | // which isn't a valid run mode for the main app logic. Shut |
| 3935 | // down the app so that next time it's launched, it gets the |
| Christopher Tate | 3dda518 | 2010-02-24 16:06:18 -0800 | [diff] [blame] | 3936 | // usual full initialization. Note that this is only done for |
| 3937 | // full-system restores: when a single app has requested a restore, |
| 3938 | // it is explicitly not killed following that operation. |
| 3939 | if (mTargetPackage == null && (packageInfo.applicationInfo.flags |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3940 | & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3941 | if (DEBUG) Slog.d(TAG, "Restore complete, killing host process of " |
| Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3942 | + packageInfo.applicationInfo.processName); |
| 3943 | mActivityManager.killApplicationProcess( |
| 3944 | packageInfo.applicationInfo.processName, |
| 3945 | packageInfo.applicationInfo.uid); |
| 3946 | } |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3947 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3948 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3949 | |
| 3950 | // if we get this far, report success to the observer |
| 3951 | error = 0; |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3952 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3953 | EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, count, millis); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3954 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3955 | Slog.e(TAG, "Error in restore thread", e); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3956 | } finally { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3957 | if (DEBUG) Slog.d(TAG, "finishing restore mObserver=" + mObserver); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 3958 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3959 | try { |
| 3960 | mTransport.finishRestore(); |
| 3961 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3962 | Slog.e(TAG, "Error finishing restore", e); |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 3963 | } |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3964 | |
| 3965 | if (mObserver != null) { |
| 3966 | try { |
| 3967 | mObserver.restoreFinished(error); |
| 3968 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3969 | Slog.d(TAG, "Restore observer died at restoreFinished"); |
| Christopher Tate | 7d562ec | 2009-06-25 18:03:43 -0700 | [diff] [blame] | 3970 | } |
| 3971 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3972 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3973 | // If this was a restoreAll operation, record that this was our |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 3974 | // ancestral dataset, as well as the set of apps that are possibly |
| 3975 | // restoreable from the dataset |
| 3976 | if (mTargetPackage == null && pmAgent != null) { |
| 3977 | mAncestralPackages = pmAgent.getRestoredPackages(); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 3978 | mAncestralToken = mToken; |
| 3979 | writeRestoreTokens(); |
| 3980 | } |
| 3981 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3982 | // We must under all circumstances tell the Package Manager to |
| 3983 | // proceed with install notifications if it's waiting for us. |
| 3984 | if (mPmToken > 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3985 | if (DEBUG) Slog.v(TAG, "finishing PM token " + mPmToken); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 3986 | try { |
| 3987 | mPackageManagerBinder.finishPackageInstall(mPmToken); |
| 3988 | } catch (RemoteException e) { /* can't happen */ } |
| 3989 | } |
| 3990 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 3991 | // Furthermore we need to reset the session timeout clock |
| 3992 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 3993 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, |
| 3994 | TIMEOUT_RESTORE_INTERVAL); |
| 3995 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 3996 | // done; we can finally release the wakelock |
| 3997 | mWakelock.release(); |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | |
| Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 4001 | // Do the guts of a restore of one application, using mTransport.getRestoreData(). |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4002 | void processOneRestore(PackageInfo app, int appVersionCode, IBackupAgent agent, |
| 4003 | boolean needFullBackup) { |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4004 | // !!! TODO: actually run the restore through mTransport |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4005 | final String packageName = app.packageName; |
| 4006 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4007 | if (DEBUG) Slog.d(TAG, "processOneRestore packageName=" + packageName); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 4008 | |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4009 | // !!! TODO: get the dirs from the transport |
| 4010 | File backupDataName = new File(mDataDir, packageName + ".restore"); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4011 | File newStateName = new File(mStateDir, packageName + ".new"); |
| 4012 | File savedStateName = new File(mStateDir, packageName); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4013 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4014 | ParcelFileDescriptor backupData = null; |
| 4015 | ParcelFileDescriptor newState = null; |
| 4016 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4017 | final int token = generateToken(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4018 | try { |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4019 | // Run the transport's restore pass |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4020 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 4021 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 4022 | ParcelFileDescriptor.MODE_CREATE | |
| 4023 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 4024 | |
| Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 4025 | if (mTransport.getRestoreData(backupData) != BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4026 | Slog.e(TAG, "Error getting restore data for " + packageName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4027 | EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4028 | return; |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | // Okay, we have the data. Now have the agent do the restore. |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4032 | backupData.close(); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4033 | backupData = ParcelFileDescriptor.open(backupDataName, |
| 4034 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 4035 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4036 | newState = ParcelFileDescriptor.open(newStateName, |
| 4037 | ParcelFileDescriptor.MODE_READ_WRITE | |
| 4038 | ParcelFileDescriptor.MODE_CREATE | |
| 4039 | ParcelFileDescriptor.MODE_TRUNCATE); |
| 4040 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4041 | // Kick off the restore, checking for hung agents |
| 4042 | prepareOperationTimeout(token, TIMEOUT_RESTORE_INTERVAL); |
| 4043 | agent.doRestore(backupData, appVersionCode, newState, token, mBackupManagerBinder); |
| 4044 | boolean success = waitUntilOperationComplete(token); |
| 4045 | |
| 4046 | if (!success) { |
| 4047 | throw new RuntimeException("restore timeout"); |
| 4048 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4049 | |
| 4050 | // if everything went okay, remember the recorded state now |
| Christopher Tate | 90967f4 | 2009-09-20 15:28:33 -0700 | [diff] [blame] | 4051 | // |
| 4052 | // !!! TODO: the restored data should be migrated on the server |
| 4053 | // side into the current dataset. In that case the new state file |
| 4054 | // we just created would reflect the data already extant in the |
| 4055 | // backend, so there'd be nothing more to do. Until that happens, |
| 4056 | // however, we need to make sure that we record the data to the |
| 4057 | // current backend dataset. (Yes, this means shipping the data over |
| 4058 | // the wire in both directions. That's bad, but consistency comes |
| 4059 | // first, then efficiency.) Once we introduce server-side data |
| 4060 | // migration to the newly-restored device's dataset, we will change |
| 4061 | // the following from a discard of the newly-written state to the |
| 4062 | // "correct" operation of renaming into the canonical state blob. |
| 4063 | newStateName.delete(); // TODO: remove; see above comment |
| 4064 | //newStateName.renameTo(savedStateName); // TODO: replace with this |
| 4065 | |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4066 | int size = (int) backupDataName.length(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4067 | EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE, packageName, size); |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4068 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4069 | Slog.e(TAG, "Error restoring data for " + packageName, e); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4070 | EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName, e.toString()); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4071 | |
| Christopher Tate | 9673304 | 2009-07-20 14:49:13 -0700 | [diff] [blame] | 4072 | // If the agent fails restore, it might have put the app's data |
| 4073 | // into an incoherent state. For consistency we wipe its data |
| 4074 | // again in this case before propagating the exception |
| Christopher Tate | 9673304 | 2009-07-20 14:49:13 -0700 | [diff] [blame] | 4075 | clearApplicationDataSynchronous(packageName); |
| Christopher Tate | 1531dc8 | 2009-07-24 16:37:43 -0700 | [diff] [blame] | 4076 | } finally { |
| 4077 | backupDataName.delete(); |
| Dan Egnor | bb9001c | 2009-07-27 12:20:13 -0700 | [diff] [blame] | 4078 | try { if (backupData != null) backupData.close(); } catch (IOException e) {} |
| 4079 | try { if (newState != null) newState.close(); } catch (IOException e) {} |
| 4080 | backupData = newState = null; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4081 | synchronized (mCurrentOperations) { |
| 4082 | mCurrentOperations.delete(token); |
| 4083 | } |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4084 | |
| 4085 | // If we know a priori that we'll need to perform a full post-restore backup |
| 4086 | // pass, clear the new state file data. This means we're discarding work that |
| 4087 | // was just done by the app's agent, but this way the agent doesn't need to |
| 4088 | // take any special action based on global device state. |
| 4089 | if (needFullBackup) { |
| 4090 | newStateName.delete(); |
| 4091 | } |
| Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 4092 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4093 | } |
| 4094 | } |
| 4095 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4096 | class PerformClearTask implements Runnable { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4097 | IBackupTransport mTransport; |
| 4098 | PackageInfo mPackage; |
| 4099 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4100 | PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) { |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4101 | mTransport = transport; |
| 4102 | mPackage = packageInfo; |
| 4103 | } |
| 4104 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4105 | public void run() { |
| 4106 | try { |
| 4107 | // Clear the on-device backup state to ensure a full backup next time |
| 4108 | File stateDir = new File(mBaseStateDir, mTransport.transportDirName()); |
| 4109 | File stateFile = new File(stateDir, mPackage.packageName); |
| 4110 | stateFile.delete(); |
| 4111 | |
| 4112 | // Tell the transport to remove all the persistent storage for the app |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 4113 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4114 | mTransport.clearBackupData(mPackage); |
| 4115 | } catch (RemoteException e) { |
| 4116 | // can't happen; the transport is local |
| 4117 | } finally { |
| 4118 | try { |
| Christopher Tate | 13f4a64 | 2009-09-30 20:06:45 -0700 | [diff] [blame] | 4119 | // TODO - need to handle failures |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4120 | mTransport.finishBackup(); |
| 4121 | } catch (RemoteException e) { |
| 4122 | // can't happen; the transport is local |
| 4123 | } |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4124 | |
| 4125 | // Last but not least, release the cpu |
| 4126 | mWakelock.release(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4127 | } |
| 4128 | } |
| 4129 | } |
| 4130 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4131 | class PerformInitializeTask implements Runnable { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4132 | HashSet<String> mQueue; |
| 4133 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4134 | PerformInitializeTask(HashSet<String> transportNames) { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4135 | mQueue = transportNames; |
| 4136 | } |
| 4137 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4138 | public void run() { |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4139 | try { |
| 4140 | for (String transportName : mQueue) { |
| 4141 | IBackupTransport transport = getTransport(transportName); |
| 4142 | if (transport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4143 | Slog.e(TAG, "Requested init for " + transportName + " but not found"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4144 | continue; |
| 4145 | } |
| 4146 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4147 | Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4148 | EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName()); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4149 | long startRealtime = SystemClock.elapsedRealtime(); |
| 4150 | int status = transport.initializeDevice(); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4151 | |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4152 | if (status == BackupConstants.TRANSPORT_OK) { |
| 4153 | status = transport.finishBackup(); |
| 4154 | } |
| 4155 | |
| 4156 | // Okay, the wipe really happened. Clean up our local bookkeeping. |
| 4157 | if (status == BackupConstants.TRANSPORT_OK) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4158 | Slog.i(TAG, "Device init successful"); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4159 | int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4160 | EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4161 | resetBackupState(new File(mBaseStateDir, transport.transportDirName())); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4162 | EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4163 | synchronized (mQueueLock) { |
| 4164 | recordInitPendingLocked(false, transportName); |
| 4165 | } |
| Dan Egnor | 726247c | 2009-09-29 19:12:31 -0700 | [diff] [blame] | 4166 | } else { |
| 4167 | // If this didn't work, requeue this one and try again |
| 4168 | // after a suitable interval |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4169 | Slog.e(TAG, "Transport error in initializeDevice()"); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4170 | EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4171 | synchronized (mQueueLock) { |
| 4172 | recordInitPendingLocked(true, transportName); |
| 4173 | } |
| 4174 | // do this via another alarm to make sure of the wakelock states |
| 4175 | long delay = transport.requestBackupTime(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4176 | if (DEBUG) Slog.w(TAG, "init failed on " |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4177 | + transportName + " resched in " + delay); |
| 4178 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, |
| 4179 | System.currentTimeMillis() + delay, mRunInitIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4180 | } |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4181 | } |
| 4182 | } catch (RemoteException e) { |
| 4183 | // can't happen; the transports are local |
| 4184 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4185 | Slog.e(TAG, "Unexpected error performing init", e); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4186 | } finally { |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 4187 | // Done; release the wakelock |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4188 | mWakelock.release(); |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4193 | private void dataChangedImpl(String packageName) { |
| 4194 | HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 4195 | dataChangedImpl(packageName, targets); |
| 4196 | } |
| Christopher Tate | df01dea | 2009-06-09 20:45:02 -0700 | [diff] [blame] | 4197 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4198 | private void dataChangedImpl(String packageName, HashSet<ApplicationInfo> targets) { |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 4199 | // Record that we need a backup pass for the caller. Since multiple callers |
| 4200 | // may share a uid, we need to note all candidates within that uid and schedule |
| 4201 | // a backup pass for each of them. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4202 | EventLog.writeEvent(EventLogTags.BACKUP_DATA_CHANGED, packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4203 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4204 | if (targets == null) { |
| 4205 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 4206 | + " uid=" + Binder.getCallingUid()); |
| 4207 | return; |
| 4208 | } |
| 4209 | |
| 4210 | synchronized (mQueueLock) { |
| 4211 | // Note that this client has made data changes that need to be backed up |
| 4212 | for (ApplicationInfo app : targets) { |
| 4213 | // validate the caller-supplied package name against the known set of |
| 4214 | // packages associated with this uid |
| 4215 | if (app.packageName.equals(packageName)) { |
| 4216 | // Add the caller to the set of pending backups. If there is |
| 4217 | // one already there, then overwrite it, but no harm done. |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 4218 | BackupRequest req = new BackupRequest(packageName); |
| Christopher Tate | c28083a | 2010-12-14 16:16:44 -0800 | [diff] [blame] | 4219 | if (mPendingBackups.put(app.packageName, req) == null) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4220 | // Journal this request in case of crash. The put() |
| 4221 | // operation returned null when this package was not already |
| 4222 | // in the set; we want to avoid touching the disk redundantly. |
| 4223 | writeToJournalLocked(packageName); |
| 4224 | |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4225 | if (MORE_DEBUG) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4226 | int numKeys = mPendingBackups.size(); |
| 4227 | Slog.d(TAG, "Now awaiting backup for " + numKeys + " participants:"); |
| 4228 | for (BackupRequest b : mPendingBackups.values()) { |
| Christopher Tate | cc55f81 | 2011-08-16 16:06:53 -0700 | [diff] [blame] | 4229 | Slog.d(TAG, " + " + b); |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4230 | } |
| 4231 | } |
| 4232 | } |
| 4233 | } |
| 4234 | } |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | // Note: packageName is currently unused, but may be in the future |
| 4239 | private HashSet<ApplicationInfo> dataChangedTargets(String packageName) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4240 | // If the caller does not hold the BACKUP permission, it can only request a |
| 4241 | // backup of its own data. |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 4242 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4243 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4244 | synchronized (mBackupParticipants) { |
| 4245 | return mBackupParticipants.get(Binder.getCallingUid()); |
| 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | // a caller with full permission can ask to back up any participating app |
| 4250 | // !!! TODO: allow backup of ANY app? |
| 4251 | HashSet<ApplicationInfo> targets = new HashSet<ApplicationInfo>(); |
| 4252 | synchronized (mBackupParticipants) { |
| Christopher Tate | 63d2700 | 2009-06-16 17:16:42 -0700 | [diff] [blame] | 4253 | int N = mBackupParticipants.size(); |
| 4254 | for (int i = 0; i < N; i++) { |
| 4255 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 4256 | if (s != null) { |
| 4257 | targets.addAll(s); |
| 4258 | } |
| 4259 | } |
| 4260 | } |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4261 | return targets; |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 4262 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4263 | |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 4264 | private void writeToJournalLocked(String str) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4265 | RandomAccessFile out = null; |
| 4266 | try { |
| 4267 | if (mJournal == null) mJournal = File.createTempFile("journal", null, mJournalDir); |
| 4268 | out = new RandomAccessFile(mJournal, "rws"); |
| 4269 | out.seek(out.length()); |
| 4270 | out.writeUTF(str); |
| 4271 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4272 | Slog.e(TAG, "Can't write " + str + " to backup journal", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4273 | mJournal = null; |
| 4274 | } finally { |
| 4275 | try { if (out != null) out.close(); } catch (IOException e) {} |
| Christopher Tate | cde87f4 | 2009-06-12 12:55:53 -0700 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | |
| Brad Fitzpatrick | 3dd4233 | 2010-09-07 23:40:30 -0700 | [diff] [blame] | 4279 | // ----- IBackupManager binder interface ----- |
| 4280 | |
| 4281 | public void dataChanged(final String packageName) { |
| 4282 | final HashSet<ApplicationInfo> targets = dataChangedTargets(packageName); |
| 4283 | if (targets == null) { |
| 4284 | Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'" |
| 4285 | + " uid=" + Binder.getCallingUid()); |
| 4286 | return; |
| 4287 | } |
| 4288 | |
| 4289 | mBackupHandler.post(new Runnable() { |
| 4290 | public void run() { |
| 4291 | dataChangedImpl(packageName, targets); |
| 4292 | } |
| 4293 | }); |
| 4294 | } |
| 4295 | |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4296 | // Clear the given package's backup data from the current transport |
| 4297 | public void clearBackupData(String packageName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4298 | if (DEBUG) Slog.v(TAG, "clearBackupData() of " + packageName); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4299 | PackageInfo info; |
| 4300 | try { |
| 4301 | info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 4302 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4303 | Slog.d(TAG, "No such package '" + packageName + "' - not clearing backup data"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4304 | return; |
| 4305 | } |
| 4306 | |
| 4307 | // If the caller does not hold the BACKUP permission, it can only request a |
| 4308 | // wipe of its own backed-up data. |
| 4309 | HashSet<ApplicationInfo> apps; |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 4310 | if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(), |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4311 | Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) { |
| 4312 | apps = mBackupParticipants.get(Binder.getCallingUid()); |
| 4313 | } else { |
| 4314 | // a caller with full permission can ask to back up any participating app |
| 4315 | // !!! TODO: allow data-clear of ANY app? |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4316 | if (DEBUG) Slog.v(TAG, "Privileged caller, allowing clear of other apps"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4317 | apps = new HashSet<ApplicationInfo>(); |
| 4318 | int N = mBackupParticipants.size(); |
| 4319 | for (int i = 0; i < N; i++) { |
| 4320 | HashSet<ApplicationInfo> s = mBackupParticipants.valueAt(i); |
| 4321 | if (s != null) { |
| 4322 | apps.addAll(s); |
| 4323 | } |
| 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | // now find the given package in the set of candidate apps |
| 4328 | for (ApplicationInfo app : apps) { |
| 4329 | if (app.packageName.equals(packageName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4330 | if (DEBUG) Slog.v(TAG, "Found the app - running clear process"); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4331 | // found it; fire off the clear request |
| 4332 | synchronized (mQueueLock) { |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 4333 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4334 | mWakelock.acquire(); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4335 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR, |
| 4336 | new ClearParams(getTransport(mCurrentTransport), info)); |
| 4337 | mBackupHandler.sendMessage(msg); |
| Christopher Tate | aa93b04 | 2009-08-05 18:21:40 -0700 | [diff] [blame] | 4338 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 4339 | } |
| 4340 | break; |
| 4341 | } |
| 4342 | } |
| 4343 | } |
| 4344 | |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 4345 | // Run a backup pass immediately for any applications that have declared |
| 4346 | // that they have pending updates. |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 4347 | public void backupNow() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4348 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4349 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4350 | if (DEBUG) Slog.v(TAG, "Scheduling immediate backup pass"); |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4351 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4352 | // Because the alarms we are using can jitter, and we want an *immediate* |
| 4353 | // backup pass to happen, we restart the timer beginning with "next time," |
| 4354 | // then manually fire the backup trigger intent ourselves. |
| 4355 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4356 | try { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4357 | mRunBackupIntent.send(); |
| 4358 | } catch (PendingIntent.CanceledException e) { |
| 4359 | // should never happen |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4360 | Slog.e(TAG, "run-backup intent cancelled!"); |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4361 | } |
| Christopher Tate | 4675812 | 2009-05-06 11:22:00 -0700 | [diff] [blame] | 4362 | } |
| 4363 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 4364 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4365 | // Run a *full* backup pass for the given package, writing the resulting data stream |
| 4366 | // to the supplied file descriptor. This method is synchronous and does not return |
| 4367 | // to the caller until the backup has been completed. |
| 4368 | public void fullBackup(ParcelFileDescriptor fd, boolean includeApks, boolean includeShared, |
| 4369 | boolean doAllApps, String[] pkgList) { |
| 4370 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullBackup"); |
| 4371 | |
| 4372 | // Validate |
| 4373 | if (!doAllApps) { |
| 4374 | if (!includeShared) { |
| 4375 | // If we're backing up shared data (sdcard or equivalent), then we can run |
| 4376 | // without any supplied app names. Otherwise, we'd be doing no work, so |
| 4377 | // report the error. |
| 4378 | if (pkgList == null || pkgList.length == 0) { |
| 4379 | throw new IllegalArgumentException( |
| 4380 | "Backup requested but neither shared nor any apps named"); |
| 4381 | } |
| 4382 | } |
| 4383 | } |
| 4384 | |
| 4385 | if (DEBUG) Slog.v(TAG, "Requesting full backup: apks=" + includeApks |
| 4386 | + " shared=" + includeShared + " all=" + doAllApps |
| 4387 | + " pkgs=" + pkgList); |
| 4388 | |
| 4389 | long oldId = Binder.clearCallingIdentity(); |
| 4390 | try { |
| 4391 | FullBackupParams params = new FullBackupParams(fd, includeApks, includeShared, |
| 4392 | doAllApps, pkgList); |
| 4393 | final int token = generateToken(); |
| 4394 | synchronized (mFullConfirmations) { |
| 4395 | mFullConfirmations.put(token, params); |
| 4396 | } |
| 4397 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4398 | // start up the confirmation UI |
| 4399 | if (DEBUG) Slog.d(TAG, "Starting backup confirmation UI, token=" + token); |
| 4400 | if (!startConfirmationUi(token, FullBackup.FULL_BACKUP_INTENT_ACTION)) { |
| 4401 | Slog.e(TAG, "Unable to launch full backup confirmation"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4402 | mFullConfirmations.delete(token); |
| 4403 | return; |
| 4404 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4405 | |
| 4406 | // make sure the screen is lit for the user interaction |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4407 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 4408 | |
| 4409 | // start the confirmation countdown |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4410 | startConfirmationTimeout(token, params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4411 | |
| 4412 | // wait for the backup to be performed |
| 4413 | if (DEBUG) Slog.d(TAG, "Waiting for full backup completion..."); |
| 4414 | waitForCompletion(params); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4415 | } finally { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4416 | try { |
| 4417 | fd.close(); |
| 4418 | } catch (IOException e) { |
| 4419 | // just eat it |
| 4420 | } |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4421 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4422 | } |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4423 | if (MORE_DEBUG) Slog.d(TAG, "Full backup done; returning to caller"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | public void fullRestore(ParcelFileDescriptor fd) { |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4427 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "fullRestore"); |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4428 | Slog.i(TAG, "Beginning full restore..."); |
| 4429 | |
| 4430 | long oldId = Binder.clearCallingIdentity(); |
| 4431 | |
| 4432 | try { |
| 4433 | FullRestoreParams params = new FullRestoreParams(fd); |
| 4434 | final int token = generateToken(); |
| 4435 | synchronized (mFullConfirmations) { |
| 4436 | mFullConfirmations.put(token, params); |
| 4437 | } |
| 4438 | |
| 4439 | // start up the confirmation UI |
| 4440 | if (DEBUG) Slog.d(TAG, "Starting restore confirmation UI, token=" + token); |
| 4441 | if (!startConfirmationUi(token, FullBackup.FULL_RESTORE_INTENT_ACTION)) { |
| 4442 | Slog.e(TAG, "Unable to launch full restore confirmation"); |
| 4443 | mFullConfirmations.delete(token); |
| 4444 | return; |
| 4445 | } |
| 4446 | |
| 4447 | // make sure the screen is lit for the user interaction |
| 4448 | mPowerManager.userActivity(SystemClock.uptimeMillis(), false); |
| 4449 | |
| 4450 | // start the confirmation countdown |
| 4451 | startConfirmationTimeout(token, params); |
| 4452 | |
| 4453 | // wait for the restore to be performed |
| 4454 | if (DEBUG) Slog.d(TAG, "Waiting for full restore completion..."); |
| 4455 | waitForCompletion(params); |
| 4456 | } finally { |
| 4457 | try { |
| 4458 | fd.close(); |
| 4459 | } catch (IOException e) { |
| 4460 | Slog.w(TAG, "Error trying to close fd after full restore: " + e); |
| 4461 | } |
| 4462 | Binder.restoreCallingIdentity(oldId); |
| 4463 | Slog.i(TAG, "Full restore completed"); |
| 4464 | } |
| 4465 | } |
| 4466 | |
| 4467 | boolean startConfirmationUi(int token, String action) { |
| 4468 | try { |
| 4469 | Intent confIntent = new Intent(action); |
| 4470 | confIntent.setClassName("com.android.backupconfirm", |
| 4471 | "com.android.backupconfirm.BackupRestoreConfirmation"); |
| 4472 | confIntent.putExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, token); |
| 4473 | confIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4474 | mContext.startActivity(confIntent); |
| 4475 | } catch (ActivityNotFoundException e) { |
| 4476 | return false; |
| 4477 | } |
| 4478 | return true; |
| 4479 | } |
| 4480 | |
| 4481 | void startConfirmationTimeout(int token, FullParams params) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4482 | if (MORE_DEBUG) Slog.d(TAG, "Posting conf timeout msg after " |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4483 | + TIMEOUT_FULL_CONFIRMATION + " millis"); |
| 4484 | Message msg = mBackupHandler.obtainMessage(MSG_FULL_CONFIRMATION_TIMEOUT, |
| 4485 | token, 0, params); |
| 4486 | mBackupHandler.sendMessageDelayed(msg, TIMEOUT_FULL_CONFIRMATION); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | void waitForCompletion(FullParams params) { |
| 4490 | synchronized (params.latch) { |
| 4491 | while (params.latch.get() == false) { |
| 4492 | try { |
| 4493 | params.latch.wait(); |
| 4494 | } catch (InterruptedException e) { /* never interrupted */ } |
| 4495 | } |
| 4496 | } |
| 4497 | } |
| 4498 | |
| 4499 | void signalFullBackupRestoreCompletion(FullParams params) { |
| 4500 | synchronized (params.latch) { |
| 4501 | params.latch.set(true); |
| 4502 | params.latch.notifyAll(); |
| 4503 | } |
| 4504 | } |
| 4505 | |
| 4506 | // Confirm that the previously-requested full backup/restore operation can proceed. This |
| 4507 | // is used to require a user-facing disclosure about the operation. |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4508 | @Override |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4509 | public void acknowledgeFullBackupOrRestore(int token, boolean allow, |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 4510 | String curPassword, String encPpassword, IFullBackupRestoreObserver observer) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4511 | if (DEBUG) Slog.d(TAG, "acknowledgeFullBackupOrRestore : token=" + token |
| 4512 | + " allow=" + allow); |
| 4513 | |
| 4514 | // TODO: possibly require not just this signature-only permission, but even |
| 4515 | // require that the specific designated confirmation-UI app uid is the caller? |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4516 | mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "acknowledgeFullBackupOrRestore"); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4517 | |
| 4518 | long oldId = Binder.clearCallingIdentity(); |
| 4519 | try { |
| 4520 | |
| 4521 | FullParams params; |
| 4522 | synchronized (mFullConfirmations) { |
| 4523 | params = mFullConfirmations.get(token); |
| 4524 | if (params != null) { |
| 4525 | mBackupHandler.removeMessages(MSG_FULL_CONFIRMATION_TIMEOUT, params); |
| 4526 | mFullConfirmations.delete(token); |
| 4527 | |
| 4528 | if (allow) { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4529 | final int verb = params instanceof FullBackupParams |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4530 | ? MSG_RUN_FULL_BACKUP |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4531 | : MSG_RUN_FULL_RESTORE; |
| 4532 | |
| Christopher Tate | 728a1c4 | 2011-07-28 18:03:03 -0700 | [diff] [blame] | 4533 | params.observer = observer; |
| 4534 | params.curPassword = curPassword; |
| 4535 | params.encryptPassword = encPpassword; |
| 4536 | |
| Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4537 | if (DEBUG) Slog.d(TAG, "Sending conf message with verb " + verb); |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 4538 | mWakelock.acquire(); |
| 4539 | Message msg = mBackupHandler.obtainMessage(verb, params); |
| 4540 | mBackupHandler.sendMessage(msg); |
| 4541 | } else { |
| 4542 | Slog.w(TAG, "User rejected full backup/restore operation"); |
| 4543 | // indicate completion without having actually transferred any data |
| 4544 | signalFullBackupRestoreCompletion(params); |
| 4545 | } |
| 4546 | } else { |
| 4547 | Slog.w(TAG, "Attempted to ack full backup/restore with invalid token"); |
| 4548 | } |
| 4549 | } |
| 4550 | } finally { |
| 4551 | Binder.restoreCallingIdentity(oldId); |
| 4552 | } |
| 4553 | } |
| 4554 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4555 | // Enable/disable the backup service |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4556 | public void setBackupEnabled(boolean enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4557 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4558 | "setBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4559 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4560 | Slog.i(TAG, "Backup enabled => " + enable); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4561 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4562 | boolean wasEnabled = mEnabled; |
| 4563 | synchronized (this) { |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 4564 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 4565 | Settings.Secure.BACKUP_ENABLED, enable ? 1 : 0); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4566 | mEnabled = enable; |
| 4567 | } |
| 4568 | |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 4569 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4570 | if (enable && !wasEnabled && mProvisioned) { |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 4571 | // if we've just been enabled, start scheduling backup passes |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4572 | startBackupAlarmsLocked(BACKUP_INTERVAL); |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 4573 | } else if (!enable) { |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4574 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4575 | if (DEBUG) Slog.i(TAG, "Opting out of backup"); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4576 | |
| Christopher Tate | b6787f2 | 2009-07-02 17:40:45 -0700 | [diff] [blame] | 4577 | mAlarmManager.cancel(mRunBackupIntent); |
| Christopher Tate | 4cc86e1 | 2009-09-21 19:36:51 -0700 | [diff] [blame] | 4578 | |
| 4579 | // This also constitutes an opt-out, so we wipe any data for |
| 4580 | // this device from the backend. We start that process with |
| 4581 | // an alarm in order to guarantee wakelock states. |
| 4582 | if (wasEnabled && mProvisioned) { |
| 4583 | // NOTE: we currently flush every registered transport, not just |
| 4584 | // the currently-active one. |
| 4585 | HashSet<String> allTransports; |
| 4586 | synchronized (mTransports) { |
| 4587 | allTransports = new HashSet<String>(mTransports.keySet()); |
| 4588 | } |
| 4589 | // build the set of transports for which we are posting an init |
| 4590 | for (String transport : allTransports) { |
| 4591 | recordInitPendingLocked(true, transport); |
| 4592 | } |
| 4593 | mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), |
| 4594 | mRunInitIntent); |
| 4595 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4596 | } |
| 4597 | } |
| Christopher Tate | 49401dd | 2009-07-01 12:34:29 -0700 | [diff] [blame] | 4598 | } |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4599 | |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 4600 | // Enable/disable automatic restore of app data at install time |
| 4601 | public void setAutoRestore(boolean doAutoRestore) { |
| 4602 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4603 | "setAutoRestore"); |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 4604 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4605 | Slog.i(TAG, "Auto restore => " + doAutoRestore); |
| Christopher Tate | cce9da5 | 2010-02-03 15:11:15 -0800 | [diff] [blame] | 4606 | |
| 4607 | synchronized (this) { |
| 4608 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 4609 | Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0); |
| 4610 | mAutoRestore = doAutoRestore; |
| 4611 | } |
| 4612 | } |
| 4613 | |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4614 | // Mark the backup service as having been provisioned |
| 4615 | public void setBackupProvisioned(boolean available) { |
| 4616 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4617 | "setBackupProvisioned"); |
| 4618 | |
| 4619 | boolean wasProvisioned = mProvisioned; |
| 4620 | synchronized (this) { |
| 4621 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 4622 | Settings.Secure.BACKUP_PROVISIONED, available ? 1 : 0); |
| 4623 | mProvisioned = available; |
| 4624 | } |
| 4625 | |
| 4626 | synchronized (mQueueLock) { |
| 4627 | if (available && !wasProvisioned && mEnabled) { |
| 4628 | // we're now good to go, so start the backup alarms |
| 4629 | startBackupAlarmsLocked(FIRST_BACKUP_INTERVAL); |
| 4630 | } else if (!available) { |
| 4631 | // No longer enabled, so stop running backups |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4632 | Slog.w(TAG, "Backup service no longer provisioned"); |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4633 | mAlarmManager.cancel(mRunBackupIntent); |
| 4634 | } |
| 4635 | } |
| 4636 | } |
| 4637 | |
| 4638 | private void startBackupAlarmsLocked(long delayBeforeFirstBackup) { |
| Dan Egnor | c1c49c0 | 2009-10-30 17:35:39 -0700 | [diff] [blame] | 4639 | // We used to use setInexactRepeating(), but that may be linked to |
| 4640 | // backups running at :00 more often than not, creating load spikes. |
| 4641 | // Schedule at an exact time for now, and also add a bit of "fuzz". |
| 4642 | |
| 4643 | Random random = new Random(); |
| 4644 | long when = System.currentTimeMillis() + delayBeforeFirstBackup + |
| 4645 | random.nextInt(FUZZ_MILLIS); |
| 4646 | mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, |
| 4647 | BACKUP_INTERVAL + random.nextInt(FUZZ_MILLIS), mRunBackupIntent); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 4648 | mNextBackupPass = when; |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 4649 | } |
| 4650 | |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4651 | // Report whether the backup mechanism is currently enabled |
| 4652 | public boolean isBackupEnabled() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4653 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled"); |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 4654 | return mEnabled; // no need to synchronize just to read it |
| 4655 | } |
| 4656 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4657 | // Report the name of the currently active transport |
| 4658 | public String getCurrentTransport() { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4659 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 4e3e50c | 2009-07-02 12:14:05 -0700 | [diff] [blame] | 4660 | "getCurrentTransport"); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4661 | if (MORE_DEBUG) Slog.v(TAG, "... getCurrentTransport() returning " + mCurrentTransport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4662 | return mCurrentTransport; |
| Christopher Tate | ace7f09 | 2009-06-15 18:07:25 -0700 | [diff] [blame] | 4663 | } |
| 4664 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4665 | // Report all known, available backup transports |
| 4666 | public String[] listAllTransports() { |
| Christopher Tate | 34ebd0e | 2009-07-06 15:44:54 -0700 | [diff] [blame] | 4667 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports"); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4668 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4669 | String[] list = null; |
| 4670 | ArrayList<String> known = new ArrayList<String>(); |
| 4671 | for (Map.Entry<String, IBackupTransport> entry : mTransports.entrySet()) { |
| 4672 | if (entry.getValue() != null) { |
| 4673 | known.add(entry.getKey()); |
| 4674 | } |
| 4675 | } |
| 4676 | |
| 4677 | if (known.size() > 0) { |
| 4678 | list = new String[known.size()]; |
| 4679 | known.toArray(list); |
| 4680 | } |
| 4681 | return list; |
| 4682 | } |
| 4683 | |
| 4684 | // Select which transport to use for the next backup operation. If the given |
| 4685 | // name is not one of the available transports, no action is taken and the method |
| 4686 | // returns null. |
| 4687 | public String selectBackupTransport(String transport) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4688 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "selectBackupTransport"); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4689 | |
| 4690 | synchronized (mTransports) { |
| 4691 | String prevTransport = null; |
| 4692 | if (mTransports.get(transport) != null) { |
| 4693 | prevTransport = mCurrentTransport; |
| 4694 | mCurrentTransport = transport; |
| Dianne Hackborn | cf09829 | 2009-07-01 19:55:20 -0700 | [diff] [blame] | 4695 | Settings.Secure.putString(mContext.getContentResolver(), |
| 4696 | Settings.Secure.BACKUP_TRANSPORT, transport); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4697 | Slog.v(TAG, "selectBackupTransport() set " + mCurrentTransport |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4698 | + " returning " + prevTransport); |
| 4699 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4700 | Slog.w(TAG, "Attempt to select unavailable transport " + transport); |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4701 | } |
| 4702 | return prevTransport; |
| 4703 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 4706 | // Supply the configuration Intent for the given transport. If the name is not one |
| 4707 | // of the available transports, or if the transport does not supply any configuration |
| 4708 | // UI, the method returns null. |
| 4709 | public Intent getConfigurationIntent(String transportName) { |
| 4710 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4711 | "getConfigurationIntent"); |
| 4712 | |
| 4713 | synchronized (mTransports) { |
| 4714 | final IBackupTransport transport = mTransports.get(transportName); |
| 4715 | if (transport != null) { |
| 4716 | try { |
| 4717 | final Intent intent = transport.configurationIntent(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4718 | if (MORE_DEBUG) Slog.d(TAG, "getConfigurationIntent() returning config intent " |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 4719 | + intent); |
| 4720 | return intent; |
| 4721 | } catch (RemoteException e) { |
| 4722 | /* fall through to return null */ |
| 4723 | } |
| 4724 | } |
| 4725 | } |
| 4726 | |
| 4727 | return null; |
| 4728 | } |
| 4729 | |
| 4730 | // Supply the configuration summary string for the given transport. If the name is |
| 4731 | // not one of the available transports, or if the transport does not supply any |
| 4732 | // summary / destination string, the method can return null. |
| 4733 | // |
| 4734 | // This string is used VERBATIM as the summary text of the relevant Settings item! |
| 4735 | public String getDestinationString(String transportName) { |
| 4736 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 4737 | "getDestinationString"); |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 4738 | |
| 4739 | synchronized (mTransports) { |
| 4740 | final IBackupTransport transport = mTransports.get(transportName); |
| 4741 | if (transport != null) { |
| 4742 | try { |
| 4743 | final String text = transport.currentDestinationString(); |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4744 | if (MORE_DEBUG) Slog.d(TAG, "getDestinationString() returning " + text); |
| Christopher Tate | f5e1c29 | 2010-12-08 18:40:26 -0800 | [diff] [blame] | 4745 | return text; |
| 4746 | } catch (RemoteException e) { |
| 4747 | /* fall through to return null */ |
| 4748 | } |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | return null; |
| 4753 | } |
| 4754 | |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4755 | // Callback: a requested backup agent has been instantiated. This should only |
| 4756 | // be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4757 | public void agentConnected(String packageName, IBinder agentBinder) { |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4758 | synchronized(mAgentConnectLock) { |
| 4759 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4760 | Slog.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder); |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4761 | IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder); |
| 4762 | mConnectedAgent = agent; |
| 4763 | mConnecting = false; |
| 4764 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4765 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4766 | + " claiming agent connected"); |
| 4767 | } |
| 4768 | mAgentConnectLock.notifyAll(); |
| 4769 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | // Callback: a backup agent has failed to come up, or has unexpectedly quit. |
| 4773 | // If the agent failed to come up in the first place, the agentBinder argument |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4774 | // will be null. This should only be called from the Activity Manager. |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4775 | public void agentDisconnected(String packageName) { |
| 4776 | // TODO: handle backup being interrupted |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4777 | synchronized(mAgentConnectLock) { |
| 4778 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 4779 | mConnectedAgent = null; |
| 4780 | mConnecting = false; |
| 4781 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4782 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4783 | + " claiming agent disconnected"); |
| 4784 | } |
| 4785 | mAgentConnectLock.notifyAll(); |
| 4786 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4787 | } |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4788 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4789 | // An application being installed will need a restore pass, then the Package Manager |
| 4790 | // will need to be told when the restore is finished. |
| 4791 | public void restoreAtInstall(String packageName, int token) { |
| 4792 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4793 | Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid() |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4794 | + " attemping install-time restore"); |
| 4795 | return; |
| 4796 | } |
| 4797 | |
| 4798 | long restoreSet = getAvailableRestoreToken(packageName); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4799 | if (DEBUG) Slog.v(TAG, "restoreAtInstall pkg=" + packageName |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4800 | + " token=" + Integer.toHexString(token)); |
| 4801 | |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 4802 | if (mAutoRestore && mProvisioned && restoreSet != 0) { |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4803 | // okay, we're going to attempt a restore of this package from this restore set. |
| 4804 | // The eventual message back into the Package Manager to run the post-install |
| 4805 | // steps for 'token' will be issued from the restore handling code. |
| 4806 | |
| 4807 | // We can use a synthetic PackageInfo here because: |
| 4808 | // 1. We know it's valid, since the Package Manager supplied the name |
| 4809 | // 2. Only the packageName field will be used by the restore code |
| 4810 | PackageInfo pkg = new PackageInfo(); |
| 4811 | pkg.packageName = packageName; |
| 4812 | |
| 4813 | mWakelock.acquire(); |
| 4814 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 4815 | msg.obj = new RestoreParams(getTransport(mCurrentTransport), null, |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4816 | restoreSet, pkg, token, true); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4817 | mBackupHandler.sendMessage(msg); |
| 4818 | } else { |
| Christopher Tate | f087272 | 2010-02-25 15:22:48 -0800 | [diff] [blame] | 4819 | // Auto-restore disabled or no way to attempt a restore; just tell the Package |
| 4820 | // Manager to proceed with the post-install handling for this package. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4821 | if (DEBUG) Slog.v(TAG, "No restore set -- skipping restore"); |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4822 | try { |
| 4823 | mPackageManagerBinder.finishPackageInstall(token); |
| 4824 | } catch (RemoteException e) { /* can't happen */ } |
| 4825 | } |
| 4826 | } |
| 4827 | |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 4828 | // Hand off a restore session |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4829 | public IRestoreSession beginRestoreSession(String packageName, String transport) { |
| 4830 | if (DEBUG) Slog.v(TAG, "beginRestoreSession: pkg=" + packageName |
| 4831 | + " transport=" + transport); |
| 4832 | |
| 4833 | boolean needPermission = true; |
| 4834 | if (transport == null) { |
| 4835 | transport = mCurrentTransport; |
| 4836 | |
| 4837 | if (packageName != null) { |
| 4838 | PackageInfo app = null; |
| 4839 | try { |
| 4840 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 4841 | } catch (NameNotFoundException nnf) { |
| 4842 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| 4843 | throw new IllegalArgumentException("Package " + packageName + " not found"); |
| 4844 | } |
| 4845 | |
| 4846 | if (app.applicationInfo.uid == Binder.getCallingUid()) { |
| 4847 | // So: using the current active transport, and the caller has asked |
| 4848 | // that its own package will be restored. In this narrow use case |
| 4849 | // we do not require the caller to hold the permission. |
| 4850 | needPermission = false; |
| 4851 | } |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | if (needPermission) { |
| 4856 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4857 | "beginRestoreSession"); |
| 4858 | } else { |
| 4859 | if (DEBUG) Slog.d(TAG, "restoring self on current transport; no permission needed"); |
| 4860 | } |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4861 | |
| 4862 | synchronized(this) { |
| 4863 | if (mActiveRestoreSession != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4864 | Slog.d(TAG, "Restore session requested but one already active"); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4865 | return null; |
| 4866 | } |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4867 | mActiveRestoreSession = new ActiveRestoreSession(packageName, transport); |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4868 | mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_TIMEOUT, TIMEOUT_RESTORE_INTERVAL); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4869 | } |
| 4870 | return mActiveRestoreSession; |
| Christopher Tate | 8c850b7 | 2009-06-07 19:33:20 -0700 | [diff] [blame] | 4871 | } |
| Christopher Tate | 043dadc | 2009-06-02 16:11:00 -0700 | [diff] [blame] | 4872 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4873 | void clearRestoreSession(ActiveRestoreSession currentSession) { |
| 4874 | synchronized(this) { |
| 4875 | if (currentSession != mActiveRestoreSession) { |
| 4876 | Slog.e(TAG, "ending non-current restore session"); |
| 4877 | } else { |
| 4878 | if (DEBUG) Slog.v(TAG, "Clearing restore session and halting timeout"); |
| 4879 | mActiveRestoreSession = null; |
| 4880 | mBackupHandler.removeMessages(MSG_RESTORE_TIMEOUT); |
| 4881 | } |
| 4882 | } |
| 4883 | } |
| 4884 | |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4885 | // Note that a currently-active backup agent has notified us that it has |
| 4886 | // completed the given outstanding asynchronous backup/restore operation. |
| 4887 | public void opComplete(int token) { |
| 4888 | synchronized (mCurrentOpLock) { |
| Christopher Tate | c58efa6 | 2011-08-01 19:20:14 -0700 | [diff] [blame] | 4889 | if (MORE_DEBUG) Slog.v(TAG, "opComplete: " + Integer.toHexString(token)); |
| Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 4890 | mCurrentOperations.put(token, OP_ACKNOWLEDGED); |
| 4891 | mCurrentOpLock.notifyAll(); |
| 4892 | } |
| 4893 | } |
| 4894 | |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4895 | // ----- Restore session ----- |
| 4896 | |
| Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 4897 | class ActiveRestoreSession extends IRestoreSession.Stub { |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4898 | private static final String TAG = "RestoreSession"; |
| 4899 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4900 | private String mPackageName; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4901 | private IBackupTransport mRestoreTransport = null; |
| 4902 | RestoreSet[] mRestoreSets = null; |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4903 | boolean mEnded = false; |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4904 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4905 | ActiveRestoreSession(String packageName, String transport) { |
| 4906 | mPackageName = packageName; |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 4907 | mRestoreTransport = getTransport(transport); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | // --- Binder interface --- |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4911 | public synchronized int getAvailableRestoreSets(IRestoreObserver observer) { |
| Joe Onorato | 5933a49 | 2009-07-23 18:24:08 -0400 | [diff] [blame] | 4912 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4913 | "getAvailableRestoreSets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4914 | if (observer == null) { |
| 4915 | throw new IllegalArgumentException("Observer must not be null"); |
| 4916 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4917 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4918 | if (mEnded) { |
| 4919 | throw new IllegalStateException("Restore session already ended"); |
| 4920 | } |
| 4921 | |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4922 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4923 | try { |
| Christopher Tate | 4338304 | 2009-07-13 15:17:13 -0700 | [diff] [blame] | 4924 | if (mRestoreTransport == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4925 | Slog.w(TAG, "Null transport getting restore sets"); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4926 | return -1; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4927 | } |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4928 | // spin off the transport request to our service thread |
| 4929 | mWakelock.acquire(); |
| 4930 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_GET_RESTORE_SETS, |
| 4931 | new RestoreGetSetsParams(mRestoreTransport, this, observer)); |
| 4932 | mBackupHandler.sendMessage(msg); |
| 4933 | return 0; |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4934 | } catch (Exception e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4935 | Slog.e(TAG, "Error in getAvailableRestoreSets", e); |
| Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 4936 | return -1; |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 4937 | } finally { |
| 4938 | Binder.restoreCallingIdentity(oldId); |
| Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 4939 | } |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4940 | } |
| 4941 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 4942 | public synchronized int restoreAll(long token, IRestoreObserver observer) { |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4943 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4944 | "performRestore"); |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4945 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4946 | if (DEBUG) Slog.d(TAG, "restoreAll token=" + Long.toHexString(token) |
| Christopher Tate | f2c321a | 2009-08-10 15:43:36 -0700 | [diff] [blame] | 4947 | + " observer=" + observer); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 4948 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 4949 | if (mEnded) { |
| 4950 | throw new IllegalStateException("Restore session already ended"); |
| 4951 | } |
| 4952 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4953 | if (mRestoreTransport == null || mRestoreSets == null) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 4954 | Slog.e(TAG, "Ignoring restoreAll() with no restore set"); |
| 4955 | return -1; |
| 4956 | } |
| 4957 | |
| 4958 | if (mPackageName != null) { |
| 4959 | Slog.e(TAG, "Ignoring restoreAll() on single-package session"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 4960 | return -1; |
| 4961 | } |
| 4962 | |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4963 | synchronized (mQueueLock) { |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4964 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 4965 | if (token == mRestoreSets[i].token) { |
| 4966 | long oldId = Binder.clearCallingIdentity(); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4967 | mWakelock.acquire(); |
| 4968 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 4969 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, true); |
| Christopher Tate | 21ab6a5 | 2009-09-24 18:01:46 -0700 | [diff] [blame] | 4970 | mBackupHandler.sendMessage(msg); |
| 4971 | Binder.restoreCallingIdentity(oldId); |
| 4972 | return 0; |
| 4973 | } |
| Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 4974 | } |
| 4975 | } |
| Christopher Tate | 0e0b4ae | 2009-08-10 16:13:47 -0700 | [diff] [blame] | 4976 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4977 | Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 4978 | return -1; |
| 4979 | } |
| 4980 | |
| Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 4981 | public synchronized int restoreSome(long token, IRestoreObserver observer, |
| 4982 | String[] packages) { |
| 4983 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, |
| 4984 | "performRestore"); |
| 4985 | |
| 4986 | if (DEBUG) { |
| 4987 | StringBuilder b = new StringBuilder(128); |
| 4988 | b.append("restoreSome token="); |
| 4989 | b.append(Long.toHexString(token)); |
| 4990 | b.append(" observer="); |
| 4991 | b.append(observer.toString()); |
| 4992 | b.append(" packages="); |
| 4993 | if (packages == null) { |
| 4994 | b.append("null"); |
| 4995 | } else { |
| 4996 | b.append('{'); |
| 4997 | boolean first = true; |
| 4998 | for (String s : packages) { |
| 4999 | if (!first) { |
| 5000 | b.append(", "); |
| 5001 | } else first = false; |
| 5002 | b.append(s); |
| 5003 | } |
| 5004 | b.append('}'); |
| 5005 | } |
| 5006 | Slog.d(TAG, b.toString()); |
| 5007 | } |
| 5008 | |
| 5009 | if (mEnded) { |
| 5010 | throw new IllegalStateException("Restore session already ended"); |
| 5011 | } |
| 5012 | |
| 5013 | if (mRestoreTransport == null || mRestoreSets == null) { |
| 5014 | Slog.e(TAG, "Ignoring restoreAll() with no restore set"); |
| 5015 | return -1; |
| 5016 | } |
| 5017 | |
| 5018 | if (mPackageName != null) { |
| 5019 | Slog.e(TAG, "Ignoring restoreAll() on single-package session"); |
| 5020 | return -1; |
| 5021 | } |
| 5022 | |
| 5023 | synchronized (mQueueLock) { |
| 5024 | for (int i = 0; i < mRestoreSets.length; i++) { |
| 5025 | if (token == mRestoreSets[i].token) { |
| 5026 | long oldId = Binder.clearCallingIdentity(); |
| 5027 | mWakelock.acquire(); |
| 5028 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| 5029 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, |
| 5030 | packages, true); |
| 5031 | mBackupHandler.sendMessage(msg); |
| 5032 | Binder.restoreCallingIdentity(oldId); |
| 5033 | return 0; |
| 5034 | } |
| 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found"); |
| 5039 | return -1; |
| 5040 | } |
| 5041 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5042 | public synchronized int restorePackage(String packageName, IRestoreObserver observer) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5043 | if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5044 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5045 | if (mEnded) { |
| 5046 | throw new IllegalStateException("Restore session already ended"); |
| 5047 | } |
| 5048 | |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5049 | if (mPackageName != null) { |
| 5050 | if (! mPackageName.equals(packageName)) { |
| 5051 | Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName |
| 5052 | + " on session for package " + mPackageName); |
| 5053 | return -1; |
| 5054 | } |
| 5055 | } |
| 5056 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5057 | PackageInfo app = null; |
| 5058 | try { |
| 5059 | app = mPackageManager.getPackageInfo(packageName, 0); |
| 5060 | } catch (NameNotFoundException nnf) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5061 | Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5062 | return -1; |
| 5063 | } |
| 5064 | |
| 5065 | // If the caller is not privileged and is not coming from the target |
| 5066 | // app's uid, throw a permission exception back to the caller. |
| 5067 | int perm = mContext.checkPermission(android.Manifest.permission.BACKUP, |
| 5068 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 5069 | if ((perm == PackageManager.PERMISSION_DENIED) && |
| 5070 | (app.applicationInfo.uid != Binder.getCallingUid())) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5071 | Slog.w(TAG, "restorePackage: bad packageName=" + packageName |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5072 | + " or calling uid=" + Binder.getCallingUid()); |
| 5073 | throw new SecurityException("No permission to restore other packages"); |
| 5074 | } |
| 5075 | |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 5076 | // If the package has no backup agent, we obviously cannot proceed |
| 5077 | if (app.applicationInfo.backupAgentName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5078 | Slog.w(TAG, "Asked to restore package " + packageName + " with no agent"); |
| Christopher Tate | 7d411a3 | 2010-02-26 11:27:08 -0800 | [diff] [blame] | 5079 | return -1; |
| 5080 | } |
| 5081 | |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5082 | // So far so good; we're allowed to try to restore this package. Now |
| 5083 | // check whether there is data for it in the current dataset, falling back |
| 5084 | // to the ancestral dataset if not. |
| Christopher Tate | 1bb6906 | 2010-02-19 17:02:12 -0800 | [diff] [blame] | 5085 | long token = getAvailableRestoreToken(packageName); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5086 | |
| 5087 | // If we didn't come up with a place to look -- no ancestral dataset and |
| 5088 | // the app has never been backed up from this device -- there's nothing |
| 5089 | // to do but return failure. |
| 5090 | if (token == 0) { |
| Chris Tate | 44ab845 | 2010-11-16 15:10:49 -0800 | [diff] [blame] | 5091 | if (DEBUG) Slog.w(TAG, "No data available for this package; not restoring"); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5092 | return -1; |
| 5093 | } |
| 5094 | |
| 5095 | // Ready to go: enqueue the restore request and claim success |
| 5096 | long oldId = Binder.clearCallingIdentity(); |
| 5097 | mWakelock.acquire(); |
| 5098 | Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE); |
| Chris Tate | 249345b | 2010-10-29 12:57:04 -0700 | [diff] [blame] | 5099 | msg.obj = new RestoreParams(mRestoreTransport, observer, token, app, 0, false); |
| Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 5100 | mBackupHandler.sendMessage(msg); |
| 5101 | Binder.restoreCallingIdentity(oldId); |
| 5102 | return 0; |
| 5103 | } |
| 5104 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5105 | // Posted to the handler to tear down a restore session in a cleanly synchronized way |
| 5106 | class EndRestoreRunnable implements Runnable { |
| 5107 | BackupManagerService mBackupManager; |
| 5108 | ActiveRestoreSession mSession; |
| 5109 | |
| 5110 | EndRestoreRunnable(BackupManagerService manager, ActiveRestoreSession session) { |
| 5111 | mBackupManager = manager; |
| 5112 | mSession = session; |
| 5113 | } |
| 5114 | |
| 5115 | public void run() { |
| 5116 | // clean up the session's bookkeeping |
| 5117 | synchronized (mSession) { |
| 5118 | try { |
| 5119 | if (mSession.mRestoreTransport != null) { |
| 5120 | mSession.mRestoreTransport.finishRestore(); |
| 5121 | } |
| 5122 | } catch (Exception e) { |
| 5123 | Slog.e(TAG, "Error in finishRestore", e); |
| 5124 | } finally { |
| 5125 | mSession.mRestoreTransport = null; |
| 5126 | mSession.mEnded = true; |
| 5127 | } |
| 5128 | } |
| 5129 | |
| 5130 | // clean up the BackupManagerService side of the bookkeeping |
| 5131 | // and cancel any pending timeout message |
| 5132 | mBackupManager.clearRestoreSession(mSession); |
| 5133 | } |
| 5134 | } |
| 5135 | |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5136 | public synchronized void endRestoreSession() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5137 | if (DEBUG) Slog.d(TAG, "endRestoreSession"); |
| Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 5138 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5139 | if (mEnded) { |
| 5140 | throw new IllegalStateException("Restore session already ended"); |
| Dan Egnor | 0084da5 | 2009-07-29 12:57:16 -0700 | [diff] [blame] | 5141 | } |
| 5142 | |
| Christopher Tate | 73a3cb3 | 2010-12-13 18:27:26 -0800 | [diff] [blame] | 5143 | mBackupHandler.post(new EndRestoreRunnable(BackupManagerService.this, this)); |
| Christopher Tate | 9b3905c | 2009-06-08 15:24:01 -0700 | [diff] [blame] | 5144 | } |
| 5145 | } |
| 5146 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5147 | @Override |
| 5148 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5149 | long identityToken = Binder.clearCallingIdentity(); |
| 5150 | try { |
| 5151 | dumpInternal(pw); |
| 5152 | } finally { |
| 5153 | Binder.restoreCallingIdentity(identityToken); |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | private void dumpInternal(PrintWriter pw) { |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5158 | synchronized (mQueueLock) { |
| Christopher Tate | 8031a3d | 2009-07-06 16:36:05 -0700 | [diff] [blame] | 5159 | pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled") |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5160 | + " / " + (!mProvisioned ? "not " : "") + "provisioned / " |
| Christopher Tate | c2af5d3 | 2010-02-02 15:18:58 -0800 | [diff] [blame] | 5161 | + (this.mPendingInits.size() == 0 ? "not " : "") + "pending init"); |
| Christopher Tate | ae06ed9 | 2010-02-25 17:13:28 -0800 | [diff] [blame] | 5162 | pw.println("Auto-restore is " + (mAutoRestore ? "enabled" : "disabled")); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5163 | pw.println("Last backup pass: " + mLastBackupPass |
| 5164 | + " (now = " + System.currentTimeMillis() + ')'); |
| 5165 | pw.println(" next scheduled: " + mNextBackupPass); |
| 5166 | |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5167 | pw.println("Available transports:"); |
| 5168 | for (String t : listAllTransports()) { |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5169 | pw.println((t.equals(mCurrentTransport) ? " * " : " ") + t); |
| 5170 | try { |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5171 | IBackupTransport transport = getTransport(t); |
| 5172 | File dir = new File(mBaseStateDir, transport.transportDirName()); |
| 5173 | pw.println(" destination: " + transport.currentDestinationString()); |
| 5174 | pw.println(" intent: " + transport.configurationIntent()); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5175 | for (File f : dir.listFiles()) { |
| 5176 | pw.println(" " + f.getName() + " - " + f.length() + " state bytes"); |
| 5177 | } |
| Fabrice Di Meglio | 8aac3ee | 2011-01-12 18:47:14 -0800 | [diff] [blame] | 5178 | } catch (Exception e) { |
| 5179 | Slog.e(TAG, "Error in transport", e); |
| Dan Egnor | 852f8e4 | 2009-09-30 11:20:45 -0700 | [diff] [blame] | 5180 | pw.println(" Error: " + e); |
| 5181 | } |
| Christopher Tate | 9171749 | 2009-06-26 21:07:13 -0700 | [diff] [blame] | 5182 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5183 | |
| 5184 | pw.println("Pending init: " + mPendingInits.size()); |
| 5185 | for (String s : mPendingInits) { |
| 5186 | pw.println(" " + s); |
| 5187 | } |
| 5188 | |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5189 | int N = mBackupParticipants.size(); |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5190 | pw.println("Participants:"); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5191 | for (int i=0; i<N; i++) { |
| 5192 | int uid = mBackupParticipants.keyAt(i); |
| 5193 | pw.print(" uid: "); |
| 5194 | pw.println(uid); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5195 | HashSet<ApplicationInfo> participants = mBackupParticipants.valueAt(i); |
| 5196 | for (ApplicationInfo app: participants) { |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5197 | pw.println(" " + app.packageName); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5198 | } |
| 5199 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5200 | |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 5201 | pw.println("Ancestral packages: " |
| 5202 | + (mAncestralPackages == null ? "none" : mAncestralPackages.size())); |
| Christopher Tate | 5923c97 | 2010-04-04 17:45:35 -0700 | [diff] [blame] | 5203 | if (mAncestralPackages != null) { |
| 5204 | for (String pkg : mAncestralPackages) { |
| 5205 | pw.println(" " + pkg); |
| 5206 | } |
| Christopher Tate | b49ceb3 | 2010-02-08 16:22:24 -0800 | [diff] [blame] | 5207 | } |
| 5208 | |
| Christopher Tate | 73e0252 | 2009-07-15 14:18:26 -0700 | [diff] [blame] | 5209 | pw.println("Ever backed up: " + mEverStoredApps.size()); |
| 5210 | for (String pkg : mEverStoredApps) { |
| 5211 | pw.println(" " + pkg); |
| 5212 | } |
| Christopher Tate | 55f931a | 2009-09-29 17:17:34 -0700 | [diff] [blame] | 5213 | |
| 5214 | pw.println("Pending backup: " + mPendingBackups.size()); |
| Christopher Tate | 6aa41f4 | 2009-06-19 14:14:22 -0700 | [diff] [blame] | 5215 | for (BackupRequest req : mPendingBackups.values()) { |
| Christopher Tate | 6ef58a1 | 2009-06-29 14:56:28 -0700 | [diff] [blame] | 5216 | pw.println(" " + req); |
| Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 5217 | } |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 5218 | } |
| 5219 | } |
| Christopher Tate | 487529a | 2009-04-29 14:03:25 -0700 | [diff] [blame] | 5220 | } |