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