BackupManagerService.java revision 25c50f98e3176bd12edd122e91ea9a1ee580d7ce
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
17package com.android.server.backup;
18
19import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_NAME;
20import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_VERSION;
21import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_OLD_VERSION;
22import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_POLICY_ALLOW_APKS;
23import static android.app.backup.BackupManagerMonitor.EXTRA_LOG_MANIFEST_PACKAGE_NAME;
24import static android.app.backup.BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT;
25import static android.app.backup.BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY;
26import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER;
27import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH;
28import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_SYSTEM_APP_NO_AGENT;
29import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE;
30import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_APK_NOT_INSTALLED;
31import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK;
32import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_MISSING_SIGNATURE;
33import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_EXPECTED_DIFFERENT_PACKAGE;
34import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_RESTORE_ANY_VERSION;
35import static android.app.backup.BackupManagerMonitor.LOG_EVENT_ID_VERSIONS_MATCH;
36import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_BACKUP_IN_FOREGROUND;
37
38import android.app.ActivityManager;
39import android.app.AlarmManager;
40import android.app.AppGlobals;
41import android.app.ApplicationThreadConstants;
42import android.app.IActivityManager;
43import android.app.IBackupAgent;
44import android.app.PackageInstallObserver;
45import android.app.PendingIntent;
46import android.app.backup.BackupAgent;
47import android.app.backup.BackupDataInput;
48import android.app.backup.BackupDataOutput;
49import android.app.backup.BackupManager;
50import android.app.backup.BackupManagerMonitor;
51import android.app.backup.BackupProgress;
52import android.app.backup.BackupTransport;
53import android.app.backup.FullBackup;
54import android.app.backup.FullBackupDataOutput;
55import android.app.backup.IBackupManager;
56import android.app.backup.IBackupManagerMonitor;
57import android.app.backup.IBackupObserver;
58import android.app.backup.IFullBackupRestoreObserver;
59import android.app.backup.IRestoreObserver;
60import android.app.backup.IRestoreSession;
61import android.app.backup.ISelectBackupTransportCallback;
62import android.app.backup.RestoreDescription;
63import android.app.backup.RestoreSet;
64import android.app.backup.SelectBackupTransportCallback;
65import android.content.ActivityNotFoundException;
66import android.content.BroadcastReceiver;
67import android.content.ComponentName;
68import android.content.ContentResolver;
69import android.content.Context;
70import android.content.Intent;
71import android.content.IntentFilter;
72import android.content.ServiceConnection;
73import android.content.pm.ApplicationInfo;
74import android.content.pm.IPackageDataObserver;
75import android.content.pm.IPackageDeleteObserver;
76import android.content.pm.IPackageManager;
77import android.content.pm.PackageInfo;
78import android.content.pm.PackageManager;
79import android.content.pm.PackageManager.NameNotFoundException;
80import android.content.pm.Signature;
81import android.database.ContentObserver;
82import android.net.Uri;
83import android.os.PowerSaveState;
84import android.os.Binder;
85import android.os.Build;
86import android.os.Bundle;
87import android.os.Environment;
88import android.os.Environment.UserEnvironment;
89import android.os.Handler;
90import android.os.HandlerThread;
91import android.os.IBinder;
92import android.os.Looper;
93import android.os.Message;
94import android.os.ParcelFileDescriptor;
95import android.os.PowerManager;
96import android.os.Process;
97import android.os.RemoteException;
98import android.os.SELinux;
99import android.os.ServiceManager;
100import android.os.SystemClock;
101import android.os.UserHandle;
102import android.os.WorkSource;
103import android.os.storage.IStorageManager;
104import android.os.storage.StorageManager;
105import android.provider.Settings;
106import android.system.ErrnoException;
107import android.system.Os;
108import android.text.TextUtils;
109import android.util.AtomicFile;
110import android.util.EventLog;
111import android.util.Log;
112import android.util.Pair;
113import android.util.Slog;
114import android.util.SparseArray;
115import android.util.StringBuilderPrinter;
116
117import com.android.internal.annotations.GuardedBy;
118import com.android.internal.backup.IBackupTransport;
119import com.android.internal.backup.IObbBackupService;
120import com.android.internal.util.DumpUtils;
121import com.android.server.AppWidgetBackupBridge;
122import com.android.server.EventLogTags;
123import com.android.server.SystemConfig;
124import com.android.server.SystemService;
125import com.android.server.backup.PackageManagerBackupAgent.Metadata;
126
127import com.android.server.power.BatterySaverPolicy.ServiceType;
128import libcore.io.IoUtils;
129
130import java.io.BufferedInputStream;
131import java.io.BufferedOutputStream;
132import java.io.ByteArrayInputStream;
133import java.io.ByteArrayOutputStream;
134import java.io.DataInputStream;
135import java.io.DataOutputStream;
136import java.io.EOFException;
137import java.io.File;
138import java.io.FileDescriptor;
139import java.io.FileInputStream;
140import java.io.FileNotFoundException;
141import java.io.FileOutputStream;
142import java.io.IOException;
143import java.io.InputStream;
144import java.io.OutputStream;
145import java.io.PrintWriter;
146import java.io.RandomAccessFile;
147import java.security.InvalidAlgorithmParameterException;
148import java.security.InvalidKeyException;
149import java.security.Key;
150import java.security.MessageDigest;
151import java.security.NoSuchAlgorithmException;
152import java.security.SecureRandom;
153import java.security.spec.InvalidKeySpecException;
154import java.security.spec.KeySpec;
155import java.text.SimpleDateFormat;
156import java.util.ArrayDeque;
157import java.util.ArrayList;
158import java.util.Arrays;
159import java.util.Collections;
160import java.util.Date;
161import java.util.HashMap;
162import java.util.HashSet;
163import java.util.Iterator;
164import java.util.List;
165import java.util.Map.Entry;
166import java.util.Objects;
167import java.util.Queue;
168import java.util.Random;
169import java.util.Set;
170import java.util.TreeMap;
171import java.util.concurrent.CountDownLatch;
172import java.util.concurrent.TimeUnit;
173import java.util.concurrent.atomic.AtomicBoolean;
174import java.util.concurrent.atomic.AtomicInteger;
175import java.util.concurrent.atomic.AtomicLong;
176import java.util.zip.Deflater;
177import java.util.zip.DeflaterOutputStream;
178import java.util.zip.InflaterInputStream;
179
180import javax.crypto.BadPaddingException;
181import javax.crypto.Cipher;
182import javax.crypto.CipherInputStream;
183import javax.crypto.CipherOutputStream;
184import javax.crypto.IllegalBlockSizeException;
185import javax.crypto.NoSuchPaddingException;
186import javax.crypto.SecretKey;
187import javax.crypto.SecretKeyFactory;
188import javax.crypto.spec.IvParameterSpec;
189import javax.crypto.spec.PBEKeySpec;
190import javax.crypto.spec.SecretKeySpec;
191
192public class BackupManagerService implements BackupManagerServiceInterface {
193
194    private static final String TAG = "BackupManagerService";
195    static final boolean DEBUG = true;
196    static final boolean MORE_DEBUG = false;
197    static final boolean DEBUG_SCHEDULING = MORE_DEBUG || true;
198
199    // File containing backup-enabled state.  Contains a single byte;
200    // nonzero == enabled.  File missing or contains a zero byte == disabled.
201    static final String BACKUP_ENABLE_FILE = "backup_enabled";
202
203    // System-private key used for backing up an app's widget state.  Must
204    // begin with U+FFxx by convention (we reserve all keys starting
205    // with U+FF00 or higher for system use).
206    static final String KEY_WIDGET_STATE = "\uffed\uffedwidget";
207
208    // Historical and current algorithm names
209    static final String PBKDF_CURRENT = "PBKDF2WithHmacSHA1";
210    static final String PBKDF_FALLBACK = "PBKDF2WithHmacSHA1And8bit";
211
212    // Name and current contents version of the full-backup manifest file
213    //
214    // Manifest version history:
215    //
216    // 1 : initial release
217    static final String BACKUP_MANIFEST_FILENAME = "_manifest";
218    static final int BACKUP_MANIFEST_VERSION = 1;
219
220    // External archive format version history:
221    //
222    // 1 : initial release
223    // 2 : no format change per se; version bump to facilitate PBKDF2 version skew detection
224    // 3 : introduced "_meta" metadata file; no other format change per se
225    // 4 : added support for new device-encrypted storage locations
226    // 5 : added support for key-value packages
227    static final int BACKUP_FILE_VERSION = 5;
228    static final String BACKUP_FILE_HEADER_MAGIC = "ANDROID BACKUP\n";
229    static final int BACKUP_PW_FILE_VERSION = 2;
230    static final String BACKUP_METADATA_FILENAME = "_meta";
231    static final int BACKUP_METADATA_VERSION = 1;
232    static final int BACKUP_WIDGET_METADATA_TOKEN = 0x01FFED01;
233
234    static final int TAR_HEADER_LONG_RADIX = 8;
235    static final int TAR_HEADER_OFFSET_FILESIZE = 124;
236    static final int TAR_HEADER_LENGTH_FILESIZE = 12;
237    static final int TAR_HEADER_OFFSET_MODTIME = 136;
238    static final int TAR_HEADER_LENGTH_MODTIME = 12;
239    static final int TAR_HEADER_OFFSET_MODE = 100;
240    static final int TAR_HEADER_LENGTH_MODE = 8;
241    static final int TAR_HEADER_OFFSET_PATH_PREFIX = 345;
242    static final int TAR_HEADER_LENGTH_PATH_PREFIX = 155;
243    static final int TAR_HEADER_OFFSET_PATH = 0;
244    static final int TAR_HEADER_LENGTH_PATH = 100;
245    static final int TAR_HEADER_OFFSET_TYPE_CHAR = 156;
246
247    static final boolean COMPRESS_FULL_BACKUPS = true; // should be true in production
248
249    static final String SETTINGS_PACKAGE = "com.android.providers.settings";
250    static final String SHARED_BACKUP_AGENT_PACKAGE = "com.android.sharedstoragebackup";
251    static final String SERVICE_ACTION_TRANSPORT_HOST = "android.backup.TRANSPORT_HOST";
252
253    // Retry interval for clear/init when the transport is unavailable
254    private static final long TRANSPORT_RETRY_INTERVAL = 1 * AlarmManager.INTERVAL_HOUR;
255
256    private static final String RUN_BACKUP_ACTION = "android.app.backup.intent.RUN";
257    private static final String RUN_INITIALIZE_ACTION = "android.app.backup.intent.INIT";
258    private static final int MSG_RUN_BACKUP = 1;
259    private static final int MSG_RUN_ADB_BACKUP = 2;
260    private static final int MSG_RUN_RESTORE = 3;
261    private static final int MSG_RUN_CLEAR = 4;
262    private static final int MSG_RUN_INITIALIZE = 5;
263    private static final int MSG_RUN_GET_RESTORE_SETS = 6;
264    private static final int MSG_RESTORE_SESSION_TIMEOUT = 8;
265    private static final int MSG_FULL_CONFIRMATION_TIMEOUT = 9;
266    private static final int MSG_RUN_ADB_RESTORE = 10;
267    private static final int MSG_RETRY_INIT = 11;
268    private static final int MSG_RETRY_CLEAR = 12;
269    private static final int MSG_WIDGET_BROADCAST = 13;
270    private static final int MSG_RUN_FULL_TRANSPORT_BACKUP = 14;
271    private static final int MSG_REQUEST_BACKUP = 15;
272    private static final int MSG_SCHEDULE_BACKUP_PACKAGE = 16;
273    private static final int MSG_BACKUP_OPERATION_TIMEOUT = 17;
274    private static final int MSG_RESTORE_OPERATION_TIMEOUT = 18;
275
276    // backup task state machine tick
277    static final int MSG_BACKUP_RESTORE_STEP = 20;
278    static final int MSG_OP_COMPLETE = 21;
279
280    // Timeout interval for deciding that a bind or clear-data has taken too long
281    static final long TIMEOUT_INTERVAL = 10 * 1000;
282
283    // Timeout intervals for agent backup & restore operations
284    static final long TIMEOUT_BACKUP_INTERVAL = 30 * 1000;
285    static final long TIMEOUT_FULL_BACKUP_INTERVAL = 5 * 60 * 1000;
286    static final long TIMEOUT_SHARED_BACKUP_INTERVAL = 30 * 60 * 1000;
287    static final long TIMEOUT_RESTORE_INTERVAL = 60 * 1000;
288    static final long TIMEOUT_RESTORE_FINISHED_INTERVAL = 30 * 1000;
289
290    // User confirmation timeout for a full backup/restore operation.  It's this long in
291    // order to give them time to enter the backup password.
292    static final long TIMEOUT_FULL_CONFIRMATION = 60 * 1000;
293
294    // How long between attempts to perform a full-data backup of any given app
295    static final long MIN_FULL_BACKUP_INTERVAL = 1000 * 60 * 60 * 24; // one day
296
297    // If an app is busy when we want to do a full-data backup, how long to defer the retry.
298    // This is fuzzed, so there are two parameters; backoff_min + Rand[0, backoff_fuzz)
299    static final long BUSY_BACKOFF_MIN_MILLIS = 1000 * 60 * 60;  // one hour
300    static final int BUSY_BACKOFF_FUZZ = 1000 * 60 * 60 * 2;  // two hours
301
302    Context mContext;
303    private PackageManager mPackageManager;
304    IPackageManager mPackageManagerBinder;
305    private IActivityManager mActivityManager;
306    private PowerManager mPowerManager;
307    private AlarmManager mAlarmManager;
308    private IStorageManager mStorageManager;
309
310    IBackupManager mBackupManagerBinder;
311
312    private final TransportManager mTransportManager;
313
314    boolean mEnabled;   // access to this is synchronized on 'this'
315    boolean mProvisioned;
316    boolean mAutoRestore;
317    PowerManager.WakeLock mWakelock;
318    HandlerThread mHandlerThread;
319    BackupHandler mBackupHandler;
320    PendingIntent mRunBackupIntent, mRunInitIntent;
321    BroadcastReceiver mRunBackupReceiver, mRunInitReceiver;
322    // map UIDs to the set of participating packages under that UID
323    final SparseArray<HashSet<String>> mBackupParticipants
324            = new SparseArray<HashSet<String>>();
325    // set of backup services that have pending changes
326    class BackupRequest {
327        public String packageName;
328
329        BackupRequest(String pkgName) {
330            packageName = pkgName;
331        }
332
333        public String toString() {
334            return "BackupRequest{pkg=" + packageName + "}";
335        }
336    }
337    // Backups that we haven't started yet.  Keys are package names.
338    HashMap<String,BackupRequest> mPendingBackups
339            = new HashMap<String,BackupRequest>();
340
341    // Pseudoname that we use for the Package Manager metadata "package"
342    static final String PACKAGE_MANAGER_SENTINEL = "@pm@";
343
344    // locking around the pending-backup management
345    final Object mQueueLock = new Object();
346
347    // The thread performing the sequence of queued backups binds to each app's agent
348    // in succession.  Bind notifications are asynchronously delivered through the
349    // Activity Manager; use this lock object to signal when a requested binding has
350    // completed.
351    final Object mAgentConnectLock = new Object();
352    IBackupAgent mConnectedAgent;
353    volatile boolean mBackupRunning;
354    volatile boolean mConnecting;
355    volatile long mLastBackupPass;
356
357    // For debugging, we maintain a progress trace of operations during backup
358    static final boolean DEBUG_BACKUP_TRACE = true;
359    final List<String> mBackupTrace = new ArrayList<String>();
360
361    // A similar synchronization mechanism around clearing apps' data for restore
362    final Object mClearDataLock = new Object();
363    volatile boolean mClearingData;
364
365    @GuardedBy("mPendingRestores")
366    private boolean mIsRestoreInProgress;
367    @GuardedBy("mPendingRestores")
368    private final Queue<PerformUnifiedRestoreTask> mPendingRestores = new ArrayDeque<>();
369
370    ActiveRestoreSession mActiveRestoreSession;
371
372    // Watch the device provisioning operation during setup
373    ContentObserver mProvisionedObserver;
374
375    // The published binder is actually to a singleton trampoline object that calls
376    // through to the proper code.  This indirection lets us turn down the heavy
377    // implementation object on the fly without disturbing binders that have been
378    // cached elsewhere in the system.
379    static Trampoline sInstance;
380    static Trampoline getInstance() {
381        // Always constructed during system bringup, so no need to lazy-init
382        return sInstance;
383    }
384
385    public static final class Lifecycle extends SystemService {
386
387        public Lifecycle(Context context) {
388            super(context);
389            sInstance = new Trampoline(context);
390        }
391
392        @Override
393        public void onStart() {
394            publishBinderService(Context.BACKUP_SERVICE, sInstance);
395        }
396
397        @Override
398        public void onUnlockUser(int userId) {
399            if (userId == UserHandle.USER_SYSTEM) {
400                sInstance.initialize(userId);
401
402                // Migrate legacy setting
403                if (!backupSettingMigrated(userId)) {
404                    if (DEBUG) {
405                        Slog.i(TAG, "Backup enable apparently not migrated");
406                    }
407                    final ContentResolver r = sInstance.mContext.getContentResolver();
408                    final int enableState = Settings.Secure.getIntForUser(r,
409                            Settings.Secure.BACKUP_ENABLED, -1, userId);
410                    if (enableState >= 0) {
411                        if (DEBUG) {
412                            Slog.i(TAG, "Migrating enable state " + (enableState != 0));
413                        }
414                        writeBackupEnableState(enableState != 0, userId);
415                        Settings.Secure.putStringForUser(r,
416                                Settings.Secure.BACKUP_ENABLED, null, userId);
417                    } else {
418                        if (DEBUG) {
419                            Slog.i(TAG, "Backup not yet configured; retaining null enable state");
420                        }
421                    }
422                }
423
424                try {
425                    sInstance.setBackupEnabled(readBackupEnableState(userId));
426                } catch (RemoteException e) {
427                    // can't happen; it's a local object
428                }
429            }
430        }
431    }
432
433    class ProvisionedObserver extends ContentObserver {
434        public ProvisionedObserver(Handler handler) {
435            super(handler);
436        }
437
438        public void onChange(boolean selfChange) {
439            final boolean wasProvisioned = mProvisioned;
440            final boolean isProvisioned = deviceIsProvisioned();
441            // latch: never unprovision
442            mProvisioned = wasProvisioned || isProvisioned;
443            if (MORE_DEBUG) {
444                Slog.d(TAG, "Provisioning change: was=" + wasProvisioned
445                        + " is=" + isProvisioned + " now=" + mProvisioned);
446            }
447
448            synchronized (mQueueLock) {
449                if (mProvisioned && !wasProvisioned && mEnabled) {
450                    // we're now good to go, so start the backup alarms
451                    if (MORE_DEBUG) Slog.d(TAG, "Now provisioned, so starting backups");
452                    KeyValueBackupJob.schedule(mContext);
453                    scheduleNextFullBackupJob(0);
454                }
455            }
456        }
457    }
458
459    class RestoreGetSetsParams {
460        public IBackupTransport transport;
461        public ActiveRestoreSession session;
462        public IRestoreObserver observer;
463        public IBackupManagerMonitor monitor;
464
465        RestoreGetSetsParams(IBackupTransport _transport, ActiveRestoreSession _session,
466                IRestoreObserver _observer, IBackupManagerMonitor _monitor) {
467            transport = _transport;
468            session = _session;
469            observer = _observer;
470            monitor = _monitor;
471        }
472    }
473
474    class RestoreParams {
475        public IBackupTransport transport;
476        public String dirName;
477        public IRestoreObserver observer;
478        public IBackupManagerMonitor monitor;
479        public long token;
480        public PackageInfo pkgInfo;
481        public int pmToken; // in post-install restore, the PM's token for this transaction
482        public boolean isSystemRestore;
483        public String[] filterSet;
484
485        /**
486         * Restore a single package; no kill after restore
487         */
488        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
489                IBackupManagerMonitor _monitor, long _token, PackageInfo _pkg) {
490            transport = _transport;
491            dirName = _dirName;
492            observer = _obs;
493            monitor = _monitor;
494            token = _token;
495            pkgInfo = _pkg;
496            pmToken = 0;
497            isSystemRestore = false;
498            filterSet = null;
499        }
500
501        /**
502         * Restore at install: PM token needed, kill after restore
503         */
504        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
505                IBackupManagerMonitor _monitor, long _token, String _pkgName, int _pmToken) {
506            transport = _transport;
507            dirName = _dirName;
508            observer = _obs;
509            monitor = _monitor;
510            token = _token;
511            pkgInfo = null;
512            pmToken = _pmToken;
513            isSystemRestore = false;
514            filterSet = new String[] { _pkgName };
515        }
516
517        /**
518         * Restore everything possible.  This is the form that Setup Wizard or similar
519         * restore UXes use.
520         */
521        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
522                IBackupManagerMonitor _monitor, long _token) {
523            transport = _transport;
524            dirName = _dirName;
525            observer = _obs;
526            monitor = _monitor;
527            token = _token;
528            pkgInfo = null;
529            pmToken = 0;
530            isSystemRestore = true;
531            filterSet = null;
532        }
533
534        /**
535         * Restore some set of packages.  Leave this one up to the caller to specify
536         * whether it's to be considered a system-level restore.
537         */
538        RestoreParams(IBackupTransport _transport, String _dirName, IRestoreObserver _obs,
539                IBackupManagerMonitor _monitor, long _token,
540                String[] _filterSet, boolean _isSystemRestore) {
541            transport = _transport;
542            dirName = _dirName;
543            observer = _obs;
544            monitor = _monitor;
545            token = _token;
546            pkgInfo = null;
547            pmToken = 0;
548            isSystemRestore = _isSystemRestore;
549            filterSet = _filterSet;
550        }
551    }
552
553    class ClearParams {
554        public IBackupTransport transport;
555        public PackageInfo packageInfo;
556
557        ClearParams(IBackupTransport _transport, PackageInfo _info) {
558            transport = _transport;
559            packageInfo = _info;
560        }
561    }
562
563    class ClearRetryParams {
564        public String transportName;
565        public String packageName;
566
567        ClearRetryParams(String transport, String pkg) {
568            transportName = transport;
569            packageName = pkg;
570        }
571    }
572
573    // Parameters used by adbBackup() and adbRestore()
574    class AdbParams {
575        public ParcelFileDescriptor fd;
576        public final AtomicBoolean latch;
577        public IFullBackupRestoreObserver observer;
578        public String curPassword;     // filled in by the confirmation step
579        public String encryptPassword;
580
581        AdbParams() {
582            latch = new AtomicBoolean(false);
583        }
584    }
585
586    class AdbBackupParams extends AdbParams {
587        public boolean includeApks;
588        public boolean includeObbs;
589        public boolean includeShared;
590        public boolean doWidgets;
591        public boolean allApps;
592        public boolean includeSystem;
593        public boolean doCompress;
594        public boolean includeKeyValue;
595        public String[] packages;
596
597        AdbBackupParams(ParcelFileDescriptor output, boolean saveApks, boolean saveObbs,
598                boolean saveShared, boolean alsoWidgets, boolean doAllApps, boolean doSystem,
599                boolean compress, boolean doKeyValue, String[] pkgList) {
600            fd = output;
601            includeApks = saveApks;
602            includeObbs = saveObbs;
603            includeShared = saveShared;
604            doWidgets = alsoWidgets;
605            allApps = doAllApps;
606            includeSystem = doSystem;
607            doCompress = compress;
608            includeKeyValue = doKeyValue;
609            packages = pkgList;
610        }
611    }
612
613    class AdbRestoreParams extends AdbParams {
614        AdbRestoreParams(ParcelFileDescriptor input) {
615            fd = input;
616        }
617    }
618
619    class BackupParams {
620        public IBackupTransport transport;
621        public String dirName;
622        public ArrayList<String> kvPackages;
623        public ArrayList<String> fullPackages;
624        public IBackupObserver observer;
625        public IBackupManagerMonitor monitor;
626        public boolean userInitiated;
627        public boolean nonIncrementalBackup;
628
629        BackupParams(IBackupTransport transport, String dirName, ArrayList<String> kvPackages,
630                ArrayList<String> fullPackages, IBackupObserver observer,
631                IBackupManagerMonitor monitor,boolean userInitiated, boolean nonIncrementalBackup) {
632            this.transport = transport;
633            this.dirName = dirName;
634            this.kvPackages = kvPackages;
635            this.fullPackages = fullPackages;
636            this.observer = observer;
637            this.monitor = monitor;
638            this.userInitiated = userInitiated;
639            this.nonIncrementalBackup = nonIncrementalBackup;
640        }
641    }
642
643    // Bookkeeping of in-flight operations for timeout etc. purposes.  The operation
644    // token is the index of the entry in the pending-operations list.
645    static final int OP_PENDING = 0;
646    static final int OP_ACKNOWLEDGED = 1;
647    static final int OP_TIMEOUT = -1;
648
649    // Waiting for backup agent to respond during backup operation.
650    static final int OP_TYPE_BACKUP_WAIT = 0;
651
652    // Waiting for backup agent to respond during restore operation.
653    static final int OP_TYPE_RESTORE_WAIT = 1;
654
655    // An entire backup operation spanning multiple packages.
656    private static final int OP_TYPE_BACKUP = 2;
657
658    class Operation {
659        int state;
660        final BackupRestoreTask callback;
661        final int type;
662
663        Operation(int initialState, BackupRestoreTask callbackObj, int type) {
664            state = initialState;
665            callback = callbackObj;
666            this.type = type;
667        }
668    }
669
670    /**
671     * mCurrentOperations contains the list of currently active operations.
672     *
673     * If type of operation is OP_TYPE_WAIT, it are waiting for an ack or timeout.
674     * An operation wraps a BackupRestoreTask within it.
675     * It's the responsibility of this task to remove the operation from this array.
676     *
677     * A BackupRestore task gets notified of ack/timeout for the operation via
678     * BackupRestoreTask#handleCancel, BackupRestoreTask#operationComplete and notifyAll called
679     * on the mCurrentOpLock. {@link BackupManagerService#waitUntilOperationComplete(int)} is
680     * used in various places to 'wait' for notifyAll and detect change of pending state of an
681     * operation. So typically, an operation will be removed from this array by:
682     *   - BackupRestoreTask#handleCancel and
683     *   - BackupRestoreTask#operationComplete OR waitUntilOperationComplete. Do not remove at both
684     *     these places because waitUntilOperationComplete relies on the operation being present to
685     *     determine its completion status.
686     *
687     * If type of operation is OP_BACKUP, it is a task running backups. It provides a handle to
688     * cancel backup tasks.
689     */
690    @GuardedBy("mCurrentOpLock")
691    final SparseArray<Operation> mCurrentOperations = new SparseArray<Operation>();
692    final Object mCurrentOpLock = new Object();
693    final Random mTokenGenerator = new Random();
694
695    final SparseArray<AdbParams> mAdbBackupRestoreConfirmations = new SparseArray<AdbParams>();
696
697    // Where we keep our journal files and other bookkeeping
698    File mBaseStateDir;
699    File mDataDir;
700    File mJournalDir;
701    File mJournal;
702
703    // Backup password, if any, and the file where it's saved.  What is stored is not the
704    // password text itself; it's the result of a PBKDF2 hash with a randomly chosen (but
705    // persisted) salt.  Validation is performed by running the challenge text through the
706    // same PBKDF2 cycle with the persisted salt; if the resulting derived key string matches
707    // the saved hash string, then the challenge text matches the originally supplied
708    // password text.
709    private final SecureRandom mRng = new SecureRandom();
710    private String mPasswordHash;
711    private File mPasswordHashFile;
712    private int mPasswordVersion;
713    private File mPasswordVersionFile;
714    private byte[] mPasswordSalt;
715
716    // Configuration of PBKDF2 that we use for generating pw hashes and intermediate keys
717    static final int PBKDF2_HASH_ROUNDS = 10000;
718    static final int PBKDF2_KEY_SIZE = 256;     // bits
719    static final int PBKDF2_SALT_SIZE = 512;    // bits
720    static final String ENCRYPTION_ALGORITHM_NAME = "AES-256";
721
722    // Keep a log of all the apps we've ever backed up, and what the
723    // dataset tokens are for both the current backup dataset and
724    // the ancestral dataset.
725    private File mEverStored;
726    HashSet<String> mEverStoredApps = new HashSet<String>();
727
728    static final int CURRENT_ANCESTRAL_RECORD_VERSION = 1;  // increment when the schema changes
729    File mTokenFile;
730    Set<String> mAncestralPackages = null;
731    long mAncestralToken = 0;
732    long mCurrentToken = 0;
733
734    // Persistently track the need to do a full init
735    static final String INIT_SENTINEL_FILE_NAME = "_need_init_";
736    HashSet<String> mPendingInits = new HashSet<String>();  // transport names
737
738    // Round-robin queue for scheduling full backup passes
739    static final int SCHEDULE_FILE_VERSION = 1; // current version of the schedule file
740    class FullBackupEntry implements Comparable<FullBackupEntry> {
741        String packageName;
742        long lastBackup;
743
744        FullBackupEntry(String pkg, long when) {
745            packageName = pkg;
746            lastBackup = when;
747        }
748
749        @Override
750        public int compareTo(FullBackupEntry other) {
751            if (lastBackup < other.lastBackup) return -1;
752            else if (lastBackup > other.lastBackup) return 1;
753            else return 0;
754        }
755    }
756
757    File mFullBackupScheduleFile;
758    // If we're running a schedule-driven full backup, this is the task instance doing it
759
760    @GuardedBy("mQueueLock")
761    PerformFullTransportBackupTask mRunningFullBackupTask;
762
763    @GuardedBy("mQueueLock")
764    ArrayList<FullBackupEntry> mFullBackupQueue;
765
766    // Utility: build a new random integer token
767    @Override
768    public int generateRandomIntegerToken() {
769        int token;
770        do {
771            synchronized (mTokenGenerator) {
772                token = mTokenGenerator.nextInt();
773            }
774        } while (token < 0);
775        return token;
776    }
777
778    // High level policy: apps are generally ineligible for backup if certain conditions apply
779    public static boolean appIsEligibleForBackup(ApplicationInfo app) {
780        // 1. their manifest states android:allowBackup="false"
781        if ((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) {
782            return false;
783        }
784
785        // 2. they run as a system-level uid but do not supply their own backup agent
786        if ((app.uid < Process.FIRST_APPLICATION_UID) && (app.backupAgentName == null)) {
787            return false;
788        }
789
790        // 3. it is the special shared-storage backup package used for 'adb backup'
791        if (app.packageName.equals(BackupManagerService.SHARED_BACKUP_AGENT_PACKAGE)) {
792            return false;
793        }
794
795        return true;
796    }
797
798    // Checks if the app is in a stopped state, that means it won't receive broadcasts.
799    private static boolean appIsStopped(ApplicationInfo app) {
800        return ((app.flags & ApplicationInfo.FLAG_STOPPED) != 0);
801    }
802
803    // We also avoid backups of 'disabled' apps
804    private static boolean appIsDisabled(ApplicationInfo app, PackageManager pm) {
805        switch (pm.getApplicationEnabledSetting(app.packageName)) {
806            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
807            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER:
808            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED:
809                return true;
810
811            default:
812                return false;
813        }
814    }
815
816    /* does *not* check overall backup eligibility policy! */
817    private static boolean appGetsFullBackup(PackageInfo pkg) {
818        if (pkg.applicationInfo.backupAgentName != null) {
819            // If it has an agent, it gets full backups only if it says so
820            return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FULL_BACKUP_ONLY) != 0;
821        }
822
823        // No agent or fullBackupOnly="true" means we do indeed perform full-data backups for it
824        return true;
825    }
826
827    /* adb backup: is this app only capable of doing key/value?  We say otherwise if
828     * the app has a backup agent and does not say fullBackupOnly,
829     */
830    private static boolean appIsKeyValueOnly(PackageInfo pkg) {
831        return !appGetsFullBackup(pkg);
832    }
833
834    // ----- Asynchronous backup/restore handler thread -----
835
836    private class BackupHandler extends Handler {
837        public BackupHandler(Looper looper) {
838            super(looper);
839        }
840
841        public void handleMessage(Message msg) {
842
843            switch (msg.what) {
844            case MSG_RUN_BACKUP:
845            {
846                mLastBackupPass = System.currentTimeMillis();
847
848                IBackupTransport transport = mTransportManager.getCurrentTransportBinder();
849                if (transport == null) {
850                    Slog.v(TAG, "Backup requested but no transport available");
851                    synchronized (mQueueLock) {
852                        mBackupRunning = false;
853                    }
854                    mWakelock.release();
855                    break;
856                }
857
858                // snapshot the pending-backup set and work on that
859                ArrayList<BackupRequest> queue = new ArrayList<BackupRequest>();
860                File oldJournal = mJournal;
861                synchronized (mQueueLock) {
862                    // Do we have any work to do?  Construct the work queue
863                    // then release the synchronization lock to actually run
864                    // the backup.
865                    if (mPendingBackups.size() > 0) {
866                        for (BackupRequest b: mPendingBackups.values()) {
867                            queue.add(b);
868                        }
869                        if (DEBUG) Slog.v(TAG, "clearing pending backups");
870                        mPendingBackups.clear();
871
872                        // Start a new backup-queue journal file too
873                        mJournal = null;
874
875                    }
876                }
877
878                // At this point, we have started a new journal file, and the old
879                // file identity is being passed to the backup processing task.
880                // When it completes successfully, that old journal file will be
881                // deleted.  If we crash prior to that, the old journal is parsed
882                // at next boot and the journaled requests fulfilled.
883                boolean staged = true;
884                if (queue.size() > 0) {
885                    // Spin up a backup state sequence and set it running
886                    try {
887                        String dirName = transport.transportDirName();
888                        PerformBackupTask pbt = new PerformBackupTask(transport, dirName, queue,
889                                oldJournal, null, null, Collections.<String>emptyList(), false,
890                                false /* nonIncremental */);
891                        Message pbtMessage = obtainMessage(MSG_BACKUP_RESTORE_STEP, pbt);
892                        sendMessage(pbtMessage);
893                    } catch (Exception e) {
894                        // unable to ask the transport its dir name -- transient failure, since
895                        // the above check succeeded.  Try again next time.
896                        Slog.e(TAG, "Transport became unavailable attempting backup"
897                                + " or error initializing backup task", e);
898                        staged = false;
899                    }
900                } else {
901                    Slog.v(TAG, "Backup requested but nothing pending");
902                    staged = false;
903                }
904
905                if (!staged) {
906                    // if we didn't actually hand off the wakelock, rewind until next time
907                    synchronized (mQueueLock) {
908                        mBackupRunning = false;
909                    }
910                    mWakelock.release();
911                }
912                break;
913            }
914
915            case MSG_BACKUP_RESTORE_STEP:
916            {
917                try {
918                    BackupRestoreTask task = (BackupRestoreTask) msg.obj;
919                    if (MORE_DEBUG) Slog.v(TAG, "Got next step for " + task + ", executing");
920                    task.execute();
921                } catch (ClassCastException e) {
922                    Slog.e(TAG, "Invalid backup task in flight, obj=" + msg.obj);
923                }
924                break;
925            }
926
927            case MSG_OP_COMPLETE:
928            {
929                try {
930                    Pair<BackupRestoreTask, Long> taskWithResult =
931                            (Pair<BackupRestoreTask, Long>) msg.obj;
932                    taskWithResult.first.operationComplete(taskWithResult.second);
933                } catch (ClassCastException e) {
934                    Slog.e(TAG, "Invalid completion in flight, obj=" + msg.obj);
935                }
936                break;
937            }
938
939            case MSG_RUN_ADB_BACKUP:
940            {
941                // TODO: refactor full backup to be a looper-based state machine
942                // similar to normal backup/restore.
943                AdbBackupParams params = (AdbBackupParams)msg.obj;
944                PerformAdbBackupTask task = new PerformAdbBackupTask(params.fd,
945                        params.observer, params.includeApks, params.includeObbs,
946                        params.includeShared, params.doWidgets, params.curPassword,
947                        params.encryptPassword, params.allApps, params.includeSystem,
948                        params.doCompress, params.includeKeyValue, params.packages, params.latch);
949                (new Thread(task, "adb-backup")).start();
950                break;
951            }
952
953            case MSG_RUN_FULL_TRANSPORT_BACKUP:
954            {
955                PerformFullTransportBackupTask task = (PerformFullTransportBackupTask) msg.obj;
956                (new Thread(task, "transport-backup")).start();
957                break;
958            }
959
960            case MSG_RUN_RESTORE:
961            {
962                RestoreParams params = (RestoreParams)msg.obj;
963                Slog.d(TAG, "MSG_RUN_RESTORE observer=" + params.observer);
964
965                PerformUnifiedRestoreTask task = new PerformUnifiedRestoreTask(params.transport,
966                        params.observer, params.monitor, params.token, params.pkgInfo,
967                        params.pmToken, params.isSystemRestore, params.filterSet);
968
969                synchronized (mPendingRestores) {
970                    if (mIsRestoreInProgress) {
971                        if (DEBUG) {
972                            Slog.d(TAG, "Restore in progress, queueing.");
973                        }
974                        mPendingRestores.add(task);
975                        // This task will be picked up and executed when the the currently running
976                        // restore task finishes.
977                    } else {
978                        if (DEBUG) {
979                            Slog.d(TAG, "Starting restore.");
980                        }
981                        mIsRestoreInProgress = true;
982                        Message restoreMsg = obtainMessage(MSG_BACKUP_RESTORE_STEP, task);
983                        sendMessage(restoreMsg);
984                    }
985                }
986                break;
987            }
988
989            case MSG_RUN_ADB_RESTORE:
990            {
991                // TODO: refactor full restore to be a looper-based state machine
992                // similar to normal backup/restore.
993                AdbRestoreParams params = (AdbRestoreParams)msg.obj;
994                PerformAdbRestoreTask task = new PerformAdbRestoreTask(params.fd,
995                        params.curPassword, params.encryptPassword,
996                        params.observer, params.latch);
997                (new Thread(task, "adb-restore")).start();
998                break;
999            }
1000
1001            case MSG_RUN_CLEAR:
1002            {
1003                ClearParams params = (ClearParams)msg.obj;
1004                (new PerformClearTask(params.transport, params.packageInfo)).run();
1005                break;
1006            }
1007
1008            case MSG_RETRY_CLEAR:
1009            {
1010                // reenqueues if the transport remains unavailable
1011                ClearRetryParams params = (ClearRetryParams)msg.obj;
1012                clearBackupData(params.transportName, params.packageName);
1013                break;
1014            }
1015
1016            case MSG_RUN_INITIALIZE:
1017            {
1018                HashSet<String> queue;
1019
1020                // Snapshot the pending-init queue and work on that
1021                synchronized (mQueueLock) {
1022                    queue = new HashSet<String>(mPendingInits);
1023                    mPendingInits.clear();
1024                }
1025
1026                (new PerformInitializeTask(queue)).run();
1027                break;
1028            }
1029
1030            case MSG_RETRY_INIT:
1031            {
1032                synchronized (mQueueLock) {
1033                    recordInitPendingLocked(msg.arg1 != 0, (String)msg.obj);
1034                    mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1035                            mRunInitIntent);
1036                }
1037                break;
1038            }
1039
1040            case MSG_RUN_GET_RESTORE_SETS:
1041            {
1042                // Like other async operations, this is entered with the wakelock held
1043                RestoreSet[] sets = null;
1044                RestoreGetSetsParams params = (RestoreGetSetsParams)msg.obj;
1045                try {
1046                    sets = params.transport.getAvailableRestoreSets();
1047                    // cache the result in the active session
1048                    synchronized (params.session) {
1049                        params.session.mRestoreSets = sets;
1050                    }
1051                    if (sets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
1052                } catch (Exception e) {
1053                    Slog.e(TAG, "Error from transport getting set list: " + e.getMessage());
1054                } finally {
1055                    if (params.observer != null) {
1056                        try {
1057                            params.observer.restoreSetsAvailable(sets);
1058                        } catch (RemoteException re) {
1059                            Slog.e(TAG, "Unable to report listing to observer");
1060                        } catch (Exception e) {
1061                            Slog.e(TAG, "Restore observer threw: " + e.getMessage());
1062                        }
1063                    }
1064
1065                    // Done: reset the session timeout clock
1066                    removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
1067                    sendEmptyMessageDelayed(MSG_RESTORE_SESSION_TIMEOUT, TIMEOUT_RESTORE_INTERVAL);
1068
1069                    mWakelock.release();
1070                }
1071                break;
1072            }
1073
1074            case MSG_BACKUP_OPERATION_TIMEOUT:
1075            case MSG_RESTORE_OPERATION_TIMEOUT:
1076            {
1077                Slog.d(TAG, "Timeout message received for token=" + Integer.toHexString(msg.arg1));
1078                handleCancel(msg.arg1, false);
1079                break;
1080            }
1081
1082            case MSG_RESTORE_SESSION_TIMEOUT:
1083            {
1084                synchronized (BackupManagerService.this) {
1085                    if (mActiveRestoreSession != null) {
1086                        // Client app left the restore session dangling.  We know that it
1087                        // can't be in the middle of an actual restore operation because
1088                        // the timeout is suspended while a restore is in progress.  Clean
1089                        // up now.
1090                        Slog.w(TAG, "Restore session timed out; aborting");
1091                        mActiveRestoreSession.markTimedOut();
1092                        post(mActiveRestoreSession.new EndRestoreRunnable(
1093                                BackupManagerService.this, mActiveRestoreSession));
1094                    }
1095                }
1096                break;
1097            }
1098
1099            case MSG_FULL_CONFIRMATION_TIMEOUT:
1100            {
1101                synchronized (mAdbBackupRestoreConfirmations) {
1102                    AdbParams params = mAdbBackupRestoreConfirmations.get(msg.arg1);
1103                    if (params != null) {
1104                        Slog.i(TAG, "Full backup/restore timed out waiting for user confirmation");
1105
1106                        // Release the waiter; timeout == completion
1107                        signalAdbBackupRestoreCompletion(params);
1108
1109                        // Remove the token from the set
1110                        mAdbBackupRestoreConfirmations.delete(msg.arg1);
1111
1112                        // Report a timeout to the observer, if any
1113                        if (params.observer != null) {
1114                            try {
1115                                params.observer.onTimeout();
1116                            } catch (RemoteException e) {
1117                                /* don't care if the app has gone away */
1118                            }
1119                        }
1120                    } else {
1121                        Slog.d(TAG, "couldn't find params for token " + msg.arg1);
1122                    }
1123                }
1124                break;
1125            }
1126
1127            case MSG_WIDGET_BROADCAST:
1128            {
1129                final Intent intent = (Intent) msg.obj;
1130                mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM);
1131                break;
1132            }
1133
1134            case MSG_REQUEST_BACKUP:
1135            {
1136                BackupParams params = (BackupParams)msg.obj;
1137                if (MORE_DEBUG) {
1138                    Slog.d(TAG, "MSG_REQUEST_BACKUP observer=" + params.observer);
1139                }
1140                ArrayList<BackupRequest> kvQueue = new ArrayList<>();
1141                for (String packageName : params.kvPackages) {
1142                    kvQueue.add(new BackupRequest(packageName));
1143                }
1144                mBackupRunning = true;
1145                mWakelock.acquire();
1146
1147                PerformBackupTask pbt = new PerformBackupTask(params.transport, params.dirName,
1148                        kvQueue, null, params.observer, params.monitor, params.fullPackages, true,
1149                        params.nonIncrementalBackup);
1150                Message pbtMessage = obtainMessage(MSG_BACKUP_RESTORE_STEP, pbt);
1151                sendMessage(pbtMessage);
1152                break;
1153            }
1154
1155            case MSG_SCHEDULE_BACKUP_PACKAGE:
1156            {
1157                String pkgName = (String)msg.obj;
1158                if (MORE_DEBUG) {
1159                    Slog.d(TAG, "MSG_SCHEDULE_BACKUP_PACKAGE " + pkgName);
1160                }
1161                dataChangedImpl(pkgName);
1162                break;
1163            }
1164            }
1165        }
1166    }
1167
1168    // ----- Debug-only backup operation trace -----
1169    void addBackupTrace(String s) {
1170        if (DEBUG_BACKUP_TRACE) {
1171            synchronized (mBackupTrace) {
1172                mBackupTrace.add(s);
1173            }
1174        }
1175    }
1176
1177    void clearBackupTrace() {
1178        if (DEBUG_BACKUP_TRACE) {
1179            synchronized (mBackupTrace) {
1180                mBackupTrace.clear();
1181            }
1182        }
1183    }
1184
1185    // ----- Main service implementation -----
1186
1187    public BackupManagerService(Context context, Trampoline parent) {
1188        mContext = context;
1189        mPackageManager = context.getPackageManager();
1190        mPackageManagerBinder = AppGlobals.getPackageManager();
1191        mActivityManager = ActivityManager.getService();
1192
1193        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
1194        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
1195        mStorageManager = IStorageManager.Stub.asInterface(ServiceManager.getService("mount"));
1196
1197        mBackupManagerBinder = Trampoline.asInterface(parent.asBinder());
1198
1199        // spin up the backup/restore handler thread
1200        mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND);
1201        mHandlerThread.start();
1202        mBackupHandler = new BackupHandler(mHandlerThread.getLooper());
1203
1204        // Set up our bookkeeping
1205        final ContentResolver resolver = context.getContentResolver();
1206        mProvisioned = Settings.Global.getInt(resolver,
1207                Settings.Global.DEVICE_PROVISIONED, 0) != 0;
1208        mAutoRestore = Settings.Secure.getInt(resolver,
1209                Settings.Secure.BACKUP_AUTO_RESTORE, 1) != 0;
1210
1211        mProvisionedObserver = new ProvisionedObserver(mBackupHandler);
1212        resolver.registerContentObserver(
1213                Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
1214                false, mProvisionedObserver);
1215
1216        // If Encrypted file systems is enabled or disabled, this call will return the
1217        // correct directory.
1218        mBaseStateDir = new File(Environment.getDataDirectory(), "backup");
1219        mBaseStateDir.mkdirs();
1220        if (!SELinux.restorecon(mBaseStateDir)) {
1221            Slog.e(TAG, "SELinux restorecon failed on " + mBaseStateDir);
1222        }
1223
1224        // This dir on /cache is managed directly in init.rc
1225        mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup_stage");
1226
1227        mPasswordVersion = 1;       // unless we hear otherwise
1228        mPasswordVersionFile = new File(mBaseStateDir, "pwversion");
1229        if (mPasswordVersionFile.exists()) {
1230            FileInputStream fin = null;
1231            DataInputStream in = null;
1232            try {
1233                fin = new FileInputStream(mPasswordVersionFile);
1234                in = new DataInputStream(fin);
1235                mPasswordVersion = in.readInt();
1236            } catch (IOException e) {
1237                Slog.e(TAG, "Unable to read backup pw version");
1238            } finally {
1239                try {
1240                    if (in != null) in.close();
1241                    if (fin != null) fin.close();
1242                } catch (IOException e) {
1243                    Slog.w(TAG, "Error closing pw version files");
1244                }
1245            }
1246        }
1247
1248        mPasswordHashFile = new File(mBaseStateDir, "pwhash");
1249        if (mPasswordHashFile.exists()) {
1250            FileInputStream fin = null;
1251            DataInputStream in = null;
1252            try {
1253                fin = new FileInputStream(mPasswordHashFile);
1254                in = new DataInputStream(new BufferedInputStream(fin));
1255                // integer length of the salt array, followed by the salt,
1256                // then the hex pw hash string
1257                int saltLen = in.readInt();
1258                byte[] salt = new byte[saltLen];
1259                in.readFully(salt);
1260                mPasswordHash = in.readUTF();
1261                mPasswordSalt = salt;
1262            } catch (IOException e) {
1263                Slog.e(TAG, "Unable to read saved backup pw hash");
1264            } finally {
1265                try {
1266                    if (in != null) in.close();
1267                    if (fin != null) fin.close();
1268                } catch (IOException e) {
1269                    Slog.w(TAG, "Unable to close streams");
1270                }
1271            }
1272        }
1273
1274        // Alarm receivers for scheduled backups & initialization operations
1275        mRunBackupReceiver = new RunBackupReceiver();
1276        IntentFilter filter = new IntentFilter();
1277        filter.addAction(RUN_BACKUP_ACTION);
1278        context.registerReceiver(mRunBackupReceiver, filter,
1279                android.Manifest.permission.BACKUP, null);
1280
1281        mRunInitReceiver = new RunInitializeReceiver();
1282        filter = new IntentFilter();
1283        filter.addAction(RUN_INITIALIZE_ACTION);
1284        context.registerReceiver(mRunInitReceiver, filter,
1285                android.Manifest.permission.BACKUP, null);
1286
1287        Intent backupIntent = new Intent(RUN_BACKUP_ACTION);
1288        backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1289        mRunBackupIntent = PendingIntent.getBroadcast(context, MSG_RUN_BACKUP, backupIntent, 0);
1290
1291        Intent initIntent = new Intent(RUN_INITIALIZE_ACTION);
1292        backupIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1293        mRunInitIntent = PendingIntent.getBroadcast(context, MSG_RUN_INITIALIZE, initIntent, 0);
1294
1295        // Set up the backup-request journaling
1296        mJournalDir = new File(mBaseStateDir, "pending");
1297        mJournalDir.mkdirs();   // creates mBaseStateDir along the way
1298        mJournal = null;        // will be created on first use
1299
1300        // Set up the various sorts of package tracking we do
1301        mFullBackupScheduleFile = new File(mBaseStateDir, "fb-schedule");
1302        initPackageTracking();
1303
1304        // Build our mapping of uid to backup client services.  This implicitly
1305        // schedules a backup pass on the Package Manager metadata the first
1306        // time anything needs to be backed up.
1307        synchronized (mBackupParticipants) {
1308            addPackageParticipantsLocked(null);
1309        }
1310
1311        // Set up our transport options and initialize the default transport
1312        // TODO: Don't create transports that we don't need to?
1313        SystemConfig systemConfig = SystemConfig.getInstance();
1314        Set<ComponentName> transportWhitelist = systemConfig.getBackupTransportWhitelist();
1315
1316        String transport = Settings.Secure.getString(context.getContentResolver(),
1317                Settings.Secure.BACKUP_TRANSPORT);
1318        if (TextUtils.isEmpty(transport)) {
1319            transport = null;
1320        }
1321        String currentTransport = transport;
1322        if (DEBUG) Slog.v(TAG, "Starting with transport " + currentTransport);
1323
1324        mTransportManager = new TransportManager(context, transportWhitelist, currentTransport,
1325                mTransportBoundListener, mHandlerThread.getLooper());
1326        mTransportManager.registerAllTransports();
1327
1328        // Now that we know about valid backup participants, parse any
1329        // leftover journal files into the pending backup set
1330        mBackupHandler.post(() -> parseLeftoverJournals());
1331
1332        // Power management
1333        mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*");
1334    }
1335
1336    private class RunBackupReceiver extends BroadcastReceiver {
1337        public void onReceive(Context context, Intent intent) {
1338            if (RUN_BACKUP_ACTION.equals(intent.getAction())) {
1339                synchronized (mQueueLock) {
1340                    if (mPendingInits.size() > 0) {
1341                        // If there are pending init operations, we process those
1342                        // and then settle into the usual periodic backup schedule.
1343                        if (MORE_DEBUG) Slog.v(TAG, "Init pending at scheduled backup");
1344                        try {
1345                            mAlarmManager.cancel(mRunInitIntent);
1346                            mRunInitIntent.send();
1347                        } catch (PendingIntent.CanceledException ce) {
1348                            Slog.e(TAG, "Run init intent cancelled");
1349                            // can't really do more than bail here
1350                        }
1351                    } else {
1352                        // Don't run backups now if we're disabled or not yet
1353                        // fully set up.
1354                        if (mEnabled && mProvisioned) {
1355                            if (!mBackupRunning) {
1356                                if (DEBUG) Slog.v(TAG, "Running a backup pass");
1357
1358                                // Acquire the wakelock and pass it to the backup thread.  it will
1359                                // be released once backup concludes.
1360                                mBackupRunning = true;
1361                                mWakelock.acquire();
1362
1363                                Message msg = mBackupHandler.obtainMessage(MSG_RUN_BACKUP);
1364                                mBackupHandler.sendMessage(msg);
1365                            } else {
1366                                Slog.i(TAG, "Backup time but one already running");
1367                            }
1368                        } else {
1369                            Slog.w(TAG, "Backup pass but e=" + mEnabled + " p=" + mProvisioned);
1370                        }
1371                    }
1372                }
1373            }
1374        }
1375    }
1376
1377    private class RunInitializeReceiver extends BroadcastReceiver {
1378        public void onReceive(Context context, Intent intent) {
1379            if (RUN_INITIALIZE_ACTION.equals(intent.getAction())) {
1380                synchronized (mQueueLock) {
1381                    if (DEBUG) Slog.v(TAG, "Running a device init");
1382
1383                    // Acquire the wakelock and pass it to the init thread.  it will
1384                    // be released once init concludes.
1385                    mWakelock.acquire();
1386
1387                    Message msg = mBackupHandler.obtainMessage(MSG_RUN_INITIALIZE);
1388                    mBackupHandler.sendMessage(msg);
1389                }
1390            }
1391        }
1392    }
1393
1394    private void initPackageTracking() {
1395        if (MORE_DEBUG) Slog.v(TAG, "` tracking");
1396
1397        // Remember our ancestral dataset
1398        mTokenFile = new File(mBaseStateDir, "ancestral");
1399        try {
1400            RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r");
1401            int version = tf.readInt();
1402            if (version == CURRENT_ANCESTRAL_RECORD_VERSION) {
1403                mAncestralToken = tf.readLong();
1404                mCurrentToken = tf.readLong();
1405
1406                int numPackages = tf.readInt();
1407                if (numPackages >= 0) {
1408                    mAncestralPackages = new HashSet<String>();
1409                    for (int i = 0; i < numPackages; i++) {
1410                        String pkgName = tf.readUTF();
1411                        mAncestralPackages.add(pkgName);
1412                    }
1413                }
1414            }
1415            tf.close();
1416        } catch (FileNotFoundException fnf) {
1417            // Probably innocuous
1418            Slog.v(TAG, "No ancestral data");
1419        } catch (IOException e) {
1420            Slog.w(TAG, "Unable to read token file", e);
1421        }
1422
1423        // Keep a log of what apps we've ever backed up.  Because we might have
1424        // rebooted in the middle of an operation that was removing something from
1425        // this log, we sanity-check its contents here and reconstruct it.
1426        mEverStored = new File(mBaseStateDir, "processed");
1427        File tempProcessedFile = new File(mBaseStateDir, "processed.new");
1428
1429        // If we were in the middle of removing something from the ever-backed-up
1430        // file, there might be a transient "processed.new" file still present.
1431        // Ignore it -- we'll validate "processed" against the current package set.
1432        if (tempProcessedFile.exists()) {
1433            tempProcessedFile.delete();
1434        }
1435
1436        // If there are previous contents, parse them out then start a new
1437        // file to continue the recordkeeping.
1438        if (mEverStored.exists()) {
1439            RandomAccessFile temp = null;
1440            RandomAccessFile in = null;
1441
1442            try {
1443                temp = new RandomAccessFile(tempProcessedFile, "rws");
1444                in = new RandomAccessFile(mEverStored, "r");
1445
1446                // Loop until we hit EOF
1447                while (true) {
1448                    String pkg = in.readUTF();
1449                    try {
1450                        // is this package still present?
1451                        mPackageManager.getPackageInfo(pkg, 0);
1452                        // if we get here then yes it is; remember it
1453                        mEverStoredApps.add(pkg);
1454                        temp.writeUTF(pkg);
1455                        if (MORE_DEBUG) Slog.v(TAG, "   + " + pkg);
1456                    } catch (NameNotFoundException e) {
1457                        // nope, this package was uninstalled; don't include it
1458                        if (MORE_DEBUG) Slog.v(TAG, "   - " + pkg);
1459                    }
1460                }
1461            } catch (EOFException e) {
1462                // Once we've rewritten the backup history log, atomically replace the
1463                // old one with the new one then reopen the file for continuing use.
1464                if (!tempProcessedFile.renameTo(mEverStored)) {
1465                    Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored);
1466                }
1467            } catch (IOException e) {
1468                Slog.e(TAG, "Error in processed file", e);
1469            } finally {
1470                try { if (temp != null) temp.close(); } catch (IOException e) {}
1471                try { if (in != null) in.close(); } catch (IOException e) {}
1472            }
1473        }
1474
1475        synchronized (mQueueLock) {
1476            // Resume the full-data backup queue
1477            mFullBackupQueue = readFullBackupSchedule();
1478        }
1479
1480        // Register for broadcasts about package install, etc., so we can
1481        // update the provider list.
1482        IntentFilter filter = new IntentFilter();
1483        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
1484        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1485        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
1486        filter.addDataScheme("package");
1487        mContext.registerReceiver(mBroadcastReceiver, filter);
1488        // Register for events related to sdcard installation.
1489        IntentFilter sdFilter = new IntentFilter();
1490        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
1491        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
1492        mContext.registerReceiver(mBroadcastReceiver, sdFilter);
1493    }
1494
1495    private ArrayList<FullBackupEntry> readFullBackupSchedule() {
1496        boolean changed = false;
1497        ArrayList<FullBackupEntry> schedule = null;
1498        List<PackageInfo> apps =
1499                PackageManagerBackupAgent.getStorableApplications(mPackageManager);
1500
1501        if (mFullBackupScheduleFile.exists()) {
1502            FileInputStream fstream = null;
1503            BufferedInputStream bufStream = null;
1504            DataInputStream in = null;
1505            try {
1506                fstream = new FileInputStream(mFullBackupScheduleFile);
1507                bufStream = new BufferedInputStream(fstream);
1508                in = new DataInputStream(bufStream);
1509
1510                int version = in.readInt();
1511                if (version != SCHEDULE_FILE_VERSION) {
1512                    Slog.e(TAG, "Unknown backup schedule version " + version);
1513                    return null;
1514                }
1515
1516                final int N = in.readInt();
1517                schedule = new ArrayList<FullBackupEntry>(N);
1518
1519                // HashSet instead of ArraySet specifically because we want the eventual
1520                // lookups against O(hundreds) of entries to be as fast as possible, and
1521                // we discard the set immediately after the scan so the extra memory
1522                // overhead is transient.
1523                HashSet<String> foundApps = new HashSet<String>(N);
1524
1525                for (int i = 0; i < N; i++) {
1526                    String pkgName = in.readUTF();
1527                    long lastBackup = in.readLong();
1528                    foundApps.add(pkgName); // all apps that we've addressed already
1529                    try {
1530                        PackageInfo pkg = mPackageManager.getPackageInfo(pkgName, 0);
1531                        if (appGetsFullBackup(pkg) && appIsEligibleForBackup(pkg.applicationInfo)) {
1532                            schedule.add(new FullBackupEntry(pkgName, lastBackup));
1533                        } else {
1534                            if (DEBUG) {
1535                                Slog.i(TAG, "Package " + pkgName
1536                                        + " no longer eligible for full backup");
1537                            }
1538                        }
1539                    } catch (NameNotFoundException e) {
1540                        if (DEBUG) {
1541                            Slog.i(TAG, "Package " + pkgName
1542                                    + " not installed; dropping from full backup");
1543                        }
1544                    }
1545                }
1546
1547                // New apps can arrive "out of band" via OTA and similar, so we also need to
1548                // scan to make sure that we're tracking all full-backup candidates properly
1549                for (PackageInfo app : apps) {
1550                    if (appGetsFullBackup(app) && appIsEligibleForBackup(app.applicationInfo)) {
1551                        if (!foundApps.contains(app.packageName)) {
1552                            if (MORE_DEBUG) {
1553                                Slog.i(TAG, "New full backup app " + app.packageName + " found");
1554                            }
1555                            schedule.add(new FullBackupEntry(app.packageName, 0));
1556                            changed = true;
1557                        }
1558                    }
1559                }
1560
1561                Collections.sort(schedule);
1562            } catch (Exception e) {
1563                Slog.e(TAG, "Unable to read backup schedule", e);
1564                mFullBackupScheduleFile.delete();
1565                schedule = null;
1566            } finally {
1567                IoUtils.closeQuietly(in);
1568                IoUtils.closeQuietly(bufStream);
1569                IoUtils.closeQuietly(fstream);
1570            }
1571        }
1572
1573        if (schedule == null) {
1574            // no prior queue record, or unable to read it.  Set up the queue
1575            // from scratch.
1576            changed = true;
1577            schedule = new ArrayList<FullBackupEntry>(apps.size());
1578            for (PackageInfo info : apps) {
1579                if (appGetsFullBackup(info) && appIsEligibleForBackup(info.applicationInfo)) {
1580                    schedule.add(new FullBackupEntry(info.packageName, 0));
1581                }
1582            }
1583        }
1584
1585        if (changed) {
1586            writeFullBackupScheduleAsync();
1587        }
1588        return schedule;
1589    }
1590
1591    Runnable mFullBackupScheduleWriter = new Runnable() {
1592        @Override public void run() {
1593            synchronized (mQueueLock) {
1594                try {
1595                    ByteArrayOutputStream bufStream = new ByteArrayOutputStream(4096);
1596                    DataOutputStream bufOut = new DataOutputStream(bufStream);
1597                    bufOut.writeInt(SCHEDULE_FILE_VERSION);
1598
1599                    // version 1:
1600                    //
1601                    // [int] # of packages in the queue = N
1602                    // N * {
1603                    //     [utf8] package name
1604                    //     [long] last backup time for this package
1605                    //     }
1606                    int N = mFullBackupQueue.size();
1607                    bufOut.writeInt(N);
1608
1609                    for (int i = 0; i < N; i++) {
1610                        FullBackupEntry entry = mFullBackupQueue.get(i);
1611                        bufOut.writeUTF(entry.packageName);
1612                        bufOut.writeLong(entry.lastBackup);
1613                    }
1614                    bufOut.flush();
1615
1616                    AtomicFile af = new AtomicFile(mFullBackupScheduleFile);
1617                    FileOutputStream out = af.startWrite();
1618                    out.write(bufStream.toByteArray());
1619                    af.finishWrite(out);
1620                } catch (Exception e) {
1621                    Slog.e(TAG, "Unable to write backup schedule!", e);
1622                }
1623            }
1624        }
1625    };
1626
1627    private void writeFullBackupScheduleAsync() {
1628        mBackupHandler.removeCallbacks(mFullBackupScheduleWriter);
1629        mBackupHandler.post(mFullBackupScheduleWriter);
1630    }
1631
1632    private void parseLeftoverJournals() {
1633        for (File f : mJournalDir.listFiles()) {
1634            if (mJournal == null || f.compareTo(mJournal) != 0) {
1635                // This isn't the current journal, so it must be a leftover.  Read
1636                // out the package names mentioned there and schedule them for
1637                // backup.
1638                DataInputStream in = null;
1639                try {
1640                    Slog.i(TAG, "Found stale backup journal, scheduling");
1641                    // Journals will tend to be on the order of a few kilobytes(around 4k), hence,
1642                    // setting the buffer size to 8192.
1643                    InputStream bufferedInputStream = new BufferedInputStream(
1644                            new FileInputStream(f), 8192);
1645                    in = new DataInputStream(bufferedInputStream);
1646                    while (true) {
1647                        String packageName = in.readUTF();
1648                        if (MORE_DEBUG) Slog.i(TAG, "  " + packageName);
1649                        dataChangedImpl(packageName);
1650                    }
1651                } catch (EOFException e) {
1652                    // no more data; we're done
1653                } catch (Exception e) {
1654                    Slog.e(TAG, "Can't read " + f, e);
1655                } finally {
1656                    // close/delete the file
1657                    try { if (in != null) in.close(); } catch (IOException e) {}
1658                    f.delete();
1659                }
1660            }
1661        }
1662    }
1663
1664    private SecretKey buildPasswordKey(String algorithm, String pw, byte[] salt, int rounds) {
1665        return buildCharArrayKey(algorithm, pw.toCharArray(), salt, rounds);
1666    }
1667
1668    private SecretKey buildCharArrayKey(String algorithm, char[] pwArray, byte[] salt, int rounds) {
1669        try {
1670            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
1671            KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE);
1672            return keyFactory.generateSecret(ks);
1673        } catch (InvalidKeySpecException e) {
1674            Slog.e(TAG, "Invalid key spec for PBKDF2!");
1675        } catch (NoSuchAlgorithmException e) {
1676            Slog.e(TAG, "PBKDF2 unavailable!");
1677        }
1678        return null;
1679    }
1680
1681    private String buildPasswordHash(String algorithm, String pw, byte[] salt, int rounds) {
1682        SecretKey key = buildPasswordKey(algorithm, pw, salt, rounds);
1683        if (key != null) {
1684            return byteArrayToHex(key.getEncoded());
1685        }
1686        return null;
1687    }
1688
1689    private String byteArrayToHex(byte[] data) {
1690        StringBuilder buf = new StringBuilder(data.length * 2);
1691        for (int i = 0; i < data.length; i++) {
1692            buf.append(Byte.toHexString(data[i], true));
1693        }
1694        return buf.toString();
1695    }
1696
1697    private byte[] hexToByteArray(String digits) {
1698        final int bytes = digits.length() / 2;
1699        if (2*bytes != digits.length()) {
1700            throw new IllegalArgumentException("Hex string must have an even number of digits");
1701        }
1702
1703        byte[] result = new byte[bytes];
1704        for (int i = 0; i < digits.length(); i += 2) {
1705            result[i/2] = (byte) Integer.parseInt(digits.substring(i, i+2), 16);
1706        }
1707        return result;
1708    }
1709
1710    private byte[] makeKeyChecksum(String algorithm, byte[] pwBytes, byte[] salt, int rounds) {
1711        char[] mkAsChar = new char[pwBytes.length];
1712        for (int i = 0; i < pwBytes.length; i++) {
1713            mkAsChar[i] = (char) pwBytes[i];
1714        }
1715
1716        Key checksum = buildCharArrayKey(algorithm, mkAsChar, salt, rounds);
1717        return checksum.getEncoded();
1718    }
1719
1720    // Used for generating random salts or passwords
1721    private byte[] randomBytes(int bits) {
1722        byte[] array = new byte[bits / 8];
1723        mRng.nextBytes(array);
1724        return array;
1725    }
1726
1727    boolean passwordMatchesSaved(String algorithm, String candidatePw, int rounds) {
1728        if (mPasswordHash == null) {
1729            // no current password case -- require that 'currentPw' be null or empty
1730            if (candidatePw == null || "".equals(candidatePw)) {
1731                return true;
1732            } // else the non-empty candidate does not match the empty stored pw
1733        } else {
1734            // hash the stated current pw and compare to the stored one
1735            if (candidatePw != null && candidatePw.length() > 0) {
1736                String currentPwHash = buildPasswordHash(algorithm, candidatePw, mPasswordSalt, rounds);
1737                if (mPasswordHash.equalsIgnoreCase(currentPwHash)) {
1738                    // candidate hash matches the stored hash -- the password matches
1739                    return true;
1740                }
1741            } // else the stored pw is nonempty but the candidate is empty; no match
1742        }
1743        return false;
1744    }
1745
1746    @Override
1747    public boolean setBackupPassword(String currentPw, String newPw) {
1748        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
1749                "setBackupPassword");
1750
1751        // When processing v1 passwords we may need to try two different PBKDF2 checksum regimes
1752        final boolean pbkdf2Fallback = (mPasswordVersion < BACKUP_PW_FILE_VERSION);
1753
1754        // If the supplied pw doesn't hash to the the saved one, fail.  The password
1755        // might be caught in the legacy crypto mismatch; verify that too.
1756        if (!passwordMatchesSaved(PBKDF_CURRENT, currentPw, PBKDF2_HASH_ROUNDS)
1757                && !(pbkdf2Fallback && passwordMatchesSaved(PBKDF_FALLBACK,
1758                        currentPw, PBKDF2_HASH_ROUNDS))) {
1759            return false;
1760        }
1761
1762        // Snap up to current on the pw file version
1763        mPasswordVersion = BACKUP_PW_FILE_VERSION;
1764        FileOutputStream pwFout = null;
1765        DataOutputStream pwOut = null;
1766        try {
1767            pwFout = new FileOutputStream(mPasswordVersionFile);
1768            pwOut = new DataOutputStream(pwFout);
1769            pwOut.writeInt(mPasswordVersion);
1770        } catch (IOException e) {
1771            Slog.e(TAG, "Unable to write backup pw version; password not changed");
1772            return false;
1773        } finally {
1774            try {
1775                if (pwOut != null) pwOut.close();
1776                if (pwFout != null) pwFout.close();
1777            } catch (IOException e) {
1778                Slog.w(TAG, "Unable to close pw version record");
1779            }
1780        }
1781
1782        // Clearing the password is okay
1783        if (newPw == null || newPw.isEmpty()) {
1784            if (mPasswordHashFile.exists()) {
1785                if (!mPasswordHashFile.delete()) {
1786                    // Unable to delete the old pw file, so fail
1787                    Slog.e(TAG, "Unable to clear backup password");
1788                    return false;
1789                }
1790            }
1791            mPasswordHash = null;
1792            mPasswordSalt = null;
1793            return true;
1794        }
1795
1796        try {
1797            // Okay, build the hash of the new backup password
1798            byte[] salt = randomBytes(PBKDF2_SALT_SIZE);
1799            String newPwHash = buildPasswordHash(PBKDF_CURRENT, newPw, salt, PBKDF2_HASH_ROUNDS);
1800
1801            OutputStream pwf = null, buffer = null;
1802            DataOutputStream out = null;
1803            try {
1804                pwf = new FileOutputStream(mPasswordHashFile);
1805                buffer = new BufferedOutputStream(pwf);
1806                out = new DataOutputStream(buffer);
1807                // integer length of the salt array, followed by the salt,
1808                // then the hex pw hash string
1809                out.writeInt(salt.length);
1810                out.write(salt);
1811                out.writeUTF(newPwHash);
1812                out.flush();
1813                mPasswordHash = newPwHash;
1814                mPasswordSalt = salt;
1815                return true;
1816            } finally {
1817                if (out != null) out.close();
1818                if (buffer != null) buffer.close();
1819                if (pwf != null) pwf.close();
1820            }
1821        } catch (IOException e) {
1822            Slog.e(TAG, "Unable to set backup password");
1823        }
1824        return false;
1825    }
1826
1827    @Override
1828    public boolean hasBackupPassword() {
1829        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
1830                "hasBackupPassword");
1831
1832        return mPasswordHash != null && mPasswordHash.length() > 0;
1833    }
1834
1835    private boolean backupPasswordMatches(String currentPw) {
1836        if (hasBackupPassword()) {
1837            final boolean pbkdf2Fallback = (mPasswordVersion < BACKUP_PW_FILE_VERSION);
1838            if (!passwordMatchesSaved(PBKDF_CURRENT, currentPw, PBKDF2_HASH_ROUNDS)
1839                    && !(pbkdf2Fallback && passwordMatchesSaved(PBKDF_FALLBACK,
1840                            currentPw, PBKDF2_HASH_ROUNDS))) {
1841                if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting");
1842                return false;
1843            }
1844        }
1845        return true;
1846    }
1847
1848    // Maintain persistent state around whether need to do an initialize operation.
1849    // Must be called with the queue lock held.
1850    void recordInitPendingLocked(boolean isPending, String transportName) {
1851        if (MORE_DEBUG) Slog.i(TAG, "recordInitPendingLocked: " + isPending
1852                + " on transport " + transportName);
1853        mBackupHandler.removeMessages(MSG_RETRY_INIT);
1854
1855        try {
1856            IBackupTransport transport = mTransportManager.getTransportBinder(transportName);
1857            if (transport != null) {
1858                String transportDirName = transport.transportDirName();
1859                File stateDir = new File(mBaseStateDir, transportDirName);
1860                File initPendingFile = new File(stateDir, INIT_SENTINEL_FILE_NAME);
1861
1862                if (isPending) {
1863                    // We need an init before we can proceed with sending backup data.
1864                    // Record that with an entry in our set of pending inits, as well as
1865                    // journaling it via creation of a sentinel file.
1866                    mPendingInits.add(transportName);
1867                    try {
1868                        (new FileOutputStream(initPendingFile)).close();
1869                    } catch (IOException ioe) {
1870                        // Something is badly wrong with our permissions; just try to move on
1871                    }
1872                } else {
1873                    // No more initialization needed; wipe the journal and reset our state.
1874                    initPendingFile.delete();
1875                    mPendingInits.remove(transportName);
1876                }
1877                return; // done; don't fall through to the error case
1878            }
1879        } catch (Exception e) {
1880            // transport threw when asked its name; fall through to the lookup-failed case
1881            Slog.e(TAG, "Transport " + transportName + " failed to report name: "
1882                    + e.getMessage());
1883        }
1884
1885        // The named transport doesn't exist or threw.  This operation is
1886        // important, so we record the need for a an init and post a message
1887        // to retry the init later.
1888        if (isPending) {
1889            mPendingInits.add(transportName);
1890            mBackupHandler.sendMessageDelayed(
1891                    mBackupHandler.obtainMessage(MSG_RETRY_INIT,
1892                            (isPending ? 1 : 0),
1893                            0,
1894                            transportName),
1895                    TRANSPORT_RETRY_INTERVAL);
1896        }
1897    }
1898
1899    // Reset all of our bookkeeping, in response to having been told that
1900    // the backend data has been wiped [due to idle expiry, for example],
1901    // so we must re-upload all saved settings.
1902    void resetBackupState(File stateFileDir) {
1903        synchronized (mQueueLock) {
1904            // Wipe the "what we've ever backed up" tracking
1905            mEverStoredApps.clear();
1906            mEverStored.delete();
1907
1908            mCurrentToken = 0;
1909            writeRestoreTokens();
1910
1911            // Remove all the state files
1912            for (File sf : stateFileDir.listFiles()) {
1913                // ... but don't touch the needs-init sentinel
1914                if (!sf.getName().equals(INIT_SENTINEL_FILE_NAME)) {
1915                    sf.delete();
1916                }
1917            }
1918        }
1919
1920        // Enqueue a new backup of every participant
1921        synchronized (mBackupParticipants) {
1922            final int N = mBackupParticipants.size();
1923            for (int i=0; i<N; i++) {
1924                HashSet<String> participants = mBackupParticipants.valueAt(i);
1925                if (participants != null) {
1926                    for (String packageName : participants) {
1927                        dataChangedImpl(packageName);
1928                    }
1929                }
1930            }
1931        }
1932    }
1933
1934    private TransportManager.TransportBoundListener mTransportBoundListener =
1935            new TransportManager.TransportBoundListener() {
1936        @Override
1937        public boolean onTransportBound(IBackupTransport transport) {
1938            // If the init sentinel file exists, we need to be sure to perform the init
1939            // as soon as practical.  We also create the state directory at registration
1940            // time to ensure it's present from the outset.
1941            String name = null;
1942            try {
1943                name = transport.name();
1944                String transportDirName = transport.transportDirName();
1945                File stateDir = new File(mBaseStateDir, transportDirName);
1946                stateDir.mkdirs();
1947
1948                File initSentinel = new File(stateDir, INIT_SENTINEL_FILE_NAME);
1949                if (initSentinel.exists()) {
1950                    synchronized (mQueueLock) {
1951                        mPendingInits.add(name);
1952
1953                        // TODO: pick a better starting time than now + 1 minute
1954                        long delay = 1000 * 60; // one minute, in milliseconds
1955                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
1956                                System.currentTimeMillis() + delay, mRunInitIntent);
1957                    }
1958                }
1959                return true;
1960            } catch (Exception e) {
1961                // the transport threw when asked its file naming prefs; declare it invalid
1962                Slog.w(TAG, "Failed to regiser transport: " + name);
1963                return false;
1964            }
1965        }
1966    };
1967
1968    // ----- Track installation/removal of packages -----
1969    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1970        public void onReceive(Context context, Intent intent) {
1971            if (MORE_DEBUG) Slog.d(TAG, "Received broadcast " + intent);
1972
1973            String action = intent.getAction();
1974            boolean replacing = false;
1975            boolean added = false;
1976            boolean changed = false;
1977            Bundle extras = intent.getExtras();
1978            String pkgList[] = null;
1979            if (Intent.ACTION_PACKAGE_ADDED.equals(action) ||
1980                    Intent.ACTION_PACKAGE_REMOVED.equals(action) ||
1981                    Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1982                Uri uri = intent.getData();
1983                if (uri == null) {
1984                    return;
1985                }
1986                String pkgName = uri.getSchemeSpecificPart();
1987                if (pkgName != null) {
1988                    pkgList = new String[] { pkgName };
1989                }
1990                changed = Intent.ACTION_PACKAGE_CHANGED.equals(action);
1991
1992                // At package-changed we only care about looking at new transport states
1993                if (changed) {
1994                    String[] components =
1995                            intent.getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
1996
1997                    if (MORE_DEBUG) {
1998                        Slog.i(TAG, "Package " + pkgName + " changed; rechecking");
1999                        for (int i = 0; i < components.length; i++) {
2000                            Slog.i(TAG, "   * " + components[i]);
2001                        }
2002                    }
2003
2004                    mTransportManager.onPackageChanged(pkgName, components);
2005                    return; // nothing more to do in the PACKAGE_CHANGED case
2006                }
2007
2008                added = Intent.ACTION_PACKAGE_ADDED.equals(action);
2009                replacing = extras.getBoolean(Intent.EXTRA_REPLACING, false);
2010            } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
2011                added = true;
2012                pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
2013            } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
2014                added = false;
2015                pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
2016            }
2017
2018            if (pkgList == null || pkgList.length == 0) {
2019                return;
2020            }
2021
2022            final int uid = extras.getInt(Intent.EXTRA_UID);
2023            if (added) {
2024                synchronized (mBackupParticipants) {
2025                    if (replacing) {
2026                        // This is the package-replaced case; we just remove the entry
2027                        // under the old uid and fall through to re-add.  If an app
2028                        // just added key/value backup participation, this picks it up
2029                        // as a known participant.
2030                        removePackageParticipantsLocked(pkgList, uid);
2031                    }
2032                    addPackageParticipantsLocked(pkgList);
2033                }
2034                // If they're full-backup candidates, add them there instead
2035                final long now = System.currentTimeMillis();
2036                for (String packageName : pkgList) {
2037                    try {
2038                        PackageInfo app = mPackageManager.getPackageInfo(packageName, 0);
2039                        if (appGetsFullBackup(app) && appIsEligibleForBackup(app.applicationInfo)) {
2040                            enqueueFullBackup(packageName, now);
2041                            scheduleNextFullBackupJob(0);
2042                        } else {
2043                            // The app might have just transitioned out of full-data into
2044                            // doing key/value backups, or might have just disabled backups
2045                            // entirely.  Make sure it is no longer in the full-data queue.
2046                            synchronized (mQueueLock) {
2047                                dequeueFullBackupLocked(packageName);
2048                            }
2049                            writeFullBackupScheduleAsync();
2050                        }
2051
2052                        mTransportManager.onPackageAdded(packageName);
2053
2054                    } catch (NameNotFoundException e) {
2055                        // doesn't really exist; ignore it
2056                        if (DEBUG) {
2057                            Slog.w(TAG, "Can't resolve new app " + packageName);
2058                        }
2059                    }
2060                }
2061
2062                // Whenever a package is added or updated we need to update
2063                // the package metadata bookkeeping.
2064                dataChangedImpl(PACKAGE_MANAGER_SENTINEL);
2065            } else {
2066                if (replacing) {
2067                    // The package is being updated.  We'll receive a PACKAGE_ADDED shortly.
2068                } else {
2069                    // Outright removal.  In the full-data case, the app will be dropped
2070                    // from the queue when its (now obsolete) name comes up again for
2071                    // backup.
2072                    synchronized (mBackupParticipants) {
2073                        removePackageParticipantsLocked(pkgList, uid);
2074                    }
2075                }
2076                for (String pkgName : pkgList) {
2077                    mTransportManager.onPackageRemoved(pkgName);
2078                }
2079            }
2080        }
2081    };
2082
2083    // Add the backup agents in the given packages to our set of known backup participants.
2084    // If 'packageNames' is null, adds all backup agents in the whole system.
2085    void addPackageParticipantsLocked(String[] packageNames) {
2086        // Look for apps that define the android:backupAgent attribute
2087        List<PackageInfo> targetApps = allAgentPackages();
2088        if (packageNames != null) {
2089            if (MORE_DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: #" + packageNames.length);
2090            for (String packageName : packageNames) {
2091                addPackageParticipantsLockedInner(packageName, targetApps);
2092            }
2093        } else {
2094            if (MORE_DEBUG) Slog.v(TAG, "addPackageParticipantsLocked: all");
2095            addPackageParticipantsLockedInner(null, targetApps);
2096        }
2097    }
2098
2099    private void addPackageParticipantsLockedInner(String packageName,
2100            List<PackageInfo> targetPkgs) {
2101        if (MORE_DEBUG) {
2102            Slog.v(TAG, "Examining " + packageName + " for backup agent");
2103        }
2104
2105        for (PackageInfo pkg : targetPkgs) {
2106            if (packageName == null || pkg.packageName.equals(packageName)) {
2107                int uid = pkg.applicationInfo.uid;
2108                HashSet<String> set = mBackupParticipants.get(uid);
2109                if (set == null) {
2110                    set = new HashSet<>();
2111                    mBackupParticipants.put(uid, set);
2112                }
2113                set.add(pkg.packageName);
2114                if (MORE_DEBUG) Slog.v(TAG, "Agent found; added");
2115
2116                // Schedule a backup for it on general principles
2117                if (MORE_DEBUG) Slog.i(TAG, "Scheduling backup for new app " + pkg.packageName);
2118                Message msg = mBackupHandler
2119                        .obtainMessage(MSG_SCHEDULE_BACKUP_PACKAGE, pkg.packageName);
2120                mBackupHandler.sendMessage(msg);
2121            }
2122        }
2123    }
2124
2125    // Remove the given packages' entries from our known active set.
2126    void removePackageParticipantsLocked(String[] packageNames, int oldUid) {
2127        if (packageNames == null) {
2128            Slog.w(TAG, "removePackageParticipants with null list");
2129            return;
2130        }
2131
2132        if (MORE_DEBUG) Slog.v(TAG, "removePackageParticipantsLocked: uid=" + oldUid
2133                + " #" + packageNames.length);
2134        for (String pkg : packageNames) {
2135            // Known previous UID, so we know which package set to check
2136            HashSet<String> set = mBackupParticipants.get(oldUid);
2137            if (set != null && set.contains(pkg)) {
2138                removePackageFromSetLocked(set, pkg);
2139                if (set.isEmpty()) {
2140                    if (MORE_DEBUG) Slog.v(TAG, "  last one of this uid; purging set");
2141                    mBackupParticipants.remove(oldUid);
2142                }
2143            }
2144        }
2145    }
2146
2147    private void removePackageFromSetLocked(final HashSet<String> set,
2148            final String packageName) {
2149        if (set.contains(packageName)) {
2150            // Found it.  Remove this one package from the bookkeeping, and
2151            // if it's the last participating app under this uid we drop the
2152            // (now-empty) set as well.
2153            // Note that we deliberately leave it 'known' in the "ever backed up"
2154            // bookkeeping so that its current-dataset data will be retrieved
2155            // if the app is subsequently reinstalled
2156            if (MORE_DEBUG) Slog.v(TAG, "  removing participant " + packageName);
2157            set.remove(packageName);
2158            mPendingBackups.remove(packageName);
2159        }
2160    }
2161
2162    // Returns the set of all applications that define an android:backupAgent attribute
2163    List<PackageInfo> allAgentPackages() {
2164        // !!! TODO: cache this and regenerate only when necessary
2165        int flags = PackageManager.GET_SIGNATURES;
2166        List<PackageInfo> packages = mPackageManager.getInstalledPackages(flags);
2167        int N = packages.size();
2168        for (int a = N-1; a >= 0; a--) {
2169            PackageInfo pkg = packages.get(a);
2170            try {
2171                ApplicationInfo app = pkg.applicationInfo;
2172                if (((app.flags&ApplicationInfo.FLAG_ALLOW_BACKUP) == 0)
2173                        || app.backupAgentName == null
2174                        || (app.flags&ApplicationInfo.FLAG_FULL_BACKUP_ONLY) != 0) {
2175                    packages.remove(a);
2176                }
2177                else {
2178                    // we will need the shared library path, so look that up and store it here.
2179                    // This is used implicitly when we pass the PackageInfo object off to
2180                    // the Activity Manager to launch the app for backup/restore purposes.
2181                    app = mPackageManager.getApplicationInfo(pkg.packageName,
2182                            PackageManager.GET_SHARED_LIBRARY_FILES);
2183                    pkg.applicationInfo.sharedLibraryFiles = app.sharedLibraryFiles;
2184                }
2185            } catch (NameNotFoundException e) {
2186                packages.remove(a);
2187            }
2188        }
2189        return packages;
2190    }
2191
2192    // Called from the backup tasks: record that the given app has been successfully
2193    // backed up at least once.  This includes both key/value and full-data backups
2194    // through the transport.
2195    void logBackupComplete(String packageName) {
2196        if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) return;
2197
2198        synchronized (mEverStoredApps) {
2199            if (!mEverStoredApps.add(packageName)) return;
2200
2201            RandomAccessFile out = null;
2202            try {
2203                out = new RandomAccessFile(mEverStored, "rws");
2204                out.seek(out.length());
2205                out.writeUTF(packageName);
2206            } catch (IOException e) {
2207                Slog.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored);
2208            } finally {
2209                try { if (out != null) out.close(); } catch (IOException e) {}
2210            }
2211        }
2212    }
2213
2214    // Remove our awareness of having ever backed up the given package
2215    void removeEverBackedUp(String packageName) {
2216        if (DEBUG) Slog.v(TAG, "Removing backed-up knowledge of " + packageName);
2217        if (MORE_DEBUG) Slog.v(TAG, "New set:");
2218
2219        synchronized (mEverStoredApps) {
2220            // Rewrite the file and rename to overwrite.  If we reboot in the middle,
2221            // we'll recognize on initialization time that the package no longer
2222            // exists and fix it up then.
2223            File tempKnownFile = new File(mBaseStateDir, "processed.new");
2224            RandomAccessFile known = null;
2225            try {
2226                known = new RandomAccessFile(tempKnownFile, "rws");
2227                mEverStoredApps.remove(packageName);
2228                for (String s : mEverStoredApps) {
2229                    known.writeUTF(s);
2230                    if (MORE_DEBUG) Slog.v(TAG, "    " + s);
2231                }
2232                known.close();
2233                known = null;
2234                if (!tempKnownFile.renameTo(mEverStored)) {
2235                    throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored);
2236                }
2237            } catch (IOException e) {
2238                // Bad: we couldn't create the new copy.  For safety's sake we
2239                // abandon the whole process and remove all what's-backed-up
2240                // state entirely, meaning we'll force a backup pass for every
2241                // participant on the next boot or [re]install.
2242                Slog.w(TAG, "Error rewriting " + mEverStored, e);
2243                mEverStoredApps.clear();
2244                tempKnownFile.delete();
2245                mEverStored.delete();
2246            } finally {
2247                try { if (known != null) known.close(); } catch (IOException e) {}
2248            }
2249        }
2250    }
2251
2252    // Persistently record the current and ancestral backup tokens as well
2253    // as the set of packages with data [supposedly] available in the
2254    // ancestral dataset.
2255    void writeRestoreTokens() {
2256        try {
2257            RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd");
2258
2259            // First, the version number of this record, for futureproofing
2260            af.writeInt(CURRENT_ANCESTRAL_RECORD_VERSION);
2261
2262            // Write the ancestral and current tokens
2263            af.writeLong(mAncestralToken);
2264            af.writeLong(mCurrentToken);
2265
2266            // Now write the set of ancestral packages
2267            if (mAncestralPackages == null) {
2268                af.writeInt(-1);
2269            } else {
2270                af.writeInt(mAncestralPackages.size());
2271                if (DEBUG) Slog.v(TAG, "Ancestral packages:  " + mAncestralPackages.size());
2272                for (String pkgName : mAncestralPackages) {
2273                    af.writeUTF(pkgName);
2274                    if (MORE_DEBUG) Slog.v(TAG, "   " + pkgName);
2275                }
2276            }
2277            af.close();
2278        } catch (IOException e) {
2279            Slog.w(TAG, "Unable to write token file:", e);
2280        }
2281    }
2282
2283    // What name is this transport registered under...?
2284    private String getTransportName(IBackupTransport transport) {
2285        if (MORE_DEBUG) {
2286            Slog.v(TAG, "Searching for transport name of " + transport);
2287        }
2288        return mTransportManager.getTransportName(transport);
2289    }
2290
2291    // fire off a backup agent, blocking until it attaches or times out
2292    @Override
2293    public IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
2294        IBackupAgent agent = null;
2295        synchronized(mAgentConnectLock) {
2296            mConnecting = true;
2297            mConnectedAgent = null;
2298            try {
2299                if (mActivityManager.bindBackupAgent(app.packageName, mode,
2300                        UserHandle.USER_OWNER)) {
2301                    Slog.d(TAG, "awaiting agent for " + app);
2302
2303                    // success; wait for the agent to arrive
2304                    // only wait 10 seconds for the bind to happen
2305                    long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
2306                    while (mConnecting && mConnectedAgent == null
2307                            && (System.currentTimeMillis() < timeoutMark)) {
2308                        try {
2309                            mAgentConnectLock.wait(5000);
2310                        } catch (InterruptedException e) {
2311                            // just bail
2312                            Slog.w(TAG, "Interrupted: " + e);
2313                            mConnecting = false;
2314                            mConnectedAgent = null;
2315                        }
2316                    }
2317
2318                    // if we timed out with no connect, abort and move on
2319                    if (mConnecting == true) {
2320                        Slog.w(TAG, "Timeout waiting for agent " + app);
2321                        mConnectedAgent = null;
2322                    }
2323                    if (DEBUG) Slog.i(TAG, "got agent " + mConnectedAgent);
2324                    agent = mConnectedAgent;
2325                }
2326            } catch (RemoteException e) {
2327                // can't happen - ActivityManager is local
2328            }
2329        }
2330        if (agent == null) {
2331            try {
2332                mActivityManager.clearPendingBackup();
2333            } catch (RemoteException e) {
2334                // can't happen - ActivityManager is local
2335            }
2336        }
2337        return agent;
2338    }
2339
2340    // clear an application's data, blocking until the operation completes or times out
2341    void clearApplicationDataSynchronous(String packageName) {
2342        // Don't wipe packages marked allowClearUserData=false
2343        try {
2344            PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
2345            if ((info.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA) == 0) {
2346                if (MORE_DEBUG) Slog.i(TAG, "allowClearUserData=false so not wiping "
2347                        + packageName);
2348                return;
2349            }
2350        } catch (NameNotFoundException e) {
2351            Slog.w(TAG, "Tried to clear data for " + packageName + " but not found");
2352            return;
2353        }
2354
2355        ClearDataObserver observer = new ClearDataObserver();
2356
2357        synchronized(mClearDataLock) {
2358            mClearingData = true;
2359            try {
2360                mActivityManager.clearApplicationUserData(packageName, observer, 0);
2361            } catch (RemoteException e) {
2362                // can't happen because the activity manager is in this process
2363            }
2364
2365            // only wait 10 seconds for the clear data to happen
2366            long timeoutMark = System.currentTimeMillis() + TIMEOUT_INTERVAL;
2367            while (mClearingData && (System.currentTimeMillis() < timeoutMark)) {
2368                try {
2369                    mClearDataLock.wait(5000);
2370                } catch (InterruptedException e) {
2371                    // won't happen, but still.
2372                    mClearingData = false;
2373                }
2374            }
2375        }
2376    }
2377
2378    class ClearDataObserver extends IPackageDataObserver.Stub {
2379        public void onRemoveCompleted(String packageName, boolean succeeded) {
2380            synchronized(mClearDataLock) {
2381                mClearingData = false;
2382                mClearDataLock.notifyAll();
2383            }
2384        }
2385    }
2386
2387    // Get the restore-set token for the best-available restore set for this package:
2388    // the active set if possible, else the ancestral one.  Returns zero if none available.
2389    @Override
2390    public long getAvailableRestoreToken(String packageName) {
2391        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
2392                "getAvailableRestoreToken");
2393
2394        long token = mAncestralToken;
2395        synchronized (mQueueLock) {
2396            if (mEverStoredApps.contains(packageName)) {
2397                if (MORE_DEBUG) {
2398                    Slog.i(TAG, "App in ever-stored, so using current token");
2399                }
2400                token = mCurrentToken;
2401            }
2402        }
2403        if (MORE_DEBUG) Slog.i(TAG, "getAvailableRestoreToken() == " + token);
2404        return token;
2405    }
2406
2407    @Override
2408    public int requestBackup(String[] packages, IBackupObserver observer, int flags) {
2409        return requestBackup(packages, observer, null, flags);
2410    }
2411
2412    @Override
2413    public int requestBackup(String[] packages, IBackupObserver observer,
2414            IBackupManagerMonitor monitor, int flags) {
2415        mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "requestBackup");
2416
2417        if (packages == null || packages.length < 1) {
2418            Slog.e(TAG, "No packages named for backup request");
2419            sendBackupFinished(observer, BackupManager.ERROR_TRANSPORT_ABORTED);
2420            monitor = monitorEvent(monitor, BackupManagerMonitor.LOG_EVENT_ID_NO_PACKAGES,
2421                    null, BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT, null);
2422            throw new IllegalArgumentException("No packages are provided for backup");
2423        }
2424
2425        IBackupTransport transport = mTransportManager.getCurrentTransportBinder();
2426        if (transport == null) {
2427            sendBackupFinished(observer, BackupManager.ERROR_TRANSPORT_ABORTED);
2428            monitor = monitorEvent(monitor, BackupManagerMonitor.LOG_EVENT_ID_TRANSPORT_IS_NULL,
2429                    null, BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT, null);
2430            return BackupManager.ERROR_TRANSPORT_ABORTED;
2431        }
2432
2433        ArrayList<String> fullBackupList = new ArrayList<>();
2434        ArrayList<String> kvBackupList = new ArrayList<>();
2435        for (String packageName : packages) {
2436            if (PACKAGE_MANAGER_SENTINEL.equals(packageName)) {
2437                kvBackupList.add(packageName);
2438                continue;
2439            }
2440            try {
2441                PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName,
2442                        PackageManager.GET_SIGNATURES);
2443                if (!appIsEligibleForBackup(packageInfo.applicationInfo)) {
2444                    sendBackupOnPackageResult(observer, packageName,
2445                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
2446                    continue;
2447                }
2448                if (appGetsFullBackup(packageInfo)) {
2449                    fullBackupList.add(packageInfo.packageName);
2450                } else {
2451                    kvBackupList.add(packageInfo.packageName);
2452                }
2453            } catch (NameNotFoundException e) {
2454                sendBackupOnPackageResult(observer, packageName,
2455                        BackupManager.ERROR_PACKAGE_NOT_FOUND);
2456            }
2457        }
2458        EventLog.writeEvent(EventLogTags.BACKUP_REQUESTED, packages.length, kvBackupList.size(),
2459                fullBackupList.size());
2460        if (MORE_DEBUG) {
2461            Slog.i(TAG, "Backup requested for " + packages.length + " packages, of them: " +
2462                fullBackupList.size() + " full backups, " + kvBackupList.size() + " k/v backups");
2463        }
2464
2465        String dirName;
2466        try {
2467            dirName = transport.transportDirName();
2468        } catch (Exception e) {
2469            Slog.e(TAG, "Transport unavailable while attempting backup: " + e.getMessage());
2470            sendBackupFinished(observer, BackupManager.ERROR_TRANSPORT_ABORTED);
2471            return BackupManager.ERROR_TRANSPORT_ABORTED;
2472        }
2473
2474        boolean nonIncrementalBackup = (flags & BackupManager.FLAG_NON_INCREMENTAL_BACKUP) != 0;
2475
2476        Message msg = mBackupHandler.obtainMessage(MSG_REQUEST_BACKUP);
2477        msg.obj = new BackupParams(transport, dirName, kvBackupList, fullBackupList, observer,
2478                monitor, true, nonIncrementalBackup);
2479        mBackupHandler.sendMessage(msg);
2480        return BackupManager.SUCCESS;
2481    }
2482
2483    // Cancel all running backups.
2484    @Override
2485    public void cancelBackups(){
2486        mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "cancelBackups");
2487        if (MORE_DEBUG) {
2488            Slog.i(TAG, "cancelBackups() called.");
2489        }
2490        final long oldToken = Binder.clearCallingIdentity();
2491        try {
2492            List<Integer> operationsToCancel = new ArrayList<>();
2493            synchronized (mCurrentOpLock) {
2494                for (int i = 0; i < mCurrentOperations.size(); i++) {
2495                    Operation op = mCurrentOperations.valueAt(i);
2496                    int token = mCurrentOperations.keyAt(i);
2497                    if (op.type == OP_TYPE_BACKUP) {
2498                        operationsToCancel.add(token);
2499                    }
2500                }
2501            }
2502            for (Integer token : operationsToCancel) {
2503                handleCancel(token, true /* cancelAll */);
2504            }
2505            // We don't want the backup jobs to kick in any time soon.
2506            // Reschedules them to run in the distant future.
2507            KeyValueBackupJob.schedule(mContext, BUSY_BACKOFF_MIN_MILLIS);
2508            FullBackupJob.schedule(mContext, 2 * BUSY_BACKOFF_MIN_MILLIS);
2509        } finally {
2510            Binder.restoreCallingIdentity(oldToken);
2511        }
2512    }
2513
2514    @Override
2515    public void prepareOperationTimeout(int token, long interval, BackupRestoreTask callback,
2516        int operationType) {
2517        if (operationType != OP_TYPE_BACKUP_WAIT && operationType != OP_TYPE_RESTORE_WAIT) {
2518            Slog.wtf(TAG, "prepareOperationTimeout() doesn't support operation " +
2519                    Integer.toHexString(token) + " of type " + operationType);
2520            return;
2521        }
2522        if (MORE_DEBUG) Slog.v(TAG, "starting timeout: token=" + Integer.toHexString(token)
2523                + " interval=" + interval + " callback=" + callback);
2524
2525        synchronized (mCurrentOpLock) {
2526            mCurrentOperations.put(token, new Operation(OP_PENDING, callback, operationType));
2527            Message msg = mBackupHandler.obtainMessage(getMessageIdForOperationType(operationType),
2528                    token, 0, callback);
2529            mBackupHandler.sendMessageDelayed(msg, interval);
2530        }
2531    }
2532
2533    private int getMessageIdForOperationType(int operationType) {
2534        switch (operationType) {
2535            case OP_TYPE_BACKUP_WAIT:
2536                return MSG_BACKUP_OPERATION_TIMEOUT;
2537            case OP_TYPE_RESTORE_WAIT:
2538                return MSG_RESTORE_OPERATION_TIMEOUT;
2539            default:
2540                Slog.wtf(TAG, "getMessageIdForOperationType called on invalid operation type: " +
2541                        operationType);
2542                return -1;
2543        }
2544    }
2545
2546    private void removeOperation(int token) {
2547        if (MORE_DEBUG) {
2548            Slog.d(TAG, "Removing operation token=" + Integer.toHexString(token));
2549        }
2550        synchronized (mCurrentOpLock) {
2551            if (mCurrentOperations.get(token) == null) {
2552                Slog.w(TAG, "Duplicate remove for operation. token=" +
2553                        Integer.toHexString(token));
2554            }
2555            mCurrentOperations.remove(token);
2556        }
2557    }
2558
2559    // synchronous waiter case
2560    @Override
2561    public boolean waitUntilOperationComplete(int token) {
2562        if (MORE_DEBUG) Slog.i(TAG, "Blocking until operation complete for "
2563                + Integer.toHexString(token));
2564        int finalState = OP_PENDING;
2565        Operation op = null;
2566        synchronized (mCurrentOpLock) {
2567            while (true) {
2568                op = mCurrentOperations.get(token);
2569                if (op == null) {
2570                    // mysterious disappearance: treat as success with no callback
2571                    break;
2572                } else {
2573                    if (op.state == OP_PENDING) {
2574                        try {
2575                            mCurrentOpLock.wait();
2576                        } catch (InterruptedException e) {
2577                        }
2578                        // When the wait is notified we loop around and recheck the current state
2579                    } else {
2580                        if (MORE_DEBUG) {
2581                            Slog.d(TAG, "Unblocked waiting for operation token=" +
2582                                    Integer.toHexString(token));
2583                        }
2584                        // No longer pending; we're done
2585                        finalState = op.state;
2586                        break;
2587                    }
2588                }
2589            }
2590        }
2591
2592        removeOperation(token);
2593        if (op != null) {
2594            mBackupHandler.removeMessages(getMessageIdForOperationType(op.type));
2595        }
2596        if (MORE_DEBUG) Slog.v(TAG, "operation " + Integer.toHexString(token)
2597                + " complete: finalState=" + finalState);
2598        return finalState == OP_ACKNOWLEDGED;
2599    }
2600
2601    void handleCancel(int token, boolean cancelAll) {
2602        // Notify any synchronous waiters
2603        Operation op = null;
2604        synchronized (mCurrentOpLock) {
2605            op = mCurrentOperations.get(token);
2606            if (MORE_DEBUG) {
2607                if (op == null) Slog.w(TAG, "Cancel of token " + Integer.toHexString(token)
2608                        + " but no op found");
2609            }
2610            int state = (op != null) ? op.state : OP_TIMEOUT;
2611            if (state == OP_ACKNOWLEDGED) {
2612                // The operation finished cleanly, so we have nothing more to do.
2613                if (DEBUG) {
2614                    Slog.w(TAG, "Operation already got an ack." +
2615                            "Should have been removed from mCurrentOperations.");
2616                }
2617                op = null;
2618                mCurrentOperations.delete(token);
2619            } else if (state == OP_PENDING) {
2620                if (DEBUG) Slog.v(TAG, "Cancel: token=" + Integer.toHexString(token));
2621                op.state = OP_TIMEOUT;
2622                // Can't delete op from mCurrentOperations here. waitUntilOperationComplete may be
2623                // called after we receive cancel here. We need this op's state there.
2624
2625                // Remove all pending timeout messages of types OP_TYPE_BACKUP_WAIT and
2626                // OP_TYPE_RESTORE_WAIT. On the other hand, OP_TYPE_BACKUP cannot time out and
2627                // doesn't require cancellation.
2628                if (op.type == OP_TYPE_BACKUP_WAIT || op.type == OP_TYPE_RESTORE_WAIT) {
2629                    mBackupHandler.removeMessages(getMessageIdForOperationType(op.type));
2630                }
2631            }
2632            mCurrentOpLock.notifyAll();
2633        }
2634
2635        // If there's a TimeoutHandler for this event, call it
2636        if (op != null && op.callback != null) {
2637            if (MORE_DEBUG) {
2638                Slog.v(TAG, "   Invoking cancel on " + op.callback);
2639            }
2640            op.callback.handleCancel(cancelAll);
2641        }
2642    }
2643
2644    // ----- Back up a set of applications via a worker thread -----
2645
2646    enum BackupState {
2647        INITIAL,
2648        RUNNING_QUEUE,
2649        FINAL
2650    }
2651
2652    /**
2653     * This class handles the process of backing up a given list of key/value backup packages.
2654     * Also takes in a list of pending dolly backups and kicks them off when key/value backups
2655     * are done.
2656     *
2657     * Flow:
2658     * If required, backup @pm@.
2659     * For each pending key/value backup package:
2660     *     - Bind to agent.
2661     *     - Call agent.doBackup()
2662     *     - Wait either for cancel/timeout or operationComplete() callback from the agent.
2663     * Start task to perform dolly backups.
2664     *
2665     * There are three entry points into this class:
2666     *     - execute() [Called from the handler thread]
2667     *     - operationComplete(long result) [Called from the handler thread]
2668     *     - handleCancel(boolean cancelAll) [Can be called from any thread]
2669     * These methods synchronize on mCancelLock.
2670     *
2671     * Interaction with mCurrentOperations:
2672     *     - An entry for this task is put into mCurrentOperations for the entire lifetime of the
2673     *       task. This is useful to cancel the task if required.
2674     *     - An ephemeral entry is put into mCurrentOperations each time we are waiting on for
2675     *       response from a backup agent. This is used to plumb timeouts and completion callbacks.
2676     */
2677    class PerformBackupTask implements BackupRestoreTask {
2678        private static final String TAG = "PerformBackupTask";
2679
2680        private final Object mCancelLock = new Object();
2681
2682        IBackupTransport mTransport;
2683        ArrayList<BackupRequest> mQueue;
2684        ArrayList<BackupRequest> mOriginalQueue;
2685        File mStateDir;
2686        File mJournal;
2687        BackupState mCurrentState;
2688        List<String> mPendingFullBackups;
2689        IBackupObserver mObserver;
2690        IBackupManagerMonitor mMonitor;
2691
2692        private final PerformFullTransportBackupTask mFullBackupTask;
2693        private final int mCurrentOpToken;
2694        private volatile int mEphemeralOpToken;
2695
2696        // carried information about the current in-flight operation
2697        IBackupAgent mAgentBinder;
2698        PackageInfo mCurrentPackage;
2699        File mSavedStateName;
2700        File mBackupDataName;
2701        File mNewStateName;
2702        ParcelFileDescriptor mSavedState;
2703        ParcelFileDescriptor mBackupData;
2704        ParcelFileDescriptor mNewState;
2705        int mStatus;
2706        boolean mFinished;
2707        final boolean mUserInitiated;
2708        final boolean mNonIncremental;
2709
2710        private volatile boolean mCancelAll;
2711
2712        public PerformBackupTask(IBackupTransport transport, String dirName,
2713                ArrayList<BackupRequest> queue, File journal, IBackupObserver observer,
2714                IBackupManagerMonitor monitor, List<String> pendingFullBackups,
2715                boolean userInitiated, boolean nonIncremental) {
2716            mTransport = transport;
2717            mOriginalQueue = queue;
2718            mQueue = new ArrayList<>();
2719            mJournal = journal;
2720            mObserver = observer;
2721            mMonitor = monitor;
2722            mPendingFullBackups = pendingFullBackups;
2723            mUserInitiated = userInitiated;
2724            mNonIncremental = nonIncremental;
2725
2726            mStateDir = new File(mBaseStateDir, dirName);
2727            mCurrentOpToken = generateRandomIntegerToken();
2728
2729            mFinished = false;
2730
2731            synchronized (mCurrentOpLock) {
2732                if (isBackupOperationInProgress()) {
2733                    if (DEBUG) {
2734                        Slog.d(TAG, "Skipping backup since one is already in progress.");
2735                    }
2736                    mCancelAll = true;
2737                    mFullBackupTask = null;
2738                    mCurrentState = BackupState.FINAL;
2739                    addBackupTrace("Skipped. Backup already in progress.");
2740                } else {
2741                    mCurrentState = BackupState.INITIAL;
2742                    CountDownLatch latch = new CountDownLatch(1);
2743                    String[] fullBackups =
2744                            mPendingFullBackups.toArray(new String[mPendingFullBackups.size()]);
2745                    mFullBackupTask =
2746                            new PerformFullTransportBackupTask(/*fullBackupRestoreObserver*/ null,
2747                                    fullBackups, /*updateSchedule*/ false, /*runningJob*/ null,
2748                                    latch,
2749                                    mObserver, mMonitor, mUserInitiated);
2750
2751                    registerTask();
2752                    addBackupTrace("STATE => INITIAL");
2753                }
2754            }
2755        }
2756
2757        /**
2758         * Put this task in the repository of running tasks.
2759         */
2760        private void registerTask() {
2761            synchronized (mCurrentOpLock) {
2762                mCurrentOperations.put(mCurrentOpToken, new Operation(OP_PENDING, this,
2763                        OP_TYPE_BACKUP));
2764            }
2765        }
2766
2767        /**
2768         * Remove this task from repository of running tasks.
2769         */
2770        private void unregisterTask() {
2771            removeOperation(mCurrentOpToken);
2772        }
2773
2774        // Main entry point: perform one chunk of work, updating the state as appropriate
2775        // and reposting the next chunk to the primary backup handler thread.
2776        @Override
2777        @GuardedBy("mCancelLock")
2778        public void execute() {
2779            synchronized (mCancelLock) {
2780                switch (mCurrentState) {
2781                    case INITIAL:
2782                        beginBackup();
2783                        break;
2784
2785                    case RUNNING_QUEUE:
2786                        invokeNextAgent();
2787                        break;
2788
2789                    case FINAL:
2790                        if (!mFinished) finalizeBackup();
2791                        else {
2792                            Slog.e(TAG, "Duplicate finish");
2793                        }
2794                        mFinished = true;
2795                        break;
2796                }
2797            }
2798        }
2799
2800        // We're starting a backup pass.  Initialize the transport and send
2801        // the PM metadata blob if we haven't already.
2802        void beginBackup() {
2803            if (DEBUG_BACKUP_TRACE) {
2804                clearBackupTrace();
2805                StringBuilder b = new StringBuilder(256);
2806                b.append("beginBackup: [");
2807                for (BackupRequest req : mOriginalQueue) {
2808                    b.append(' ');
2809                    b.append(req.packageName);
2810                }
2811                b.append(" ]");
2812                addBackupTrace(b.toString());
2813            }
2814
2815            mAgentBinder = null;
2816            mStatus = BackupTransport.TRANSPORT_OK;
2817
2818            // Sanity check: if the queue is empty we have no work to do.
2819            if (mOriginalQueue.isEmpty() && mPendingFullBackups.isEmpty()) {
2820                Slog.w(TAG, "Backup begun with an empty queue - nothing to do.");
2821                addBackupTrace("queue empty at begin");
2822                sendBackupFinished(mObserver, BackupManager.SUCCESS);
2823                executeNextState(BackupState.FINAL);
2824                return;
2825            }
2826
2827            // We need to retain the original queue contents in case of transport
2828            // failure, but we want a working copy that we can manipulate along
2829            // the way.
2830            mQueue = (ArrayList<BackupRequest>) mOriginalQueue.clone();
2831
2832            // When the transport is forcing non-incremental key/value payloads, we send the
2833            // metadata only if it explicitly asks for it.
2834            boolean skipPm = mNonIncremental;
2835
2836            // The app metadata pseudopackage might also be represented in the
2837            // backup queue if apps have been added/removed since the last time
2838            // we performed a backup.  Drop it from the working queue now that
2839            // we're committed to evaluating it for backup regardless.
2840            for (int i = 0; i < mQueue.size(); i++) {
2841                if (PACKAGE_MANAGER_SENTINEL.equals(mQueue.get(i).packageName)) {
2842                    if (MORE_DEBUG) {
2843                        Slog.i(TAG, "Metadata in queue; eliding");
2844                    }
2845                    mQueue.remove(i);
2846                    skipPm = false;
2847                    break;
2848                }
2849            }
2850
2851            if (DEBUG) Slog.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
2852
2853            File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL);
2854            try {
2855                final String transportName = mTransport.transportDirName();
2856                EventLog.writeEvent(EventLogTags.BACKUP_START, transportName);
2857
2858                // If we haven't stored package manager metadata yet, we must init the transport.
2859                if (mStatus == BackupTransport.TRANSPORT_OK && pmState.length() <= 0) {
2860                    Slog.i(TAG, "Initializing (wiping) backup state and transport storage");
2861                    addBackupTrace("initializing transport " + transportName);
2862                    resetBackupState(mStateDir);  // Just to make sure.
2863                    mStatus = mTransport.initializeDevice();
2864
2865                    addBackupTrace("transport.initializeDevice() == " + mStatus);
2866                    if (mStatus == BackupTransport.TRANSPORT_OK) {
2867                        EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
2868                    } else {
2869                        EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
2870                        Slog.e(TAG, "Transport error in initializeDevice()");
2871                    }
2872                }
2873
2874                if (skipPm) {
2875                    Slog.d(TAG, "Skipping backup of package metadata.");
2876                    executeNextState(BackupState.RUNNING_QUEUE);
2877                } else {
2878                    // The package manager doesn't have a proper <application> etc, but since
2879                    // it's running here in the system process we can just set up its agent
2880                    // directly and use a synthetic BackupRequest.  We always run this pass
2881                    // because it's cheap and this way we guarantee that we don't get out of
2882                    // step even if we're selecting among various transports at run time.
2883                    if (mStatus == BackupTransport.TRANSPORT_OK) {
2884                        PackageManagerBackupAgent pmAgent = new PackageManagerBackupAgent(
2885                                mPackageManager);
2886                        mStatus = invokeAgentForBackup(PACKAGE_MANAGER_SENTINEL,
2887                                IBackupAgent.Stub.asInterface(pmAgent.onBind()), mTransport);
2888                        addBackupTrace("PMBA invoke: " + mStatus);
2889
2890                        // Because the PMBA is a local instance, it has already executed its
2891                        // backup callback and returned.  Blow away the lingering (spurious)
2892                        // pending timeout message for it.
2893                        mBackupHandler.removeMessages(MSG_BACKUP_OPERATION_TIMEOUT);
2894                    }
2895                }
2896
2897                if (mStatus == BackupTransport.TRANSPORT_NOT_INITIALIZED) {
2898                    // The backend reports that our dataset has been wiped.  Note this in
2899                    // the event log; the no-success code below will reset the backup
2900                    // state as well.
2901                    EventLog.writeEvent(EventLogTags.BACKUP_RESET, mTransport.transportDirName());
2902                }
2903            } catch (Exception e) {
2904                Slog.e(TAG, "Error in backup thread", e);
2905                addBackupTrace("Exception in backup thread: " + e);
2906                mStatus = BackupTransport.TRANSPORT_ERROR;
2907            } finally {
2908                // If we've succeeded so far, invokeAgentForBackup() will have run the PM
2909                // metadata and its completion/timeout callback will continue the state
2910                // machine chain.  If it failed that won't happen; we handle that now.
2911                addBackupTrace("exiting prelim: " + mStatus);
2912                if (mStatus != BackupTransport.TRANSPORT_OK) {
2913                    // if things went wrong at this point, we need to
2914                    // restage everything and try again later.
2915                    resetBackupState(mStateDir);  // Just to make sure.
2916                    // In case of any other error, it's backup transport error.
2917                    sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
2918                    executeNextState(BackupState.FINAL);
2919                }
2920            }
2921        }
2922
2923        // Transport has been initialized and the PM metadata submitted successfully
2924        // if that was warranted.  Now we process the single next thing in the queue.
2925        void invokeNextAgent() {
2926            mStatus = BackupTransport.TRANSPORT_OK;
2927            addBackupTrace("invoke q=" + mQueue.size());
2928
2929            // Sanity check that we have work to do.  If not, skip to the end where
2930            // we reestablish the wakelock invariants etc.
2931            if (mQueue.isEmpty()) {
2932                if (MORE_DEBUG) Slog.i(TAG, "queue now empty");
2933                executeNextState(BackupState.FINAL);
2934                return;
2935            }
2936
2937            // pop the entry we're going to process on this step
2938            BackupRequest request = mQueue.get(0);
2939            mQueue.remove(0);
2940
2941            Slog.d(TAG, "starting key/value backup of " + request);
2942            addBackupTrace("launch agent for " + request.packageName);
2943
2944            // Verify that the requested app exists; it might be something that
2945            // requested a backup but was then uninstalled.  The request was
2946            // journalled and rather than tamper with the journal it's safer
2947            // to sanity-check here.  This also gives us the classname of the
2948            // package's backup agent.
2949            try {
2950                mCurrentPackage = mPackageManager.getPackageInfo(request.packageName,
2951                        PackageManager.GET_SIGNATURES);
2952                if (!appIsEligibleForBackup(mCurrentPackage.applicationInfo)) {
2953                    // The manifest has changed but we had a stale backup request pending.
2954                    // This won't happen again because the app won't be requesting further
2955                    // backups.
2956                    Slog.i(TAG, "Package " + request.packageName
2957                            + " no longer supports backup; skipping");
2958                    addBackupTrace("skipping - not eligible, completion is noop");
2959                    // Shouldn't happen in case of requested backup, as pre-check was done in
2960                    // #requestBackup(), except to app update done concurrently
2961                    sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
2962                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
2963                    executeNextState(BackupState.RUNNING_QUEUE);
2964                    return;
2965                }
2966
2967                if (appGetsFullBackup(mCurrentPackage)) {
2968                    // It's possible that this app *formerly* was enqueued for key/value backup,
2969                    // but has since been updated and now only supports the full-data path.
2970                    // Don't proceed with a key/value backup for it in this case.
2971                    Slog.i(TAG, "Package " + request.packageName
2972                            + " requests full-data rather than key/value; skipping");
2973                    addBackupTrace("skipping - fullBackupOnly, completion is noop");
2974                    // Shouldn't happen in case of requested backup, as pre-check was done in
2975                    // #requestBackup()
2976                    sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
2977                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
2978                    executeNextState(BackupState.RUNNING_QUEUE);
2979                    return;
2980                }
2981
2982                if (appIsStopped(mCurrentPackage.applicationInfo)) {
2983                    // The app has been force-stopped or cleared or just installed,
2984                    // and not yet launched out of that state, so just as it won't
2985                    // receive broadcasts, we won't run it for backup.
2986                    addBackupTrace("skipping - stopped");
2987                    sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
2988                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
2989                    executeNextState(BackupState.RUNNING_QUEUE);
2990                    return;
2991                }
2992
2993                IBackupAgent agent = null;
2994                try {
2995                    mWakelock.setWorkSource(new WorkSource(mCurrentPackage.applicationInfo.uid));
2996                    agent = bindToAgentSynchronous(mCurrentPackage.applicationInfo,
2997                            ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL);
2998                    addBackupTrace("agent bound; a? = " + (agent != null));
2999                    if (agent != null) {
3000                        mAgentBinder = agent;
3001                        mStatus = invokeAgentForBackup(request.packageName, agent, mTransport);
3002                        // at this point we'll either get a completion callback from the
3003                        // agent, or a timeout message on the main handler.  either way, we're
3004                        // done here as long as we're successful so far.
3005                    } else {
3006                        // Timeout waiting for the agent
3007                        mStatus = BackupTransport.AGENT_ERROR;
3008                    }
3009                } catch (SecurityException ex) {
3010                    // Try for the next one.
3011                    Slog.d(TAG, "error in bind/backup", ex);
3012                    mStatus = BackupTransport.AGENT_ERROR;
3013                            addBackupTrace("agent SE");
3014                }
3015            } catch (NameNotFoundException e) {
3016                Slog.d(TAG, "Package does not exist; skipping");
3017                addBackupTrace("no such package");
3018                mStatus = BackupTransport.AGENT_UNKNOWN;
3019            } finally {
3020                mWakelock.setWorkSource(null);
3021
3022                // If there was an agent error, no timeout/completion handling will occur.
3023                // That means we need to direct to the next state ourselves.
3024                if (mStatus != BackupTransport.TRANSPORT_OK) {
3025                    BackupState nextState = BackupState.RUNNING_QUEUE;
3026                    mAgentBinder = null;
3027
3028                    // An agent-level failure means we reenqueue this one agent for
3029                    // a later retry, but otherwise proceed normally.
3030                    if (mStatus == BackupTransport.AGENT_ERROR) {
3031                        if (MORE_DEBUG) Slog.i(TAG, "Agent failure for " + request.packageName
3032                                + " - restaging");
3033                        dataChangedImpl(request.packageName);
3034                        mStatus = BackupTransport.TRANSPORT_OK;
3035                        if (mQueue.isEmpty()) nextState = BackupState.FINAL;
3036                        sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
3037                                BackupManager.ERROR_AGENT_FAILURE);
3038                    } else if (mStatus == BackupTransport.AGENT_UNKNOWN) {
3039                        // Failed lookup of the app, so we couldn't bring up an agent, but
3040                        // we're otherwise fine.  Just drop it and go on to the next as usual.
3041                        mStatus = BackupTransport.TRANSPORT_OK;
3042                        sendBackupOnPackageResult(mObserver, mCurrentPackage.packageName,
3043                                BackupManager.ERROR_PACKAGE_NOT_FOUND);
3044                    } else {
3045                        // Transport-level failure means we reenqueue everything
3046                        revertAndEndBackup();
3047                        nextState = BackupState.FINAL;
3048                    }
3049
3050                    executeNextState(nextState);
3051                } else {
3052                    // success case
3053                    addBackupTrace("expecting completion/timeout callback");
3054                }
3055            }
3056        }
3057
3058        void finalizeBackup() {
3059            addBackupTrace("finishing");
3060
3061            // Mark packages that we didn't backup (because backup was cancelled, etc.) as needing
3062            // backup.
3063            for (BackupRequest req : mQueue) {
3064                dataChangedImpl(req.packageName);
3065            }
3066
3067            // Either backup was successful, in which case we of course do not need
3068            // this pass's journal any more; or it failed, in which case we just
3069            // re-enqueued all of these packages in the current active journal.
3070            // Either way, we no longer need this pass's journal.
3071            if (mJournal != null && !mJournal.delete()) {
3072                Slog.e(TAG, "Unable to remove backup journal file " + mJournal);
3073            }
3074
3075            // If everything actually went through and this is the first time we've
3076            // done a backup, we can now record what the current backup dataset token
3077            // is.
3078            if ((mCurrentToken == 0) && (mStatus == BackupTransport.TRANSPORT_OK)) {
3079                addBackupTrace("success; recording token");
3080                try {
3081                    mCurrentToken = mTransport.getCurrentRestoreSet();
3082                    writeRestoreTokens();
3083                } catch (Exception e) {
3084                    // nothing for it at this point, unfortunately, but this will be
3085                    // recorded the next time we fully succeed.
3086                    Slog.e(TAG, "Transport threw reporting restore set: " + e.getMessage());
3087                    addBackupTrace("transport threw returning token");
3088                }
3089            }
3090
3091            // Set up the next backup pass - at this point we can set mBackupRunning
3092            // to false to allow another pass to fire, because we're done with the
3093            // state machine sequence and the wakelock is refcounted.
3094            synchronized (mQueueLock) {
3095                mBackupRunning = false;
3096                if (mStatus == BackupTransport.TRANSPORT_NOT_INITIALIZED) {
3097                    // Make sure we back up everything and perform the one-time init
3098                    if (MORE_DEBUG) Slog.d(TAG, "Server requires init; rerunning");
3099                    addBackupTrace("init required; rerunning");
3100                    try {
3101                        final String name = mTransportManager.getTransportName(mTransport);
3102                        if (name != null) {
3103                            mPendingInits.add(name);
3104                        } else {
3105                            if (DEBUG) {
3106                                Slog.w(TAG, "Couldn't find name of transport " + mTransport
3107                                        + " for init");
3108                            }
3109                        }
3110                    } catch (Exception e) {
3111                        Slog.w(TAG, "Failed to query transport name for init: " + e.getMessage());
3112                        // swallow it and proceed; we don't rely on this
3113                    }
3114                    clearMetadata();
3115                    backupNow();
3116                }
3117            }
3118
3119            clearBackupTrace();
3120
3121            unregisterTask();
3122
3123            if (!mCancelAll && mStatus == BackupTransport.TRANSPORT_OK &&
3124                    mPendingFullBackups != null && !mPendingFullBackups.isEmpty()) {
3125                Slog.d(TAG, "Starting full backups for: " + mPendingFullBackups);
3126                // Acquiring wakelock for PerformFullTransportBackupTask before its start.
3127                mWakelock.acquire();
3128                (new Thread(mFullBackupTask, "full-transport-requested")).start();
3129            } else if (mCancelAll) {
3130                if (mFullBackupTask != null) {
3131                    mFullBackupTask.unregisterTask();
3132                }
3133                sendBackupFinished(mObserver, BackupManager.ERROR_BACKUP_CANCELLED);
3134            } else {
3135                mFullBackupTask.unregisterTask();
3136                switch (mStatus) {
3137                    case BackupTransport.TRANSPORT_OK:
3138                        sendBackupFinished(mObserver, BackupManager.SUCCESS);
3139                        break;
3140                    case BackupTransport.TRANSPORT_NOT_INITIALIZED:
3141                        sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
3142                        break;
3143                    case BackupTransport.TRANSPORT_ERROR:
3144                    default:
3145                        sendBackupFinished(mObserver, BackupManager.ERROR_TRANSPORT_ABORTED);
3146                        break;
3147                }
3148            }
3149            Slog.i(BackupManagerService.TAG, "K/V backup pass finished.");
3150            // Only once we're entirely finished do we release the wakelock for k/v backup.
3151            mWakelock.release();
3152        }
3153
3154        // Remove the PM metadata state. This will generate an init on the next pass.
3155        void clearMetadata() {
3156            final File pmState = new File(mStateDir, PACKAGE_MANAGER_SENTINEL);
3157            if (pmState.exists()) pmState.delete();
3158        }
3159
3160        // Invoke an agent's doBackup() and start a timeout message spinning on the main
3161        // handler in case it doesn't get back to us.
3162        int invokeAgentForBackup(String packageName, IBackupAgent agent,
3163                IBackupTransport transport) {
3164            if (DEBUG) Slog.d(TAG, "invokeAgentForBackup on " + packageName);
3165            addBackupTrace("invoking " + packageName);
3166
3167            File blankStateName = new File(mStateDir, "blank_state");
3168            mSavedStateName = new File(mStateDir, packageName);
3169            mBackupDataName = new File(mDataDir, packageName + ".data");
3170            mNewStateName = new File(mStateDir, packageName + ".new");
3171            if (MORE_DEBUG) Slog.d(TAG, "data file: " + mBackupDataName);
3172
3173            mSavedState = null;
3174            mBackupData = null;
3175            mNewState = null;
3176
3177            boolean callingAgent = false;
3178            mEphemeralOpToken = generateRandomIntegerToken();
3179            try {
3180                // Look up the package info & signatures.  This is first so that if it
3181                // throws an exception, there's no file setup yet that would need to
3182                // be unraveled.
3183                if (packageName.equals(PACKAGE_MANAGER_SENTINEL)) {
3184                    // The metadata 'package' is synthetic; construct one and make
3185                    // sure our global state is pointed at it
3186                    mCurrentPackage = new PackageInfo();
3187                    mCurrentPackage.packageName = packageName;
3188                }
3189
3190                // In a full backup, we pass a null ParcelFileDescriptor as
3191                // the saved-state "file". For key/value backups we pass the old state if
3192                // an incremental backup is required, and a blank state otherwise.
3193                mSavedState = ParcelFileDescriptor.open(
3194                        mNonIncremental ? blankStateName : mSavedStateName,
3195                        ParcelFileDescriptor.MODE_READ_ONLY |
3196                        ParcelFileDescriptor.MODE_CREATE);  // Make an empty file if necessary
3197
3198                mBackupData = ParcelFileDescriptor.open(mBackupDataName,
3199                        ParcelFileDescriptor.MODE_READ_WRITE |
3200                        ParcelFileDescriptor.MODE_CREATE |
3201                        ParcelFileDescriptor.MODE_TRUNCATE);
3202
3203                if (!SELinux.restorecon(mBackupDataName)) {
3204                    Slog.e(TAG, "SELinux restorecon failed on " + mBackupDataName);
3205                }
3206
3207                mNewState = ParcelFileDescriptor.open(mNewStateName,
3208                        ParcelFileDescriptor.MODE_READ_WRITE |
3209                        ParcelFileDescriptor.MODE_CREATE |
3210                        ParcelFileDescriptor.MODE_TRUNCATE);
3211
3212                final long quota = mTransport.getBackupQuota(packageName, false /* isFullBackup */);
3213                callingAgent = true;
3214
3215                // Initiate the target's backup pass
3216                addBackupTrace("setting timeout");
3217                prepareOperationTimeout(mEphemeralOpToken, TIMEOUT_BACKUP_INTERVAL, this,
3218                        OP_TYPE_BACKUP_WAIT);
3219                addBackupTrace("calling agent doBackup()");
3220
3221                agent.doBackup(mSavedState, mBackupData, mNewState, quota, mEphemeralOpToken,
3222                        mBackupManagerBinder);
3223            } catch (Exception e) {
3224                Slog.e(TAG, "Error invoking for backup on " + packageName + ". " + e);
3225                addBackupTrace("exception: " + e);
3226                EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName,
3227                        e.toString());
3228                errorCleanup();
3229                return callingAgent ? BackupTransport.AGENT_ERROR
3230                        : BackupTransport.TRANSPORT_ERROR;
3231            } finally {
3232                if (mNonIncremental) {
3233                    blankStateName.delete();
3234                }
3235            }
3236
3237            // At this point the agent is off and running.  The next thing to happen will
3238            // either be a callback from the agent, at which point we'll process its data
3239            // for transport, or a timeout.  Either way the next phase will happen in
3240            // response to the TimeoutHandler interface callbacks.
3241            addBackupTrace("invoke success");
3242            return BackupTransport.TRANSPORT_OK;
3243        }
3244
3245        public void failAgent(IBackupAgent agent, String message) {
3246            try {
3247                agent.fail(message);
3248            } catch (Exception e) {
3249                Slog.w(TAG, "Error conveying failure to " + mCurrentPackage.packageName);
3250            }
3251        }
3252
3253        // SHA-1 a byte array and return the result in hex
3254        private String SHA1Checksum(byte[] input) {
3255            final byte[] checksum;
3256            try {
3257                MessageDigest md = MessageDigest.getInstance("SHA-1");
3258                checksum = md.digest(input);
3259            } catch (NoSuchAlgorithmException e) {
3260                Slog.e(TAG, "Unable to use SHA-1!");
3261                return "00";
3262            }
3263
3264            StringBuffer sb = new StringBuffer(checksum.length * 2);
3265            for (int i = 0; i < checksum.length; i++) {
3266                sb.append(Integer.toHexString(checksum[i]));
3267            }
3268            return sb.toString();
3269        }
3270
3271        private void writeWidgetPayloadIfAppropriate(FileDescriptor fd, String pkgName)
3272                throws IOException {
3273            // TODO: http://b/22388012
3274            byte[] widgetState = AppWidgetBackupBridge.getWidgetState(pkgName,
3275                    UserHandle.USER_SYSTEM);
3276            // has the widget state changed since last time?
3277            final File widgetFile = new File(mStateDir, pkgName + "_widget");
3278            final boolean priorStateExists = widgetFile.exists();
3279
3280            if (MORE_DEBUG) {
3281                if (priorStateExists || widgetState != null) {
3282                    Slog.i(TAG, "Checking widget update: state=" + (widgetState != null)
3283                            + " prior=" + priorStateExists);
3284                }
3285            }
3286
3287            if (!priorStateExists && widgetState == null) {
3288                // no prior state, no new state => nothing to do
3289                return;
3290            }
3291
3292            // if the new state is not null, we might need to compare checksums to
3293            // determine whether to update the widget blob in the archive.  If the
3294            // widget state *is* null, we know a priori at this point that we simply
3295            // need to commit a deletion for it.
3296            String newChecksum = null;
3297            if (widgetState != null) {
3298                newChecksum = SHA1Checksum(widgetState);
3299                if (priorStateExists) {
3300                    final String priorChecksum;
3301                    try (
3302                        FileInputStream fin = new FileInputStream(widgetFile);
3303                        DataInputStream in = new DataInputStream(fin)
3304                    ) {
3305                        priorChecksum = in.readUTF();
3306                    }
3307                    if (Objects.equals(newChecksum, priorChecksum)) {
3308                        // Same checksum => no state change => don't rewrite the widget data
3309                        return;
3310                    }
3311                }
3312            } // else widget state *became* empty, so we need to commit a deletion
3313
3314            BackupDataOutput out = new BackupDataOutput(fd);
3315            if (widgetState != null) {
3316                try (
3317                    FileOutputStream fout = new FileOutputStream(widgetFile);
3318                    DataOutputStream stateOut = new DataOutputStream(fout)
3319                ) {
3320                    stateOut.writeUTF(newChecksum);
3321                }
3322
3323                out.writeEntityHeader(KEY_WIDGET_STATE, widgetState.length);
3324                out.writeEntityData(widgetState, widgetState.length);
3325            } else {
3326                // Widget state for this app has been removed; commit a deletion
3327                out.writeEntityHeader(KEY_WIDGET_STATE, -1);
3328                widgetFile.delete();
3329            }
3330        }
3331
3332        @Override
3333        @GuardedBy("mCancelLock")
3334        public void operationComplete(long unusedResult) {
3335            removeOperation(mEphemeralOpToken);
3336            synchronized (mCancelLock) {
3337                // The agent reported back to us!
3338                if (mFinished) {
3339                    Slog.d(TAG, "operationComplete received after task finished.");
3340                    return;
3341                }
3342
3343                if (mBackupData == null) {
3344                    // This callback was racing with our timeout, so we've cleaned up the
3345                    // agent state already and are on to the next thing.  We have nothing
3346                    // further to do here: agent state having been cleared means that we've
3347                    // initiated the appropriate next operation.
3348                    final String pkg = (mCurrentPackage != null)
3349                            ? mCurrentPackage.packageName : "[none]";
3350                    if (MORE_DEBUG) {
3351                        Slog.i(TAG, "Callback after agent teardown: " + pkg);
3352                    }
3353                    addBackupTrace("late opComplete; curPkg = " + pkg);
3354                    return;
3355                }
3356
3357                final String pkgName = mCurrentPackage.packageName;
3358                final long filepos = mBackupDataName.length();
3359                FileDescriptor fd = mBackupData.getFileDescriptor();
3360                try {
3361                    // If it's a 3rd party app, see whether they wrote any protected keys
3362                    // and complain mightily if they are attempting shenanigans.
3363                    if (mCurrentPackage.applicationInfo != null &&
3364                            (mCurrentPackage.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
3365                                    == 0) {
3366                        ParcelFileDescriptor readFd = ParcelFileDescriptor.open(mBackupDataName,
3367                                ParcelFileDescriptor.MODE_READ_ONLY);
3368                        BackupDataInput in = new BackupDataInput(readFd.getFileDescriptor());
3369                        try {
3370                            while (in.readNextHeader()) {
3371                                final String key = in.getKey();
3372                                if (key != null && key.charAt(0) >= 0xff00) {
3373                                    // Not okay: crash them and bail.
3374                                    failAgent(mAgentBinder, "Illegal backup key: " + key);
3375                                    addBackupTrace("illegal key " + key + " from " + pkgName);
3376                                    EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, pkgName,
3377                                            "bad key");
3378                                    mMonitor = monitorEvent(mMonitor,
3379                                            BackupManagerMonitor.LOG_EVENT_ID_ILLEGAL_KEY,
3380                                            mCurrentPackage,
3381                                            BackupManagerMonitor
3382                                                    .LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
3383                                            putMonitoringExtra(null,
3384                                                    BackupManagerMonitor.EXTRA_LOG_ILLEGAL_KEY,
3385                                                    key));
3386                                    mBackupHandler.removeMessages(MSG_BACKUP_OPERATION_TIMEOUT);
3387                                    sendBackupOnPackageResult(mObserver, pkgName,
3388                                            BackupManager.ERROR_AGENT_FAILURE);
3389                                    errorCleanup();
3390                                    // agentErrorCleanup() implicitly executes next state properly
3391                                    return;
3392                                }
3393                                in.skipEntityData();
3394                            }
3395                        } finally {
3396                            if (readFd != null) {
3397                                readFd.close();
3398                            }
3399                        }
3400                    }
3401
3402                    // Piggyback the widget state payload, if any
3403                    writeWidgetPayloadIfAppropriate(fd, pkgName);
3404                } catch (IOException e) {
3405                    // Hard disk error; recovery/failure policy TBD.  For now roll back,
3406                    // but we may want to consider this a transport-level failure (i.e.
3407                    // we're in such a bad state that we can't contemplate doing backup
3408                    // operations any more during this pass).
3409                    Slog.w(TAG, "Unable to save widget state for " + pkgName);
3410                    try {
3411                        Os.ftruncate(fd, filepos);
3412                    } catch (ErrnoException ee) {
3413                        Slog.w(TAG, "Unable to roll back!");
3414                    }
3415                }
3416
3417                // Spin the data off to the transport and proceed with the next stage.
3418                if (MORE_DEBUG) Slog.v(TAG, "operationComplete(): sending data to transport for "
3419                        + pkgName);
3420                mBackupHandler.removeMessages(MSG_BACKUP_OPERATION_TIMEOUT);
3421                clearAgentState();
3422                addBackupTrace("operation complete");
3423
3424                ParcelFileDescriptor backupData = null;
3425                mStatus = BackupTransport.TRANSPORT_OK;
3426                long size = 0;
3427                try {
3428                    size = mBackupDataName.length();
3429                    if (size > 0) {
3430                        if (mStatus == BackupTransport.TRANSPORT_OK) {
3431                            backupData = ParcelFileDescriptor.open(mBackupDataName,
3432                                    ParcelFileDescriptor.MODE_READ_ONLY);
3433                            addBackupTrace("sending data to transport");
3434                            int flags = mUserInitiated ? BackupTransport.FLAG_USER_INITIATED : 0;
3435                            mStatus = mTransport.performBackup(mCurrentPackage, backupData, flags);
3436                        }
3437
3438                        // TODO - We call finishBackup() for each application backed up, because
3439                        // we need to know now whether it succeeded or failed.  Instead, we should
3440                        // hold off on finishBackup() until the end, which implies holding off on
3441                        // renaming *all* the output state files (see below) until that happens.
3442
3443                        addBackupTrace("data delivered: " + mStatus);
3444                        if (mStatus == BackupTransport.TRANSPORT_OK) {
3445                            addBackupTrace("finishing op on transport");
3446                            mStatus = mTransport.finishBackup();
3447                            addBackupTrace("finished: " + mStatus);
3448                        } else if (mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
3449                            addBackupTrace("transport rejected package");
3450                        }
3451                    } else {
3452                        if (MORE_DEBUG) Slog.i(TAG,
3453                                "no backup data written; not calling transport");
3454                        addBackupTrace("no data to send");
3455                        mMonitor = monitorEvent(mMonitor,
3456                                BackupManagerMonitor.LOG_EVENT_ID_NO_DATA_TO_SEND,
3457                                mCurrentPackage,
3458                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
3459                                null);
3460                    }
3461
3462                    if (mStatus == BackupTransport.TRANSPORT_OK) {
3463                        // After successful transport, delete the now-stale data
3464                        // and juggle the files so that next time we supply the agent
3465                        // with the new state file it just created.
3466                        mBackupDataName.delete();
3467                        mNewStateName.renameTo(mSavedStateName);
3468                        sendBackupOnPackageResult(mObserver, pkgName, BackupManager.SUCCESS);
3469                        EventLog.writeEvent(EventLogTags.BACKUP_PACKAGE, pkgName, size);
3470                        logBackupComplete(pkgName);
3471                    } else if (mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
3472                        // The transport has rejected backup of this specific package.  Roll it
3473                        // back but proceed with running the rest of the queue.
3474                        mBackupDataName.delete();
3475                        mNewStateName.delete();
3476                        sendBackupOnPackageResult(mObserver, pkgName,
3477                                BackupManager.ERROR_TRANSPORT_PACKAGE_REJECTED);
3478                        EventLogTags.writeBackupAgentFailure(pkgName, "Transport rejected");
3479                    } else if (mStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
3480                        sendBackupOnPackageResult(mObserver, pkgName,
3481                                BackupManager.ERROR_TRANSPORT_QUOTA_EXCEEDED);
3482                        EventLog.writeEvent(EventLogTags.BACKUP_QUOTA_EXCEEDED, pkgName);
3483                    } else {
3484                        // Actual transport-level failure to communicate the data to the backend
3485                        sendBackupOnPackageResult(mObserver, pkgName,
3486                                BackupManager.ERROR_TRANSPORT_ABORTED);
3487                        EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, pkgName);
3488                    }
3489                } catch (Exception e) {
3490                    sendBackupOnPackageResult(mObserver, pkgName,
3491                            BackupManager.ERROR_TRANSPORT_ABORTED);
3492                    Slog.e(TAG, "Transport error backing up " + pkgName, e);
3493                    EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, pkgName);
3494                    mStatus = BackupTransport.TRANSPORT_ERROR;
3495                } finally {
3496                    try {
3497                        if (backupData != null) backupData.close();
3498                    } catch (IOException e) {
3499                    }
3500                }
3501
3502                final BackupState nextState;
3503                if (mStatus == BackupTransport.TRANSPORT_OK
3504                        || mStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
3505                    // Success or single-package rejection.  Proceed with the next app if any,
3506                    // otherwise we're done.
3507                    nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE;
3508                } else if (mStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
3509                    if (MORE_DEBUG) {
3510                        Slog.d(TAG, "Package " + mCurrentPackage.packageName +
3511                                " hit quota limit on k/v backup");
3512                    }
3513                    if (mAgentBinder != null) {
3514                        try {
3515                            long quota = mTransport.getBackupQuota(mCurrentPackage.packageName,
3516                                    false);
3517                            mAgentBinder.doQuotaExceeded(size, quota);
3518                        } catch (Exception e) {
3519                            Slog.e(TAG, "Unable to notify about quota exceeded: " + e.getMessage());
3520                        }
3521                    }
3522                    nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE;
3523                } else {
3524                    // Any other error here indicates a transport-level failure.  That means
3525                    // we need to halt everything and reschedule everything for next time.
3526                    revertAndEndBackup();
3527                    nextState = BackupState.FINAL;
3528                }
3529
3530                executeNextState(nextState);
3531            }
3532        }
3533
3534
3535        @Override
3536        @GuardedBy("mCancelLock")
3537        public void handleCancel(boolean cancelAll) {
3538            removeOperation(mEphemeralOpToken);
3539            synchronized (mCancelLock) {
3540                if (mFinished) {
3541                    // We have already cancelled this operation.
3542                    if (MORE_DEBUG) {
3543                        Slog.d(TAG, "Ignoring stale cancel. cancelAll=" + cancelAll);
3544                    }
3545                    return;
3546                }
3547                mCancelAll = cancelAll;
3548                final String logPackageName = (mCurrentPackage != null)
3549                        ? mCurrentPackage.packageName
3550                        : "no_package_yet";
3551                Slog.i(TAG, "Cancel backing up " + logPackageName);
3552                EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, logPackageName);
3553                addBackupTrace("cancel of " + logPackageName + ", cancelAll=" + cancelAll);
3554                mMonitor = monitorEvent(mMonitor,
3555                        BackupManagerMonitor.LOG_EVENT_ID_KEY_VALUE_BACKUP_CANCEL,
3556                        mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT,
3557                        putMonitoringExtra(null, BackupManagerMonitor.EXTRA_LOG_CANCEL_ALL,
3558                                mCancelAll));
3559                errorCleanup();
3560                if (!cancelAll) {
3561                    // The current agent either timed out or was cancelled running doBackup().
3562                    // Restage it for the next time we run a backup pass.
3563                    // !!! TODO: keep track of failure counts per agent, and blacklist those which
3564                    // fail repeatedly (i.e. have proved themselves to be buggy).
3565                    executeNextState(
3566                            mQueue.isEmpty() ? BackupState.FINAL : BackupState.RUNNING_QUEUE);
3567                    dataChangedImpl(mCurrentPackage.packageName);
3568                } else {
3569                    finalizeBackup();
3570                }
3571            }
3572        }
3573
3574        void revertAndEndBackup() {
3575            if (MORE_DEBUG) Slog.i(TAG, "Reverting backup queue - restaging everything");
3576            addBackupTrace("transport error; reverting");
3577
3578            // We want to reset the backup schedule based on whatever the transport suggests
3579            // by way of retry/backoff time.
3580            long delay;
3581            try {
3582                delay = mTransport.requestBackupTime();
3583            } catch (Exception e) {
3584                Slog.w(TAG, "Unable to contact transport for recommended backoff: " + e.getMessage());
3585                delay = 0;  // use the scheduler's default
3586            }
3587            KeyValueBackupJob.schedule(mContext, delay);
3588
3589            for (BackupRequest request : mOriginalQueue) {
3590                dataChangedImpl(request.packageName);
3591            }
3592
3593        }
3594
3595        void errorCleanup() {
3596            mBackupDataName.delete();
3597            mNewStateName.delete();
3598            clearAgentState();
3599        }
3600
3601        // Cleanup common to both success and failure cases
3602        void clearAgentState() {
3603            try { if (mSavedState != null) mSavedState.close(); } catch (IOException e) {}
3604            try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {}
3605            try { if (mNewState != null) mNewState.close(); } catch (IOException e) {}
3606            synchronized (mCurrentOpLock) {
3607                // Current-operation callback handling requires the validity of these various
3608                // bits of internal state as an invariant of the operation still being live.
3609                // This means we make sure to clear all of the state in unison inside the lock.
3610                mCurrentOperations.remove(mEphemeralOpToken);
3611                mSavedState = mBackupData = mNewState = null;
3612            }
3613
3614            // If this was a pseudopackage there's no associated Activity Manager state
3615            if (mCurrentPackage.applicationInfo != null) {
3616                addBackupTrace("unbinding " + mCurrentPackage.packageName);
3617                try {  // unbind even on timeout, just in case
3618                    mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo);
3619                } catch (RemoteException e) { /* can't happen; activity manager is local */ }
3620            }
3621        }
3622
3623        void executeNextState(BackupState nextState) {
3624            if (MORE_DEBUG) Slog.i(TAG, " => executing next step on "
3625                    + this + " nextState=" + nextState);
3626            addBackupTrace("executeNextState => " + nextState);
3627            mCurrentState = nextState;
3628            Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this);
3629            mBackupHandler.sendMessage(msg);
3630        }
3631    }
3632
3633    private boolean isBackupOperationInProgress() {
3634        synchronized (mCurrentOpLock) {
3635            for (int i = 0; i < mCurrentOperations.size(); i++) {
3636                Operation op = mCurrentOperations.valueAt(i);
3637                if (op.type == OP_TYPE_BACKUP && op.state == OP_PENDING) {
3638                    return true;
3639                }
3640            }
3641        }
3642        return false;
3643    }
3644
3645
3646    // ----- Full backup/restore to a file/socket -----
3647
3648    class FullBackupObbConnection implements ServiceConnection {
3649        volatile IObbBackupService mService;
3650
3651        FullBackupObbConnection() {
3652            mService = null;
3653        }
3654
3655        public void establish() {
3656            if (MORE_DEBUG) Slog.i(TAG, "Initiating bind of OBB service on " + this);
3657            Intent obbIntent = new Intent().setComponent(new ComponentName(
3658                    "com.android.sharedstoragebackup",
3659                    "com.android.sharedstoragebackup.ObbBackupService"));
3660            BackupManagerService.this.mContext.bindServiceAsUser(
3661                    obbIntent, this, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM);
3662        }
3663
3664        public void tearDown() {
3665            BackupManagerService.this.mContext.unbindService(this);
3666        }
3667
3668        public boolean backupObbs(PackageInfo pkg, OutputStream out) {
3669            boolean success = false;
3670            waitForConnection();
3671
3672            ParcelFileDescriptor[] pipes = null;
3673            try {
3674                pipes = ParcelFileDescriptor.createPipe();
3675                int token = generateRandomIntegerToken();
3676                prepareOperationTimeout(token, TIMEOUT_FULL_BACKUP_INTERVAL,
3677                        null, OP_TYPE_BACKUP_WAIT);
3678                mService.backupObbs(pkg.packageName, pipes[1], token, mBackupManagerBinder);
3679                routeSocketDataToOutput(pipes[0], out);
3680                success = waitUntilOperationComplete(token);
3681            } catch (Exception e) {
3682                Slog.w(TAG, "Unable to back up OBBs for " + pkg, e);
3683            } finally {
3684                try {
3685                    out.flush();
3686                    if (pipes != null) {
3687                        if (pipes[0] != null) pipes[0].close();
3688                        if (pipes[1] != null) pipes[1].close();
3689                    }
3690                } catch (IOException e) {
3691                    Slog.w(TAG, "I/O error closing down OBB backup", e);
3692                }
3693            }
3694            return success;
3695        }
3696
3697        public void restoreObbFile(String pkgName, ParcelFileDescriptor data,
3698                long fileSize, int type, String path, long mode, long mtime,
3699                int token, IBackupManager callbackBinder) {
3700            waitForConnection();
3701
3702            try {
3703                mService.restoreObbFile(pkgName, data, fileSize, type, path, mode, mtime,
3704                        token, callbackBinder);
3705            } catch (Exception e) {
3706                Slog.w(TAG, "Unable to restore OBBs for " + pkgName, e);
3707            }
3708        }
3709
3710        private void waitForConnection() {
3711            synchronized (this) {
3712                while (mService == null) {
3713                    if (MORE_DEBUG) Slog.i(TAG, "...waiting for OBB service binding...");
3714                    try {
3715                        this.wait();
3716                    } catch (InterruptedException e) { /* never interrupted */ }
3717                }
3718                if (MORE_DEBUG) Slog.i(TAG, "Connected to OBB service; continuing");
3719            }
3720        }
3721
3722        @Override
3723        public void onServiceConnected(ComponentName name, IBinder service) {
3724            synchronized (this) {
3725                mService = IObbBackupService.Stub.asInterface(service);
3726                if (MORE_DEBUG) Slog.i(TAG, "OBB service connection " + mService
3727                        + " connected on " + this);
3728                this.notifyAll();
3729            }
3730        }
3731
3732        @Override
3733        public void onServiceDisconnected(ComponentName name) {
3734            synchronized (this) {
3735                mService = null;
3736                if (MORE_DEBUG) Slog.i(TAG, "OBB service connection disconnected on " + this);
3737                this.notifyAll();
3738            }
3739        }
3740
3741    }
3742
3743    static void routeSocketDataToOutput(ParcelFileDescriptor inPipe, OutputStream out)
3744            throws IOException {
3745        // We do not take close() responsibility for the pipe FD
3746        FileInputStream raw = new FileInputStream(inPipe.getFileDescriptor());
3747        DataInputStream in = new DataInputStream(raw);
3748
3749        byte[] buffer = new byte[32 * 1024];
3750        int chunkTotal;
3751        while ((chunkTotal = in.readInt()) > 0) {
3752            while (chunkTotal > 0) {
3753                int toRead = (chunkTotal > buffer.length) ? buffer.length : chunkTotal;
3754                int nRead = in.read(buffer, 0, toRead);
3755                out.write(buffer, 0, nRead);
3756                chunkTotal -= nRead;
3757            }
3758        }
3759    }
3760
3761    @Override
3762    public void tearDownAgentAndKill(ApplicationInfo app) {
3763        if (app == null) {
3764            // Null means the system package, so just quietly move on.  :)
3765            return;
3766        }
3767
3768        try {
3769            // unbind and tidy up even on timeout or failure, just in case
3770            mActivityManager.unbindBackupAgent(app);
3771
3772            // The agent was running with a stub Application object, so shut it down.
3773            // !!! We hardcode the confirmation UI's package name here rather than use a
3774            //     manifest flag!  TODO something less direct.
3775            if (app.uid >= Process.FIRST_APPLICATION_UID
3776                    && !app.packageName.equals("com.android.backupconfirm")) {
3777                if (MORE_DEBUG) Slog.d(TAG, "Killing agent host process");
3778                mActivityManager.killApplicationProcess(app.processName, app.uid);
3779            } else {
3780                if (MORE_DEBUG) Slog.d(TAG, "Not killing after operation: " + app.processName);
3781            }
3782        } catch (RemoteException e) {
3783            Slog.d(TAG, "Lost app trying to shut down");
3784        }
3785    }
3786
3787    // Core logic for performing one package's full backup, gathering the tarball from the
3788    // application and emitting it to the designated OutputStream.
3789
3790    // Callout from the engine to an interested participant that might need to communicate
3791    // with the agent prior to asking it to move data
3792    interface FullBackupPreflight {
3793        /**
3794         * Perform the preflight operation necessary for the given package.
3795         * @param pkg The name of the package being proposed for full-data backup
3796         * @param agent Live BackupAgent binding to the target app's agent
3797         * @return BackupTransport.TRANSPORT_OK to proceed with the backup operation,
3798         *         or one of the other BackupTransport.* error codes as appropriate
3799         */
3800        int preflightFullBackup(PackageInfo pkg, IBackupAgent agent);
3801
3802        long getExpectedSizeOrErrorCode();
3803    };
3804
3805    class FullBackupEngine {
3806        OutputStream mOutput;
3807        FullBackupPreflight mPreflightHook;
3808        BackupRestoreTask mTimeoutMonitor;
3809        IBackupAgent mAgent;
3810        File mFilesDir;
3811        File mManifestFile;
3812        File mMetadataFile;
3813        boolean mIncludeApks;
3814        PackageInfo mPkg;
3815        private final long mQuota;
3816        private final int mOpToken;
3817
3818        class FullBackupRunner implements Runnable {
3819            PackageInfo mPackage;
3820            byte[] mWidgetData;
3821            IBackupAgent mAgent;
3822            ParcelFileDescriptor mPipe;
3823            int mToken;
3824            boolean mSendApk;
3825            boolean mWriteManifest;
3826
3827            FullBackupRunner(PackageInfo pack, IBackupAgent agent, ParcelFileDescriptor pipe,
3828                             int token, boolean sendApk, boolean writeManifest, byte[] widgetData)
3829                    throws IOException {
3830                mPackage = pack;
3831                mWidgetData = widgetData;
3832                mAgent = agent;
3833                mPipe = ParcelFileDescriptor.dup(pipe.getFileDescriptor());
3834                mToken = token;
3835                mSendApk = sendApk;
3836                mWriteManifest = writeManifest;
3837            }
3838
3839            @Override
3840            public void run() {
3841                try {
3842                    FullBackupDataOutput output = new FullBackupDataOutput(mPipe);
3843
3844                    if (mWriteManifest) {
3845                        final boolean writeWidgetData = mWidgetData != null;
3846                        if (MORE_DEBUG) Slog.d(TAG, "Writing manifest for " + mPackage.packageName);
3847                        writeAppManifest(mPackage, mPackageManager, mManifestFile, mSendApk, writeWidgetData);
3848                        FullBackup.backupToTar(mPackage.packageName, null, null,
3849                                mFilesDir.getAbsolutePath(),
3850                                mManifestFile.getAbsolutePath(),
3851                                output);
3852                        mManifestFile.delete();
3853
3854                        // We only need to write a metadata file if we have widget data to stash
3855                        if (writeWidgetData) {
3856                            writeMetadata(mPackage, mMetadataFile, mWidgetData);
3857                            FullBackup.backupToTar(mPackage.packageName, null, null,
3858                                    mFilesDir.getAbsolutePath(),
3859                                    mMetadataFile.getAbsolutePath(),
3860                                    output);
3861                            mMetadataFile.delete();
3862                        }
3863                    }
3864
3865                    if (mSendApk) {
3866                        writeApkToBackup(mPackage, output);
3867                    }
3868
3869                    final boolean isSharedStorage =
3870                            mPackage.packageName.equals(SHARED_BACKUP_AGENT_PACKAGE);
3871                    final long timeout = isSharedStorage ?
3872                            TIMEOUT_SHARED_BACKUP_INTERVAL : TIMEOUT_FULL_BACKUP_INTERVAL;
3873
3874                    if (DEBUG) Slog.d(TAG, "Calling doFullBackup() on " + mPackage.packageName);
3875                    prepareOperationTimeout(mToken, timeout, mTimeoutMonitor /* in parent class */,
3876                            OP_TYPE_BACKUP_WAIT);
3877                    mAgent.doFullBackup(mPipe, mQuota, mToken, mBackupManagerBinder);
3878                } catch (IOException e) {
3879                    Slog.e(TAG, "Error running full backup for " + mPackage.packageName);
3880                } catch (RemoteException e) {
3881                    Slog.e(TAG, "Remote agent vanished during full backup of "
3882                            + mPackage.packageName);
3883                } finally {
3884                    try {
3885                        mPipe.close();
3886                    } catch (IOException e) {}
3887                }
3888            }
3889        }
3890
3891        FullBackupEngine(OutputStream output, FullBackupPreflight preflightHook, PackageInfo pkg,
3892                         boolean alsoApks, BackupRestoreTask timeoutMonitor, long quota, int opToken) {
3893            mOutput = output;
3894            mPreflightHook = preflightHook;
3895            mPkg = pkg;
3896            mIncludeApks = alsoApks;
3897            mTimeoutMonitor = timeoutMonitor;
3898            mFilesDir = new File("/data/system");
3899            mManifestFile = new File(mFilesDir, BACKUP_MANIFEST_FILENAME);
3900            mMetadataFile = new File(mFilesDir, BACKUP_METADATA_FILENAME);
3901            mQuota = quota;
3902            mOpToken = opToken;
3903        }
3904
3905        public int preflightCheck() throws RemoteException {
3906            if (mPreflightHook == null) {
3907                if (MORE_DEBUG) {
3908                    Slog.v(TAG, "No preflight check");
3909                }
3910                return BackupTransport.TRANSPORT_OK;
3911            }
3912            if (initializeAgent()) {
3913                int result = mPreflightHook.preflightFullBackup(mPkg, mAgent);
3914                if (MORE_DEBUG) {
3915                    Slog.v(TAG, "preflight returned " + result);
3916                }
3917                return result;
3918            } else {
3919                Slog.w(TAG, "Unable to bind to full agent for " + mPkg.packageName);
3920                return BackupTransport.AGENT_ERROR;
3921            }
3922        }
3923
3924        public int backupOnePackage() throws RemoteException {
3925            int result = BackupTransport.AGENT_ERROR;
3926
3927            if (initializeAgent()) {
3928                ParcelFileDescriptor[] pipes = null;
3929                try {
3930                    pipes = ParcelFileDescriptor.createPipe();
3931
3932                    ApplicationInfo app = mPkg.applicationInfo;
3933                    final boolean isSharedStorage =
3934                            mPkg.packageName.equals(SHARED_BACKUP_AGENT_PACKAGE);
3935                    final boolean sendApk = mIncludeApks
3936                            && !isSharedStorage
3937                            && ((app.privateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) == 0)
3938                            && ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0 ||
3939                            (app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
3940
3941                    // TODO: http://b/22388012
3942                    byte[] widgetBlob = AppWidgetBackupBridge.getWidgetState(mPkg.packageName,
3943                            UserHandle.USER_SYSTEM);
3944
3945                    FullBackupRunner runner = new FullBackupRunner(mPkg, mAgent, pipes[1],
3946                            mOpToken, sendApk, !isSharedStorage, widgetBlob);
3947                    pipes[1].close();   // the runner has dup'd it
3948                    pipes[1] = null;
3949                    Thread t = new Thread(runner, "app-data-runner");
3950                    t.start();
3951
3952                    // Now pull data from the app and stuff it into the output
3953                    routeSocketDataToOutput(pipes[0], mOutput);
3954
3955                    if (!waitUntilOperationComplete(mOpToken)) {
3956                        Slog.e(TAG, "Full backup failed on package " + mPkg.packageName);
3957                    } else {
3958                        if (MORE_DEBUG) {
3959                            Slog.d(TAG, "Full package backup success: " + mPkg.packageName);
3960                        }
3961                        result = BackupTransport.TRANSPORT_OK;
3962                    }
3963                } catch (IOException e) {
3964                    Slog.e(TAG, "Error backing up " + mPkg.packageName + ": " + e.getMessage());
3965                    result = BackupTransport.AGENT_ERROR;
3966                } finally {
3967                    try {
3968                        // flush after every package
3969                        mOutput.flush();
3970                        if (pipes != null) {
3971                            if (pipes[0] != null) pipes[0].close();
3972                            if (pipes[1] != null) pipes[1].close();
3973                        }
3974                    } catch (IOException e) {
3975                        Slog.w(TAG, "Error bringing down backup stack");
3976                        result = BackupTransport.TRANSPORT_ERROR;
3977                    }
3978                }
3979            } else {
3980                Slog.w(TAG, "Unable to bind to full agent for " + mPkg.packageName);
3981            }
3982            tearDown();
3983            return result;
3984        }
3985
3986        public void sendQuotaExceeded(final long backupDataBytes, final long quotaBytes) {
3987            if (initializeAgent()) {
3988                try {
3989                    mAgent.doQuotaExceeded(backupDataBytes, quotaBytes);
3990                } catch (RemoteException e) {
3991                    Slog.e(TAG, "Remote exception while telling agent about quota exceeded");
3992                }
3993            }
3994        }
3995
3996        private boolean initializeAgent() {
3997            if (mAgent == null) {
3998                if (MORE_DEBUG) {
3999                    Slog.d(TAG, "Binding to full backup agent : " + mPkg.packageName);
4000                }
4001                mAgent = bindToAgentSynchronous(mPkg.applicationInfo,
4002                        ApplicationThreadConstants.BACKUP_MODE_FULL);
4003            }
4004            return mAgent != null;
4005        }
4006
4007        private void writeApkToBackup(PackageInfo pkg, FullBackupDataOutput output) {
4008            // Forward-locked apps, system-bundled .apks, etc are filtered out before we get here
4009            // TODO: handle backing up split APKs
4010            final String appSourceDir = pkg.applicationInfo.getBaseCodePath();
4011            final String apkDir = new File(appSourceDir).getParent();
4012            FullBackup.backupToTar(pkg.packageName, FullBackup.APK_TREE_TOKEN, null,
4013                    apkDir, appSourceDir, output);
4014
4015            // TODO: migrate this to SharedStorageBackup, since AID_SYSTEM
4016            // doesn't have access to external storage.
4017
4018            // Save associated .obb content if it exists and we did save the apk
4019            // check for .obb and save those too
4020            // TODO: http://b/22388012
4021            final UserEnvironment userEnv = new UserEnvironment(UserHandle.USER_SYSTEM);
4022            final File obbDir = userEnv.buildExternalStorageAppObbDirs(pkg.packageName)[0];
4023            if (obbDir != null) {
4024                if (MORE_DEBUG) Log.i(TAG, "obb dir: " + obbDir.getAbsolutePath());
4025                File[] obbFiles = obbDir.listFiles();
4026                if (obbFiles != null) {
4027                    final String obbDirName = obbDir.getAbsolutePath();
4028                    for (File obb : obbFiles) {
4029                        FullBackup.backupToTar(pkg.packageName, FullBackup.OBB_TREE_TOKEN, null,
4030                                obbDirName, obb.getAbsolutePath(), output);
4031                    }
4032                }
4033            }
4034        }
4035
4036        // Widget metadata format. All header entries are strings ending in LF:
4037        //
4038        // Version 1 header:
4039        //     BACKUP_METADATA_VERSION, currently "1"
4040        //     package name
4041        //
4042        // File data (all integers are binary in network byte order)
4043        // *N: 4 : integer token identifying which metadata blob
4044        //     4 : integer size of this blob = N
4045        //     N : raw bytes of this metadata blob
4046        //
4047        // Currently understood blobs (always in network byte order):
4048        //
4049        //     widgets : metadata token = 0x01FFED01 (BACKUP_WIDGET_METADATA_TOKEN)
4050        //
4051        // Unrecognized blobs are *ignored*, not errors.
4052        private void writeMetadata(PackageInfo pkg, File destination, byte[] widgetData)
4053                throws IOException {
4054            StringBuilder b = new StringBuilder(512);
4055            StringBuilderPrinter printer = new StringBuilderPrinter(b);
4056            printer.println(Integer.toString(BACKUP_METADATA_VERSION));
4057            printer.println(pkg.packageName);
4058
4059            FileOutputStream fout = new FileOutputStream(destination);
4060            BufferedOutputStream bout = new BufferedOutputStream(fout);
4061            DataOutputStream out = new DataOutputStream(bout);
4062            bout.write(b.toString().getBytes());    // bypassing DataOutputStream
4063
4064            if (widgetData != null && widgetData.length > 0) {
4065                out.writeInt(BACKUP_WIDGET_METADATA_TOKEN);
4066                out.writeInt(widgetData.length);
4067                out.write(widgetData);
4068            }
4069            bout.flush();
4070            out.close();
4071
4072            // As with the manifest file, guarantee idempotence of the archive metadata
4073            // for the widget block by using a fixed mtime on the transient file.
4074            destination.setLastModified(0);
4075        }
4076
4077        private void tearDown() {
4078            if (mPkg != null) {
4079                tearDownAgentAndKill(mPkg.applicationInfo);
4080            }
4081        }
4082    }
4083
4084    static void writeAppManifest(PackageInfo pkg, PackageManager packageManager, File manifestFile,
4085            boolean withApk, boolean withWidgets) throws IOException {
4086        // Manifest format. All data are strings ending in LF:
4087        //     BACKUP_MANIFEST_VERSION, currently 1
4088        //
4089        // Version 1:
4090        //     package name
4091        //     package's versionCode
4092        //     platform versionCode
4093        //     getInstallerPackageName() for this package (maybe empty)
4094        //     boolean: "1" if archive includes .apk; any other string means not
4095        //     number of signatures == N
4096        // N*:    signature byte array in ascii format per Signature.toCharsString()
4097        StringBuilder builder = new StringBuilder(4096);
4098        StringBuilderPrinter printer = new StringBuilderPrinter(builder);
4099
4100        printer.println(Integer.toString(BACKUP_MANIFEST_VERSION));
4101        printer.println(pkg.packageName);
4102        printer.println(Integer.toString(pkg.versionCode));
4103        printer.println(Integer.toString(Build.VERSION.SDK_INT));
4104
4105        String installerName = packageManager.getInstallerPackageName(pkg.packageName);
4106        printer.println((installerName != null) ? installerName : "");
4107
4108        printer.println(withApk ? "1" : "0");
4109        if (pkg.signatures == null) {
4110            printer.println("0");
4111        } else {
4112            printer.println(Integer.toString(pkg.signatures.length));
4113            for (Signature sig : pkg.signatures) {
4114                printer.println(sig.toCharsString());
4115            }
4116        }
4117
4118        FileOutputStream outstream = new FileOutputStream(manifestFile);
4119        outstream.write(builder.toString().getBytes());
4120        outstream.close();
4121
4122        // We want the manifest block in the archive stream to be idempotent:
4123        // each time we generate a backup stream for the app, we want the manifest
4124        // block to be identical.  The underlying tar mechanism sees it as a file,
4125        // though, and will propagate its mtime, causing the tar header to vary.
4126        // Avoid this problem by pinning the mtime to zero.
4127        manifestFile.setLastModified(0);
4128    }
4129
4130    // Generic driver skeleton for full backup operations
4131    abstract class FullBackupTask implements Runnable {
4132        IFullBackupRestoreObserver mObserver;
4133
4134        FullBackupTask(IFullBackupRestoreObserver observer) {
4135            mObserver = observer;
4136        }
4137
4138        // wrappers for observer use
4139        final void sendStartBackup() {
4140            if (mObserver != null) {
4141                try {
4142                    mObserver.onStartBackup();
4143                } catch (RemoteException e) {
4144                    Slog.w(TAG, "full backup observer went away: startBackup");
4145                    mObserver = null;
4146                }
4147            }
4148        }
4149
4150        final void sendOnBackupPackage(String name) {
4151            if (mObserver != null) {
4152                try {
4153                    // TODO: use a more user-friendly name string
4154                    mObserver.onBackupPackage(name);
4155                } catch (RemoteException e) {
4156                    Slog.w(TAG, "full backup observer went away: backupPackage");
4157                    mObserver = null;
4158                }
4159            }
4160        }
4161
4162        final void sendEndBackup() {
4163            if (mObserver != null) {
4164                try {
4165                    mObserver.onEndBackup();
4166                } catch (RemoteException e) {
4167                    Slog.w(TAG, "full backup observer went away: endBackup");
4168                    mObserver = null;
4169                }
4170            }
4171        }
4172    }
4173
4174    boolean deviceIsEncrypted() {
4175        try {
4176            return mStorageManager.getEncryptionState()
4177                     != StorageManager.ENCRYPTION_STATE_NONE
4178                && mStorageManager.getPasswordType()
4179                     != StorageManager.CRYPT_TYPE_DEFAULT;
4180        } catch (Exception e) {
4181            // If we can't talk to the storagemanager service we have a serious problem; fail
4182            // "secure" i.e. assuming that the device is encrypted.
4183            Slog.e(TAG, "Unable to communicate with storagemanager service: " + e.getMessage());
4184            return true;
4185        }
4186    }
4187
4188    // Full backup task variant used for adb backup
4189    class PerformAdbBackupTask extends FullBackupTask implements BackupRestoreTask {
4190        FullBackupEngine mBackupEngine;
4191        final AtomicBoolean mLatch;
4192
4193        ParcelFileDescriptor mOutputFile;
4194        DeflaterOutputStream mDeflater;
4195        boolean mIncludeApks;
4196        boolean mIncludeObbs;
4197        boolean mIncludeShared;
4198        boolean mDoWidgets;
4199        boolean mAllApps;
4200        boolean mIncludeSystem;
4201        boolean mCompress;
4202        boolean mKeyValue;
4203        ArrayList<String> mPackages;
4204        PackageInfo mCurrentTarget;
4205        String mCurrentPassword;
4206        String mEncryptPassword;
4207        private final int mCurrentOpToken;
4208
4209        PerformAdbBackupTask(ParcelFileDescriptor fd, IFullBackupRestoreObserver observer,
4210                boolean includeApks, boolean includeObbs, boolean includeShared, boolean doWidgets,
4211                String curPassword, String encryptPassword, boolean doAllApps, boolean doSystem,
4212                boolean doCompress, boolean doKeyValue, String[] packages, AtomicBoolean latch) {
4213            super(observer);
4214            mCurrentOpToken = generateRandomIntegerToken();
4215            mLatch = latch;
4216
4217            mOutputFile = fd;
4218            mIncludeApks = includeApks;
4219            mIncludeObbs = includeObbs;
4220            mIncludeShared = includeShared;
4221            mDoWidgets = doWidgets;
4222            mAllApps = doAllApps;
4223            mIncludeSystem = doSystem;
4224            mPackages = (packages == null)
4225                    ? new ArrayList<String>()
4226                    : new ArrayList<String>(Arrays.asList(packages));
4227            mCurrentPassword = curPassword;
4228            // when backing up, if there is a current backup password, we require that
4229            // the user use a nonempty encryption password as well.  if one is supplied
4230            // in the UI we use that, but if the UI was left empty we fall back to the
4231            // current backup password (which was supplied by the user as well).
4232            if (encryptPassword == null || "".equals(encryptPassword)) {
4233                mEncryptPassword = curPassword;
4234            } else {
4235                mEncryptPassword = encryptPassword;
4236            }
4237            if (MORE_DEBUG) {
4238                Slog.w(TAG, "Encrypting backup with passphrase=" + mEncryptPassword);
4239            }
4240            mCompress = doCompress;
4241            mKeyValue = doKeyValue;
4242        }
4243
4244        void addPackagesToSet(TreeMap<String, PackageInfo> set, List<String> pkgNames) {
4245            for (String pkgName : pkgNames) {
4246                if (!set.containsKey(pkgName)) {
4247                    try {
4248                        PackageInfo info = mPackageManager.getPackageInfo(pkgName,
4249                                PackageManager.GET_SIGNATURES);
4250                        set.put(pkgName, info);
4251                    } catch (NameNotFoundException e) {
4252                        Slog.w(TAG, "Unknown package " + pkgName + ", skipping");
4253                    }
4254                }
4255            }
4256        }
4257
4258        private OutputStream emitAesBackupHeader(StringBuilder headerbuf,
4259                OutputStream ofstream) throws Exception {
4260            // User key will be used to encrypt the master key.
4261            byte[] newUserSalt = randomBytes(PBKDF2_SALT_SIZE);
4262            SecretKey userKey = buildPasswordKey(PBKDF_CURRENT, mEncryptPassword, newUserSalt,
4263                    PBKDF2_HASH_ROUNDS);
4264
4265            // the master key is random for each backup
4266            byte[] masterPw = new byte[256 / 8];
4267            mRng.nextBytes(masterPw);
4268            byte[] checksumSalt = randomBytes(PBKDF2_SALT_SIZE);
4269
4270            // primary encryption of the datastream with the random key
4271            Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
4272            SecretKeySpec masterKeySpec = new SecretKeySpec(masterPw, "AES");
4273            c.init(Cipher.ENCRYPT_MODE, masterKeySpec);
4274            OutputStream finalOutput = new CipherOutputStream(ofstream, c);
4275
4276            // line 4: name of encryption algorithm
4277            headerbuf.append(ENCRYPTION_ALGORITHM_NAME);
4278            headerbuf.append('\n');
4279            // line 5: user password salt [hex]
4280            headerbuf.append(byteArrayToHex(newUserSalt));
4281            headerbuf.append('\n');
4282            // line 6: master key checksum salt [hex]
4283            headerbuf.append(byteArrayToHex(checksumSalt));
4284            headerbuf.append('\n');
4285            // line 7: number of PBKDF2 rounds used [decimal]
4286            headerbuf.append(PBKDF2_HASH_ROUNDS);
4287            headerbuf.append('\n');
4288
4289            // line 8: IV of the user key [hex]
4290            Cipher mkC = Cipher.getInstance("AES/CBC/PKCS5Padding");
4291            mkC.init(Cipher.ENCRYPT_MODE, userKey);
4292
4293            byte[] IV = mkC.getIV();
4294            headerbuf.append(byteArrayToHex(IV));
4295            headerbuf.append('\n');
4296
4297            // line 9: master IV + key blob, encrypted by the user key [hex].  Blob format:
4298            //    [byte] IV length = Niv
4299            //    [array of Niv bytes] IV itself
4300            //    [byte] master key length = Nmk
4301            //    [array of Nmk bytes] master key itself
4302            //    [byte] MK checksum hash length = Nck
4303            //    [array of Nck bytes] master key checksum hash
4304            //
4305            // The checksum is the (master key + checksum salt), run through the
4306            // stated number of PBKDF2 rounds
4307            IV = c.getIV();
4308            byte[] mk = masterKeySpec.getEncoded();
4309            byte[] checksum = makeKeyChecksum(PBKDF_CURRENT, masterKeySpec.getEncoded(),
4310                    checksumSalt, PBKDF2_HASH_ROUNDS);
4311
4312            ByteArrayOutputStream blob = new ByteArrayOutputStream(IV.length + mk.length
4313                    + checksum.length + 3);
4314            DataOutputStream mkOut = new DataOutputStream(blob);
4315            mkOut.writeByte(IV.length);
4316            mkOut.write(IV);
4317            mkOut.writeByte(mk.length);
4318            mkOut.write(mk);
4319            mkOut.writeByte(checksum.length);
4320            mkOut.write(checksum);
4321            mkOut.flush();
4322            byte[] encryptedMk = mkC.doFinal(blob.toByteArray());
4323            headerbuf.append(byteArrayToHex(encryptedMk));
4324            headerbuf.append('\n');
4325
4326            return finalOutput;
4327        }
4328
4329        private void finalizeBackup(OutputStream out) {
4330            try {
4331                // A standard 'tar' EOF sequence: two 512-byte blocks of all zeroes.
4332                byte[] eof = new byte[512 * 2]; // newly allocated == zero filled
4333                out.write(eof);
4334            } catch (IOException e) {
4335                Slog.w(TAG, "Error attempting to finalize backup stream");
4336            }
4337        }
4338
4339        @Override
4340        public void run() {
4341            String includeKeyValue = mKeyValue ? ", including key-value backups" : "";
4342            Slog.i(TAG, "--- Performing adb backup" + includeKeyValue + " ---");
4343
4344            TreeMap<String, PackageInfo> packagesToBackup = new TreeMap<String, PackageInfo>();
4345            FullBackupObbConnection obbConnection = new FullBackupObbConnection();
4346            obbConnection.establish();  // we'll want this later
4347
4348            sendStartBackup();
4349
4350            // doAllApps supersedes the package set if any
4351            if (mAllApps) {
4352                List<PackageInfo> allPackages = mPackageManager.getInstalledPackages(
4353                        PackageManager.GET_SIGNATURES);
4354                for (int i = 0; i < allPackages.size(); i++) {
4355                    PackageInfo pkg = allPackages.get(i);
4356                    // Exclude system apps if we've been asked to do so
4357                    if (mIncludeSystem == true
4358                            || ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) {
4359                        packagesToBackup.put(pkg.packageName, pkg);
4360                    }
4361                }
4362            }
4363
4364            // If we're doing widget state as well, ensure that we have all the involved
4365            // host & provider packages in the set
4366            if (mDoWidgets) {
4367                // TODO: http://b/22388012
4368                List<String> pkgs =
4369                        AppWidgetBackupBridge.getWidgetParticipants(UserHandle.USER_SYSTEM);
4370                if (pkgs != null) {
4371                    if (MORE_DEBUG) {
4372                        Slog.i(TAG, "Adding widget participants to backup set:");
4373                        StringBuilder sb = new StringBuilder(128);
4374                        sb.append("   ");
4375                        for (String s : pkgs) {
4376                            sb.append(' ');
4377                            sb.append(s);
4378                        }
4379                        Slog.i(TAG, sb.toString());
4380                    }
4381                    addPackagesToSet(packagesToBackup, pkgs);
4382                }
4383            }
4384
4385            // Now process the command line argument packages, if any. Note that explicitly-
4386            // named system-partition packages will be included even if includeSystem was
4387            // set to false.
4388            if (mPackages != null) {
4389                addPackagesToSet(packagesToBackup, mPackages);
4390            }
4391
4392            // Now we cull any inapplicable / inappropriate packages from the set.  This
4393            // includes the special shared-storage agent package; we handle that one
4394            // explicitly at the end of the backup pass. Packages supporting key-value backup are
4395            // added to their own queue, and handled after packages supporting fullbackup.
4396            ArrayList<PackageInfo> keyValueBackupQueue = new ArrayList<>();
4397            Iterator<Entry<String, PackageInfo>> iter = packagesToBackup.entrySet().iterator();
4398            while (iter.hasNext()) {
4399                PackageInfo pkg = iter.next().getValue();
4400                if (!appIsEligibleForBackup(pkg.applicationInfo)
4401                        || appIsStopped(pkg.applicationInfo)) {
4402                    iter.remove();
4403                    if (DEBUG) {
4404                        Slog.i(TAG, "Package " + pkg.packageName
4405                                + " is not eligible for backup, removing.");
4406                    }
4407                } else if (appIsKeyValueOnly(pkg)) {
4408                    iter.remove();
4409                    if (DEBUG) {
4410                        Slog.i(TAG, "Package " + pkg.packageName
4411                                + " is key-value.");
4412                    }
4413                    keyValueBackupQueue.add(pkg);
4414                }
4415            }
4416
4417            // flatten the set of packages now so we can explicitly control the ordering
4418            ArrayList<PackageInfo> backupQueue =
4419                    new ArrayList<PackageInfo>(packagesToBackup.values());
4420            FileOutputStream ofstream = new FileOutputStream(mOutputFile.getFileDescriptor());
4421            OutputStream out = null;
4422
4423            PackageInfo pkg = null;
4424            try {
4425                boolean encrypting = (mEncryptPassword != null && mEncryptPassword.length() > 0);
4426
4427                // Only allow encrypted backups of encrypted devices
4428                if (deviceIsEncrypted() && !encrypting) {
4429                    Slog.e(TAG, "Unencrypted backup of encrypted device; aborting");
4430                    return;
4431                }
4432
4433                OutputStream finalOutput = ofstream;
4434
4435                // Verify that the given password matches the currently-active
4436                // backup password, if any
4437                if (!backupPasswordMatches(mCurrentPassword)) {
4438                    if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting");
4439                    return;
4440                }
4441
4442                // Write the global file header.  All strings are UTF-8 encoded; lines end
4443                // with a '\n' byte.  Actual backup data begins immediately following the
4444                // final '\n'.
4445                //
4446                // line 1: "ANDROID BACKUP"
4447                // line 2: backup file format version, currently "5"
4448                // line 3: compressed?  "0" if not compressed, "1" if compressed.
4449                // line 4: name of encryption algorithm [currently only "none" or "AES-256"]
4450                //
4451                // When line 4 is not "none", then additional header data follows:
4452                //
4453                // line 5: user password salt [hex]
4454                // line 6: master key checksum salt [hex]
4455                // line 7: number of PBKDF2 rounds to use (same for user & master) [decimal]
4456                // line 8: IV of the user key [hex]
4457                // line 9: master key blob [hex]
4458                //     IV of the master key, master key itself, master key checksum hash
4459                //
4460                // The master key checksum is the master key plus its checksum salt, run through
4461                // 10k rounds of PBKDF2.  This is used to verify that the user has supplied the
4462                // correct password for decrypting the archive:  the master key decrypted from
4463                // the archive using the user-supplied password is also run through PBKDF2 in
4464                // this way, and if the result does not match the checksum as stored in the
4465                // archive, then we know that the user-supplied password does not match the
4466                // archive's.
4467                StringBuilder headerbuf = new StringBuilder(1024);
4468
4469                headerbuf.append(BACKUP_FILE_HEADER_MAGIC);
4470                headerbuf.append(BACKUP_FILE_VERSION); // integer, no trailing \n
4471                headerbuf.append(mCompress ? "\n1\n" : "\n0\n");
4472
4473                try {
4474                    // Set up the encryption stage if appropriate, and emit the correct header
4475                    if (encrypting) {
4476                        finalOutput = emitAesBackupHeader(headerbuf, finalOutput);
4477                    } else {
4478                        headerbuf.append("none\n");
4479                    }
4480
4481                    byte[] header = headerbuf.toString().getBytes("UTF-8");
4482                    ofstream.write(header);
4483
4484                    // Set up the compression stage feeding into the encryption stage (if any)
4485                    if (mCompress) {
4486                        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
4487                        finalOutput = new DeflaterOutputStream(finalOutput, deflater, true);
4488                    }
4489
4490                    out = finalOutput;
4491                } catch (Exception e) {
4492                    // Should never happen!
4493                    Slog.e(TAG, "Unable to emit archive header", e);
4494                    return;
4495                }
4496
4497                // Shared storage if requested
4498                if (mIncludeShared) {
4499                    try {
4500                        pkg = mPackageManager.getPackageInfo(SHARED_BACKUP_AGENT_PACKAGE, 0);
4501                        backupQueue.add(pkg);
4502                    } catch (NameNotFoundException e) {
4503                        Slog.e(TAG, "Unable to find shared-storage backup handler");
4504                    }
4505                }
4506
4507                // Now actually run the constructed backup sequence for full backup
4508                int N = backupQueue.size();
4509                for (int i = 0; i < N; i++) {
4510                    pkg = backupQueue.get(i);
4511                    if (DEBUG) {
4512                        Slog.i(TAG,"--- Performing full backup for package " + pkg.packageName
4513                                + " ---");
4514                    }
4515                    final boolean isSharedStorage =
4516                            pkg.packageName.equals(SHARED_BACKUP_AGENT_PACKAGE);
4517
4518                    mBackupEngine = new FullBackupEngine(out, null, pkg, mIncludeApks, this, Long.MAX_VALUE, mCurrentOpToken);
4519                    sendOnBackupPackage(isSharedStorage ? "Shared storage" : pkg.packageName);
4520
4521                    // Don't need to check preflight result as there is no preflight hook.
4522                    mCurrentTarget = pkg;
4523                    mBackupEngine.backupOnePackage();
4524
4525                    // after the app's agent runs to handle its private filesystem
4526                    // contents, back up any OBB content it has on its behalf.
4527                    if (mIncludeObbs) {
4528                        boolean obbOkay = obbConnection.backupObbs(pkg, out);
4529                        if (!obbOkay) {
4530                            throw new RuntimeException("Failure writing OBB stack for " + pkg);
4531                        }
4532                    }
4533                }
4534                // And for key-value backup if enabled
4535                if (mKeyValue) {
4536                    for (PackageInfo keyValuePackage : keyValueBackupQueue) {
4537                        if (DEBUG) {
4538                            Slog.i(TAG, "--- Performing key-value backup for package "
4539                                    + keyValuePackage.packageName + " ---");
4540                        }
4541                        KeyValueAdbBackupEngine kvBackupEngine =
4542                                new KeyValueAdbBackupEngine(out, keyValuePackage,
4543                                        BackupManagerService.this,
4544                                        mPackageManager, mBaseStateDir, mDataDir);
4545                        sendOnBackupPackage(keyValuePackage.packageName);
4546                        kvBackupEngine.backupOnePackage();
4547                    }
4548                }
4549
4550                // Done!
4551                finalizeBackup(out);
4552            } catch (RemoteException e) {
4553                Slog.e(TAG, "App died during full backup");
4554            } catch (Exception e) {
4555                Slog.e(TAG, "Internal exception during full backup", e);
4556            } finally {
4557                try {
4558                    if (out != null) {
4559                        out.flush();
4560                        out.close();
4561                    }
4562                    mOutputFile.close();
4563                } catch (IOException e) {
4564                    /* nothing we can do about this */
4565                }
4566                synchronized (mLatch) {
4567                    mLatch.set(true);
4568                    mLatch.notifyAll();
4569                }
4570                sendEndBackup();
4571                obbConnection.tearDown();
4572                if (DEBUG) Slog.d(TAG, "Full backup pass complete.");
4573                mWakelock.release();
4574            }
4575        }
4576
4577        // BackupRestoreTask methods, used for timeout handling
4578        @Override
4579        public void execute() {
4580            // Unused
4581        }
4582
4583        @Override
4584        public void operationComplete(long result) {
4585            // Unused
4586        }
4587
4588        @Override
4589        public void handleCancel(boolean cancelAll) {
4590            final PackageInfo target = mCurrentTarget;
4591            if (DEBUG) {
4592                Slog.w(TAG, "adb backup cancel of " + target);
4593            }
4594            if (target != null) {
4595                tearDownAgentAndKill(mCurrentTarget.applicationInfo);
4596            }
4597            removeOperation(mCurrentOpToken);
4598        }
4599    }
4600
4601    /**
4602     * Full backup task extension used for transport-oriented operation.
4603     *
4604     * Flow:
4605     * For each requested package:
4606     *     - Spin off a new SinglePackageBackupRunner (mBackupRunner) for the current package.
4607     *     - Wait until preflight is complete. (mBackupRunner.getPreflightResultBlocking())
4608     *     - If preflight data size is within limit, start reading data from agent pipe and writing
4609     *       to transport pipe. While there is data to send, call transport.sendBackupData(int) to
4610     *       tell the transport how many bytes to expect on its pipe.
4611     *     - After sending all data, call transport.finishBackup() if things went well. And
4612     *       transport.cancelFullBackup() otherwise.
4613     *
4614     * Interactions with mCurrentOperations:
4615     *     - An entry for this object is added to mCurrentOperations for the entire lifetime of this
4616     *       object. Used to cancel the operation.
4617     *     - SinglePackageBackupRunner and SinglePackageBackupPreflight will put ephemeral entries
4618     *       to get timeouts or operation complete callbacks.
4619     *
4620     * Handling cancels:
4621     *     - The contract we provide is that the task won't interact with the transport after
4622     *       handleCancel() is done executing.
4623     *     - This task blocks at 3 points: 1. Preflight result check 2. Reading on agent side pipe
4624     *       and 3. Get backup result from mBackupRunner.
4625     *     - Bubbling up handleCancel to mBackupRunner handles all 3: 1. Calls handleCancel on the
4626     *       preflight operation which counts down on the preflight latch. 2. Tears down the agent,
4627     *       so read() returns -1. 3. Notifies mCurrentOpLock which unblocks
4628     *       mBackupRunner.getBackupResultBlocking().
4629     */
4630    class PerformFullTransportBackupTask extends FullBackupTask implements BackupRestoreTask {
4631        static final String TAG = "PFTBT";
4632
4633        private final Object mCancelLock = new Object();
4634
4635        ArrayList<PackageInfo> mPackages;
4636        PackageInfo mCurrentPackage;
4637        boolean mUpdateSchedule;
4638        CountDownLatch mLatch;
4639        FullBackupJob mJob;             // if a scheduled job needs to be finished afterwards
4640        IBackupObserver mBackupObserver;
4641        IBackupManagerMonitor mMonitor;
4642        boolean mUserInitiated;
4643        private volatile IBackupTransport mTransport;
4644        SinglePackageBackupRunner mBackupRunner;
4645        private final int mBackupRunnerOpToken;
4646
4647        // This is true when a backup operation for some package is in progress.
4648        private volatile boolean mIsDoingBackup;
4649        private volatile boolean mCancelAll;
4650        private final int mCurrentOpToken;
4651
4652        PerformFullTransportBackupTask(IFullBackupRestoreObserver observer,
4653                String[] whichPackages, boolean updateSchedule,
4654                FullBackupJob runningJob, CountDownLatch latch, IBackupObserver backupObserver,
4655                IBackupManagerMonitor monitor, boolean userInitiated) {
4656            super(observer);
4657            mUpdateSchedule = updateSchedule;
4658            mLatch = latch;
4659            mJob = runningJob;
4660            mPackages = new ArrayList<PackageInfo>(whichPackages.length);
4661            mBackupObserver = backupObserver;
4662            mMonitor = monitor;
4663            mUserInitiated = userInitiated;
4664            mCurrentOpToken = generateRandomIntegerToken();
4665            mBackupRunnerOpToken = generateRandomIntegerToken();
4666
4667            if (isBackupOperationInProgress()) {
4668                if (DEBUG) {
4669                    Slog.d(TAG, "Skipping full backup. A backup is already in progress.");
4670                }
4671                mCancelAll = true;
4672                return;
4673            }
4674
4675            registerTask();
4676
4677            for (String pkg : whichPackages) {
4678                try {
4679                    PackageInfo info = mPackageManager.getPackageInfo(pkg,
4680                            PackageManager.GET_SIGNATURES);
4681                    mCurrentPackage = info;
4682                    if (!appIsEligibleForBackup(info.applicationInfo)) {
4683                        // Cull any packages that have indicated that backups are not permitted,
4684                        // that run as system-domain uids but do not define their own backup agents,
4685                        // as well as any explicit mention of the 'special' shared-storage agent
4686                        // package (we handle that one at the end).
4687                        if (MORE_DEBUG) {
4688                            Slog.d(TAG, "Ignoring ineligible package " + pkg);
4689                        }
4690                        mMonitor = monitorEvent(mMonitor,
4691                                BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_INELIGIBLE,
4692                                mCurrentPackage,
4693                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
4694                                null);
4695                        sendBackupOnPackageResult(mBackupObserver, pkg,
4696                            BackupManager.ERROR_BACKUP_NOT_ALLOWED);
4697                        continue;
4698                    } else if (!appGetsFullBackup(info)) {
4699                        // Cull any packages that are found in the queue but now aren't supposed
4700                        // to get full-data backup operations.
4701                        if (MORE_DEBUG) {
4702                            Slog.d(TAG, "Ignoring full-data backup of key/value participant "
4703                                    + pkg);
4704                        }
4705                        mMonitor = monitorEvent(mMonitor,
4706                                BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_KEY_VALUE_PARTICIPANT,
4707                                mCurrentPackage,
4708                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
4709                                null);
4710                        sendBackupOnPackageResult(mBackupObserver, pkg,
4711                                BackupManager.ERROR_BACKUP_NOT_ALLOWED);
4712                        continue;
4713                    } else if (appIsStopped(info.applicationInfo)) {
4714                        // Cull any packages in the 'stopped' state: they've either just been
4715                        // installed or have explicitly been force-stopped by the user.  In both
4716                        // cases we do not want to launch them for backup.
4717                        if (MORE_DEBUG) {
4718                            Slog.d(TAG, "Ignoring stopped package " + pkg);
4719                        }
4720                        mMonitor = monitorEvent(mMonitor,
4721                                BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_STOPPED,
4722                                mCurrentPackage,
4723                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
4724                                null);
4725                        sendBackupOnPackageResult(mBackupObserver, pkg,
4726                                BackupManager.ERROR_BACKUP_NOT_ALLOWED);
4727                        continue;
4728                    }
4729                    mPackages.add(info);
4730                } catch (NameNotFoundException e) {
4731                    Slog.i(TAG, "Requested package " + pkg + " not found; ignoring");
4732                    mMonitor = monitorEvent(mMonitor,
4733                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_NOT_FOUND,
4734                            mCurrentPackage,
4735                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
4736                            null);
4737                }
4738            }
4739        }
4740
4741        private void registerTask() {
4742            synchronized (mCurrentOpLock) {
4743                Slog.d(TAG, "backupmanager pftbt token=" + Integer.toHexString(mCurrentOpToken));
4744                mCurrentOperations.put(mCurrentOpToken, new Operation(OP_PENDING, this,
4745                        OP_TYPE_BACKUP));
4746            }
4747        }
4748
4749        private void unregisterTask() {
4750            removeOperation(mCurrentOpToken);
4751        }
4752
4753        @Override
4754        public void execute() {
4755            // Nothing to do.
4756        }
4757
4758        @Override
4759        public void handleCancel(boolean cancelAll) {
4760            synchronized (mCancelLock) {
4761                // We only support 'cancelAll = true' case for this task. Cancelling of a single package
4762
4763                // due to timeout is handled by SinglePackageBackupRunner and SinglePackageBackupPreflight.
4764
4765                if (!cancelAll) {
4766                    Slog.wtf(TAG, "Expected cancelAll to be true.");
4767                }
4768
4769                if (mCancelAll) {
4770                    Slog.d(TAG, "Ignoring duplicate cancel call.");
4771                    return;
4772                }
4773
4774                mCancelAll = true;
4775                if (mIsDoingBackup) {
4776                    BackupManagerService.this.handleCancel(mBackupRunnerOpToken, cancelAll);
4777                    try {
4778                        mTransport.cancelFullBackup();
4779                    } catch (RemoteException e) {
4780                        Slog.w(TAG, "Error calling cancelFullBackup() on transport: " + e);
4781                        // Can't do much.
4782                    }
4783                }
4784            }
4785        }
4786
4787        @Override
4788        public void operationComplete(long result) {
4789            // Nothing to do.
4790        }
4791
4792        @Override
4793        public void run() {
4794
4795            // data from the app, passed to us for bridging to the transport
4796            ParcelFileDescriptor[] enginePipes = null;
4797
4798            // Pipe through which we write data to the transport
4799            ParcelFileDescriptor[] transportPipes = null;
4800
4801            long backoff = 0;
4802            int backupRunStatus = BackupManager.SUCCESS;
4803
4804            try {
4805                if (!mEnabled || !mProvisioned) {
4806                    // Backups are globally disabled, so don't proceed.
4807                    if (DEBUG) {
4808                        Slog.i(TAG, "full backup requested but enabled=" + mEnabled
4809                                + " provisioned=" + mProvisioned + "; ignoring");
4810                    }
4811                    int monitoringEvent;
4812                    if (!mEnabled) {
4813                        monitoringEvent = BackupManagerMonitor.LOG_EVENT_ID_BACKUP_DISABLED;
4814                    } else {
4815                        monitoringEvent = BackupManagerMonitor.LOG_EVENT_ID_DEVICE_NOT_PROVISIONED;
4816                    }
4817                    mMonitor = monitorEvent(mMonitor, monitoringEvent, null,
4818                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
4819                    mUpdateSchedule = false;
4820                    backupRunStatus = BackupManager.ERROR_BACKUP_NOT_ALLOWED;
4821                    return;
4822                }
4823
4824                mTransport = mTransportManager.getCurrentTransportBinder();
4825                if (mTransport == null) {
4826                    Slog.w(TAG, "Transport not present; full data backup not performed");
4827                    backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
4828                    mMonitor = monitorEvent(mMonitor,
4829                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_TRANSPORT_NOT_PRESENT,
4830                            mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT,
4831                            null);
4832                    return;
4833                }
4834
4835                // Set up to send data to the transport
4836                final int N = mPackages.size();
4837                final byte[] buffer = new byte[8192];
4838                for (int i = 0; i < N; i++) {
4839                    PackageInfo currentPackage = mPackages.get(i);
4840                    String packageName = currentPackage.packageName;
4841                    if (DEBUG) {
4842                        Slog.i(TAG, "Initiating full-data transport backup of " + packageName
4843                                + " token: " + mCurrentOpToken);
4844                    }
4845                    EventLog.writeEvent(EventLogTags.FULL_BACKUP_PACKAGE, packageName);
4846
4847                    transportPipes = ParcelFileDescriptor.createPipe();
4848
4849                    // Tell the transport the data's coming
4850                    int flags = mUserInitiated ? BackupTransport.FLAG_USER_INITIATED : 0;
4851                    int backupPackageStatus;
4852                    long quota = Long.MAX_VALUE;
4853                    synchronized (mCancelLock) {
4854                        if (mCancelAll) {
4855                            break;
4856                        }
4857                        backupPackageStatus = mTransport.performFullBackup(currentPackage,
4858                                transportPipes[0], flags);
4859
4860                        if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
4861                            quota = mTransport.getBackupQuota(currentPackage.packageName,
4862                                    true /* isFullBackup */);
4863                            // Now set up the backup engine / data source end of things
4864                            enginePipes = ParcelFileDescriptor.createPipe();
4865                            mBackupRunner =
4866                                    new SinglePackageBackupRunner(enginePipes[1], currentPackage,
4867                                            mTransport, quota, mBackupRunnerOpToken);
4868                            // The runner dup'd the pipe half, so we close it here
4869                            enginePipes[1].close();
4870                            enginePipes[1] = null;
4871
4872                            mIsDoingBackup = true;
4873                        }
4874                    }
4875                    if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
4876
4877                        // The transport has its own copy of the read end of the pipe,
4878                        // so close ours now
4879                        transportPipes[0].close();
4880                        transportPipes[0] = null;
4881
4882                        // Spin off the runner to fetch the app's data and pipe it
4883                        // into the engine pipes
4884                        (new Thread(mBackupRunner, "package-backup-bridge")).start();
4885
4886                        // Read data off the engine pipe and pass it to the transport
4887                        // pipe until we hit EOD on the input stream.  We do not take
4888                        // close() responsibility for these FDs into these stream wrappers.
4889                        FileInputStream in = new FileInputStream(
4890                                enginePipes[0].getFileDescriptor());
4891                        FileOutputStream out = new FileOutputStream(
4892                                transportPipes[1].getFileDescriptor());
4893                        long totalRead = 0;
4894                        final long preflightResult = mBackupRunner.getPreflightResultBlocking();
4895                        // Preflight result is negative if some error happened on preflight.
4896                        if (preflightResult < 0) {
4897                            if (MORE_DEBUG) {
4898                                Slog.d(TAG, "Backup error after preflight of package "
4899                                        + packageName + ": " + preflightResult
4900                                        + ", not running backup.");
4901                            }
4902                            mMonitor = monitorEvent(mMonitor,
4903                                    BackupManagerMonitor.LOG_EVENT_ID_ERROR_PREFLIGHT,
4904                                    mCurrentPackage,
4905                                    BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
4906                                    putMonitoringExtra(null,
4907                                            BackupManagerMonitor.EXTRA_LOG_PREFLIGHT_ERROR,
4908                                            preflightResult));
4909                            backupPackageStatus = (int) preflightResult;
4910                        } else {
4911                            int nRead = 0;
4912                            do {
4913                                nRead = in.read(buffer);
4914                                if (MORE_DEBUG) {
4915                                    Slog.v(TAG, "in.read(buffer) from app: " + nRead);
4916                                }
4917                                if (nRead > 0) {
4918                                    out.write(buffer, 0, nRead);
4919                                    synchronized (mCancelLock) {
4920                                        if (!mCancelAll) {
4921                                            backupPackageStatus = mTransport.sendBackupData(nRead);
4922                                        }
4923                                    }
4924                                    totalRead += nRead;
4925                                    if (mBackupObserver != null && preflightResult > 0) {
4926                                        sendBackupOnUpdate(mBackupObserver, packageName,
4927                                                new BackupProgress(preflightResult, totalRead));
4928                                    }
4929                                }
4930                            } while (nRead > 0
4931                                    && backupPackageStatus == BackupTransport.TRANSPORT_OK);
4932                            // Despite preflight succeeded, package still can hit quota on flight.
4933                            if (backupPackageStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
4934                                Slog.w(TAG, "Package hit quota limit in-flight " + packageName
4935                                        + ": " + totalRead + " of " + quota);
4936                                mMonitor = monitorEvent(mMonitor,
4937                                        BackupManagerMonitor.LOG_EVENT_ID_QUOTA_HIT_PREFLIGHT,
4938                                        mCurrentPackage,
4939                                        BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT,
4940                                        null);
4941                                mBackupRunner.sendQuotaExceeded(totalRead, quota);
4942                            }
4943                        }
4944
4945                        final int backupRunnerResult = mBackupRunner.getBackupResultBlocking();
4946
4947                        synchronized (mCancelLock) {
4948                            mIsDoingBackup = false;
4949                            // If mCancelCurrent is true, we have already called cancelFullBackup().
4950                            if (!mCancelAll) {
4951                                if (backupRunnerResult == BackupTransport.TRANSPORT_OK) {
4952                                    // If we were otherwise in a good state, now interpret the final
4953                                    // result based on what finishBackup() returns.  If we're in a
4954                                    // failure case already, preserve that result and ignore whatever
4955                                    // finishBackup() reports.
4956                                    final int finishResult = mTransport.finishBackup();
4957                                    if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
4958                                        backupPackageStatus = finishResult;
4959                                    }
4960                                } else {
4961                                    mTransport.cancelFullBackup();
4962                                }
4963                            }
4964                        }
4965
4966                        // A transport-originated error here means that we've hit an error that the
4967                        // runner doesn't know about, so it's still moving data but we're pulling the
4968                        // rug out from under it.  Don't ask for its result:  we already know better
4969                        // and we'll hang if we block waiting for it, since it relies on us to
4970                        // read back the data it's writing into the engine.  Just proceed with
4971                        // a graceful failure.  The runner/engine mechanism will tear itself
4972                        // down cleanly when we close the pipes from this end.  Transport-level
4973                        // errors take precedence over agent/app-specific errors for purposes of
4974                        // determining our course of action.
4975                        if (backupPackageStatus == BackupTransport.TRANSPORT_OK) {
4976                            // We still could fail in backup runner thread.
4977                            if (backupRunnerResult != BackupTransport.TRANSPORT_OK) {
4978                                // If there was an error in runner thread and
4979                                // not TRANSPORT_ERROR here, overwrite it.
4980                                backupPackageStatus = backupRunnerResult;
4981                            }
4982                        } else {
4983                            if (MORE_DEBUG) {
4984                                Slog.i(TAG, "Transport-level failure; cancelling agent work");
4985                            }
4986                        }
4987
4988                        if (MORE_DEBUG) {
4989                            Slog.i(TAG, "Done delivering backup data: result="
4990                                    + backupPackageStatus);
4991                        }
4992
4993                        if (backupPackageStatus != BackupTransport.TRANSPORT_OK) {
4994                            Slog.e(TAG, "Error " + backupPackageStatus + " backing up "
4995                                    + packageName);
4996                        }
4997
4998                        // Also ask the transport how long it wants us to wait before
4999                        // moving on to the next package, if any.
5000                        backoff = mTransport.requestFullBackupTime();
5001                        if (DEBUG_SCHEDULING) {
5002                            Slog.i(TAG, "Transport suggested backoff=" + backoff);
5003                        }
5004
5005                    }
5006
5007                    // Roll this package to the end of the backup queue if we're
5008                    // in a queue-driven mode (regardless of success/failure)
5009                    if (mUpdateSchedule) {
5010                        enqueueFullBackup(packageName, System.currentTimeMillis());
5011                    }
5012
5013                    if (backupPackageStatus == BackupTransport.TRANSPORT_PACKAGE_REJECTED) {
5014                        sendBackupOnPackageResult(mBackupObserver, packageName,
5015                                BackupManager.ERROR_TRANSPORT_PACKAGE_REJECTED);
5016                        if (DEBUG) {
5017                            Slog.i(TAG, "Transport rejected backup of " + packageName
5018                                    + ", skipping");
5019                        }
5020                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_AGENT_FAILURE, packageName,
5021                                "transport rejected");
5022                        // Do nothing, clean up, and continue looping.
5023                    } else if (backupPackageStatus == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
5024                        sendBackupOnPackageResult(mBackupObserver, packageName,
5025                                BackupManager.ERROR_TRANSPORT_QUOTA_EXCEEDED);
5026                        if (DEBUG) {
5027                            Slog.i(TAG, "Transport quota exceeded for package: " + packageName);
5028                            EventLog.writeEvent(EventLogTags.FULL_BACKUP_QUOTA_EXCEEDED,
5029                                    packageName);
5030                        }
5031                        // Do nothing, clean up, and continue looping.
5032                    } else if (backupPackageStatus == BackupTransport.AGENT_ERROR) {
5033                        sendBackupOnPackageResult(mBackupObserver, packageName,
5034                                BackupManager.ERROR_AGENT_FAILURE);
5035                        Slog.w(TAG, "Application failure for package: " + packageName);
5036                        EventLog.writeEvent(EventLogTags.BACKUP_AGENT_FAILURE, packageName);
5037                        tearDownAgentAndKill(currentPackage.applicationInfo);
5038                        // Do nothing, clean up, and continue looping.
5039                    } else if (backupPackageStatus == BackupManager.ERROR_BACKUP_CANCELLED) {
5040                        sendBackupOnPackageResult(mBackupObserver, packageName,
5041                                BackupManager.ERROR_BACKUP_CANCELLED);
5042                        Slog.w(TAG, "Backup cancelled. package=" + packageName +
5043                                ", cancelAll=" + mCancelAll);
5044                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_CANCELLED, packageName);
5045                        tearDownAgentAndKill(currentPackage.applicationInfo);
5046                        // Do nothing, clean up, and continue looping.
5047                    } else if (backupPackageStatus != BackupTransport.TRANSPORT_OK) {
5048                        sendBackupOnPackageResult(mBackupObserver, packageName,
5049                            BackupManager.ERROR_TRANSPORT_ABORTED);
5050                        Slog.w(TAG, "Transport failed; aborting backup: " + backupPackageStatus);
5051                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_TRANSPORT_FAILURE);
5052                        // Abort entire backup pass.
5053                        backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
5054                        return;
5055                    } else {
5056                        // Success!
5057                        sendBackupOnPackageResult(mBackupObserver, packageName,
5058                                BackupManager.SUCCESS);
5059                        EventLog.writeEvent(EventLogTags.FULL_BACKUP_SUCCESS, packageName);
5060                        logBackupComplete(packageName);
5061                    }
5062                    cleanUpPipes(transportPipes);
5063                    cleanUpPipes(enginePipes);
5064                    if (currentPackage.applicationInfo != null) {
5065                        Slog.i(TAG, "Unbinding agent in " + packageName);
5066                        addBackupTrace("unbinding " + packageName);
5067                        try {
5068                            mActivityManager.unbindBackupAgent(currentPackage.applicationInfo);
5069                        } catch (RemoteException e) { /* can't happen; activity manager is local */ }
5070                    }
5071                }
5072            } catch (Exception e) {
5073                backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
5074                Slog.w(TAG, "Exception trying full transport backup", e);
5075                mMonitor = monitorEvent(mMonitor,
5076                        BackupManagerMonitor.LOG_EVENT_ID_EXCEPTION_FULL_BACKUP,
5077                        mCurrentPackage,
5078                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
5079                        putMonitoringExtra(null,
5080                                BackupManagerMonitor.EXTRA_LOG_EXCEPTION_FULL_BACKUP,
5081                                Log.getStackTraceString(e)));
5082
5083            } finally {
5084
5085                if (mCancelAll) {
5086                    backupRunStatus = BackupManager.ERROR_BACKUP_CANCELLED;
5087                }
5088
5089                if (DEBUG) {
5090                    Slog.i(TAG, "Full backup completed with status: " + backupRunStatus);
5091                }
5092                sendBackupFinished(mBackupObserver, backupRunStatus);
5093
5094                cleanUpPipes(transportPipes);
5095                cleanUpPipes(enginePipes);
5096
5097                unregisterTask();
5098
5099                if (mJob != null) {
5100                    mJob.finishBackupPass();
5101                }
5102
5103                synchronized (mQueueLock) {
5104                    mRunningFullBackupTask = null;
5105                }
5106
5107                mLatch.countDown();
5108
5109                // Now that we're actually done with schedule-driven work, reschedule
5110                // the next pass based on the new queue state.
5111                if (mUpdateSchedule) {
5112                    scheduleNextFullBackupJob(backoff);
5113                }
5114
5115                Slog.i(BackupManagerService.TAG, "Full data backup pass finished.");
5116                mWakelock.release();
5117            }
5118        }
5119
5120        void cleanUpPipes(ParcelFileDescriptor[] pipes) {
5121            if (pipes != null) {
5122                if (pipes[0] != null) {
5123                    ParcelFileDescriptor fd = pipes[0];
5124                    pipes[0] = null;
5125                    try {
5126                        fd.close();
5127                    } catch (IOException e) {
5128                        Slog.w(TAG, "Unable to close pipe!");
5129                    }
5130                }
5131                if (pipes[1] != null) {
5132                    ParcelFileDescriptor fd = pipes[1];
5133                    pipes[1] = null;
5134                    try {
5135                        fd.close();
5136                    } catch (IOException e) {
5137                        Slog.w(TAG, "Unable to close pipe!");
5138                    }
5139                }
5140            }
5141        }
5142
5143        // Run the backup and pipe it back to the given socket -- expects to run on
5144        // a standalone thread.  The  runner owns this half of the pipe, and closes
5145        // it to indicate EOD to the other end.
5146        class SinglePackageBackupPreflight implements BackupRestoreTask, FullBackupPreflight {
5147            final AtomicLong mResult = new AtomicLong(BackupTransport.AGENT_ERROR);
5148            final CountDownLatch mLatch = new CountDownLatch(1);
5149            final IBackupTransport mTransport;
5150            final long mQuota;
5151            private final int mCurrentOpToken;
5152
5153            SinglePackageBackupPreflight(IBackupTransport transport, long quota, int currentOpToken) {
5154                mTransport = transport;
5155                mQuota = quota;
5156                mCurrentOpToken = currentOpToken;
5157            }
5158
5159            @Override
5160            public int preflightFullBackup(PackageInfo pkg, IBackupAgent agent) {
5161                int result;
5162                try {
5163                    prepareOperationTimeout(mCurrentOpToken, TIMEOUT_FULL_BACKUP_INTERVAL,
5164                            this, OP_TYPE_BACKUP_WAIT);
5165                    addBackupTrace("preflighting");
5166                    if (MORE_DEBUG) {
5167                        Slog.d(TAG, "Preflighting full payload of " + pkg.packageName);
5168                    }
5169                    agent.doMeasureFullBackup(mQuota, mCurrentOpToken, mBackupManagerBinder);
5170
5171                    // Now wait to get our result back.  If this backstop timeout is reached without
5172                    // the latch being thrown, flow will continue as though a result or "normal"
5173                    // timeout had been produced.  In case of a real backstop timeout, mResult
5174                    // will still contain the value it was constructed with, AGENT_ERROR, which
5175                    // intentionaly falls into the "just report failure" code.
5176                    mLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
5177
5178                    long totalSize = mResult.get();
5179                    // If preflight timed out, mResult will contain error code as int.
5180                    if (totalSize < 0) {
5181                        return (int) totalSize;
5182                    }
5183                    if (MORE_DEBUG) {
5184                        Slog.v(TAG, "Got preflight response; size=" + totalSize);
5185                    }
5186
5187                    result = mTransport.checkFullBackupSize(totalSize);
5188                    if (result == BackupTransport.TRANSPORT_QUOTA_EXCEEDED) {
5189                        if (MORE_DEBUG) {
5190                            Slog.d(TAG, "Package hit quota limit on preflight " +
5191                                    pkg.packageName + ": " + totalSize + " of " + mQuota);
5192                        }
5193                        agent.doQuotaExceeded(totalSize, mQuota);
5194                    }
5195                } catch (Exception e) {
5196                    Slog.w(TAG, "Exception preflighting " + pkg.packageName + ": " + e.getMessage());
5197                    result = BackupTransport.AGENT_ERROR;
5198                }
5199                return result;
5200            }
5201
5202            @Override
5203            public void execute() {
5204                // Unused.
5205            }
5206
5207            @Override
5208            public void operationComplete(long result) {
5209                // got the callback, and our preflightFullBackup() method is waiting for the result
5210                if (MORE_DEBUG) {
5211                    Slog.i(TAG, "Preflight op complete, result=" + result);
5212                }
5213                mResult.set(result);
5214                mLatch.countDown();
5215                removeOperation(mCurrentOpToken);
5216            }
5217
5218            @Override
5219            public void handleCancel(boolean cancelAll) {
5220                if (MORE_DEBUG) {
5221                    Slog.i(TAG, "Preflight cancelled; failing");
5222                }
5223                mResult.set(BackupTransport.AGENT_ERROR);
5224                mLatch.countDown();
5225                removeOperation(mCurrentOpToken);
5226            }
5227
5228            @Override
5229            public long getExpectedSizeOrErrorCode() {
5230                try {
5231                    mLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
5232                    return mResult.get();
5233                } catch (InterruptedException e) {
5234                    return BackupTransport.NO_MORE_DATA;
5235                }
5236            }
5237        }
5238
5239        class SinglePackageBackupRunner implements Runnable, BackupRestoreTask {
5240            final ParcelFileDescriptor mOutput;
5241            final PackageInfo mTarget;
5242            final SinglePackageBackupPreflight mPreflight;
5243            final CountDownLatch mPreflightLatch;
5244            final CountDownLatch mBackupLatch;
5245            private final int mCurrentOpToken;
5246            private final int mEphemeralToken;
5247            private FullBackupEngine mEngine;
5248            private volatile int mPreflightResult;
5249            private volatile int mBackupResult;
5250            private final long mQuota;
5251            private volatile boolean mIsCancelled;
5252
5253            SinglePackageBackupRunner(ParcelFileDescriptor output, PackageInfo target,
5254                    IBackupTransport transport, long quota, int currentOpToken) throws IOException {
5255                mOutput = ParcelFileDescriptor.dup(output.getFileDescriptor());
5256                mTarget = target;
5257                mCurrentOpToken = currentOpToken;
5258                mEphemeralToken = generateRandomIntegerToken();
5259                mPreflight = new SinglePackageBackupPreflight(transport, quota, mEphemeralToken);
5260                mPreflightLatch = new CountDownLatch(1);
5261                mBackupLatch = new CountDownLatch(1);
5262                mPreflightResult = BackupTransport.AGENT_ERROR;
5263                mBackupResult = BackupTransport.AGENT_ERROR;
5264                mQuota = quota;
5265                registerTask();
5266            }
5267
5268            void registerTask() {
5269                synchronized (mCurrentOpLock) {
5270                    mCurrentOperations.put(mCurrentOpToken, new Operation(OP_PENDING, this,
5271                            OP_TYPE_BACKUP_WAIT));
5272                }
5273            }
5274
5275            void unregisterTask() {
5276                synchronized (mCurrentOpLock) {
5277                    mCurrentOperations.remove(mCurrentOpToken);
5278                }
5279            }
5280
5281            @Override
5282            public void run() {
5283                FileOutputStream out = new FileOutputStream(mOutput.getFileDescriptor());
5284                mEngine = new FullBackupEngine(out, mPreflight, mTarget, false, this, mQuota, mCurrentOpToken);
5285                try {
5286                    try {
5287                        if (!mIsCancelled) {
5288                            mPreflightResult = mEngine.preflightCheck();
5289                        }
5290                    } finally {
5291                        mPreflightLatch.countDown();
5292                    }
5293                    // If there is no error on preflight, continue backup.
5294                    if (mPreflightResult == BackupTransport.TRANSPORT_OK) {
5295                        if (!mIsCancelled) {
5296                            mBackupResult = mEngine.backupOnePackage();
5297                        }
5298                    }
5299                } catch (Exception e) {
5300                    Slog.e(TAG, "Exception during full package backup of " + mTarget.packageName);
5301                } finally {
5302                    unregisterTask();
5303                    mBackupLatch.countDown();
5304                    try {
5305                        mOutput.close();
5306                    } catch (IOException e) {
5307                        Slog.w(TAG, "Error closing transport pipe in runner");
5308                    }
5309                }
5310            }
5311
5312            public void sendQuotaExceeded(final long backupDataBytes, final long quotaBytes) {
5313                mEngine.sendQuotaExceeded(backupDataBytes, quotaBytes);
5314            }
5315
5316            // If preflight succeeded, returns positive number - preflight size,
5317            // otherwise return negative error code.
5318            long getPreflightResultBlocking() {
5319                try {
5320                    mPreflightLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
5321                    if (mIsCancelled) {
5322                        return BackupManager.ERROR_BACKUP_CANCELLED;
5323                    }
5324                    if (mPreflightResult == BackupTransport.TRANSPORT_OK) {
5325                        return mPreflight.getExpectedSizeOrErrorCode();
5326                    } else {
5327                        return mPreflightResult;
5328                    }
5329                } catch (InterruptedException e) {
5330                    return BackupTransport.AGENT_ERROR;
5331                }
5332            }
5333
5334            int getBackupResultBlocking() {
5335                try {
5336                    mBackupLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
5337                    if (mIsCancelled) {
5338                        return BackupManager.ERROR_BACKUP_CANCELLED;
5339                    }
5340                    return mBackupResult;
5341                } catch (InterruptedException e) {
5342                    return BackupTransport.AGENT_ERROR;
5343                }
5344            }
5345
5346
5347            // BackupRestoreTask interface: specifically, timeout detection
5348
5349            @Override
5350            public void execute() { /* intentionally empty */ }
5351
5352            @Override
5353            public void operationComplete(long result) { /* intentionally empty */ }
5354
5355            @Override
5356            public void handleCancel(boolean cancelAll) {
5357                if (DEBUG) {
5358                    Slog.w(TAG, "Full backup cancel of " + mTarget.packageName);
5359                }
5360
5361                mMonitor = monitorEvent(mMonitor,
5362                        BackupManagerMonitor.LOG_EVENT_ID_FULL_BACKUP_CANCEL,
5363                        mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
5364                mIsCancelled = true;
5365                // Cancel tasks spun off by this task.
5366                BackupManagerService.this.handleCancel(mEphemeralToken, cancelAll);
5367                tearDownAgentAndKill(mTarget.applicationInfo);
5368                // Free up everyone waiting on this task and its children.
5369                mPreflightLatch.countDown();
5370                mBackupLatch.countDown();
5371                // We are done with this operation.
5372                removeOperation(mCurrentOpToken);
5373            }
5374        }
5375    }
5376
5377    // ----- Full-data backup scheduling -----
5378
5379    /**
5380     * Schedule a job to tell us when it's a good time to run a full backup
5381     */
5382    void scheduleNextFullBackupJob(long transportMinLatency) {
5383        synchronized (mQueueLock) {
5384            if (mFullBackupQueue.size() > 0) {
5385                // schedule the next job at the point in the future when the least-recently
5386                // backed up app comes due for backup again; or immediately if it's already
5387                // due.
5388                final long upcomingLastBackup = mFullBackupQueue.get(0).lastBackup;
5389                final long timeSinceLast = System.currentTimeMillis() - upcomingLastBackup;
5390                final long appLatency = (timeSinceLast < MIN_FULL_BACKUP_INTERVAL)
5391                        ? (MIN_FULL_BACKUP_INTERVAL - timeSinceLast) : 0;
5392                final long latency = Math.max(transportMinLatency, appLatency);
5393                Runnable r = new Runnable() {
5394                    @Override public void run() {
5395                        FullBackupJob.schedule(mContext, latency);
5396                    }
5397                };
5398                mBackupHandler.postDelayed(r, 2500);
5399            } else {
5400                if (DEBUG_SCHEDULING) {
5401                    Slog.i(TAG, "Full backup queue empty; not scheduling");
5402                }
5403            }
5404        }
5405    }
5406
5407    /**
5408     * Remove a package from the full-data queue.
5409     */
5410    void dequeueFullBackupLocked(String packageName) {
5411        final int N = mFullBackupQueue.size();
5412        for (int i = N-1; i >= 0; i--) {
5413            final FullBackupEntry e = mFullBackupQueue.get(i);
5414            if (packageName.equals(e.packageName)) {
5415                mFullBackupQueue.remove(i);
5416            }
5417        }
5418    }
5419
5420    /**
5421     * Enqueue full backup for the given app, with a note about when it last ran.
5422     */
5423    void enqueueFullBackup(String packageName, long lastBackedUp) {
5424        FullBackupEntry newEntry = new FullBackupEntry(packageName, lastBackedUp);
5425        synchronized (mQueueLock) {
5426            // First, sanity check that we aren't adding a duplicate.  Slow but
5427            // straightforward; we'll have at most on the order of a few hundred
5428            // items in this list.
5429            dequeueFullBackupLocked(packageName);
5430
5431            // This is also slow but easy for modest numbers of apps: work backwards
5432            // from the end of the queue until we find an item whose last backup
5433            // time was before this one, then insert this new entry after it.  If we're
5434            // adding something new we don't bother scanning, and just prepend.
5435            int which = -1;
5436            if (lastBackedUp > 0) {
5437                for (which = mFullBackupQueue.size() - 1; which >= 0; which--) {
5438                    final FullBackupEntry entry = mFullBackupQueue.get(which);
5439                    if (entry.lastBackup <= lastBackedUp) {
5440                        mFullBackupQueue.add(which + 1, newEntry);
5441                        break;
5442                    }
5443                }
5444            }
5445            if (which < 0) {
5446                // this one is earlier than any existing one, so prepend
5447                mFullBackupQueue.add(0, newEntry);
5448            }
5449        }
5450        writeFullBackupScheduleAsync();
5451    }
5452
5453    private boolean fullBackupAllowable(IBackupTransport transport) {
5454        if (transport == null) {
5455            Slog.w(TAG, "Transport not present; full data backup not performed");
5456            return false;
5457        }
5458
5459        // Don't proceed unless we have already established package metadata
5460        // for the current dataset via a key/value backup pass.
5461        try {
5462            File stateDir = new File(mBaseStateDir, transport.transportDirName());
5463            File pmState = new File(stateDir, PACKAGE_MANAGER_SENTINEL);
5464            if (pmState.length() <= 0) {
5465                if (DEBUG) {
5466                    Slog.i(TAG, "Full backup requested but dataset not yet initialized");
5467                }
5468                return false;
5469            }
5470        } catch (Exception e) {
5471            Slog.w(TAG, "Unable to get transport name: " + e.getMessage());
5472            return false;
5473        }
5474
5475        return true;
5476    }
5477
5478    /**
5479     * Conditions are right for a full backup operation, so run one.  The model we use is
5480     * to perform one app backup per scheduled job execution, and to reschedule the job
5481     * with zero latency as long as conditions remain right and we still have work to do.
5482     *
5483     * <p>This is the "start a full backup operation" entry point called by the scheduled job.
5484     *
5485     * @return Whether ongoing work will continue.  The return value here will be passed
5486     *         along as the return value to the scheduled job's onStartJob() callback.
5487     */
5488    @Override
5489    public boolean beginFullBackup(FullBackupJob scheduledJob) {
5490        long now = System.currentTimeMillis();
5491        FullBackupEntry entry = null;
5492        long latency = MIN_FULL_BACKUP_INTERVAL;
5493
5494        if (!mEnabled || !mProvisioned) {
5495            // Backups are globally disabled, so don't proceed.  We also don't reschedule
5496            // the job driving automatic backups; that job will be scheduled again when
5497            // the user enables backup.
5498            if (MORE_DEBUG) {
5499                Slog.i(TAG, "beginFullBackup but e=" + mEnabled
5500                        + " p=" + mProvisioned + "; ignoring");
5501            }
5502            return false;
5503        }
5504
5505        // Don't run the backup if we're in battery saver mode, but reschedule
5506        // to try again in the not-so-distant future.
5507        final PowerSaveState result =
5508                mPowerManager.getPowerSaveState(ServiceType.FULL_BACKUP);
5509        if (result.batterySaverEnabled) {
5510            if (DEBUG) Slog.i(TAG, "Deferring scheduled full backups in battery saver mode");
5511            FullBackupJob.schedule(mContext, KeyValueBackupJob.BATCH_INTERVAL);
5512            return false;
5513        }
5514
5515        if (DEBUG_SCHEDULING) {
5516            Slog.i(TAG, "Beginning scheduled full backup operation");
5517        }
5518
5519        // Great; we're able to run full backup jobs now.  See if we have any work to do.
5520        synchronized (mQueueLock) {
5521            if (mRunningFullBackupTask != null) {
5522                Slog.e(TAG, "Backup triggered but one already/still running!");
5523                return false;
5524            }
5525
5526            // At this point we think that we have work to do, but possibly not right now.
5527            // Any exit without actually running backups will also require that we
5528            // reschedule the job.
5529            boolean runBackup = true;
5530            boolean headBusy;
5531
5532            do {
5533                // Recheck each time, because culling due to ineligibility may
5534                // have emptied the queue.
5535                if (mFullBackupQueue.size() == 0) {
5536                    // no work to do so just bow out
5537                    if (DEBUG) {
5538                        Slog.i(TAG, "Backup queue empty; doing nothing");
5539                    }
5540                    runBackup = false;
5541                    break;
5542                }
5543
5544                headBusy = false;
5545
5546                if (!fullBackupAllowable(mTransportManager.getCurrentTransportBinder())) {
5547                    if (MORE_DEBUG) {
5548                        Slog.i(TAG, "Preconditions not met; not running full backup");
5549                    }
5550                    runBackup = false;
5551                    // Typically this means we haven't run a key/value backup yet.  Back off
5552                    // full-backup operations by the key/value job's run interval so that
5553                    // next time we run, we are likely to be able to make progress.
5554                    latency = KeyValueBackupJob.BATCH_INTERVAL;
5555                }
5556
5557                if (runBackup) {
5558                    entry = mFullBackupQueue.get(0);
5559                    long timeSinceRun = now - entry.lastBackup;
5560                    runBackup = (timeSinceRun >= MIN_FULL_BACKUP_INTERVAL);
5561                    if (!runBackup) {
5562                        // It's too early to back up the next thing in the queue, so bow out
5563                        if (MORE_DEBUG) {
5564                            Slog.i(TAG, "Device ready but too early to back up next app");
5565                        }
5566                        // Wait until the next app in the queue falls due for a full data backup
5567                        latency = MIN_FULL_BACKUP_INTERVAL - timeSinceRun;
5568                        break;  // we know we aren't doing work yet, so bail.
5569                    }
5570
5571                    try {
5572                        PackageInfo appInfo = mPackageManager.getPackageInfo(entry.packageName, 0);
5573                        if (!appGetsFullBackup(appInfo)) {
5574                            // The head app isn't supposed to get full-data backups [any more];
5575                            // so we cull it and force a loop around to consider the new head
5576                            // app.
5577                            if (MORE_DEBUG) {
5578                                Slog.i(TAG, "Culling package " + entry.packageName
5579                                        + " in full-backup queue but not eligible");
5580                            }
5581                            mFullBackupQueue.remove(0);
5582                            headBusy = true; // force the while() condition
5583                            continue;
5584                        }
5585
5586                        final int privFlags = appInfo.applicationInfo.privateFlags;
5587                        headBusy = (privFlags & PRIVATE_FLAG_BACKUP_IN_FOREGROUND) == 0
5588                                && mActivityManager.isAppForeground(appInfo.applicationInfo.uid);
5589
5590                        if (headBusy) {
5591                            final long nextEligible = System.currentTimeMillis()
5592                                    + BUSY_BACKOFF_MIN_MILLIS
5593                                    + mTokenGenerator.nextInt(BUSY_BACKOFF_FUZZ);
5594                            if (DEBUG_SCHEDULING) {
5595                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
5596                                Slog.i(TAG, "Full backup time but " + entry.packageName
5597                                        + " is busy; deferring to "
5598                                        + sdf.format(new Date(nextEligible)));
5599                            }
5600                            // This relocates the app's entry from the head of the queue to
5601                            // its order-appropriate position further down, so upon looping
5602                            // a new candidate will be considered at the head.
5603                            enqueueFullBackup(entry.packageName,
5604                                    nextEligible - MIN_FULL_BACKUP_INTERVAL);
5605                        }
5606                    } catch (NameNotFoundException nnf) {
5607                        // So, we think we want to back this up, but it turns out the package
5608                        // in question is no longer installed.  We want to drop it from the
5609                        // queue entirely and move on, but if there's nothing else in the queue
5610                        // we should bail entirely.  headBusy cannot have been set to true yet.
5611                        runBackup = (mFullBackupQueue.size() > 1);
5612                    } catch (RemoteException e) {
5613                        // Cannot happen; the Activity Manager is in the same process
5614                    }
5615                }
5616            } while (headBusy);
5617
5618            if (!runBackup) {
5619                if (DEBUG_SCHEDULING) {
5620                    Slog.i(TAG, "Nothing pending full backup; rescheduling +" + latency);
5621                }
5622                final long deferTime = latency;     // pin for the closure
5623                mBackupHandler.post(new Runnable() {
5624                    @Override public void run() {
5625                        FullBackupJob.schedule(mContext, deferTime);
5626                    }
5627                });
5628                return false;
5629            }
5630
5631            // Okay, the top thing is ready for backup now.  Do it.
5632            mFullBackupQueue.remove(0);
5633            CountDownLatch latch = new CountDownLatch(1);
5634            String[] pkg = new String[] {entry.packageName};
5635            mRunningFullBackupTask = new PerformFullTransportBackupTask(null, pkg, true,
5636                    scheduledJob, latch, null, null, false /* userInitiated */);
5637            // Acquiring wakelock for PerformFullTransportBackupTask before its start.
5638            mWakelock.acquire();
5639            (new Thread(mRunningFullBackupTask)).start();
5640        }
5641
5642        return true;
5643    }
5644
5645    // The job scheduler says our constraints don't hold any more,
5646    // so tear down any ongoing backup task right away.
5647    @Override
5648    public void endFullBackup() {
5649        // offload the mRunningFullBackupTask.handleCancel() call to another thread,
5650        // as we might have to wait for mCancelLock
5651        Runnable endFullBackupRunnable = new Runnable() {
5652            @Override
5653            public void run() {
5654                PerformFullTransportBackupTask pftbt = null;
5655                synchronized (mQueueLock) {
5656                    if (mRunningFullBackupTask != null) {
5657                        if (DEBUG_SCHEDULING) {
5658                            Slog.i(TAG, "Telling running backup to stop");
5659                        }
5660                        pftbt = mRunningFullBackupTask;
5661                    }
5662                }
5663                pftbt.handleCancel(true);
5664            }
5665        };
5666        new Thread(endFullBackupRunnable, "end-full-backup").start();
5667    }
5668
5669    // ----- Restore infrastructure -----
5670
5671    abstract class RestoreEngine {
5672        static final String TAG = "RestoreEngine";
5673
5674        public static final int SUCCESS = 0;
5675        public static final int TARGET_FAILURE = -2;
5676        public static final int TRANSPORT_FAILURE = -3;
5677
5678        private AtomicBoolean mRunning = new AtomicBoolean(false);
5679        private AtomicInteger mResult = new AtomicInteger(SUCCESS);
5680
5681        public boolean isRunning() {
5682            return mRunning.get();
5683        }
5684
5685        public void setRunning(boolean stillRunning) {
5686            synchronized (mRunning) {
5687                mRunning.set(stillRunning);
5688                mRunning.notifyAll();
5689            }
5690        }
5691
5692        public int waitForResult() {
5693            synchronized (mRunning) {
5694                while (isRunning()) {
5695                    try {
5696                        mRunning.wait();
5697                    } catch (InterruptedException e) {}
5698                }
5699            }
5700            return getResult();
5701        }
5702
5703        public int getResult() {
5704            return mResult.get();
5705        }
5706
5707        public void setResult(int result) {
5708            mResult.set(result);
5709        }
5710
5711        // TODO: abstract restore state and APIs
5712    }
5713
5714    // ----- Full restore from a file/socket -----
5715
5716    enum RestorePolicy {
5717        IGNORE,
5718        ACCEPT,
5719        ACCEPT_IF_APK
5720    }
5721
5722    // Full restore engine, used by both adb restore and transport-based full restore
5723    class FullRestoreEngine extends RestoreEngine {
5724        // Task in charge of monitoring timeouts
5725        BackupRestoreTask mMonitorTask;
5726
5727        // Dedicated observer, if any
5728        IFullBackupRestoreObserver mObserver;
5729
5730        IBackupManagerMonitor mMonitor;
5731
5732        // Where we're delivering the file data as we go
5733        IBackupAgent mAgent;
5734
5735        // Are we permitted to only deliver a specific package's metadata?
5736        PackageInfo mOnlyPackage;
5737
5738        boolean mAllowApks;
5739        boolean mAllowObbs;
5740
5741        // Which package are we currently handling data for?
5742        String mAgentPackage;
5743
5744        // Info for working with the target app process
5745        ApplicationInfo mTargetApp;
5746
5747        // Machinery for restoring OBBs
5748        FullBackupObbConnection mObbConnection = null;
5749
5750        // possible handling states for a given package in the restore dataset
5751        final HashMap<String, RestorePolicy> mPackagePolicies
5752                = new HashMap<String, RestorePolicy>();
5753
5754        // installer package names for each encountered app, derived from the manifests
5755        final HashMap<String, String> mPackageInstallers = new HashMap<String, String>();
5756
5757        // Signatures for a given package found in its manifest file
5758        final HashMap<String, Signature[]> mManifestSignatures
5759                = new HashMap<String, Signature[]>();
5760
5761        // Packages we've already wiped data on when restoring their first file
5762        final HashSet<String> mClearedPackages = new HashSet<String>();
5763
5764        // How much data have we moved?
5765        long mBytes;
5766
5767        // Working buffer
5768        byte[] mBuffer;
5769
5770        // Pipes for moving data
5771        ParcelFileDescriptor[] mPipes = null;
5772
5773        // Widget blob to be restored out-of-band
5774        byte[] mWidgetData = null;
5775
5776        private final int mEphemeralOpToken;
5777
5778        // Runner that can be placed in a separate thread to do in-process
5779        // invocations of the full restore API asynchronously. Used by adb restore.
5780        class RestoreFileRunnable implements Runnable {
5781            IBackupAgent mAgent;
5782            FileMetadata mInfo;
5783            ParcelFileDescriptor mSocket;
5784            int mToken;
5785
5786            RestoreFileRunnable(IBackupAgent agent, FileMetadata info,
5787                    ParcelFileDescriptor socket, int token) throws IOException {
5788                mAgent = agent;
5789                mInfo = info;
5790                mToken = token;
5791
5792                // This class is used strictly for process-local binder invocations.  The
5793                // semantics of ParcelFileDescriptor differ in this case; in particular, we
5794                // do not automatically get a 'dup'ed descriptor that we can can continue
5795                // to use asynchronously from the caller.  So, we make sure to dup it ourselves
5796                // before proceeding to do the restore.
5797                mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
5798            }
5799
5800            @Override
5801            public void run() {
5802                try {
5803                    mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type,
5804                            mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime,
5805                            mToken, mBackupManagerBinder);
5806                } catch (RemoteException e) {
5807                    // never happens; this is used strictly for local binder calls
5808                }
5809            }
5810        }
5811
5812        public FullRestoreEngine(BackupRestoreTask monitorTask, IFullBackupRestoreObserver observer,
5813                IBackupManagerMonitor monitor, PackageInfo onlyPackage, boolean allowApks,
5814                boolean allowObbs, int ephemeralOpToken) {
5815            mEphemeralOpToken = ephemeralOpToken;
5816            mMonitorTask = monitorTask;
5817            mObserver = observer;
5818            mMonitor = monitor;
5819            mOnlyPackage = onlyPackage;
5820            mAllowApks = allowApks;
5821            mAllowObbs = allowObbs;
5822            mBuffer = new byte[32 * 1024];
5823            mBytes = 0;
5824        }
5825
5826        public IBackupAgent getAgent() {
5827            return mAgent;
5828        }
5829
5830        public byte[] getWidgetData() {
5831            return mWidgetData;
5832        }
5833
5834        public boolean restoreOneFile(InputStream instream, boolean mustKillAgent) {
5835            if (!isRunning()) {
5836                Slog.w(TAG, "Restore engine used after halting");
5837                return false;
5838            }
5839
5840            FileMetadata info;
5841            try {
5842                if (MORE_DEBUG) {
5843                    Slog.v(TAG, "Reading tar header for restoring file");
5844                }
5845                info = readTarHeaders(instream);
5846                if (info != null) {
5847                    if (MORE_DEBUG) {
5848                        dumpFileMetadata(info);
5849                    }
5850
5851                    final String pkg = info.packageName;
5852                    if (!pkg.equals(mAgentPackage)) {
5853                        // In the single-package case, it's a semantic error to expect
5854                        // one app's data but see a different app's on the wire
5855                        if (mOnlyPackage != null) {
5856                            if (!pkg.equals(mOnlyPackage.packageName)) {
5857                                Slog.w(TAG, "Expected data for " + mOnlyPackage
5858                                        + " but saw " + pkg);
5859                                setResult(RestoreEngine.TRANSPORT_FAILURE);
5860                                setRunning(false);
5861                                return false;
5862                            }
5863                        }
5864
5865                        // okay, change in package; set up our various
5866                        // bookkeeping if we haven't seen it yet
5867                        if (!mPackagePolicies.containsKey(pkg)) {
5868                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
5869                        }
5870
5871                        // Clean up the previous agent relationship if necessary,
5872                        // and let the observer know we're considering a new app.
5873                        if (mAgent != null) {
5874                            if (DEBUG) Slog.d(TAG, "Saw new package; finalizing old one");
5875                            // Now we're really done
5876                            tearDownPipes();
5877                            tearDownAgent(mTargetApp);
5878                            mTargetApp = null;
5879                            mAgentPackage = null;
5880                        }
5881                    }
5882
5883                    if (info.path.equals(BACKUP_MANIFEST_FILENAME)) {
5884                        mPackagePolicies.put(pkg, readAppManifest(info, instream));
5885                        mPackageInstallers.put(pkg, info.installerPackageName);
5886                        // We've read only the manifest content itself at this point,
5887                        // so consume the footer before looping around to the next
5888                        // input file
5889                        skipTarPadding(info.size, instream);
5890                        sendOnRestorePackage(pkg);
5891                    } else if (info.path.equals(BACKUP_METADATA_FILENAME)) {
5892                        // Metadata blobs!
5893                        readMetadata(info, instream);
5894                        skipTarPadding(info.size, instream);
5895                    } else {
5896                        // Non-manifest, so it's actual file data.  Is this a package
5897                        // we're ignoring?
5898                        boolean okay = true;
5899                        RestorePolicy policy = mPackagePolicies.get(pkg);
5900                        switch (policy) {
5901                            case IGNORE:
5902                                okay = false;
5903                                break;
5904
5905                            case ACCEPT_IF_APK:
5906                                // If we're in accept-if-apk state, then the first file we
5907                                // see MUST be the apk.
5908                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
5909                                    if (DEBUG) Slog.d(TAG, "APK file; installing");
5910                                    // Try to install the app.
5911                                    String installerName = mPackageInstallers.get(pkg);
5912                                    okay = installApk(info, installerName, instream);
5913                                    // good to go; promote to ACCEPT
5914                                    mPackagePolicies.put(pkg, (okay)
5915                                            ? RestorePolicy.ACCEPT
5916                                                    : RestorePolicy.IGNORE);
5917                                    // At this point we've consumed this file entry
5918                                    // ourselves, so just strip the tar footer and
5919                                    // go on to the next file in the input stream
5920                                    skipTarPadding(info.size, instream);
5921                                    return true;
5922                                } else {
5923                                    // File data before (or without) the apk.  We can't
5924                                    // handle it coherently in this case so ignore it.
5925                                    mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
5926                                    okay = false;
5927                                }
5928                                break;
5929
5930                            case ACCEPT:
5931                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
5932                                    if (DEBUG) Slog.d(TAG, "apk present but ACCEPT");
5933                                    // we can take the data without the apk, so we
5934                                    // *want* to do so.  skip the apk by declaring this
5935                                    // one file not-okay without changing the restore
5936                                    // policy for the package.
5937                                    okay = false;
5938                                }
5939                                break;
5940
5941                            default:
5942                                // Something has gone dreadfully wrong when determining
5943                                // the restore policy from the manifest.  Ignore the
5944                                // rest of this package's data.
5945                                Slog.e(TAG, "Invalid policy from manifest");
5946                                okay = false;
5947                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
5948                                break;
5949                        }
5950
5951                        // Is it a *file* we need to drop?
5952                        if (!isRestorableFile(info)) {
5953                            okay = false;
5954                        }
5955
5956                        // If the policy is satisfied, go ahead and set up to pipe the
5957                        // data to the agent.
5958                        if (MORE_DEBUG && okay && mAgent != null) {
5959                            Slog.i(TAG, "Reusing existing agent instance");
5960                        }
5961                        if (okay && mAgent == null) {
5962                            if (MORE_DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg);
5963
5964                            try {
5965                                mTargetApp = mPackageManager.getApplicationInfo(pkg, 0);
5966
5967                                // If we haven't sent any data to this app yet, we probably
5968                                // need to clear it first.  Check that.
5969                                if (!mClearedPackages.contains(pkg)) {
5970                                    // apps with their own backup agents are
5971                                    // responsible for coherently managing a full
5972                                    // restore.
5973                                    if (mTargetApp.backupAgentName == null) {
5974                                        if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore");
5975                                        clearApplicationDataSynchronous(pkg);
5976                                    } else {
5977                                        if (MORE_DEBUG) Slog.d(TAG, "backup agent ("
5978                                                + mTargetApp.backupAgentName + ") => no clear");
5979                                    }
5980                                    mClearedPackages.add(pkg);
5981                                } else {
5982                                    if (MORE_DEBUG) {
5983                                        Slog.d(TAG, "We've initialized this app already; no clear required");
5984                                    }
5985                                }
5986
5987                                // All set; now set up the IPC and launch the agent
5988                                setUpPipes();
5989                                mAgent = bindToAgentSynchronous(mTargetApp,
5990                                        ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL);
5991                                mAgentPackage = pkg;
5992                            } catch (IOException e) {
5993                                // fall through to error handling
5994                            } catch (NameNotFoundException e) {
5995                                // fall through to error handling
5996                            }
5997
5998                            if (mAgent == null) {
5999                                Slog.e(TAG, "Unable to create agent for " + pkg);
6000                                okay = false;
6001                                tearDownPipes();
6002                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
6003                            }
6004                        }
6005
6006                        // Sanity check: make sure we never give data to the wrong app.  This
6007                        // should never happen but a little paranoia here won't go amiss.
6008                        if (okay && !pkg.equals(mAgentPackage)) {
6009                            Slog.e(TAG, "Restoring data for " + pkg
6010                                    + " but agent is for " + mAgentPackage);
6011                            okay = false;
6012                        }
6013
6014                        // At this point we have an agent ready to handle the full
6015                        // restore data as well as a pipe for sending data to
6016                        // that agent.  Tell the agent to start reading from the
6017                        // pipe.
6018                        if (okay) {
6019                            boolean agentSuccess = true;
6020                            long toCopy = info.size;
6021                            try {
6022                                prepareOperationTimeout(mEphemeralOpToken,
6023                                        TIMEOUT_FULL_BACKUP_INTERVAL, mMonitorTask,
6024                                        OP_TYPE_RESTORE_WAIT);
6025
6026                                if (info.domain.equals(FullBackup.OBB_TREE_TOKEN)) {
6027                                    if (DEBUG) Slog.d(TAG, "Restoring OBB file for " + pkg
6028                                            + " : " + info.path);
6029                                    mObbConnection.restoreObbFile(pkg, mPipes[0],
6030                                            info.size, info.type, info.path, info.mode,
6031                                            info.mtime, mEphemeralOpToken, mBackupManagerBinder);
6032                                } else {
6033                                    if (MORE_DEBUG) Slog.d(TAG, "Invoking agent to restore file "
6034                                            + info.path);
6035                                    // fire up the app's agent listening on the socket.  If
6036                                    // the agent is running in the system process we can't
6037                                    // just invoke it asynchronously, so we provide a thread
6038                                    // for it here.
6039                                    if (mTargetApp.processName.equals("system")) {
6040                                        Slog.d(TAG, "system process agent - spinning a thread");
6041                                        RestoreFileRunnable runner = new RestoreFileRunnable(
6042                                                mAgent, info, mPipes[0], mEphemeralOpToken);
6043                                        new Thread(runner, "restore-sys-runner").start();
6044                                    } else {
6045                                        mAgent.doRestoreFile(mPipes[0], info.size, info.type,
6046                                                info.domain, info.path, info.mode, info.mtime,
6047                                                mEphemeralOpToken, mBackupManagerBinder);
6048                                    }
6049                                }
6050                            } catch (IOException e) {
6051                                // couldn't dup the socket for a process-local restore
6052                                Slog.d(TAG, "Couldn't establish restore");
6053                                agentSuccess = false;
6054                                okay = false;
6055                            } catch (RemoteException e) {
6056                                // whoops, remote entity went away.  We'll eat the content
6057                                // ourselves, then, and not copy it over.
6058                                Slog.e(TAG, "Agent crashed during full restore");
6059                                agentSuccess = false;
6060                                okay = false;
6061                            }
6062
6063                            // Copy over the data if the agent is still good
6064                            if (okay) {
6065                                if (MORE_DEBUG) {
6066                                    Slog.v(TAG, "  copying to restore agent: "
6067                                            + toCopy + " bytes");
6068                                }
6069                                boolean pipeOkay = true;
6070                                FileOutputStream pipe = new FileOutputStream(
6071                                        mPipes[1].getFileDescriptor());
6072                                while (toCopy > 0) {
6073                                    int toRead = (toCopy > mBuffer.length)
6074                                            ? mBuffer.length : (int)toCopy;
6075                                    int nRead = instream.read(mBuffer, 0, toRead);
6076                                    if (nRead >= 0) mBytes += nRead;
6077                                    if (nRead <= 0) break;
6078                                    toCopy -= nRead;
6079
6080                                    // send it to the output pipe as long as things
6081                                    // are still good
6082                                    if (pipeOkay) {
6083                                        try {
6084                                            pipe.write(mBuffer, 0, nRead);
6085                                        } catch (IOException e) {
6086                                            Slog.e(TAG, "Failed to write to restore pipe: "
6087                                                    + e.getMessage());
6088                                            pipeOkay = false;
6089                                        }
6090                                    }
6091                                }
6092
6093                                // done sending that file!  Now we just need to consume
6094                                // the delta from info.size to the end of block.
6095                                skipTarPadding(info.size, instream);
6096
6097                                // and now that we've sent it all, wait for the remote
6098                                // side to acknowledge receipt
6099                                agentSuccess = waitUntilOperationComplete(mEphemeralOpToken);
6100                            }
6101
6102                            // okay, if the remote end failed at any point, deal with
6103                            // it by ignoring the rest of the restore on it
6104                            if (!agentSuccess) {
6105                                Slog.w(TAG, "Agent failure; ending restore");
6106                                mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT);
6107                                tearDownPipes();
6108                                tearDownAgent(mTargetApp);
6109                                mAgent = null;
6110                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
6111
6112                                // If this was a single-package restore, we halt immediately
6113                                // with an agent error under these circumstances
6114                                if (mOnlyPackage != null) {
6115                                    setResult(RestoreEngine.TARGET_FAILURE);
6116                                    setRunning(false);
6117                                    return false;
6118                                }
6119                            }
6120                        }
6121
6122                        // Problems setting up the agent communication, an explicitly
6123                        // dropped file, or an already-ignored package: skip to the
6124                        // next stream entry by reading and discarding this file.
6125                        if (!okay) {
6126                            if (MORE_DEBUG) Slog.d(TAG, "[discarding file content]");
6127                            long bytesToConsume = (info.size + 511) & ~511;
6128                            while (bytesToConsume > 0) {
6129                                int toRead = (bytesToConsume > mBuffer.length)
6130                                        ? mBuffer.length : (int)bytesToConsume;
6131                                long nRead = instream.read(mBuffer, 0, toRead);
6132                                if (nRead >= 0) mBytes += nRead;
6133                                if (nRead <= 0) break;
6134                                bytesToConsume -= nRead;
6135                            }
6136                        }
6137                    }
6138                }
6139            } catch (IOException e) {
6140                if (DEBUG) Slog.w(TAG, "io exception on restore socket read: " + e.getMessage());
6141                setResult(RestoreEngine.TRANSPORT_FAILURE);
6142                info = null;
6143            }
6144
6145            // If we got here we're either running smoothly or we've finished
6146            if (info == null) {
6147                if (MORE_DEBUG) {
6148                    Slog.i(TAG, "No [more] data for this package; tearing down");
6149                }
6150                tearDownPipes();
6151                setRunning(false);
6152                if (mustKillAgent) {
6153                    tearDownAgent(mTargetApp);
6154                }
6155            }
6156            return (info != null);
6157        }
6158
6159        void setUpPipes() throws IOException {
6160            mPipes = ParcelFileDescriptor.createPipe();
6161        }
6162
6163        void tearDownPipes() {
6164            // Teardown might arise from the inline restore processing or from the asynchronous
6165            // timeout mechanism, and these might race.  Make sure we don't try to close and
6166            // null out the pipes twice.
6167            synchronized (this) {
6168                if (mPipes != null) {
6169                    try {
6170                        mPipes[0].close();
6171                        mPipes[0] = null;
6172                        mPipes[1].close();
6173                        mPipes[1] = null;
6174                    } catch (IOException e) {
6175                        Slog.w(TAG, "Couldn't close agent pipes", e);
6176                    }
6177                    mPipes = null;
6178                }
6179            }
6180        }
6181
6182        void tearDownAgent(ApplicationInfo app) {
6183            if (mAgent != null) {
6184                tearDownAgentAndKill(app);
6185                mAgent = null;
6186            }
6187        }
6188
6189        void handleTimeout() {
6190            tearDownPipes();
6191            setResult(RestoreEngine.TARGET_FAILURE);
6192            setRunning(false);
6193        }
6194
6195        class RestoreInstallObserver extends PackageInstallObserver {
6196            final AtomicBoolean mDone = new AtomicBoolean();
6197            String mPackageName;
6198            int mResult;
6199
6200            public void reset() {
6201                synchronized (mDone) {
6202                    mDone.set(false);
6203                }
6204            }
6205
6206            public void waitForCompletion() {
6207                synchronized (mDone) {
6208                    while (mDone.get() == false) {
6209                        try {
6210                            mDone.wait();
6211                        } catch (InterruptedException e) { }
6212                    }
6213                }
6214            }
6215
6216            int getResult() {
6217                return mResult;
6218            }
6219
6220            @Override
6221            public void onPackageInstalled(String packageName, int returnCode,
6222                    String msg, Bundle extras) {
6223                synchronized (mDone) {
6224                    mResult = returnCode;
6225                    mPackageName = packageName;
6226                    mDone.set(true);
6227                    mDone.notifyAll();
6228                }
6229            }
6230        }
6231
6232        class RestoreDeleteObserver extends IPackageDeleteObserver.Stub {
6233            final AtomicBoolean mDone = new AtomicBoolean();
6234            int mResult;
6235
6236            public void reset() {
6237                synchronized (mDone) {
6238                    mDone.set(false);
6239                }
6240            }
6241
6242            public void waitForCompletion() {
6243                synchronized (mDone) {
6244                    while (mDone.get() == false) {
6245                        try {
6246                            mDone.wait();
6247                        } catch (InterruptedException e) { }
6248                    }
6249                }
6250            }
6251
6252            @Override
6253            public void packageDeleted(String packageName, int returnCode) throws RemoteException {
6254                synchronized (mDone) {
6255                    mResult = returnCode;
6256                    mDone.set(true);
6257                    mDone.notifyAll();
6258                }
6259            }
6260        }
6261
6262        final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver();
6263        final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver();
6264
6265        boolean installApk(FileMetadata info, String installerPackage, InputStream instream) {
6266            boolean okay = true;
6267
6268            if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName);
6269
6270            // The file content is an .apk file.  Copy it out to a staging location and
6271            // attempt to install it.
6272            File apkFile = new File(mDataDir, info.packageName);
6273            try {
6274                FileOutputStream apkStream = new FileOutputStream(apkFile);
6275                byte[] buffer = new byte[32 * 1024];
6276                long size = info.size;
6277                while (size > 0) {
6278                    long toRead = (buffer.length < size) ? buffer.length : size;
6279                    int didRead = instream.read(buffer, 0, (int)toRead);
6280                    if (didRead >= 0) mBytes += didRead;
6281                    apkStream.write(buffer, 0, didRead);
6282                    size -= didRead;
6283                }
6284                apkStream.close();
6285
6286                // make sure the installer can read it
6287                apkFile.setReadable(true, false);
6288
6289                // Now install it
6290                Uri packageUri = Uri.fromFile(apkFile);
6291                mInstallObserver.reset();
6292                mPackageManager.installPackage(packageUri, mInstallObserver,
6293                        PackageManager.INSTALL_REPLACE_EXISTING | PackageManager.INSTALL_FROM_ADB,
6294                        installerPackage);
6295                mInstallObserver.waitForCompletion();
6296
6297                if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) {
6298                    // The only time we continue to accept install of data even if the
6299                    // apk install failed is if we had already determined that we could
6300                    // accept the data regardless.
6301                    if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) {
6302                        okay = false;
6303                    }
6304                } else {
6305                    // Okay, the install succeeded.  Make sure it was the right app.
6306                    boolean uninstall = false;
6307                    if (!mInstallObserver.mPackageName.equals(info.packageName)) {
6308                        Slog.w(TAG, "Restore stream claimed to include apk for "
6309                                + info.packageName + " but apk was really "
6310                                + mInstallObserver.mPackageName);
6311                        // delete the package we just put in place; it might be fraudulent
6312                        okay = false;
6313                        uninstall = true;
6314                    } else {
6315                        try {
6316                            PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName,
6317                                    PackageManager.GET_SIGNATURES);
6318                            if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) {
6319                                Slog.w(TAG, "Restore stream contains apk of package "
6320                                        + info.packageName + " but it disallows backup/restore");
6321                                okay = false;
6322                            } else {
6323                                // So far so good -- do the signatures match the manifest?
6324                                Signature[] sigs = mManifestSignatures.get(info.packageName);
6325                                if (signaturesMatch(sigs, pkg)) {
6326                                    // If this is a system-uid app without a declared backup agent,
6327                                    // don't restore any of the file data.
6328                                    if ((pkg.applicationInfo.uid < Process.FIRST_APPLICATION_UID)
6329                                            && (pkg.applicationInfo.backupAgentName == null)) {
6330                                        Slog.w(TAG, "Installed app " + info.packageName
6331                                                + " has restricted uid and no agent");
6332                                        okay = false;
6333                                    }
6334                                } else {
6335                                    Slog.w(TAG, "Installed app " + info.packageName
6336                                            + " signatures do not match restore manifest");
6337                                    okay = false;
6338                                    uninstall = true;
6339                                }
6340                            }
6341                        } catch (NameNotFoundException e) {
6342                            Slog.w(TAG, "Install of package " + info.packageName
6343                                    + " succeeded but now not found");
6344                            okay = false;
6345                        }
6346                    }
6347
6348                    // If we're not okay at this point, we need to delete the package
6349                    // that we just installed.
6350                    if (uninstall) {
6351                        mDeleteObserver.reset();
6352                        mPackageManager.deletePackage(mInstallObserver.mPackageName,
6353                                mDeleteObserver, 0);
6354                        mDeleteObserver.waitForCompletion();
6355                    }
6356                }
6357            } catch (IOException e) {
6358                Slog.e(TAG, "Unable to transcribe restored apk for install");
6359                okay = false;
6360            } finally {
6361                apkFile.delete();
6362            }
6363
6364            return okay;
6365        }
6366
6367        // Given an actual file content size, consume the post-content padding mandated
6368        // by the tar format.
6369        void skipTarPadding(long size, InputStream instream) throws IOException {
6370            long partial = (size + 512) % 512;
6371            if (partial > 0) {
6372                final int needed = 512 - (int)partial;
6373                if (MORE_DEBUG) {
6374                    Slog.i(TAG, "Skipping tar padding: " + needed + " bytes");
6375                }
6376                byte[] buffer = new byte[needed];
6377                if (readExactly(instream, buffer, 0, needed) == needed) {
6378                    mBytes += needed;
6379                } else throw new IOException("Unexpected EOF in padding");
6380            }
6381        }
6382
6383        // Read a widget metadata file, returning the restored blob
6384        void readMetadata(FileMetadata info, InputStream instream) throws IOException {
6385            // Fail on suspiciously large widget dump files
6386            if (info.size > 64 * 1024) {
6387                throw new IOException("Metadata too big; corrupt? size=" + info.size);
6388            }
6389
6390            byte[] buffer = new byte[(int) info.size];
6391            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
6392                mBytes += info.size;
6393            } else throw new IOException("Unexpected EOF in widget data");
6394
6395            String[] str = new String[1];
6396            int offset = extractLine(buffer, 0, str);
6397            int version = Integer.parseInt(str[0]);
6398            if (version == BACKUP_MANIFEST_VERSION) {
6399                offset = extractLine(buffer, offset, str);
6400                final String pkg = str[0];
6401                if (info.packageName.equals(pkg)) {
6402                    // Data checks out -- the rest of the buffer is a concatenation of
6403                    // binary blobs as described in the comment at writeAppWidgetData()
6404                    ByteArrayInputStream bin = new ByteArrayInputStream(buffer,
6405                            offset, buffer.length - offset);
6406                    DataInputStream in = new DataInputStream(bin);
6407                    while (bin.available() > 0) {
6408                        int token = in.readInt();
6409                        int size = in.readInt();
6410                        if (size > 64 * 1024) {
6411                            throw new IOException("Datum "
6412                                    + Integer.toHexString(token)
6413                                    + " too big; corrupt? size=" + info.size);
6414                        }
6415                        switch (token) {
6416                            case BACKUP_WIDGET_METADATA_TOKEN:
6417                            {
6418                                if (MORE_DEBUG) {
6419                                    Slog.i(TAG, "Got widget metadata for " + info.packageName);
6420                                }
6421                                mWidgetData = new byte[size];
6422                                in.read(mWidgetData);
6423                                break;
6424                            }
6425                            default:
6426                            {
6427                                if (DEBUG) {
6428                                    Slog.i(TAG, "Ignoring metadata blob "
6429                                            + Integer.toHexString(token)
6430                                            + " for " + info.packageName);
6431                                }
6432                                in.skipBytes(size);
6433                                break;
6434                            }
6435                        }
6436                    }
6437                } else {
6438                    Slog.w(TAG, "Metadata mismatch: package " + info.packageName
6439                            + " but widget data for " + pkg);
6440
6441                    Bundle monitoringExtras = putMonitoringExtra(null,
6442                            EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
6443                    monitoringExtras = putMonitoringExtra(monitoringExtras,
6444                            BackupManagerMonitor.EXTRA_LOG_WIDGET_PACKAGE_NAME, pkg);
6445                    mMonitor = monitorEvent(mMonitor,
6446                            BackupManagerMonitor.LOG_EVENT_ID_WIDGET_METADATA_MISMATCH,
6447                            null,
6448                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6449                            monitoringExtras);
6450                }
6451            } else {
6452                Slog.w(TAG, "Unsupported metadata version " + version);
6453
6454                Bundle monitoringExtras = putMonitoringExtra(null, EXTRA_LOG_EVENT_PACKAGE_NAME,
6455                        info.packageName);
6456                monitoringExtras = putMonitoringExtra(monitoringExtras,
6457                        EXTRA_LOG_EVENT_PACKAGE_VERSION, version);
6458                mMonitor = monitorEvent(mMonitor,
6459                        BackupManagerMonitor.LOG_EVENT_ID_WIDGET_UNKNOWN_VERSION,
6460                        null,
6461                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6462                        monitoringExtras);
6463            }
6464        }
6465
6466        // Returns a policy constant
6467        RestorePolicy readAppManifest(FileMetadata info, InputStream instream)
6468                throws IOException {
6469            // Fail on suspiciously large manifest files
6470            if (info.size > 64 * 1024) {
6471                throw new IOException("Restore manifest too big; corrupt? size=" + info.size);
6472            }
6473
6474            byte[] buffer = new byte[(int) info.size];
6475            if (MORE_DEBUG) {
6476                Slog.i(TAG, "   readAppManifest() looking for " + info.size + " bytes, "
6477                        + mBytes + " already consumed");
6478            }
6479            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
6480                mBytes += info.size;
6481            } else throw new IOException("Unexpected EOF in manifest");
6482
6483            RestorePolicy policy = RestorePolicy.IGNORE;
6484            String[] str = new String[1];
6485            int offset = 0;
6486
6487            try {
6488                offset = extractLine(buffer, offset, str);
6489                int version = Integer.parseInt(str[0]);
6490                if (version == BACKUP_MANIFEST_VERSION) {
6491                    offset = extractLine(buffer, offset, str);
6492                    String manifestPackage = str[0];
6493                    // TODO: handle <original-package>
6494                    if (manifestPackage.equals(info.packageName)) {
6495                        offset = extractLine(buffer, offset, str);
6496                        version = Integer.parseInt(str[0]);  // app version
6497                        offset = extractLine(buffer, offset, str);
6498                        // This is the platform version, which we don't use, but we parse it
6499                        // as a safety against corruption in the manifest.
6500                        Integer.parseInt(str[0]);
6501                        offset = extractLine(buffer, offset, str);
6502                        info.installerPackageName = (str[0].length() > 0) ? str[0] : null;
6503                        offset = extractLine(buffer, offset, str);
6504                        boolean hasApk = str[0].equals("1");
6505                        offset = extractLine(buffer, offset, str);
6506                        int numSigs = Integer.parseInt(str[0]);
6507                        if (numSigs > 0) {
6508                            Signature[] sigs = new Signature[numSigs];
6509                            for (int i = 0; i < numSigs; i++) {
6510                                offset = extractLine(buffer, offset, str);
6511                                sigs[i] = new Signature(str[0]);
6512                            }
6513                            mManifestSignatures.put(info.packageName, sigs);
6514
6515                            // Okay, got the manifest info we need...
6516                            try {
6517                                PackageInfo pkgInfo = mPackageManager.getPackageInfo(
6518                                        info.packageName, PackageManager.GET_SIGNATURES);
6519                                // Fall through to IGNORE if the app explicitly disallows backup
6520                                final int flags = pkgInfo.applicationInfo.flags;
6521                                if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) {
6522                                    // Restore system-uid-space packages only if they have
6523                                    // defined a custom backup agent
6524                                    if ((pkgInfo.applicationInfo.uid >= Process.FIRST_APPLICATION_UID)
6525                                            || (pkgInfo.applicationInfo.backupAgentName != null)) {
6526                                        // Verify signatures against any installed version; if they
6527                                        // don't match, then we fall though and ignore the data.  The
6528                                        // signatureMatch() method explicitly ignores the signature
6529                                        // check for packages installed on the system partition, because
6530                                        // such packages are signed with the platform cert instead of
6531                                        // the app developer's cert, so they're different on every
6532                                        // device.
6533                                        if (signaturesMatch(sigs, pkgInfo)) {
6534                                            if ((pkgInfo.applicationInfo.flags
6535                                                    & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) != 0) {
6536                                                Slog.i(TAG, "Package has restoreAnyVersion; taking data");
6537                                                mMonitor = monitorEvent(mMonitor,
6538                                                        LOG_EVENT_ID_RESTORE_ANY_VERSION,
6539                                                        pkgInfo,
6540                                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6541                                                        null);
6542                                                policy = RestorePolicy.ACCEPT;
6543                                            } else if (pkgInfo.versionCode >= version) {
6544                                                Slog.i(TAG, "Sig + version match; taking data");
6545                                                policy = RestorePolicy.ACCEPT;
6546                                                mMonitor = monitorEvent(mMonitor,
6547                                                        LOG_EVENT_ID_VERSIONS_MATCH,
6548                                                        pkgInfo,
6549                                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6550                                                        null);
6551                                            } else {
6552                                                // The data is from a newer version of the app than
6553                                                // is presently installed.  That means we can only
6554                                                // use it if the matching apk is also supplied.
6555                                                if (mAllowApks) {
6556                                                    Slog.i(TAG, "Data version " + version
6557                                                            + " is newer than installed version "
6558                                                            + pkgInfo.versionCode
6559                                                            + " - requiring apk");
6560                                                    policy = RestorePolicy.ACCEPT_IF_APK;
6561                                                } else {
6562                                                    Slog.i(TAG, "Data requires newer version "
6563                                                            + version + "; ignoring");
6564                                                    mMonitor = monitorEvent(mMonitor,
6565                                                            LOG_EVENT_ID_VERSION_OF_BACKUP_OLDER,
6566                                                            pkgInfo,
6567                                                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6568                                                            putMonitoringExtra(null,
6569                                                                    EXTRA_LOG_OLD_VERSION,
6570                                                                    version));
6571
6572                                                    policy = RestorePolicy.IGNORE;
6573                                                }
6574                                            }
6575                                        } else {
6576                                            Slog.w(TAG, "Restore manifest signatures do not match "
6577                                                    + "installed application for " + info.packageName);
6578                                            mMonitor = monitorEvent(mMonitor,
6579                                                    LOG_EVENT_ID_FULL_RESTORE_SIGNATURE_MISMATCH,
6580                                                    pkgInfo,
6581                                                    LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6582                                                    null);
6583                                        }
6584                                    } else {
6585                                        Slog.w(TAG, "Package " + info.packageName
6586                                                + " is system level with no agent");
6587                                        mMonitor = monitorEvent(mMonitor,
6588                                                LOG_EVENT_ID_SYSTEM_APP_NO_AGENT,
6589                                                pkgInfo,
6590                                                LOG_EVENT_CATEGORY_AGENT,
6591                                                null);
6592                                    }
6593                                } else {
6594                                    if (DEBUG) Slog.i(TAG, "Restore manifest from "
6595                                            + info.packageName + " but allowBackup=false");
6596                                    mMonitor = monitorEvent(mMonitor,
6597                                            LOG_EVENT_ID_FULL_RESTORE_ALLOW_BACKUP_FALSE,
6598                                            pkgInfo,
6599                                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6600                                            null);
6601                                }
6602                            } catch (NameNotFoundException e) {
6603                                // Okay, the target app isn't installed.  We can process
6604                                // the restore properly only if the dataset provides the
6605                                // apk file and we can successfully install it.
6606                                if (mAllowApks) {
6607                                    if (DEBUG) Slog.i(TAG, "Package " + info.packageName
6608                                            + " not installed; requiring apk in dataset");
6609                                    policy = RestorePolicy.ACCEPT_IF_APK;
6610                                } else {
6611                                    policy = RestorePolicy.IGNORE;
6612                                }
6613                                Bundle monitoringExtras = putMonitoringExtra(null,
6614                                        EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
6615                                monitoringExtras = putMonitoringExtra(monitoringExtras,
6616                                        EXTRA_LOG_POLICY_ALLOW_APKS, mAllowApks);
6617                                mMonitor = monitorEvent(mMonitor,
6618                                        LOG_EVENT_ID_APK_NOT_INSTALLED,
6619                                        null,
6620                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6621                                        monitoringExtras);
6622                            }
6623
6624                            if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) {
6625                                Slog.i(TAG, "Cannot restore package " + info.packageName
6626                                        + " without the matching .apk");
6627                                mMonitor = monitorEvent(mMonitor,
6628                                        LOG_EVENT_ID_CANNOT_RESTORE_WITHOUT_APK,
6629                                        null,
6630                                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6631                                        putMonitoringExtra(null,
6632                                                EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
6633                            }
6634                        } else {
6635                            Slog.i(TAG, "Missing signature on backed-up package "
6636                                    + info.packageName);
6637                            mMonitor = monitorEvent(mMonitor,
6638                                    LOG_EVENT_ID_MISSING_SIGNATURE,
6639                                    null,
6640                                    LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6641                                    putMonitoringExtra(null,
6642                                            EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
6643                        }
6644                    } else {
6645                        Slog.i(TAG, "Expected package " + info.packageName
6646                                + " but restore manifest claims " + manifestPackage);
6647                        Bundle monitoringExtras = putMonitoringExtra(null,
6648                                EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
6649                        monitoringExtras = putMonitoringExtra(monitoringExtras,
6650                                EXTRA_LOG_MANIFEST_PACKAGE_NAME, manifestPackage);
6651                        mMonitor = monitorEvent(mMonitor,
6652                                LOG_EVENT_ID_EXPECTED_DIFFERENT_PACKAGE,
6653                                null,
6654                                LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6655                                monitoringExtras);
6656                    }
6657                } else {
6658                    Slog.i(TAG, "Unknown restore manifest version " + version
6659                            + " for package " + info.packageName);
6660                    Bundle monitoringExtras = putMonitoringExtra(null,
6661                            EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName);
6662                    monitoringExtras = putMonitoringExtra(monitoringExtras,
6663                            EXTRA_LOG_EVENT_PACKAGE_VERSION, version);
6664                    mMonitor = monitorEvent(mMonitor,
6665                            BackupManagerMonitor.LOG_EVENT_ID_UNKNOWN_VERSION,
6666                            null,
6667                            LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6668                            monitoringExtras);
6669
6670                }
6671            } catch (NumberFormatException e) {
6672                Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName);
6673                mMonitor = monitorEvent(mMonitor,
6674                        BackupManagerMonitor.LOG_EVENT_ID_CORRUPT_MANIFEST,
6675                        null,
6676                        LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
6677                        putMonitoringExtra(null, EXTRA_LOG_EVENT_PACKAGE_NAME, info.packageName));
6678            } catch (IllegalArgumentException e) {
6679                Slog.w(TAG, e.getMessage());
6680            }
6681
6682            return policy;
6683        }
6684
6685        // Builds a line from a byte buffer starting at 'offset', and returns
6686        // the index of the next unconsumed data in the buffer.
6687        int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException {
6688            final int end = buffer.length;
6689            if (offset >= end) throw new IOException("Incomplete data");
6690
6691            int pos;
6692            for (pos = offset; pos < end; pos++) {
6693                byte c = buffer[pos];
6694                // at LF we declare end of line, and return the next char as the
6695                // starting point for the next time through
6696                if (c == '\n') {
6697                    break;
6698                }
6699            }
6700            outStr[0] = new String(buffer, offset, pos - offset);
6701            pos++;  // may be pointing an extra byte past the end but that's okay
6702            return pos;
6703        }
6704
6705        void dumpFileMetadata(FileMetadata info) {
6706            if (MORE_DEBUG) {
6707                StringBuilder b = new StringBuilder(128);
6708
6709                // mode string
6710                b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-');
6711                b.append(((info.mode & 0400) != 0) ? 'r' : '-');
6712                b.append(((info.mode & 0200) != 0) ? 'w' : '-');
6713                b.append(((info.mode & 0100) != 0) ? 'x' : '-');
6714                b.append(((info.mode & 0040) != 0) ? 'r' : '-');
6715                b.append(((info.mode & 0020) != 0) ? 'w' : '-');
6716                b.append(((info.mode & 0010) != 0) ? 'x' : '-');
6717                b.append(((info.mode & 0004) != 0) ? 'r' : '-');
6718                b.append(((info.mode & 0002) != 0) ? 'w' : '-');
6719                b.append(((info.mode & 0001) != 0) ? 'x' : '-');
6720                b.append(String.format(" %9d ", info.size));
6721
6722                Date stamp = new Date(info.mtime);
6723                b.append(new SimpleDateFormat("MMM dd HH:mm:ss ").format(stamp));
6724
6725                b.append(info.packageName);
6726                b.append(" :: ");
6727                b.append(info.domain);
6728                b.append(" :: ");
6729                b.append(info.path);
6730
6731                Slog.i(TAG, b.toString());
6732            }
6733        }
6734
6735        // Consume a tar file header block [sequence] and accumulate the relevant metadata
6736        FileMetadata readTarHeaders(InputStream instream) throws IOException {
6737            byte[] block = new byte[512];
6738            FileMetadata info = null;
6739
6740            boolean gotHeader = readTarHeader(instream, block);
6741            if (gotHeader) {
6742                try {
6743                    // okay, presume we're okay, and extract the various metadata
6744                    info = new FileMetadata();
6745                    info.size = extractRadix(block, TAR_HEADER_OFFSET_FILESIZE,
6746                            TAR_HEADER_LENGTH_FILESIZE, TAR_HEADER_LONG_RADIX);
6747                    info.mtime = extractRadix(block, TAR_HEADER_OFFSET_MODTIME,
6748                            TAR_HEADER_LENGTH_MODTIME, TAR_HEADER_LONG_RADIX);
6749                    info.mode = extractRadix(block, TAR_HEADER_OFFSET_MODE,
6750                            TAR_HEADER_LENGTH_MODE, TAR_HEADER_LONG_RADIX);
6751
6752                    info.path = extractString(block, TAR_HEADER_OFFSET_PATH_PREFIX,
6753                            TAR_HEADER_LENGTH_PATH_PREFIX);
6754                    String path = extractString(block, TAR_HEADER_OFFSET_PATH,
6755                            TAR_HEADER_LENGTH_PATH);
6756                    if (path.length() > 0) {
6757                        if (info.path.length() > 0) info.path += '/';
6758                        info.path += path;
6759                    }
6760
6761                    // tar link indicator field: 1 byte at offset 156 in the header.
6762                    int typeChar = block[TAR_HEADER_OFFSET_TYPE_CHAR];
6763                    if (typeChar == 'x') {
6764                        // pax extended header, so we need to read that
6765                        gotHeader = readPaxExtendedHeader(instream, info);
6766                        if (gotHeader) {
6767                            // and after a pax extended header comes another real header -- read
6768                            // that to find the real file type
6769                            gotHeader = readTarHeader(instream, block);
6770                        }
6771                        if (!gotHeader) throw new IOException("Bad or missing pax header");
6772
6773                        typeChar = block[TAR_HEADER_OFFSET_TYPE_CHAR];
6774                    }
6775
6776                    switch (typeChar) {
6777                        case '0': info.type = BackupAgent.TYPE_FILE; break;
6778                        case '5': {
6779                            info.type = BackupAgent.TYPE_DIRECTORY;
6780                            if (info.size != 0) {
6781                                Slog.w(TAG, "Directory entry with nonzero size in header");
6782                                info.size = 0;
6783                            }
6784                            break;
6785                        }
6786                        case 0: {
6787                            // presume EOF
6788                            if (MORE_DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info);
6789                            return null;
6790                        }
6791                        default: {
6792                            Slog.e(TAG, "Unknown tar entity type: " + typeChar);
6793                            throw new IOException("Unknown entity type " + typeChar);
6794                        }
6795                    }
6796
6797                    // Parse out the path
6798                    //
6799                    // first: apps/shared/unrecognized
6800                    if (FullBackup.SHARED_PREFIX.regionMatches(0,
6801                            info.path, 0, FullBackup.SHARED_PREFIX.length())) {
6802                        // File in shared storage.  !!! TODO: implement this.
6803                        info.path = info.path.substring(FullBackup.SHARED_PREFIX.length());
6804                        info.packageName = SHARED_BACKUP_AGENT_PACKAGE;
6805                        info.domain = FullBackup.SHARED_STORAGE_TOKEN;
6806                        if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path);
6807                    } else if (FullBackup.APPS_PREFIX.regionMatches(0,
6808                            info.path, 0, FullBackup.APPS_PREFIX.length())) {
6809                        // App content!  Parse out the package name and domain
6810
6811                        // strip the apps/ prefix
6812                        info.path = info.path.substring(FullBackup.APPS_PREFIX.length());
6813
6814                        // extract the package name
6815                        int slash = info.path.indexOf('/');
6816                        if (slash < 0) throw new IOException("Illegal semantic path in " + info.path);
6817                        info.packageName = info.path.substring(0, slash);
6818                        info.path = info.path.substring(slash+1);
6819
6820                        // if it's a manifest or metadata payload we're done, otherwise parse
6821                        // out the domain into which the file will be restored
6822                        if (!info.path.equals(BACKUP_MANIFEST_FILENAME)
6823                                && !info.path.equals(BACKUP_METADATA_FILENAME)) {
6824                            slash = info.path.indexOf('/');
6825                            if (slash < 0) {
6826                                throw new IOException("Illegal semantic path in non-manifest "
6827                                        + info.path);
6828                            }
6829                            info.domain = info.path.substring(0, slash);
6830                            info.path = info.path.substring(slash + 1);
6831                        }
6832                    }
6833                } catch (IOException e) {
6834                    if (DEBUG) {
6835                        Slog.e(TAG, "Parse error in header: " + e.getMessage());
6836                        if (MORE_DEBUG) {
6837                            HEXLOG(block);
6838                        }
6839                    }
6840                    throw e;
6841                }
6842            }
6843            return info;
6844        }
6845
6846        private boolean isRestorableFile(FileMetadata info) {
6847            if (FullBackup.CACHE_TREE_TOKEN.equals(info.domain)) {
6848                if (MORE_DEBUG) {
6849                    Slog.i(TAG, "Dropping cache file path " + info.path);
6850                }
6851                return false;
6852            }
6853
6854            if (FullBackup.ROOT_TREE_TOKEN.equals(info.domain)) {
6855                // It's possible this is "no-backup" dir contents in an archive stream
6856                // produced on a device running a version of the OS that predates that
6857                // API.  Respect the no-backup intention and don't let the data get to
6858                // the app.
6859                if (info.path.startsWith("no_backup/")) {
6860                    if (MORE_DEBUG) {
6861                        Slog.i(TAG, "Dropping no_backup file path " + info.path);
6862                    }
6863                    return false;
6864                }
6865            }
6866
6867            // The path needs to be canonical
6868            if (info.path.contains("..") || info.path.contains("//")) {
6869                if (MORE_DEBUG) {
6870                    Slog.w(TAG, "Dropping invalid path " + info.path);
6871                }
6872                return false;
6873            }
6874
6875            // Otherwise we think this file is good to go
6876            return true;
6877        }
6878
6879        private void HEXLOG(byte[] block) {
6880            int offset = 0;
6881            int todo = block.length;
6882            StringBuilder buf = new StringBuilder(64);
6883            while (todo > 0) {
6884                buf.append(String.format("%04x   ", offset));
6885                int numThisLine = (todo > 16) ? 16 : todo;
6886                for (int i = 0; i < numThisLine; i++) {
6887                    buf.append(String.format("%02x ", block[offset+i]));
6888                }
6889                Slog.i("hexdump", buf.toString());
6890                buf.setLength(0);
6891                todo -= numThisLine;
6892                offset += numThisLine;
6893            }
6894        }
6895
6896        // Read exactly the given number of bytes into a buffer at the stated offset.
6897        // Returns false if EOF is encountered before the requested number of bytes
6898        // could be read.
6899        int readExactly(InputStream in, byte[] buffer, int offset, int size)
6900                throws IOException {
6901            if (size <= 0) throw new IllegalArgumentException("size must be > 0");
6902if (MORE_DEBUG) Slog.i(TAG, "  ... readExactly(" + size + ") called");
6903            int soFar = 0;
6904            while (soFar < size) {
6905                int nRead = in.read(buffer, offset + soFar, size - soFar);
6906                if (nRead <= 0) {
6907                    if (MORE_DEBUG) Slog.w(TAG, "- wanted exactly " + size + " but got only " + soFar);
6908                    break;
6909                }
6910                soFar += nRead;
6911if (MORE_DEBUG) Slog.v(TAG, "   + got " + nRead + "; now wanting " + (size - soFar));
6912            }
6913            return soFar;
6914        }
6915
6916        boolean readTarHeader(InputStream instream, byte[] block) throws IOException {
6917            final int got = readExactly(instream, block, 0, 512);
6918            if (got == 0) return false;     // Clean EOF
6919            if (got < 512) throw new IOException("Unable to read full block header");
6920            mBytes += 512;
6921            return true;
6922        }
6923
6924        // overwrites 'info' fields based on the pax extended header
6925        boolean readPaxExtendedHeader(InputStream instream, FileMetadata info)
6926                throws IOException {
6927            // We should never see a pax extended header larger than this
6928            if (info.size > 32*1024) {
6929                Slog.w(TAG, "Suspiciously large pax header size " + info.size
6930                        + " - aborting");
6931                throw new IOException("Sanity failure: pax header size " + info.size);
6932            }
6933
6934            // read whole blocks, not just the content size
6935            int numBlocks = (int)((info.size + 511) >> 9);
6936            byte[] data = new byte[numBlocks * 512];
6937            if (readExactly(instream, data, 0, data.length) < data.length) {
6938                throw new IOException("Unable to read full pax header");
6939            }
6940            mBytes += data.length;
6941
6942            final int contentSize = (int) info.size;
6943            int offset = 0;
6944            do {
6945                // extract the line at 'offset'
6946                int eol = offset+1;
6947                while (eol < contentSize && data[eol] != ' ') eol++;
6948                if (eol >= contentSize) {
6949                    // error: we just hit EOD looking for the end of the size field
6950                    throw new IOException("Invalid pax data");
6951                }
6952                // eol points to the space between the count and the key
6953                int linelen = (int) extractRadix(data, offset, eol - offset, 10);
6954                int key = eol + 1;  // start of key=value
6955                eol = offset + linelen - 1; // trailing LF
6956                int value;
6957                for (value = key+1; data[value] != '=' && value <= eol; value++);
6958                if (value > eol) {
6959                    throw new IOException("Invalid pax declaration");
6960                }
6961
6962                // pax requires that key/value strings be in UTF-8
6963                String keyStr = new String(data, key, value-key, "UTF-8");
6964                // -1 to strip the trailing LF
6965                String valStr = new String(data, value+1, eol-value-1, "UTF-8");
6966
6967                if ("path".equals(keyStr)) {
6968                    info.path = valStr;
6969                } else if ("size".equals(keyStr)) {
6970                    info.size = Long.parseLong(valStr);
6971                } else {
6972                    if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key);
6973                }
6974
6975                offset += linelen;
6976            } while (offset < contentSize);
6977
6978            return true;
6979        }
6980
6981        long extractRadix(byte[] data, int offset, int maxChars, int radix)
6982                throws IOException {
6983            long value = 0;
6984            final int end = offset + maxChars;
6985            for (int i = offset; i < end; i++) {
6986                final byte b = data[i];
6987                // Numeric fields in tar can terminate with either NUL or SPC
6988                if (b == 0 || b == ' ') break;
6989                if (b < '0' || b > ('0' + radix - 1)) {
6990                    throw new IOException("Invalid number in header: '" + (char)b
6991                            + "' for radix " + radix);
6992                }
6993                value = radix * value + (b - '0');
6994            }
6995            return value;
6996        }
6997
6998        String extractString(byte[] data, int offset, int maxChars) throws IOException {
6999            final int end = offset + maxChars;
7000            int eos = offset;
7001            // tar string fields terminate early with a NUL
7002            while (eos < end && data[eos] != 0) eos++;
7003            return new String(data, offset, eos-offset, "US-ASCII");
7004        }
7005
7006        void sendStartRestore() {
7007            if (mObserver != null) {
7008                try {
7009                    mObserver.onStartRestore();
7010                } catch (RemoteException e) {
7011                    Slog.w(TAG, "full restore observer went away: startRestore");
7012                    mObserver = null;
7013                }
7014            }
7015        }
7016
7017        void sendOnRestorePackage(String name) {
7018            if (mObserver != null) {
7019                try {
7020                    // TODO: use a more user-friendly name string
7021                    mObserver.onRestorePackage(name);
7022                } catch (RemoteException e) {
7023                    Slog.w(TAG, "full restore observer went away: restorePackage");
7024                    mObserver = null;
7025                }
7026            }
7027        }
7028
7029        void sendEndRestore() {
7030            if (mObserver != null) {
7031                try {
7032                    mObserver.onEndRestore();
7033                } catch (RemoteException e) {
7034                    Slog.w(TAG, "full restore observer went away: endRestore");
7035                    mObserver = null;
7036                }
7037            }
7038        }
7039    }
7040
7041    // ***** end new engine class ***
7042
7043    // Used for synchronizing doRestoreFinished during adb restore
7044    class AdbRestoreFinishedLatch implements BackupRestoreTask {
7045        static final String TAG = "AdbRestoreFinishedLatch";
7046        final CountDownLatch mLatch;
7047        private final int mCurrentOpToken;
7048
7049        AdbRestoreFinishedLatch(int currentOpToken) {
7050            mLatch = new CountDownLatch(1);
7051            mCurrentOpToken = currentOpToken;
7052        }
7053
7054        void await() {
7055            boolean latched = false;
7056            try {
7057                latched = mLatch.await(TIMEOUT_FULL_BACKUP_INTERVAL, TimeUnit.MILLISECONDS);
7058            } catch (InterruptedException e) {
7059                Slog.w(TAG, "Interrupted!");
7060            }
7061        }
7062
7063        @Override
7064        public void execute() {
7065            // Unused
7066        }
7067
7068        @Override
7069        public void operationComplete(long result) {
7070            if (MORE_DEBUG) {
7071                Slog.w(TAG, "adb onRestoreFinished() complete");
7072            }
7073            mLatch.countDown();
7074            removeOperation(mCurrentOpToken);
7075        }
7076
7077        @Override
7078        public void handleCancel(boolean cancelAll) {
7079            if (DEBUG) {
7080                Slog.w(TAG, "adb onRestoreFinished() timed out");
7081            }
7082            mLatch.countDown();
7083            removeOperation(mCurrentOpToken);
7084        }
7085    }
7086
7087    class PerformAdbRestoreTask implements Runnable {
7088        ParcelFileDescriptor mInputFile;
7089        String mCurrentPassword;
7090        String mDecryptPassword;
7091        IFullBackupRestoreObserver mObserver;
7092        AtomicBoolean mLatchObject;
7093        IBackupAgent mAgent;
7094        PackageManagerBackupAgent mPackageManagerBackupAgent;
7095        String mAgentPackage;
7096        ApplicationInfo mTargetApp;
7097        FullBackupObbConnection mObbConnection = null;
7098        ParcelFileDescriptor[] mPipes = null;
7099        byte[] mWidgetData = null;
7100
7101        long mBytes;
7102
7103        // Runner that can be placed on a separate thread to do in-process invocation
7104        // of the "restore finished" API asynchronously.  Used by adb restore.
7105        class RestoreFinishedRunnable implements Runnable {
7106            final IBackupAgent mAgent;
7107            final int mToken;
7108
7109            RestoreFinishedRunnable(IBackupAgent agent, int token) {
7110                mAgent = agent;
7111                mToken = token;
7112            }
7113
7114            @Override
7115            public void run() {
7116                try {
7117                    mAgent.doRestoreFinished(mToken, mBackupManagerBinder);
7118                } catch (RemoteException e) {
7119                    // never happens; this is used only for local binder calls
7120                }
7121            }
7122        }
7123
7124        // possible handling states for a given package in the restore dataset
7125        final HashMap<String, RestorePolicy> mPackagePolicies
7126                = new HashMap<String, RestorePolicy>();
7127
7128        // installer package names for each encountered app, derived from the manifests
7129        final HashMap<String, String> mPackageInstallers = new HashMap<String, String>();
7130
7131        // Signatures for a given package found in its manifest file
7132        final HashMap<String, Signature[]> mManifestSignatures
7133                = new HashMap<String, Signature[]>();
7134
7135        // Packages we've already wiped data on when restoring their first file
7136        final HashSet<String> mClearedPackages = new HashSet<String>();
7137
7138        PerformAdbRestoreTask(ParcelFileDescriptor fd, String curPassword, String decryptPassword,
7139                IFullBackupRestoreObserver observer, AtomicBoolean latch) {
7140            mInputFile = fd;
7141            mCurrentPassword = curPassword;
7142            mDecryptPassword = decryptPassword;
7143            mObserver = observer;
7144            mLatchObject = latch;
7145            mAgent = null;
7146            mPackageManagerBackupAgent = new PackageManagerBackupAgent(mPackageManager);
7147            mAgentPackage = null;
7148            mTargetApp = null;
7149            mObbConnection = new FullBackupObbConnection();
7150
7151            // Which packages we've already wiped data on.  We prepopulate this
7152            // with a whitelist of packages known to be unclearable.
7153            mClearedPackages.add("android");
7154            mClearedPackages.add(SETTINGS_PACKAGE);
7155        }
7156
7157        class RestoreFileRunnable implements Runnable {
7158            IBackupAgent mAgent;
7159            FileMetadata mInfo;
7160            ParcelFileDescriptor mSocket;
7161            int mToken;
7162
7163            RestoreFileRunnable(IBackupAgent agent, FileMetadata info,
7164                    ParcelFileDescriptor socket, int token) throws IOException {
7165                mAgent = agent;
7166                mInfo = info;
7167                mToken = token;
7168
7169                // This class is used strictly for process-local binder invocations.  The
7170                // semantics of ParcelFileDescriptor differ in this case; in particular, we
7171                // do not automatically get a 'dup'ed descriptor that we can can continue
7172                // to use asynchronously from the caller.  So, we make sure to dup it ourselves
7173                // before proceeding to do the restore.
7174                mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
7175            }
7176
7177            @Override
7178            public void run() {
7179                try {
7180                    mAgent.doRestoreFile(mSocket, mInfo.size, mInfo.type,
7181                            mInfo.domain, mInfo.path, mInfo.mode, mInfo.mtime,
7182                            mToken, mBackupManagerBinder);
7183                } catch (RemoteException e) {
7184                    // never happens; this is used strictly for local binder calls
7185                }
7186            }
7187        }
7188
7189        @Override
7190        public void run() {
7191            Slog.i(TAG, "--- Performing full-dataset restore ---");
7192            mObbConnection.establish();
7193            sendStartRestore();
7194
7195            // Are we able to restore shared-storage data?
7196            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
7197                mPackagePolicies.put(SHARED_BACKUP_AGENT_PACKAGE, RestorePolicy.ACCEPT);
7198            }
7199
7200            FileInputStream rawInStream = null;
7201            DataInputStream rawDataIn = null;
7202            try {
7203                if (!backupPasswordMatches(mCurrentPassword)) {
7204                    if (DEBUG) Slog.w(TAG, "Backup password mismatch; aborting");
7205                    return;
7206                }
7207
7208                mBytes = 0;
7209                byte[] buffer = new byte[32 * 1024];
7210                rawInStream = new FileInputStream(mInputFile.getFileDescriptor());
7211                rawDataIn = new DataInputStream(rawInStream);
7212
7213                // First, parse out the unencrypted/uncompressed header
7214                boolean compressed = false;
7215                InputStream preCompressStream = rawInStream;
7216                final InputStream in;
7217
7218                boolean okay = false;
7219                final int headerLen = BACKUP_FILE_HEADER_MAGIC.length();
7220                byte[] streamHeader = new byte[headerLen];
7221                rawDataIn.readFully(streamHeader);
7222                byte[] magicBytes = BACKUP_FILE_HEADER_MAGIC.getBytes("UTF-8");
7223                if (Arrays.equals(magicBytes, streamHeader)) {
7224                    // okay, header looks good.  now parse out the rest of the fields.
7225                    String s = readHeaderLine(rawInStream);
7226                    final int archiveVersion = Integer.parseInt(s);
7227                    if (archiveVersion <= BACKUP_FILE_VERSION) {
7228                        // okay, it's a version we recognize.  if it's version 1, we may need
7229                        // to try two different PBKDF2 regimes to compare checksums.
7230                        final boolean pbkdf2Fallback = (archiveVersion == 1);
7231
7232                        s = readHeaderLine(rawInStream);
7233                        compressed = (Integer.parseInt(s) != 0);
7234                        s = readHeaderLine(rawInStream);
7235                        if (s.equals("none")) {
7236                            // no more header to parse; we're good to go
7237                            okay = true;
7238                        } else if (mDecryptPassword != null && mDecryptPassword.length() > 0) {
7239                            preCompressStream = decodeAesHeaderAndInitialize(s, pbkdf2Fallback,
7240                                    rawInStream);
7241                            if (preCompressStream != null) {
7242                                okay = true;
7243                            }
7244                        } else Slog.w(TAG, "Archive is encrypted but no password given");
7245                    } else Slog.w(TAG, "Wrong header version: " + s);
7246                } else Slog.w(TAG, "Didn't read the right header magic");
7247
7248                if (!okay) {
7249                    Slog.w(TAG, "Invalid restore data; aborting.");
7250                    return;
7251                }
7252
7253                // okay, use the right stream layer based on compression
7254                in = (compressed) ? new InflaterInputStream(preCompressStream) : preCompressStream;
7255
7256                boolean didRestore;
7257                do {
7258                    didRestore = restoreOneFile(in, buffer);
7259                } while (didRestore);
7260
7261                if (MORE_DEBUG) Slog.v(TAG, "Done consuming input tarfile, total bytes=" + mBytes);
7262            } catch (IOException e) {
7263                Slog.e(TAG, "Unable to read restore input");
7264            } finally {
7265                tearDownPipes();
7266                tearDownAgent(mTargetApp, true);
7267
7268                try {
7269                    if (rawDataIn != null) rawDataIn.close();
7270                    if (rawInStream != null) rawInStream.close();
7271                    mInputFile.close();
7272                } catch (IOException e) {
7273                    Slog.w(TAG, "Close of restore data pipe threw", e);
7274                    /* nothing we can do about this */
7275                }
7276                synchronized (mLatchObject) {
7277                    mLatchObject.set(true);
7278                    mLatchObject.notifyAll();
7279                }
7280                mObbConnection.tearDown();
7281                sendEndRestore();
7282                Slog.d(TAG, "Full restore pass complete.");
7283                mWakelock.release();
7284            }
7285        }
7286
7287        String readHeaderLine(InputStream in) throws IOException {
7288            int c;
7289            StringBuilder buffer = new StringBuilder(80);
7290            while ((c = in.read()) >= 0) {
7291                if (c == '\n') break;   // consume and discard the newlines
7292                buffer.append((char)c);
7293            }
7294            return buffer.toString();
7295        }
7296
7297        InputStream attemptMasterKeyDecryption(String algorithm, byte[] userSalt, byte[] ckSalt,
7298                int rounds, String userIvHex, String masterKeyBlobHex, InputStream rawInStream,
7299                boolean doLog) {
7300            InputStream result = null;
7301
7302            try {
7303                Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
7304                SecretKey userKey = buildPasswordKey(algorithm, mDecryptPassword, userSalt,
7305                        rounds);
7306                byte[] IV = hexToByteArray(userIvHex);
7307                IvParameterSpec ivSpec = new IvParameterSpec(IV);
7308                c.init(Cipher.DECRYPT_MODE,
7309                        new SecretKeySpec(userKey.getEncoded(), "AES"),
7310                        ivSpec);
7311                byte[] mkCipher = hexToByteArray(masterKeyBlobHex);
7312                byte[] mkBlob = c.doFinal(mkCipher);
7313
7314                // first, the master key IV
7315                int offset = 0;
7316                int len = mkBlob[offset++];
7317                IV = Arrays.copyOfRange(mkBlob, offset, offset + len);
7318                offset += len;
7319                // then the master key itself
7320                len = mkBlob[offset++];
7321                byte[] mk = Arrays.copyOfRange(mkBlob,
7322                        offset, offset + len);
7323                offset += len;
7324                // and finally the master key checksum hash
7325                len = mkBlob[offset++];
7326                byte[] mkChecksum = Arrays.copyOfRange(mkBlob,
7327                        offset, offset + len);
7328
7329                // now validate the decrypted master key against the checksum
7330                byte[] calculatedCk = makeKeyChecksum(algorithm, mk, ckSalt, rounds);
7331                if (Arrays.equals(calculatedCk, mkChecksum)) {
7332                    ivSpec = new IvParameterSpec(IV);
7333                    c.init(Cipher.DECRYPT_MODE,
7334                            new SecretKeySpec(mk, "AES"),
7335                            ivSpec);
7336                    // Only if all of the above worked properly will 'result' be assigned
7337                    result = new CipherInputStream(rawInStream, c);
7338                } else if (doLog) Slog.w(TAG, "Incorrect password");
7339            } catch (InvalidAlgorithmParameterException e) {
7340                if (doLog) Slog.e(TAG, "Needed parameter spec unavailable!", e);
7341            } catch (BadPaddingException e) {
7342                // This case frequently occurs when the wrong password is used to decrypt
7343                // the master key.  Use the identical "incorrect password" log text as is
7344                // used in the checksum failure log in order to avoid providing additional
7345                // information to an attacker.
7346                if (doLog) Slog.w(TAG, "Incorrect password");
7347            } catch (IllegalBlockSizeException e) {
7348                if (doLog) Slog.w(TAG, "Invalid block size in master key");
7349            } catch (NoSuchAlgorithmException e) {
7350                if (doLog) Slog.e(TAG, "Needed decryption algorithm unavailable!");
7351            } catch (NoSuchPaddingException e) {
7352                if (doLog) Slog.e(TAG, "Needed padding mechanism unavailable!");
7353            } catch (InvalidKeyException e) {
7354                if (doLog) Slog.w(TAG, "Illegal password; aborting");
7355            }
7356
7357            return result;
7358        }
7359
7360        InputStream decodeAesHeaderAndInitialize(String encryptionName, boolean pbkdf2Fallback,
7361                InputStream rawInStream) {
7362            InputStream result = null;
7363            try {
7364                if (encryptionName.equals(ENCRYPTION_ALGORITHM_NAME)) {
7365
7366                    String userSaltHex = readHeaderLine(rawInStream); // 5
7367                    byte[] userSalt = hexToByteArray(userSaltHex);
7368
7369                    String ckSaltHex = readHeaderLine(rawInStream); // 6
7370                    byte[] ckSalt = hexToByteArray(ckSaltHex);
7371
7372                    int rounds = Integer.parseInt(readHeaderLine(rawInStream)); // 7
7373                    String userIvHex = readHeaderLine(rawInStream); // 8
7374
7375                    String masterKeyBlobHex = readHeaderLine(rawInStream); // 9
7376
7377                    // decrypt the master key blob
7378                    result = attemptMasterKeyDecryption(PBKDF_CURRENT, userSalt, ckSalt,
7379                            rounds, userIvHex, masterKeyBlobHex, rawInStream, false);
7380                    if (result == null && pbkdf2Fallback) {
7381                        result = attemptMasterKeyDecryption(PBKDF_FALLBACK, userSalt, ckSalt,
7382                                rounds, userIvHex, masterKeyBlobHex, rawInStream, true);
7383                    }
7384                } else Slog.w(TAG, "Unsupported encryption method: " + encryptionName);
7385            } catch (NumberFormatException e) {
7386                Slog.w(TAG, "Can't parse restore data header");
7387            } catch (IOException e) {
7388                Slog.w(TAG, "Can't read input header");
7389            }
7390
7391            return result;
7392        }
7393
7394        boolean restoreOneFile(InputStream instream, byte[] buffer) {
7395            FileMetadata info;
7396            try {
7397                info = readTarHeaders(instream);
7398                if (info != null) {
7399                    if (MORE_DEBUG) {
7400                        dumpFileMetadata(info);
7401                    }
7402
7403                    final String pkg = info.packageName;
7404                    if (!pkg.equals(mAgentPackage)) {
7405                        // okay, change in package; set up our various
7406                        // bookkeeping if we haven't seen it yet
7407                        if (!mPackagePolicies.containsKey(pkg)) {
7408                            mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
7409                        }
7410
7411                        // Clean up the previous agent relationship if necessary,
7412                        // and let the observer know we're considering a new app.
7413                        if (mAgent != null) {
7414                            if (DEBUG) Slog.d(TAG, "Saw new package; finalizing old one");
7415                            // Now we're really done
7416                            tearDownPipes();
7417                            tearDownAgent(mTargetApp, true);
7418                            mTargetApp = null;
7419                            mAgentPackage = null;
7420                        }
7421                    }
7422
7423                    if (info.path.equals(BACKUP_MANIFEST_FILENAME)) {
7424                        mPackagePolicies.put(pkg, readAppManifest(info, instream));
7425                        mPackageInstallers.put(pkg, info.installerPackageName);
7426                        // We've read only the manifest content itself at this point,
7427                        // so consume the footer before looping around to the next
7428                        // input file
7429                        skipTarPadding(info.size, instream);
7430                        sendOnRestorePackage(pkg);
7431                    } else if (info.path.equals(BACKUP_METADATA_FILENAME)) {
7432                        // Metadata blobs!
7433                        readMetadata(info, instream);
7434                        skipTarPadding(info.size, instream);
7435                    } else {
7436                        // Non-manifest, so it's actual file data.  Is this a package
7437                        // we're ignoring?
7438                        boolean okay = true;
7439                        RestorePolicy policy = mPackagePolicies.get(pkg);
7440                        switch (policy) {
7441                            case IGNORE:
7442                                okay = false;
7443                                break;
7444
7445                            case ACCEPT_IF_APK:
7446                                // If we're in accept-if-apk state, then the first file we
7447                                // see MUST be the apk.
7448                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
7449                                    if (DEBUG) Slog.d(TAG, "APK file; installing");
7450                                    // Try to install the app.
7451                                    String installerName = mPackageInstallers.get(pkg);
7452                                    okay = installApk(info, installerName, instream);
7453                                    // good to go; promote to ACCEPT
7454                                    mPackagePolicies.put(pkg, (okay)
7455                                            ? RestorePolicy.ACCEPT
7456                                            : RestorePolicy.IGNORE);
7457                                    // At this point we've consumed this file entry
7458                                    // ourselves, so just strip the tar footer and
7459                                    // go on to the next file in the input stream
7460                                    skipTarPadding(info.size, instream);
7461                                    return true;
7462                                } else {
7463                                    // File data before (or without) the apk.  We can't
7464                                    // handle it coherently in this case so ignore it.
7465                                    mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
7466                                    okay = false;
7467                                }
7468                                break;
7469
7470                            case ACCEPT:
7471                                if (info.domain.equals(FullBackup.APK_TREE_TOKEN)) {
7472                                    if (DEBUG) Slog.d(TAG, "apk present but ACCEPT");
7473                                    // we can take the data without the apk, so we
7474                                    // *want* to do so.  skip the apk by declaring this
7475                                    // one file not-okay without changing the restore
7476                                    // policy for the package.
7477                                    okay = false;
7478                                }
7479                                break;
7480
7481                            default:
7482                                // Something has gone dreadfully wrong when determining
7483                                // the restore policy from the manifest.  Ignore the
7484                                // rest of this package's data.
7485                                Slog.e(TAG, "Invalid policy from manifest");
7486                                okay = false;
7487                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
7488                                break;
7489                        }
7490
7491                        // The path needs to be canonical
7492                        if (info.path.contains("..") || info.path.contains("//")) {
7493                            if (MORE_DEBUG) {
7494                                Slog.w(TAG, "Dropping invalid path " + info.path);
7495                            }
7496                            okay = false;
7497                        }
7498
7499                        // If the policy is satisfied, go ahead and set up to pipe the
7500                        // data to the agent.
7501                        if (DEBUG && okay && mAgent != null) {
7502                            Slog.i(TAG, "Reusing existing agent instance");
7503                        }
7504                        if (okay && mAgent == null) {
7505                            if (DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg);
7506
7507                            try {
7508                                mTargetApp = mPackageManager.getApplicationInfo(pkg, 0);
7509
7510                                // If we haven't sent any data to this app yet, we probably
7511                                // need to clear it first.  Check that.
7512                                if (!mClearedPackages.contains(pkg)) {
7513                                    // apps with their own backup agents are
7514                                    // responsible for coherently managing a full
7515                                    // restore.
7516                                    if (mTargetApp.backupAgentName == null) {
7517                                        if (DEBUG) Slog.d(TAG, "Clearing app data preparatory to full restore");
7518                                        clearApplicationDataSynchronous(pkg);
7519                                    } else {
7520                                        if (DEBUG) Slog.d(TAG, "backup agent ("
7521                                                + mTargetApp.backupAgentName + ") => no clear");
7522                                    }
7523                                    mClearedPackages.add(pkg);
7524                                } else {
7525                                    if (DEBUG) Slog.d(TAG, "We've initialized this app already; no clear required");
7526                                }
7527
7528                                // All set; now set up the IPC and launch the agent
7529                                setUpPipes();
7530                                mAgent = bindToAgentSynchronous(mTargetApp,
7531                                        ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL);
7532                                mAgentPackage = pkg;
7533                            } catch (IOException e) {
7534                                // fall through to error handling
7535                            } catch (NameNotFoundException e) {
7536                                // fall through to error handling
7537                            }
7538
7539                            if (mAgent == null) {
7540                                if (DEBUG) Slog.d(TAG, "Unable to create agent for " + pkg);
7541                                okay = false;
7542                                tearDownPipes();
7543                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
7544                            }
7545                        }
7546
7547                        // Sanity check: make sure we never give data to the wrong app.  This
7548                        // should never happen but a little paranoia here won't go amiss.
7549                        if (okay && !pkg.equals(mAgentPackage)) {
7550                            Slog.e(TAG, "Restoring data for " + pkg
7551                                    + " but agent is for " + mAgentPackage);
7552                            okay = false;
7553                        }
7554
7555                        // At this point we have an agent ready to handle the full
7556                        // restore data as well as a pipe for sending data to
7557                        // that agent.  Tell the agent to start reading from the
7558                        // pipe.
7559                        if (okay) {
7560                            boolean agentSuccess = true;
7561                            long toCopy = info.size;
7562                            final boolean isSharedStorage = pkg.equals(SHARED_BACKUP_AGENT_PACKAGE);
7563                            final long timeout = isSharedStorage ?
7564                                    TIMEOUT_SHARED_BACKUP_INTERVAL : TIMEOUT_RESTORE_INTERVAL;
7565                            final int token = generateRandomIntegerToken();
7566                            try {
7567                                prepareOperationTimeout(token, timeout, null,
7568                                        OP_TYPE_RESTORE_WAIT);
7569                                if (FullBackup.OBB_TREE_TOKEN.equals(info.domain)) {
7570                                    if (DEBUG) Slog.d(TAG, "Restoring OBB file for " + pkg
7571                                            + " : " + info.path);
7572                                    mObbConnection.restoreObbFile(pkg, mPipes[0],
7573                                            info.size, info.type, info.path, info.mode,
7574                                            info.mtime, token, mBackupManagerBinder);
7575                                } else if (FullBackup.KEY_VALUE_DATA_TOKEN.equals(info.domain)) {
7576                                    if (DEBUG) Slog.d(TAG, "Restoring key-value file for " + pkg
7577                                            + " : " + info.path);
7578                                    KeyValueAdbRestoreEngine restoreEngine =
7579                                            new KeyValueAdbRestoreEngine(BackupManagerService.this,
7580                                                    mDataDir, info, mPipes[0], mAgent, token);
7581                                    new Thread(restoreEngine, "restore-key-value-runner").start();
7582                                } else {
7583                                    if (DEBUG) Slog.d(TAG, "Invoking agent to restore file "
7584                                            + info.path);
7585                                    // fire up the app's agent listening on the socket.  If
7586                                    // the agent is running in the system process we can't
7587                                    // just invoke it asynchronously, so we provide a thread
7588                                    // for it here.
7589                                    if (mTargetApp.processName.equals("system")) {
7590                                        Slog.d(TAG, "system process agent - spinning a thread");
7591                                        RestoreFileRunnable runner = new RestoreFileRunnable(
7592                                                mAgent, info, mPipes[0], token);
7593                                        new Thread(runner, "restore-sys-runner").start();
7594                                    } else {
7595                                        mAgent.doRestoreFile(mPipes[0], info.size, info.type,
7596                                                info.domain, info.path, info.mode, info.mtime,
7597                                                token, mBackupManagerBinder);
7598                                    }
7599                                }
7600                            } catch (IOException e) {
7601                                // couldn't dup the socket for a process-local restore
7602                                Slog.d(TAG, "Couldn't establish restore");
7603                                agentSuccess = false;
7604                                okay = false;
7605                            } catch (RemoteException e) {
7606                                // whoops, remote entity went away.  We'll eat the content
7607                                // ourselves, then, and not copy it over.
7608                                Slog.e(TAG, "Agent crashed during full restore");
7609                                agentSuccess = false;
7610                                okay = false;
7611                            }
7612
7613                            // Copy over the data if the agent is still good
7614                            if (okay) {
7615                                boolean pipeOkay = true;
7616                                FileOutputStream pipe = new FileOutputStream(
7617                                        mPipes[1].getFileDescriptor());
7618                                while (toCopy > 0) {
7619                                    int toRead = (toCopy > buffer.length)
7620                                    ? buffer.length : (int)toCopy;
7621                                    int nRead = instream.read(buffer, 0, toRead);
7622                                    if (nRead >= 0) mBytes += nRead;
7623                                    if (nRead <= 0) break;
7624                                    toCopy -= nRead;
7625
7626                                    // send it to the output pipe as long as things
7627                                    // are still good
7628                                    if (pipeOkay) {
7629                                        try {
7630                                            pipe.write(buffer, 0, nRead);
7631                                        } catch (IOException e) {
7632                                            Slog.e(TAG, "Failed to write to restore pipe", e);
7633                                            pipeOkay = false;
7634                                        }
7635                                    }
7636                                }
7637
7638                                // done sending that file!  Now we just need to consume
7639                                // the delta from info.size to the end of block.
7640                                skipTarPadding(info.size, instream);
7641
7642                                // and now that we've sent it all, wait for the remote
7643                                // side to acknowledge receipt
7644                                agentSuccess = waitUntilOperationComplete(token);
7645                            }
7646
7647                            // okay, if the remote end failed at any point, deal with
7648                            // it by ignoring the rest of the restore on it
7649                            if (!agentSuccess) {
7650                                if (DEBUG) {
7651                                    Slog.d(TAG, "Agent failure restoring " + pkg + "; now ignoring");
7652                                }
7653                                mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT);
7654                                tearDownPipes();
7655                                tearDownAgent(mTargetApp, false);
7656                                mPackagePolicies.put(pkg, RestorePolicy.IGNORE);
7657                            }
7658                        }
7659
7660                        // Problems setting up the agent communication, or an already-
7661                        // ignored package: skip to the next tar stream entry by
7662                        // reading and discarding this file.
7663                        if (!okay) {
7664                            if (DEBUG) Slog.d(TAG, "[discarding file content]");
7665                            long bytesToConsume = (info.size + 511) & ~511;
7666                            while (bytesToConsume > 0) {
7667                                int toRead = (bytesToConsume > buffer.length)
7668                                ? buffer.length : (int)bytesToConsume;
7669                                long nRead = instream.read(buffer, 0, toRead);
7670                                if (nRead >= 0) mBytes += nRead;
7671                                if (nRead <= 0) break;
7672                                bytesToConsume -= nRead;
7673                            }
7674                        }
7675                    }
7676                }
7677            } catch (IOException e) {
7678                if (DEBUG) Slog.w(TAG, "io exception on restore socket read", e);
7679                // treat as EOF
7680                info = null;
7681            }
7682
7683            return (info != null);
7684        }
7685
7686        void setUpPipes() throws IOException {
7687            mPipes = ParcelFileDescriptor.createPipe();
7688        }
7689
7690        void tearDownPipes() {
7691            if (mPipes != null) {
7692                try {
7693                    mPipes[0].close();
7694                    mPipes[0] = null;
7695                    mPipes[1].close();
7696                    mPipes[1] = null;
7697                } catch (IOException e) {
7698                    Slog.w(TAG, "Couldn't close agent pipes", e);
7699                }
7700                mPipes = null;
7701            }
7702        }
7703
7704        void tearDownAgent(ApplicationInfo app, boolean doRestoreFinished) {
7705            if (mAgent != null) {
7706                try {
7707                    // In the adb restore case, we do restore-finished here
7708                    if (doRestoreFinished) {
7709                        final int token = generateRandomIntegerToken();
7710                        final AdbRestoreFinishedLatch latch = new AdbRestoreFinishedLatch(token);
7711                        prepareOperationTimeout(token, TIMEOUT_FULL_BACKUP_INTERVAL, latch,
7712                                OP_TYPE_RESTORE_WAIT);
7713                        if (mTargetApp.processName.equals("system")) {
7714                            if (MORE_DEBUG) {
7715                                Slog.d(TAG, "system agent - restoreFinished on thread");
7716                            }
7717                            Runnable runner = new RestoreFinishedRunnable(mAgent, token);
7718                            new Thread(runner, "restore-sys-finished-runner").start();
7719                        } else {
7720                            mAgent.doRestoreFinished(token, mBackupManagerBinder);
7721                        }
7722
7723                        latch.await();
7724                    }
7725
7726                    // unbind and tidy up even on timeout or failure, just in case
7727                    mActivityManager.unbindBackupAgent(app);
7728
7729                    // The agent was running with a stub Application object, so shut it down.
7730                    // !!! We hardcode the confirmation UI's package name here rather than use a
7731                    //     manifest flag!  TODO something less direct.
7732                    if (app.uid >= Process.FIRST_APPLICATION_UID
7733                            && !app.packageName.equals("com.android.backupconfirm")) {
7734                        if (DEBUG) Slog.d(TAG, "Killing host process");
7735                        mActivityManager.killApplicationProcess(app.processName, app.uid);
7736                    } else {
7737                        if (DEBUG) Slog.d(TAG, "Not killing after full restore");
7738                    }
7739                } catch (RemoteException e) {
7740                    Slog.d(TAG, "Lost app trying to shut down");
7741                }
7742                mAgent = null;
7743            }
7744        }
7745
7746        class RestoreInstallObserver extends PackageInstallObserver {
7747            final AtomicBoolean mDone = new AtomicBoolean();
7748            String mPackageName;
7749            int mResult;
7750
7751            public void reset() {
7752                synchronized (mDone) {
7753                    mDone.set(false);
7754                }
7755            }
7756
7757            public void waitForCompletion() {
7758                synchronized (mDone) {
7759                    while (mDone.get() == false) {
7760                        try {
7761                            mDone.wait();
7762                        } catch (InterruptedException e) { }
7763                    }
7764                }
7765            }
7766
7767            int getResult() {
7768                return mResult;
7769            }
7770
7771            @Override
7772            public void onPackageInstalled(String packageName, int returnCode,
7773                    String msg, Bundle extras) {
7774                synchronized (mDone) {
7775                    mResult = returnCode;
7776                    mPackageName = packageName;
7777                    mDone.set(true);
7778                    mDone.notifyAll();
7779                }
7780            }
7781        }
7782
7783        class RestoreDeleteObserver extends IPackageDeleteObserver.Stub {
7784            final AtomicBoolean mDone = new AtomicBoolean();
7785            int mResult;
7786
7787            public void reset() {
7788                synchronized (mDone) {
7789                    mDone.set(false);
7790                }
7791            }
7792
7793            public void waitForCompletion() {
7794                synchronized (mDone) {
7795                    while (mDone.get() == false) {
7796                        try {
7797                            mDone.wait();
7798                        } catch (InterruptedException e) { }
7799                    }
7800                }
7801            }
7802
7803            @Override
7804            public void packageDeleted(String packageName, int returnCode) throws RemoteException {
7805                synchronized (mDone) {
7806                    mResult = returnCode;
7807                    mDone.set(true);
7808                    mDone.notifyAll();
7809                }
7810            }
7811        }
7812
7813        final RestoreInstallObserver mInstallObserver = new RestoreInstallObserver();
7814        final RestoreDeleteObserver mDeleteObserver = new RestoreDeleteObserver();
7815
7816        boolean installApk(FileMetadata info, String installerPackage, InputStream instream) {
7817            boolean okay = true;
7818
7819            if (DEBUG) Slog.d(TAG, "Installing from backup: " + info.packageName);
7820
7821            // The file content is an .apk file.  Copy it out to a staging location and
7822            // attempt to install it.
7823            File apkFile = new File(mDataDir, info.packageName);
7824            try {
7825                FileOutputStream apkStream = new FileOutputStream(apkFile);
7826                byte[] buffer = new byte[32 * 1024];
7827                long size = info.size;
7828                while (size > 0) {
7829                    long toRead = (buffer.length < size) ? buffer.length : size;
7830                    int didRead = instream.read(buffer, 0, (int)toRead);
7831                    if (didRead >= 0) mBytes += didRead;
7832                    apkStream.write(buffer, 0, didRead);
7833                    size -= didRead;
7834                }
7835                apkStream.close();
7836
7837                // make sure the installer can read it
7838                apkFile.setReadable(true, false);
7839
7840                // Now install it
7841                Uri packageUri = Uri.fromFile(apkFile);
7842                mInstallObserver.reset();
7843                mPackageManager.installPackage(packageUri, mInstallObserver,
7844                        PackageManager.INSTALL_REPLACE_EXISTING | PackageManager.INSTALL_FROM_ADB,
7845                        installerPackage);
7846                mInstallObserver.waitForCompletion();
7847
7848                if (mInstallObserver.getResult() != PackageManager.INSTALL_SUCCEEDED) {
7849                    // The only time we continue to accept install of data even if the
7850                    // apk install failed is if we had already determined that we could
7851                    // accept the data regardless.
7852                    if (mPackagePolicies.get(info.packageName) != RestorePolicy.ACCEPT) {
7853                        okay = false;
7854                    }
7855                } else {
7856                    // Okay, the install succeeded.  Make sure it was the right app.
7857                    boolean uninstall = false;
7858                    if (!mInstallObserver.mPackageName.equals(info.packageName)) {
7859                        Slog.w(TAG, "Restore stream claimed to include apk for "
7860                                + info.packageName + " but apk was really "
7861                                + mInstallObserver.mPackageName);
7862                        // delete the package we just put in place; it might be fraudulent
7863                        okay = false;
7864                        uninstall = true;
7865                    } else {
7866                        try {
7867                            PackageInfo pkg = mPackageManager.getPackageInfo(info.packageName,
7868                                    PackageManager.GET_SIGNATURES);
7869                            if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) == 0) {
7870                                Slog.w(TAG, "Restore stream contains apk of package "
7871                                        + info.packageName + " but it disallows backup/restore");
7872                                okay = false;
7873                            } else {
7874                                // So far so good -- do the signatures match the manifest?
7875                                Signature[] sigs = mManifestSignatures.get(info.packageName);
7876                                if (signaturesMatch(sigs, pkg)) {
7877                                    // If this is a system-uid app without a declared backup agent,
7878                                    // don't restore any of the file data.
7879                                    if ((pkg.applicationInfo.uid < Process.FIRST_APPLICATION_UID)
7880                                            && (pkg.applicationInfo.backupAgentName == null)) {
7881                                        Slog.w(TAG, "Installed app " + info.packageName
7882                                                + " has restricted uid and no agent");
7883                                        okay = false;
7884                                    }
7885                                } else {
7886                                    Slog.w(TAG, "Installed app " + info.packageName
7887                                            + " signatures do not match restore manifest");
7888                                    okay = false;
7889                                    uninstall = true;
7890                                }
7891                            }
7892                        } catch (NameNotFoundException e) {
7893                            Slog.w(TAG, "Install of package " + info.packageName
7894                                    + " succeeded but now not found");
7895                            okay = false;
7896                        }
7897                    }
7898
7899                    // If we're not okay at this point, we need to delete the package
7900                    // that we just installed.
7901                    if (uninstall) {
7902                        mDeleteObserver.reset();
7903                        mPackageManager.deletePackage(mInstallObserver.mPackageName,
7904                                mDeleteObserver, 0);
7905                        mDeleteObserver.waitForCompletion();
7906                    }
7907                }
7908            } catch (IOException e) {
7909                Slog.e(TAG, "Unable to transcribe restored apk for install");
7910                okay = false;
7911            } finally {
7912                apkFile.delete();
7913            }
7914
7915            return okay;
7916        }
7917
7918        // Given an actual file content size, consume the post-content padding mandated
7919        // by the tar format.
7920        void skipTarPadding(long size, InputStream instream) throws IOException {
7921            long partial = (size + 512) % 512;
7922            if (partial > 0) {
7923                final int needed = 512 - (int)partial;
7924                byte[] buffer = new byte[needed];
7925                if (readExactly(instream, buffer, 0, needed) == needed) {
7926                    mBytes += needed;
7927                } else throw new IOException("Unexpected EOF in padding");
7928            }
7929        }
7930
7931        // Read a widget metadata file, returning the restored blob
7932        void readMetadata(FileMetadata info, InputStream instream) throws IOException {
7933            // Fail on suspiciously large widget dump files
7934            if (info.size > 64 * 1024) {
7935                throw new IOException("Metadata too big; corrupt? size=" + info.size);
7936            }
7937
7938            byte[] buffer = new byte[(int) info.size];
7939            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
7940                mBytes += info.size;
7941            } else throw new IOException("Unexpected EOF in widget data");
7942
7943            String[] str = new String[1];
7944            int offset = extractLine(buffer, 0, str);
7945            int version = Integer.parseInt(str[0]);
7946            if (version == BACKUP_MANIFEST_VERSION) {
7947                offset = extractLine(buffer, offset, str);
7948                final String pkg = str[0];
7949                if (info.packageName.equals(pkg)) {
7950                    // Data checks out -- the rest of the buffer is a concatenation of
7951                    // binary blobs as described in the comment at writeAppWidgetData()
7952                    ByteArrayInputStream bin = new ByteArrayInputStream(buffer,
7953                            offset, buffer.length - offset);
7954                    DataInputStream in = new DataInputStream(bin);
7955                    while (bin.available() > 0) {
7956                        int token = in.readInt();
7957                        int size = in.readInt();
7958                        if (size > 64 * 1024) {
7959                            throw new IOException("Datum "
7960                                    + Integer.toHexString(token)
7961                                    + " too big; corrupt? size=" + info.size);
7962                        }
7963                        switch (token) {
7964                            case BACKUP_WIDGET_METADATA_TOKEN:
7965                            {
7966                                if (MORE_DEBUG) {
7967                                    Slog.i(TAG, "Got widget metadata for " + info.packageName);
7968                                }
7969                                mWidgetData = new byte[size];
7970                                in.read(mWidgetData);
7971                                break;
7972                            }
7973                            default:
7974                            {
7975                                if (DEBUG) {
7976                                    Slog.i(TAG, "Ignoring metadata blob "
7977                                            + Integer.toHexString(token)
7978                                            + " for " + info.packageName);
7979                                }
7980                                in.skipBytes(size);
7981                                break;
7982                            }
7983                        }
7984                    }
7985                } else {
7986                    Slog.w(TAG, "Metadata mismatch: package " + info.packageName
7987                            + " but widget data for " + pkg);
7988                }
7989            } else {
7990                Slog.w(TAG, "Unsupported metadata version " + version);
7991            }
7992        }
7993
7994        // Returns a policy constant; takes a buffer arg to reduce memory churn
7995        RestorePolicy readAppManifest(FileMetadata info, InputStream instream)
7996                throws IOException {
7997            // Fail on suspiciously large manifest files
7998            if (info.size > 64 * 1024) {
7999                throw new IOException("Restore manifest too big; corrupt? size=" + info.size);
8000            }
8001
8002            byte[] buffer = new byte[(int) info.size];
8003            if (readExactly(instream, buffer, 0, (int)info.size) == info.size) {
8004                mBytes += info.size;
8005            } else throw new IOException("Unexpected EOF in manifest");
8006
8007            RestorePolicy policy = RestorePolicy.IGNORE;
8008            String[] str = new String[1];
8009            int offset = 0;
8010
8011            try {
8012                offset = extractLine(buffer, offset, str);
8013                int version = Integer.parseInt(str[0]);
8014                if (version == BACKUP_MANIFEST_VERSION) {
8015                    offset = extractLine(buffer, offset, str);
8016                    String manifestPackage = str[0];
8017                    // TODO: handle <original-package>
8018                    if (manifestPackage.equals(info.packageName)) {
8019                        offset = extractLine(buffer, offset, str);
8020                        version = Integer.parseInt(str[0]);  // app version
8021                        offset = extractLine(buffer, offset, str);
8022                        // This is the platform version, which we don't use, but we parse it
8023                        // as a safety against corruption in the manifest.
8024                        Integer.parseInt(str[0]);
8025                        offset = extractLine(buffer, offset, str);
8026                        info.installerPackageName = (str[0].length() > 0) ? str[0] : null;
8027                        offset = extractLine(buffer, offset, str);
8028                        boolean hasApk = str[0].equals("1");
8029                        offset = extractLine(buffer, offset, str);
8030                        int numSigs = Integer.parseInt(str[0]);
8031                        if (numSigs > 0) {
8032                            Signature[] sigs = new Signature[numSigs];
8033                            for (int i = 0; i < numSigs; i++) {
8034                                offset = extractLine(buffer, offset, str);
8035                                sigs[i] = new Signature(str[0]);
8036                            }
8037                            mManifestSignatures.put(info.packageName, sigs);
8038
8039                            // Okay, got the manifest info we need...
8040                            try {
8041                                PackageInfo pkgInfo = mPackageManager.getPackageInfo(
8042                                        info.packageName, PackageManager.GET_SIGNATURES);
8043                                // Fall through to IGNORE if the app explicitly disallows backup
8044                                final int flags = pkgInfo.applicationInfo.flags;
8045                                if ((flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0) {
8046                                    // Restore system-uid-space packages only if they have
8047                                    // defined a custom backup agent
8048                                    if ((pkgInfo.applicationInfo.uid >= Process.FIRST_APPLICATION_UID)
8049                                            || (pkgInfo.applicationInfo.backupAgentName != null)) {
8050                                        // Verify signatures against any installed version; if they
8051                                        // don't match, then we fall though and ignore the data.  The
8052                                        // signatureMatch() method explicitly ignores the signature
8053                                        // check for packages installed on the system partition, because
8054                                        // such packages are signed with the platform cert instead of
8055                                        // the app developer's cert, so they're different on every
8056                                        // device.
8057                                        if (signaturesMatch(sigs, pkgInfo)) {
8058                                            if ((pkgInfo.applicationInfo.flags
8059                                                    & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) != 0) {
8060                                                Slog.i(TAG, "Package has restoreAnyVersion; taking data");
8061                                                policy = RestorePolicy.ACCEPT;
8062                                            } else if (pkgInfo.versionCode >= version) {
8063                                                Slog.i(TAG, "Sig + version match; taking data");
8064                                                policy = RestorePolicy.ACCEPT;
8065                                            } else {
8066                                                // The data is from a newer version of the app than
8067                                                // is presently installed.  That means we can only
8068                                                // use it if the matching apk is also supplied.
8069                                                Slog.d(TAG, "Data version " + version
8070                                                        + " is newer than installed version "
8071                                                        + pkgInfo.versionCode + " - requiring apk");
8072                                                policy = RestorePolicy.ACCEPT_IF_APK;
8073                                            }
8074                                        } else {
8075                                            Slog.w(TAG, "Restore manifest signatures do not match "
8076                                                    + "installed application for " + info.packageName);
8077                                        }
8078                                    } else {
8079                                        Slog.w(TAG, "Package " + info.packageName
8080                                                + " is system level with no agent");
8081                                    }
8082                                } else {
8083                                    if (DEBUG) Slog.i(TAG, "Restore manifest from "
8084                                            + info.packageName + " but allowBackup=false");
8085                                }
8086                            } catch (NameNotFoundException e) {
8087                                // Okay, the target app isn't installed.  We can process
8088                                // the restore properly only if the dataset provides the
8089                                // apk file and we can successfully install it.
8090                                if (DEBUG) Slog.i(TAG, "Package " + info.packageName
8091                                        + " not installed; requiring apk in dataset");
8092                                policy = RestorePolicy.ACCEPT_IF_APK;
8093                            }
8094
8095                            if (policy == RestorePolicy.ACCEPT_IF_APK && !hasApk) {
8096                                Slog.i(TAG, "Cannot restore package " + info.packageName
8097                                        + " without the matching .apk");
8098                            }
8099                        } else {
8100                            Slog.i(TAG, "Missing signature on backed-up package "
8101                                    + info.packageName);
8102                        }
8103                    } else {
8104                        Slog.i(TAG, "Expected package " + info.packageName
8105                                + " but restore manifest claims " + manifestPackage);
8106                    }
8107                } else {
8108                    Slog.i(TAG, "Unknown restore manifest version " + version
8109                            + " for package " + info.packageName);
8110                }
8111            } catch (NumberFormatException e) {
8112                Slog.w(TAG, "Corrupt restore manifest for package " + info.packageName);
8113            } catch (IllegalArgumentException e) {
8114                Slog.w(TAG, e.getMessage());
8115            }
8116
8117            return policy;
8118        }
8119
8120        // Builds a line from a byte buffer starting at 'offset', and returns
8121        // the index of the next unconsumed data in the buffer.
8122        int extractLine(byte[] buffer, int offset, String[] outStr) throws IOException {
8123            final int end = buffer.length;
8124            if (offset >= end) throw new IOException("Incomplete data");
8125
8126            int pos;
8127            for (pos = offset; pos < end; pos++) {
8128                byte c = buffer[pos];
8129                // at LF we declare end of line, and return the next char as the
8130                // starting point for the next time through
8131                if (c == '\n') {
8132                    break;
8133                }
8134            }
8135            outStr[0] = new String(buffer, offset, pos - offset);
8136            pos++;  // may be pointing an extra byte past the end but that's okay
8137            return pos;
8138        }
8139
8140        void dumpFileMetadata(FileMetadata info) {
8141            if (DEBUG) {
8142                StringBuilder b = new StringBuilder(128);
8143
8144                // mode string
8145                b.append((info.type == BackupAgent.TYPE_DIRECTORY) ? 'd' : '-');
8146                b.append(((info.mode & 0400) != 0) ? 'r' : '-');
8147                b.append(((info.mode & 0200) != 0) ? 'w' : '-');
8148                b.append(((info.mode & 0100) != 0) ? 'x' : '-');
8149                b.append(((info.mode & 0040) != 0) ? 'r' : '-');
8150                b.append(((info.mode & 0020) != 0) ? 'w' : '-');
8151                b.append(((info.mode & 0010) != 0) ? 'x' : '-');
8152                b.append(((info.mode & 0004) != 0) ? 'r' : '-');
8153                b.append(((info.mode & 0002) != 0) ? 'w' : '-');
8154                b.append(((info.mode & 0001) != 0) ? 'x' : '-');
8155                b.append(String.format(" %9d ", info.size));
8156
8157                Date stamp = new Date(info.mtime);
8158                b.append(new SimpleDateFormat("MMM dd HH:mm:ss ").format(stamp));
8159
8160                b.append(info.packageName);
8161                b.append(" :: ");
8162                b.append(info.domain);
8163                b.append(" :: ");
8164                b.append(info.path);
8165
8166                Slog.i(TAG, b.toString());
8167            }
8168        }
8169
8170        // Consume a tar file header block [sequence] and accumulate the relevant metadata
8171        FileMetadata readTarHeaders(InputStream instream) throws IOException {
8172            byte[] block = new byte[512];
8173            FileMetadata info = null;
8174
8175            boolean gotHeader = readTarHeader(instream, block);
8176            if (gotHeader) {
8177                try {
8178                    // okay, presume we're okay, and extract the various metadata
8179                    info = new FileMetadata();
8180                    info.size = extractRadix(block, 124, 12, 8);
8181                    info.mtime = extractRadix(block, 136, 12, 8);
8182                    info.mode = extractRadix(block, 100, 8, 8);
8183
8184                    info.path = extractString(block, 345, 155); // prefix
8185                    String path = extractString(block, 0, 100);
8186                    if (path.length() > 0) {
8187                        if (info.path.length() > 0) info.path += '/';
8188                        info.path += path;
8189                    }
8190
8191                    // tar link indicator field: 1 byte at offset 156 in the header.
8192                    int typeChar = block[156];
8193                    if (typeChar == 'x') {
8194                        // pax extended header, so we need to read that
8195                        gotHeader = readPaxExtendedHeader(instream, info);
8196                        if (gotHeader) {
8197                            // and after a pax extended header comes another real header -- read
8198                            // that to find the real file type
8199                            gotHeader = readTarHeader(instream, block);
8200                        }
8201                        if (!gotHeader) throw new IOException("Bad or missing pax header");
8202
8203                        typeChar = block[156];
8204                    }
8205
8206                    switch (typeChar) {
8207                        case '0': info.type = BackupAgent.TYPE_FILE; break;
8208                        case '5': {
8209                            info.type = BackupAgent.TYPE_DIRECTORY;
8210                            if (info.size != 0) {
8211                                Slog.w(TAG, "Directory entry with nonzero size in header");
8212                                info.size = 0;
8213                            }
8214                            break;
8215                        }
8216                        case 0: {
8217                            // presume EOF
8218                            if (DEBUG) Slog.w(TAG, "Saw type=0 in tar header block, info=" + info);
8219                            return null;
8220                        }
8221                        default: {
8222                            Slog.e(TAG, "Unknown tar entity type: " + typeChar);
8223                            throw new IOException("Unknown entity type " + typeChar);
8224                        }
8225                    }
8226
8227                    // Parse out the path
8228                    //
8229                    // first: apps/shared/unrecognized
8230                    if (FullBackup.SHARED_PREFIX.regionMatches(0,
8231                            info.path, 0, FullBackup.SHARED_PREFIX.length())) {
8232                        // File in shared storage.  !!! TODO: implement this.
8233                        info.path = info.path.substring(FullBackup.SHARED_PREFIX.length());
8234                        info.packageName = SHARED_BACKUP_AGENT_PACKAGE;
8235                        info.domain = FullBackup.SHARED_STORAGE_TOKEN;
8236                        if (DEBUG) Slog.i(TAG, "File in shared storage: " + info.path);
8237                    } else if (FullBackup.APPS_PREFIX.regionMatches(0,
8238                            info.path, 0, FullBackup.APPS_PREFIX.length())) {
8239                        // App content!  Parse out the package name and domain
8240
8241                        // strip the apps/ prefix
8242                        info.path = info.path.substring(FullBackup.APPS_PREFIX.length());
8243
8244                        // extract the package name
8245                        int slash = info.path.indexOf('/');
8246                        if (slash < 0) throw new IOException("Illegal semantic path in " + info.path);
8247                        info.packageName = info.path.substring(0, slash);
8248                        info.path = info.path.substring(slash+1);
8249
8250                        // if it's a manifest or metadata payload we're done, otherwise parse
8251                        // out the domain into which the file will be restored
8252                        if (!info.path.equals(BACKUP_MANIFEST_FILENAME)
8253                                && !info.path.equals(BACKUP_METADATA_FILENAME)) {
8254                            slash = info.path.indexOf('/');
8255                            if (slash < 0) throw new IOException("Illegal semantic path in non-manifest " + info.path);
8256                            info.domain = info.path.substring(0, slash);
8257                            info.path = info.path.substring(slash + 1);
8258                        }
8259                    }
8260                } catch (IOException e) {
8261                    if (DEBUG) {
8262                        Slog.e(TAG, "Parse error in header: " + e.getMessage());
8263                        HEXLOG(block);
8264                    }
8265                    throw e;
8266                }
8267            }
8268            return info;
8269        }
8270
8271        private void HEXLOG(byte[] block) {
8272            int offset = 0;
8273            int todo = block.length;
8274            StringBuilder buf = new StringBuilder(64);
8275            while (todo > 0) {
8276                buf.append(String.format("%04x   ", offset));
8277                int numThisLine = (todo > 16) ? 16 : todo;
8278                for (int i = 0; i < numThisLine; i++) {
8279                    buf.append(String.format("%02x ", block[offset+i]));
8280                }
8281                Slog.i("hexdump", buf.toString());
8282                buf.setLength(0);
8283                todo -= numThisLine;
8284                offset += numThisLine;
8285            }
8286        }
8287
8288        // Read exactly the given number of bytes into a buffer at the stated offset.
8289        // Returns false if EOF is encountered before the requested number of bytes
8290        // could be read.
8291        int readExactly(InputStream in, byte[] buffer, int offset, int size)
8292                throws IOException {
8293            if (size <= 0) throw new IllegalArgumentException("size must be > 0");
8294
8295            int soFar = 0;
8296            while (soFar < size) {
8297                int nRead = in.read(buffer, offset + soFar, size - soFar);
8298                if (nRead <= 0) {
8299                    if (MORE_DEBUG) Slog.w(TAG, "- wanted exactly " + size + " but got only " + soFar);
8300                    break;
8301                }
8302                soFar += nRead;
8303            }
8304            return soFar;
8305        }
8306
8307        boolean readTarHeader(InputStream instream, byte[] block) throws IOException {
8308            final int got = readExactly(instream, block, 0, 512);
8309            if (got == 0) return false;     // Clean EOF
8310            if (got < 512) throw new IOException("Unable to read full block header");
8311            mBytes += 512;
8312            return true;
8313        }
8314
8315        // overwrites 'info' fields based on the pax extended header
8316        boolean readPaxExtendedHeader(InputStream instream, FileMetadata info)
8317                throws IOException {
8318            // We should never see a pax extended header larger than this
8319            if (info.size > 32*1024) {
8320                Slog.w(TAG, "Suspiciously large pax header size " + info.size
8321                        + " - aborting");
8322                throw new IOException("Sanity failure: pax header size " + info.size);
8323            }
8324
8325            // read whole blocks, not just the content size
8326            int numBlocks = (int)((info.size + 511) >> 9);
8327            byte[] data = new byte[numBlocks * 512];
8328            if (readExactly(instream, data, 0, data.length) < data.length) {
8329                throw new IOException("Unable to read full pax header");
8330            }
8331            mBytes += data.length;
8332
8333            final int contentSize = (int) info.size;
8334            int offset = 0;
8335            do {
8336                // extract the line at 'offset'
8337                int eol = offset+1;
8338                while (eol < contentSize && data[eol] != ' ') eol++;
8339                if (eol >= contentSize) {
8340                    // error: we just hit EOD looking for the end of the size field
8341                    throw new IOException("Invalid pax data");
8342                }
8343                // eol points to the space between the count and the key
8344                int linelen = (int) extractRadix(data, offset, eol - offset, 10);
8345                int key = eol + 1;  // start of key=value
8346                eol = offset + linelen - 1; // trailing LF
8347                int value;
8348                for (value = key+1; data[value] != '=' && value <= eol; value++);
8349                if (value > eol) {
8350                    throw new IOException("Invalid pax declaration");
8351                }
8352
8353                // pax requires that key/value strings be in UTF-8
8354                String keyStr = new String(data, key, value-key, "UTF-8");
8355                // -1 to strip the trailing LF
8356                String valStr = new String(data, value+1, eol-value-1, "UTF-8");
8357
8358                if ("path".equals(keyStr)) {
8359                    info.path = valStr;
8360                } else if ("size".equals(keyStr)) {
8361                    info.size = Long.parseLong(valStr);
8362                } else {
8363                    if (DEBUG) Slog.i(TAG, "Unhandled pax key: " + key);
8364                }
8365
8366                offset += linelen;
8367            } while (offset < contentSize);
8368
8369            return true;
8370        }
8371
8372        long extractRadix(byte[] data, int offset, int maxChars, int radix)
8373                throws IOException {
8374            long value = 0;
8375            final int end = offset + maxChars;
8376            for (int i = offset; i < end; i++) {
8377                final byte b = data[i];
8378                // Numeric fields in tar can terminate with either NUL or SPC
8379                if (b == 0 || b == ' ') break;
8380                if (b < '0' || b > ('0' + radix - 1)) {
8381                    throw new IOException("Invalid number in header: '" + (char)b + "' for radix " + radix);
8382                }
8383                value = radix * value + (b - '0');
8384            }
8385            return value;
8386        }
8387
8388        String extractString(byte[] data, int offset, int maxChars) throws IOException {
8389            final int end = offset + maxChars;
8390            int eos = offset;
8391            // tar string fields terminate early with a NUL
8392            while (eos < end && data[eos] != 0) eos++;
8393            return new String(data, offset, eos-offset, "US-ASCII");
8394        }
8395
8396        void sendStartRestore() {
8397            if (mObserver != null) {
8398                try {
8399                    mObserver.onStartRestore();
8400                } catch (RemoteException e) {
8401                    Slog.w(TAG, "full restore observer went away: startRestore");
8402                    mObserver = null;
8403                }
8404            }
8405        }
8406
8407        void sendOnRestorePackage(String name) {
8408            if (mObserver != null) {
8409                try {
8410                    // TODO: use a more user-friendly name string
8411                    mObserver.onRestorePackage(name);
8412                } catch (RemoteException e) {
8413                    Slog.w(TAG, "full restore observer went away: restorePackage");
8414                    mObserver = null;
8415                }
8416            }
8417        }
8418
8419        void sendEndRestore() {
8420            if (mObserver != null) {
8421                try {
8422                    mObserver.onEndRestore();
8423                } catch (RemoteException e) {
8424                    Slog.w(TAG, "full restore observer went away: endRestore");
8425                    mObserver = null;
8426                }
8427            }
8428        }
8429    }
8430
8431    // ----- Restore handling -----
8432
8433    // Old style: directly match the stored vs on device signature blocks
8434    static boolean signaturesMatch(Signature[] storedSigs, PackageInfo target) {
8435        if (target == null) {
8436            return false;
8437        }
8438
8439        // If the target resides on the system partition, we allow it to restore
8440        // data from the like-named package in a restore set even if the signatures
8441        // do not match.  (Unlike general applications, those flashed to the system
8442        // partition will be signed with the device's platform certificate, so on
8443        // different phones the same system app will have different signatures.)
8444        if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
8445            if (MORE_DEBUG) Slog.v(TAG, "System app " + target.packageName + " - skipping sig check");
8446            return true;
8447        }
8448
8449        // Allow unsigned apps, but not signed on one device and unsigned on the other
8450        // !!! TODO: is this the right policy?
8451        Signature[] deviceSigs = target.signatures;
8452        if (MORE_DEBUG) Slog.v(TAG, "signaturesMatch(): stored=" + storedSigs
8453                + " device=" + deviceSigs);
8454        if ((storedSigs == null || storedSigs.length == 0)
8455                && (deviceSigs == null || deviceSigs.length == 0)) {
8456            return true;
8457        }
8458        if (storedSigs == null || deviceSigs == null) {
8459            return false;
8460        }
8461
8462        // !!! TODO: this demands that every stored signature match one
8463        // that is present on device, and does not demand the converse.
8464        // Is this this right policy?
8465        int nStored = storedSigs.length;
8466        int nDevice = deviceSigs.length;
8467
8468        for (int i=0; i < nStored; i++) {
8469            boolean match = false;
8470            for (int j=0; j < nDevice; j++) {
8471                if (storedSigs[i].equals(deviceSigs[j])) {
8472                    match = true;
8473                    break;
8474                }
8475            }
8476            if (!match) {
8477                return false;
8478            }
8479        }
8480        return true;
8481    }
8482
8483    // Used by both incremental and full restore
8484    void restoreWidgetData(String packageName, byte[] widgetData) {
8485        // Apply the restored widget state and generate the ID update for the app
8486        // TODO: http://b/22388012
8487        if (MORE_DEBUG) {
8488            Slog.i(TAG, "Incorporating restored widget data");
8489        }
8490        AppWidgetBackupBridge.restoreWidgetState(packageName, widgetData, UserHandle.USER_SYSTEM);
8491    }
8492
8493    // *****************************
8494    // NEW UNIFIED RESTORE IMPLEMENTATION
8495    // *****************************
8496
8497    // states of the unified-restore state machine
8498    enum UnifiedRestoreState {
8499        INITIAL,
8500        RUNNING_QUEUE,
8501        RESTORE_KEYVALUE,
8502        RESTORE_FULL,
8503        RESTORE_FINISHED,
8504        FINAL
8505    }
8506
8507    class PerformUnifiedRestoreTask implements BackupRestoreTask {
8508        // Transport we're working with to do the restore
8509        private IBackupTransport mTransport;
8510
8511        // Where per-transport saved state goes
8512        File mStateDir;
8513
8514        // Restore observer; may be null
8515        private IRestoreObserver mObserver;
8516
8517        // BackuoManagerMonitor; may be null
8518        private IBackupManagerMonitor mMonitor;
8519
8520        // Token identifying the dataset to the transport
8521        private long mToken;
8522
8523        // When this is a restore-during-install, this is the token identifying the
8524        // operation to the Package Manager, and we must ensure that we let it know
8525        // when we're finished.
8526        private int mPmToken;
8527
8528        // When this is restore-during-install, we need to tell the package manager
8529        // whether we actually launched the app, because this affects notifications
8530        // around externally-visible state transitions.
8531        private boolean mDidLaunch;
8532
8533        // Is this a whole-system restore, i.e. are we establishing a new ancestral
8534        // dataset to base future restore-at-install operations from?
8535        private boolean mIsSystemRestore;
8536
8537        // If this is a single-package restore, what package are we interested in?
8538        private PackageInfo mTargetPackage;
8539
8540        // In all cases, the calculated list of packages that we are trying to restore
8541        private List<PackageInfo> mAcceptSet;
8542
8543        // Our bookkeeping about the ancestral dataset
8544        private PackageManagerBackupAgent mPmAgent;
8545
8546        // Currently-bound backup agent for restore + restoreFinished purposes
8547        private IBackupAgent mAgent;
8548
8549        // What sort of restore we're doing now
8550        private RestoreDescription mRestoreDescription;
8551
8552        // The package we're currently restoring
8553        private PackageInfo mCurrentPackage;
8554
8555        // Widget-related data handled as part of this restore operation
8556        private byte[] mWidgetData;
8557
8558        // Number of apps restored in this pass
8559        private int mCount;
8560
8561        // When did we start?
8562        private long mStartRealtime;
8563
8564        // State machine progress
8565        private UnifiedRestoreState mState;
8566
8567        // How are things going?
8568        private int mStatus;
8569
8570        // Done?
8571        private boolean mFinished;
8572
8573        // Key/value: bookkeeping about staged data and files for agent access
8574        private File mBackupDataName;
8575        private File mStageName;
8576        private File mSavedStateName;
8577        private File mNewStateName;
8578        ParcelFileDescriptor mBackupData;
8579        ParcelFileDescriptor mNewState;
8580
8581        private final int mEphemeralOpToken;
8582
8583        // Invariant: mWakelock is already held, and this task is responsible for
8584        // releasing it at the end of the restore operation.
8585        PerformUnifiedRestoreTask(IBackupTransport transport, IRestoreObserver observer,
8586                IBackupManagerMonitor monitor, long restoreSetToken, PackageInfo targetPackage,
8587                int pmToken, boolean isFullSystemRestore, String[] filterSet) {
8588            mEphemeralOpToken = generateRandomIntegerToken();
8589            mState = UnifiedRestoreState.INITIAL;
8590            mStartRealtime = SystemClock.elapsedRealtime();
8591
8592            mTransport = transport;
8593            mObserver = observer;
8594            mMonitor = monitor;
8595            mToken = restoreSetToken;
8596            mPmToken = pmToken;
8597            mTargetPackage = targetPackage;
8598            mIsSystemRestore = isFullSystemRestore;
8599            mFinished = false;
8600            mDidLaunch = false;
8601
8602            if (targetPackage != null) {
8603                // Single package restore
8604                mAcceptSet = new ArrayList<PackageInfo>();
8605                mAcceptSet.add(targetPackage);
8606            } else {
8607                // Everything possible, or a target set
8608                if (filterSet == null) {
8609                    // We want everything and a pony
8610                    List<PackageInfo> apps =
8611                            PackageManagerBackupAgent.getStorableApplications(mPackageManager);
8612                    filterSet = packagesToNames(apps);
8613                    if (DEBUG) {
8614                        Slog.i(TAG, "Full restore; asking about " + filterSet.length + " apps");
8615                    }
8616                }
8617
8618                mAcceptSet = new ArrayList<PackageInfo>(filterSet.length);
8619
8620                // Pro tem, we insist on moving the settings provider package to last place.
8621                // Keep track of whether it's in the list, and bump it down if so.  We also
8622                // want to do the system package itself first if it's called for.
8623                boolean hasSystem = false;
8624                boolean hasSettings = false;
8625                for (int i = 0; i < filterSet.length; i++) {
8626                    try {
8627                        PackageInfo info = mPackageManager.getPackageInfo(filterSet[i], 0);
8628                        if ("android".equals(info.packageName)) {
8629                            hasSystem = true;
8630                            continue;
8631                        }
8632                        if (SETTINGS_PACKAGE.equals(info.packageName)) {
8633                            hasSettings = true;
8634                            continue;
8635                        }
8636
8637                        if (appIsEligibleForBackup(info.applicationInfo)) {
8638                            mAcceptSet.add(info);
8639                        }
8640                    } catch (NameNotFoundException e) {
8641                        // requested package name doesn't exist; ignore it
8642                    }
8643                }
8644                if (hasSystem) {
8645                    try {
8646                        mAcceptSet.add(0, mPackageManager.getPackageInfo("android", 0));
8647                    } catch (NameNotFoundException e) {
8648                        // won't happen; we know a priori that it's valid
8649                    }
8650                }
8651                if (hasSettings) {
8652                    try {
8653                        mAcceptSet.add(mPackageManager.getPackageInfo(SETTINGS_PACKAGE, 0));
8654                    } catch (NameNotFoundException e) {
8655                        // this one is always valid too
8656                    }
8657                }
8658            }
8659
8660            if (MORE_DEBUG) {
8661                Slog.v(TAG, "Restore; accept set size is " + mAcceptSet.size());
8662                for (PackageInfo info : mAcceptSet) {
8663                    Slog.v(TAG, "   " + info.packageName);
8664                }
8665            }
8666        }
8667
8668        private String[] packagesToNames(List<PackageInfo> apps) {
8669            final int N = apps.size();
8670            String[] names = new String[N];
8671            for (int i = 0; i < N; i++) {
8672                names[i] = apps.get(i).packageName;
8673            }
8674            return names;
8675        }
8676
8677        // Execute one tick of whatever state machine the task implements
8678        @Override
8679        public void execute() {
8680            if (MORE_DEBUG) Slog.v(TAG, "*** Executing restore step " + mState);
8681            switch (mState) {
8682                case INITIAL:
8683                    startRestore();
8684                    break;
8685
8686                case RUNNING_QUEUE:
8687                    dispatchNextRestore();
8688                    break;
8689
8690                case RESTORE_KEYVALUE:
8691                    restoreKeyValue();
8692                    break;
8693
8694                case RESTORE_FULL:
8695                    restoreFull();
8696                    break;
8697
8698                case RESTORE_FINISHED:
8699                    restoreFinished();
8700                    break;
8701
8702                case FINAL:
8703                    if (!mFinished) finalizeRestore();
8704                    else {
8705                        Slog.e(TAG, "Duplicate finish");
8706                    }
8707                    mFinished = true;
8708                    break;
8709            }
8710        }
8711
8712        /*
8713         * SKETCH OF OPERATION
8714         *
8715         * create one of these PerformUnifiedRestoreTask objects, telling it which
8716         * dataset & transport to address, and then parameters within the restore
8717         * operation: single target package vs many, etc.
8718         *
8719         * 1. transport.startRestore(token, list-of-packages).  If we need @pm@  it is
8720         * always placed first and the settings provider always placed last [for now].
8721         *
8722         * 1a [if we needed @pm@ then nextRestorePackage() and restore the PMBA inline]
8723         *
8724         *   [ state change => RUNNING_QUEUE ]
8725         *
8726         * NOW ITERATE:
8727         *
8728         * { 3. t.nextRestorePackage()
8729         *   4. does the metadata for this package allow us to restore it?
8730         *      does the on-disk app permit us to restore it? [re-check allowBackup etc]
8731         *   5. is this a key/value dataset?  => key/value agent restore
8732         *       [ state change => RESTORE_KEYVALUE ]
8733         *       5a. spin up agent
8734         *       5b. t.getRestoreData() to stage it properly
8735         *       5c. call into agent to perform restore
8736         *       5d. tear down agent
8737         *       [ state change => RUNNING_QUEUE ]
8738         *
8739         *   6. else it's a stream dataset:
8740         *       [ state change => RESTORE_FULL ]
8741         *       6a. instantiate the engine for a stream restore: engine handles agent lifecycles
8742         *       6b. spin off engine runner on separate thread
8743         *       6c. ITERATE getNextFullRestoreDataChunk() and copy data to engine runner socket
8744         *       [ state change => RUNNING_QUEUE ]
8745         * }
8746         *
8747         *   [ state change => FINAL ]
8748         *
8749         * 7. t.finishRestore(), release wakelock, etc.
8750         *
8751         *
8752         */
8753
8754        // state INITIAL : set up for the restore and read the metadata if necessary
8755        private  void startRestore() {
8756            sendStartRestore(mAcceptSet.size());
8757
8758            // If we're starting a full-system restore, set up to begin widget ID remapping
8759            if (mIsSystemRestore) {
8760                // TODO: http://b/22388012
8761                AppWidgetBackupBridge.restoreStarting(UserHandle.USER_SYSTEM);
8762            }
8763
8764            try {
8765                String transportDir = mTransport.transportDirName();
8766                mStateDir = new File(mBaseStateDir, transportDir);
8767
8768                // Fetch the current metadata from the dataset first
8769                PackageInfo pmPackage = new PackageInfo();
8770                pmPackage.packageName = PACKAGE_MANAGER_SENTINEL;
8771                mAcceptSet.add(0, pmPackage);
8772
8773                PackageInfo[] packages = mAcceptSet.toArray(new PackageInfo[0]);
8774                mStatus = mTransport.startRestore(mToken, packages);
8775                if (mStatus != BackupTransport.TRANSPORT_OK) {
8776                    Slog.e(TAG, "Transport error " + mStatus + "; no restore possible");
8777                    mStatus = BackupTransport.TRANSPORT_ERROR;
8778                    executeNextState(UnifiedRestoreState.FINAL);
8779                    return;
8780                }
8781
8782                RestoreDescription desc = mTransport.nextRestorePackage();
8783                if (desc == null) {
8784                    Slog.e(TAG, "No restore metadata available; halting");
8785                    mMonitor = monitorEvent(mMonitor,
8786                            BackupManagerMonitor.LOG_EVENT_ID_NO_RESTORE_METADATA_AVAILABLE,
8787                            mCurrentPackage,
8788                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
8789                    mStatus = BackupTransport.TRANSPORT_ERROR;
8790                    executeNextState(UnifiedRestoreState.FINAL);
8791                    return;
8792                }
8793                if (!PACKAGE_MANAGER_SENTINEL.equals(desc.getPackageName())) {
8794                    Slog.e(TAG, "Required package metadata but got "
8795                            + desc.getPackageName());
8796                    mMonitor = monitorEvent(mMonitor,
8797                            BackupManagerMonitor.LOG_EVENT_ID_NO_PM_METADATA_RECEIVED,
8798                            mCurrentPackage,
8799                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
8800                    mStatus = BackupTransport.TRANSPORT_ERROR;
8801                    executeNextState(UnifiedRestoreState.FINAL);
8802                    return;
8803                }
8804
8805                // Pull the Package Manager metadata from the restore set first
8806                mCurrentPackage = new PackageInfo();
8807                mCurrentPackage.packageName = PACKAGE_MANAGER_SENTINEL;
8808                mPmAgent = new PackageManagerBackupAgent(mPackageManager, null);
8809                mAgent = IBackupAgent.Stub.asInterface(mPmAgent.onBind());
8810                if (MORE_DEBUG) {
8811                    Slog.v(TAG, "initiating restore for PMBA");
8812                }
8813                initiateOneRestore(mCurrentPackage, 0);
8814                // The PM agent called operationComplete() already, because our invocation
8815                // of it is process-local and therefore synchronous.  That means that the
8816                // next-state message (RUNNING_QUEUE) is already enqueued.  Only if we're
8817                // unable to proceed with running the queue do we remove that pending
8818                // message and jump straight to the FINAL state.  Because this was
8819                // synchronous we also know that we should cancel the pending timeout
8820                // message.
8821                mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT);
8822
8823                // Verify that the backup set includes metadata.  If not, we can't do
8824                // signature/version verification etc, so we simply do not proceed with
8825                // the restore operation.
8826                if (!mPmAgent.hasMetadata()) {
8827                    Slog.e(TAG, "PM agent has no metadata, so not restoring");
8828                    mMonitor = monitorEvent(mMonitor,
8829                            BackupManagerMonitor.LOG_EVENT_ID_PM_AGENT_HAS_NO_METADATA,
8830                            mCurrentPackage,
8831                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
8832                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
8833                            PACKAGE_MANAGER_SENTINEL,
8834                            "Package manager restore metadata missing");
8835                    mStatus = BackupTransport.TRANSPORT_ERROR;
8836                    mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this);
8837                    executeNextState(UnifiedRestoreState.FINAL);
8838                    return;
8839                }
8840
8841                // Success; cache the metadata and continue as expected with the
8842                // next state already enqueued
8843
8844            } catch (Exception e) {
8845                // If we lost the transport at any time, halt
8846                Slog.e(TAG, "Unable to contact transport for restore: " + e.getMessage());
8847                mMonitor = monitorEvent(mMonitor,
8848                        BackupManagerMonitor.LOG_EVENT_ID_LOST_TRANSPORT,
8849                        null,
8850                        BackupManagerMonitor.LOG_EVENT_CATEGORY_TRANSPORT, null);
8851                mStatus = BackupTransport.TRANSPORT_ERROR;
8852                mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this);
8853                executeNextState(UnifiedRestoreState.FINAL);
8854                return;
8855            }
8856        }
8857
8858        // state RUNNING_QUEUE : figure out what the next thing to be restored is,
8859        // and fire the appropriate next step
8860        private void dispatchNextRestore() {
8861            UnifiedRestoreState nextState = UnifiedRestoreState.FINAL;
8862            try {
8863                mRestoreDescription = mTransport.nextRestorePackage();
8864                final String pkgName = (mRestoreDescription != null)
8865                        ? mRestoreDescription.getPackageName() : null;
8866                if (pkgName == null) {
8867                    Slog.e(TAG, "Failure getting next package name");
8868                    EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
8869                    nextState = UnifiedRestoreState.FINAL;
8870                    return;
8871                } else if (mRestoreDescription == RestoreDescription.NO_MORE_PACKAGES) {
8872                    // Yay we've reached the end cleanly
8873                    if (DEBUG) {
8874                        Slog.v(TAG, "No more packages; finishing restore");
8875                    }
8876                    int millis = (int) (SystemClock.elapsedRealtime() - mStartRealtime);
8877                    EventLog.writeEvent(EventLogTags.RESTORE_SUCCESS, mCount, millis);
8878                    nextState = UnifiedRestoreState.FINAL;
8879                    return;
8880                }
8881
8882                if (DEBUG) {
8883                    Slog.i(TAG, "Next restore package: " + mRestoreDescription);
8884                }
8885                sendOnRestorePackage(pkgName);
8886
8887                Metadata metaInfo = mPmAgent.getRestoredMetadata(pkgName);
8888                if (metaInfo == null) {
8889                    Slog.e(TAG, "No metadata for " + pkgName);
8890                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, pkgName,
8891                            "Package metadata missing");
8892                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
8893                    return;
8894                }
8895
8896                try {
8897                    mCurrentPackage = mPackageManager.getPackageInfo(
8898                            pkgName, PackageManager.GET_SIGNATURES);
8899                } catch (NameNotFoundException e) {
8900                    // Whoops, we thought we could restore this package but it
8901                    // turns out not to be present.  Skip it.
8902                    Slog.e(TAG, "Package not present: " + pkgName);
8903                    mMonitor = monitorEvent(mMonitor,
8904                            BackupManagerMonitor.LOG_EVENT_ID_PACKAGE_NOT_PRESENT,
8905                            mCurrentPackage,
8906                            BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
8907                            null);
8908                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, pkgName,
8909                            "Package missing on device");
8910                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
8911                    return;
8912                }
8913
8914                if (metaInfo.versionCode > mCurrentPackage.versionCode) {
8915                    // Data is from a "newer" version of the app than we have currently
8916                    // installed.  If the app has not declared that it is prepared to
8917                    // handle this case, we do not attempt the restore.
8918                    if ((mCurrentPackage.applicationInfo.flags
8919                            & ApplicationInfo.FLAG_RESTORE_ANY_VERSION) == 0) {
8920                        String message = "Source version " + metaInfo.versionCode
8921                                + " > installed version " + mCurrentPackage.versionCode;
8922                        Slog.w(TAG, "Package " + pkgName + ": " + message);
8923                        Bundle monitoringExtras = putMonitoringExtra(null,
8924                                BackupManagerMonitor.EXTRA_LOG_RESTORE_VERSION,
8925                                metaInfo.versionCode);
8926                        monitoringExtras = putMonitoringExtra(monitoringExtras,
8927                                BackupManagerMonitor.EXTRA_LOG_RESTORE_ANYWAY, false);
8928                        mMonitor = monitorEvent(mMonitor,
8929                                BackupManagerMonitor.LOG_EVENT_ID_RESTORE_VERSION_HIGHER,
8930                                mCurrentPackage,
8931                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
8932                                monitoringExtras);
8933                        EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
8934                                pkgName, message);
8935                        nextState = UnifiedRestoreState.RUNNING_QUEUE;
8936                        return;
8937                    } else {
8938                        if (DEBUG) Slog.v(TAG, "Source version " + metaInfo.versionCode
8939                                + " > installed version " + mCurrentPackage.versionCode
8940                                + " but restoreAnyVersion");
8941                        Bundle monitoringExtras = putMonitoringExtra(null,
8942                                BackupManagerMonitor.EXTRA_LOG_RESTORE_VERSION,
8943                                metaInfo.versionCode);
8944                        monitoringExtras = putMonitoringExtra(monitoringExtras,
8945                                BackupManagerMonitor.EXTRA_LOG_RESTORE_ANYWAY, true);
8946                        mMonitor = monitorEvent(mMonitor,
8947                                BackupManagerMonitor.LOG_EVENT_ID_RESTORE_VERSION_HIGHER,
8948                                mCurrentPackage,
8949                                BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY,
8950                                monitoringExtras);
8951                    }
8952                }
8953
8954                if (MORE_DEBUG) Slog.v(TAG, "Package " + pkgName
8955                        + " restore version [" + metaInfo.versionCode
8956                        + "] is compatible with installed version ["
8957                        + mCurrentPackage.versionCode + "]");
8958
8959                // Reset per-package preconditions and fire the appropriate next state
8960                mWidgetData = null;
8961                final int type = mRestoreDescription.getDataType();
8962                if (type == RestoreDescription.TYPE_KEY_VALUE) {
8963                    nextState = UnifiedRestoreState.RESTORE_KEYVALUE;
8964                } else if (type == RestoreDescription.TYPE_FULL_STREAM) {
8965                    nextState = UnifiedRestoreState.RESTORE_FULL;
8966                } else {
8967                    // Unknown restore type; ignore this package and move on
8968                    Slog.e(TAG, "Unrecognized restore type " + type);
8969                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
8970                    return;
8971                }
8972            } catch (Exception e) {
8973                Slog.e(TAG, "Can't get next restore target from transport; halting: "
8974                        + e.getMessage());
8975                EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
8976                nextState = UnifiedRestoreState.FINAL;
8977                return;
8978            } finally {
8979                executeNextState(nextState);
8980            }
8981        }
8982
8983        // state RESTORE_KEYVALUE : restore one package via key/value API set
8984        private void restoreKeyValue() {
8985            // Initiating the restore will pass responsibility for the state machine's
8986            // progress to the agent callback, so we do not always execute the
8987            // next state here.
8988            final String packageName = mCurrentPackage.packageName;
8989            // Validate some semantic requirements that apply in this way
8990            // only to the key/value restore API flow
8991            if (mCurrentPackage.applicationInfo.backupAgentName == null
8992                    || "".equals(mCurrentPackage.applicationInfo.backupAgentName)) {
8993                if (MORE_DEBUG) {
8994                    Slog.i(TAG, "Data exists for package " + packageName
8995                            + " but app has no agent; skipping");
8996                }
8997                mMonitor = monitorEvent(mMonitor,
8998                        BackupManagerMonitor.LOG_EVENT_ID_APP_HAS_NO_AGENT, mCurrentPackage,
8999                        BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
9000                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
9001                        "Package has no agent");
9002                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9003                return;
9004            }
9005
9006            Metadata metaInfo = mPmAgent.getRestoredMetadata(packageName);
9007            if (!BackupUtils.signaturesMatch(metaInfo.sigHashes, mCurrentPackage)) {
9008                Slog.w(TAG, "Signature mismatch restoring " + packageName);
9009                mMonitor = monitorEvent(mMonitor,
9010                        BackupManagerMonitor.LOG_EVENT_ID_SIGNATURE_MISMATCH, mCurrentPackage,
9011                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
9012                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
9013                        "Signature mismatch");
9014                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9015                return;
9016            }
9017
9018            // Good to go!  Set up and bind the agent...
9019            mAgent = bindToAgentSynchronous(
9020                    mCurrentPackage.applicationInfo,
9021                    ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL);
9022            if (mAgent == null) {
9023                Slog.w(TAG, "Can't find backup agent for " + packageName);
9024                mMonitor = monitorEvent(mMonitor,
9025                        BackupManagerMonitor.LOG_EVENT_ID_CANT_FIND_AGENT, mCurrentPackage,
9026                        BackupManagerMonitor.LOG_EVENT_CATEGORY_BACKUP_MANAGER_POLICY, null);
9027                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE, packageName,
9028                        "Restore agent missing");
9029                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9030                return;
9031            }
9032
9033            // Whatever happens next, we've launched the target app now; remember that.
9034            mDidLaunch = true;
9035
9036            // And then finally start the restore on this agent
9037            try {
9038                initiateOneRestore(mCurrentPackage, metaInfo.versionCode);
9039                ++mCount;
9040            } catch (Exception e) {
9041                Slog.e(TAG, "Error when attempting restore: " + e.toString());
9042                keyValueAgentErrorCleanup();
9043                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9044            }
9045        }
9046
9047        // Guts of a key/value restore operation
9048        void initiateOneRestore(PackageInfo app, int appVersionCode) {
9049            final String packageName = app.packageName;
9050
9051            if (DEBUG) Slog.d(TAG, "initiateOneRestore packageName=" + packageName);
9052
9053            // !!! TODO: get the dirs from the transport
9054            mBackupDataName = new File(mDataDir, packageName + ".restore");
9055            mStageName = new File(mDataDir, packageName + ".stage");
9056            mNewStateName = new File(mStateDir, packageName + ".new");
9057            mSavedStateName = new File(mStateDir, packageName);
9058
9059            // don't stage the 'android' package where the wallpaper data lives.  this is
9060            // an optimization: we know there's no widget data hosted/published by that
9061            // package, and this way we avoid doing a spurious copy of MB-sized wallpaper
9062            // data following the download.
9063            boolean staging = !packageName.equals("android");
9064            ParcelFileDescriptor stage;
9065            File downloadFile = (staging) ? mStageName : mBackupDataName;
9066
9067            try {
9068                // Run the transport's restore pass
9069                stage = ParcelFileDescriptor.open(downloadFile,
9070                        ParcelFileDescriptor.MODE_READ_WRITE |
9071                        ParcelFileDescriptor.MODE_CREATE |
9072                        ParcelFileDescriptor.MODE_TRUNCATE);
9073
9074                if (mTransport.getRestoreData(stage) != BackupTransport.TRANSPORT_OK) {
9075                    // Transport-level failure, so we wind everything up and
9076                    // terminate the restore operation.
9077                    Slog.e(TAG, "Error getting restore data for " + packageName);
9078                    EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
9079                    stage.close();
9080                    downloadFile.delete();
9081                    executeNextState(UnifiedRestoreState.FINAL);
9082                    return;
9083                }
9084
9085                // We have the data from the transport. Now we extract and strip
9086                // any per-package metadata (typically widget-related information)
9087                // if appropriate
9088                if (staging) {
9089                    stage.close();
9090                    stage = ParcelFileDescriptor.open(downloadFile,
9091                            ParcelFileDescriptor.MODE_READ_ONLY);
9092
9093                    mBackupData = ParcelFileDescriptor.open(mBackupDataName,
9094                            ParcelFileDescriptor.MODE_READ_WRITE |
9095                            ParcelFileDescriptor.MODE_CREATE |
9096                            ParcelFileDescriptor.MODE_TRUNCATE);
9097
9098                    BackupDataInput in = new BackupDataInput(stage.getFileDescriptor());
9099                    BackupDataOutput out = new BackupDataOutput(mBackupData.getFileDescriptor());
9100                    byte[] buffer = new byte[8192]; // will grow when needed
9101                    while (in.readNextHeader()) {
9102                        final String key = in.getKey();
9103                        final int size = in.getDataSize();
9104
9105                        // is this a special key?
9106                        if (key.equals(KEY_WIDGET_STATE)) {
9107                            if (DEBUG) {
9108                                Slog.i(TAG, "Restoring widget state for " + packageName);
9109                            }
9110                            mWidgetData = new byte[size];
9111                            in.readEntityData(mWidgetData, 0, size);
9112                        } else {
9113                            if (size > buffer.length) {
9114                                buffer = new byte[size];
9115                            }
9116                            in.readEntityData(buffer, 0, size);
9117                            out.writeEntityHeader(key, size);
9118                            out.writeEntityData(buffer, size);
9119                        }
9120                    }
9121
9122                    mBackupData.close();
9123                }
9124
9125                // Okay, we have the data.  Now have the agent do the restore.
9126                stage.close();
9127
9128                mBackupData = ParcelFileDescriptor.open(mBackupDataName,
9129                        ParcelFileDescriptor.MODE_READ_ONLY);
9130
9131                mNewState = ParcelFileDescriptor.open(mNewStateName,
9132                        ParcelFileDescriptor.MODE_READ_WRITE |
9133                        ParcelFileDescriptor.MODE_CREATE |
9134                        ParcelFileDescriptor.MODE_TRUNCATE);
9135
9136                // Kick off the restore, checking for hung agents.  The timeout or
9137                // the operationComplete() callback will schedule the next step,
9138                // so we do not do that here.
9139                prepareOperationTimeout(mEphemeralOpToken, TIMEOUT_RESTORE_INTERVAL,
9140                        this, OP_TYPE_RESTORE_WAIT);
9141                mAgent.doRestore(mBackupData, appVersionCode, mNewState,
9142                        mEphemeralOpToken, mBackupManagerBinder);
9143            } catch (Exception e) {
9144                Slog.e(TAG, "Unable to call app for restore: " + packageName, e);
9145                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
9146                        packageName, e.toString());
9147                keyValueAgentErrorCleanup();    // clears any pending timeout messages as well
9148
9149                // After a restore failure we go back to running the queue.  If there
9150                // are no more packages to be restored that will be handled by the
9151                // next step.
9152                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9153            }
9154        }
9155
9156        // state RESTORE_FULL : restore one package via streaming engine
9157        private void restoreFull() {
9158            // None of this can run on the work looper here, so we spin asynchronous
9159            // work like this:
9160            //
9161            //   StreamFeederThread: read data from mTransport.getNextFullRestoreDataChunk()
9162            //                       write it into the pipe to the engine
9163            //   EngineThread: FullRestoreEngine thread communicating with the target app
9164            //
9165            // When finished, StreamFeederThread executes next state as appropriate on the
9166            // backup looper, and the overall unified restore task resumes
9167            try {
9168                StreamFeederThread feeder = new StreamFeederThread();
9169                if (MORE_DEBUG) {
9170                    Slog.i(TAG, "Spinning threads for stream restore of "
9171                            + mCurrentPackage.packageName);
9172                }
9173                new Thread(feeder, "unified-stream-feeder").start();
9174
9175                // At this point the feeder is responsible for advancing the restore
9176                // state, so we're done here.
9177            } catch (IOException e) {
9178                // Unable to instantiate the feeder thread -- we need to bail on the
9179                // current target.  We haven't asked the transport for data yet, though,
9180                // so we can do that simply by going back to running the restore queue.
9181                Slog.e(TAG, "Unable to construct pipes for stream restore!");
9182                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9183            }
9184        }
9185
9186        // state RESTORE_FINISHED : provide the "no more data" signpost callback at the end
9187        private void restoreFinished() {
9188            if (DEBUG) {
9189                Slog.d(TAG, "restoreFinished packageName=" + mCurrentPackage.packageName);
9190            }
9191            try {
9192                prepareOperationTimeout(mEphemeralOpToken, TIMEOUT_RESTORE_FINISHED_INTERVAL, this,
9193                        OP_TYPE_RESTORE_WAIT);
9194                mAgent.doRestoreFinished(mEphemeralOpToken, mBackupManagerBinder);
9195                // If we get this far, the callback or timeout will schedule the
9196                // next restore state, so we're done
9197            } catch (Exception e) {
9198                final String packageName = mCurrentPackage.packageName;
9199                Slog.e(TAG, "Unable to finalize restore of " + packageName);
9200                EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
9201                        packageName, e.toString());
9202                keyValueAgentErrorCleanup();
9203                executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9204            }
9205        }
9206
9207        class StreamFeederThread extends RestoreEngine implements Runnable, BackupRestoreTask {
9208            final String TAG = "StreamFeederThread";
9209            FullRestoreEngine mEngine;
9210            EngineThread mEngineThread;
9211
9212            // pipe through which we read data from the transport. [0] read, [1] write
9213            ParcelFileDescriptor[] mTransportPipes;
9214
9215            // pipe through which the engine will read data.  [0] read, [1] write
9216            ParcelFileDescriptor[] mEnginePipes;
9217
9218            private final int mEphemeralOpToken;
9219
9220            public StreamFeederThread() throws IOException {
9221                mEphemeralOpToken = generateRandomIntegerToken();
9222                mTransportPipes = ParcelFileDescriptor.createPipe();
9223                mEnginePipes = ParcelFileDescriptor.createPipe();
9224                setRunning(true);
9225            }
9226
9227            @Override
9228            public void run() {
9229                UnifiedRestoreState nextState = UnifiedRestoreState.RUNNING_QUEUE;
9230                int status = BackupTransport.TRANSPORT_OK;
9231
9232                EventLog.writeEvent(EventLogTags.FULL_RESTORE_PACKAGE,
9233                        mCurrentPackage.packageName);
9234
9235                mEngine = new FullRestoreEngine(this, null, mMonitor, mCurrentPackage, false, false, mEphemeralOpToken);
9236                mEngineThread = new EngineThread(mEngine, mEnginePipes[0]);
9237
9238                ParcelFileDescriptor eWriteEnd = mEnginePipes[1];
9239                ParcelFileDescriptor tReadEnd = mTransportPipes[0];
9240                ParcelFileDescriptor tWriteEnd = mTransportPipes[1];
9241
9242                int bufferSize = 32 * 1024;
9243                byte[] buffer = new byte[bufferSize];
9244                FileOutputStream engineOut = new FileOutputStream(eWriteEnd.getFileDescriptor());
9245                FileInputStream transportIn = new FileInputStream(tReadEnd.getFileDescriptor());
9246
9247                // spin up the engine and start moving data to it
9248                new Thread(mEngineThread, "unified-restore-engine").start();
9249
9250                try {
9251                    while (status == BackupTransport.TRANSPORT_OK) {
9252                        // have the transport write some of the restoring data to us
9253                        int result = mTransport.getNextFullRestoreDataChunk(tWriteEnd);
9254                        if (result > 0) {
9255                            // The transport wrote this many bytes of restore data to the
9256                            // pipe, so pass it along to the engine.
9257                            if (MORE_DEBUG) {
9258                                Slog.v(TAG, "  <- transport provided chunk size " + result);
9259                            }
9260                            if (result > bufferSize) {
9261                                bufferSize = result;
9262                                buffer = new byte[bufferSize];
9263                            }
9264                            int toCopy = result;
9265                            while (toCopy > 0) {
9266                                int n = transportIn.read(buffer, 0, toCopy);
9267                                engineOut.write(buffer, 0, n);
9268                                toCopy -= n;
9269                                if (MORE_DEBUG) {
9270                                    Slog.v(TAG, "  -> wrote " + n + " to engine, left=" + toCopy);
9271                                }
9272                            }
9273                        } else if (result == BackupTransport.NO_MORE_DATA) {
9274                            // Clean finish.  Wind up and we're done!
9275                            if (MORE_DEBUG) {
9276                                Slog.i(TAG, "Got clean full-restore EOF for "
9277                                        + mCurrentPackage.packageName);
9278                            }
9279                            status = BackupTransport.TRANSPORT_OK;
9280                            break;
9281                        } else {
9282                            // Transport reported some sort of failure; the fall-through
9283                            // handling will deal properly with that.
9284                            Slog.e(TAG, "Error " + result + " streaming restore for "
9285                                    + mCurrentPackage.packageName);
9286                            EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
9287                            status = result;
9288                        }
9289                    }
9290                    if (MORE_DEBUG) Slog.v(TAG, "Done copying to engine, falling through");
9291                } catch (IOException e) {
9292                    // We lost our ability to communicate via the pipes.  That's worrying
9293                    // but potentially recoverable; abandon this package's restore but
9294                    // carry on with the next restore target.
9295                    Slog.e(TAG, "Unable to route data for restore");
9296                    EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
9297                            mCurrentPackage.packageName, "I/O error on pipes");
9298                    status = BackupTransport.AGENT_ERROR;
9299                } catch (Exception e) {
9300                    // The transport threw; terminate the whole operation.  Closing
9301                    // the sockets will wake up the engine and it will then tidy up the
9302                    // remote end.
9303                    Slog.e(TAG, "Transport failed during restore: " + e.getMessage());
9304                    EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
9305                    status = BackupTransport.TRANSPORT_ERROR;
9306                } finally {
9307                    // Close the transport pipes and *our* end of the engine pipe,
9308                    // but leave the engine thread's end open so that it properly
9309                    // hits EOF and winds up its operations.
9310                    IoUtils.closeQuietly(mEnginePipes[1]);
9311                    IoUtils.closeQuietly(mTransportPipes[0]);
9312                    IoUtils.closeQuietly(mTransportPipes[1]);
9313
9314                    // Don't proceed until the engine has wound up operations
9315                    mEngineThread.waitForResult();
9316
9317                    // Now we're really done with this one too
9318                    IoUtils.closeQuietly(mEnginePipes[0]);
9319
9320                    // In all cases we want to remember whether we launched
9321                    // the target app as part of our work so far.
9322                    mDidLaunch = (mEngine.getAgent() != null);
9323
9324                    // If we hit a transport-level error, we are done with everything;
9325                    // if we hit an agent error we just go back to running the queue.
9326                    if (status == BackupTransport.TRANSPORT_OK) {
9327                        // Clean finish means we issue the restore-finished callback
9328                        nextState = UnifiedRestoreState.RESTORE_FINISHED;
9329
9330                        // the engine bound the target's agent, so recover that binding
9331                        // to use for the callback.
9332                        mAgent = mEngine.getAgent();
9333
9334                        // and the restored widget data, if any
9335                        mWidgetData = mEngine.getWidgetData();
9336                    } else {
9337                        // Something went wrong somewhere.  Whether it was at the transport
9338                        // level is immaterial; we need to tell the transport to bail
9339                        try {
9340                            mTransport.abortFullRestore();
9341                        } catch (Exception e) {
9342                            // transport itself is dead; make sure we handle this as a
9343                            // fatal error
9344                            Slog.e(TAG, "Transport threw from abortFullRestore: " + e.getMessage());
9345                            status = BackupTransport.TRANSPORT_ERROR;
9346                        }
9347
9348                        // We also need to wipe the current target's data, as it's probably
9349                        // in an incoherent state.
9350                        clearApplicationDataSynchronous(mCurrentPackage.packageName);
9351
9352                        // Schedule the next state based on the nature of our failure
9353                        if (status == BackupTransport.TRANSPORT_ERROR) {
9354                            nextState = UnifiedRestoreState.FINAL;
9355                        } else {
9356                            nextState = UnifiedRestoreState.RUNNING_QUEUE;
9357                        }
9358                    }
9359                    executeNextState(nextState);
9360                    setRunning(false);
9361                }
9362            }
9363
9364            // BackupRestoreTask interface, specifically for timeout handling
9365
9366            @Override
9367            public void execute() { /* intentionally empty */ }
9368
9369            @Override
9370            public void operationComplete(long result) { /* intentionally empty */ }
9371
9372            // The app has timed out handling a restoring file
9373            @Override
9374            public void handleCancel(boolean cancelAll) {
9375                removeOperation(mEphemeralOpToken);
9376                if (DEBUG) {
9377                    Slog.w(TAG, "Full-data restore target timed out; shutting down");
9378                }
9379
9380                mMonitor = monitorEvent(mMonitor,
9381                        BackupManagerMonitor.LOG_EVENT_ID_FULL_RESTORE_TIMEOUT,
9382                        mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
9383                mEngineThread.handleTimeout();
9384
9385                IoUtils.closeQuietly(mEnginePipes[1]);
9386                mEnginePipes[1] = null;
9387                IoUtils.closeQuietly(mEnginePipes[0]);
9388                mEnginePipes[0] = null;
9389            }
9390        }
9391
9392        class EngineThread implements Runnable {
9393            FullRestoreEngine mEngine;
9394            FileInputStream mEngineStream;
9395
9396            EngineThread(FullRestoreEngine engine, ParcelFileDescriptor engineSocket) {
9397                mEngine = engine;
9398                engine.setRunning(true);
9399                // We *do* want this FileInputStream to own the underlying fd, so that
9400                // when we are finished with it, it closes this end of the pipe in a way
9401                // that signals its other end.
9402                mEngineStream = new FileInputStream(engineSocket.getFileDescriptor(), true);
9403            }
9404
9405            public boolean isRunning() {
9406                return mEngine.isRunning();
9407            }
9408
9409            public int waitForResult() {
9410                return mEngine.waitForResult();
9411            }
9412
9413            @Override
9414            public void run() {
9415                try {
9416                    while (mEngine.isRunning()) {
9417                        // Tell it to be sure to leave the agent instance up after finishing
9418                        mEngine.restoreOneFile(mEngineStream, false);
9419                    }
9420                } finally {
9421                    // Because mEngineStream adopted its underlying FD, this also
9422                    // closes this end of the pipe.
9423                    IoUtils.closeQuietly(mEngineStream);
9424                }
9425            }
9426
9427            public void handleTimeout() {
9428                IoUtils.closeQuietly(mEngineStream);
9429                mEngine.handleTimeout();
9430            }
9431        }
9432
9433        // state FINAL : tear everything down and we're done.
9434        private void finalizeRestore() {
9435            if (MORE_DEBUG) Slog.d(TAG, "finishing restore mObserver=" + mObserver);
9436
9437            try {
9438                mTransport.finishRestore();
9439            } catch (Exception e) {
9440                Slog.e(TAG, "Error finishing restore", e);
9441            }
9442
9443            // Tell the observer we're done
9444            if (mObserver != null) {
9445                try {
9446                    mObserver.restoreFinished(mStatus);
9447                } catch (RemoteException e) {
9448                    Slog.d(TAG, "Restore observer died at restoreFinished");
9449                }
9450            }
9451
9452            // Clear any ongoing session timeout.
9453            mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
9454
9455            // If we have a PM token, we must under all circumstances be sure to
9456            // handshake when we've finished.
9457            if (mPmToken > 0) {
9458                if (MORE_DEBUG) Slog.v(TAG, "finishing PM token " + mPmToken);
9459                try {
9460                    mPackageManagerBinder.finishPackageInstall(mPmToken, mDidLaunch);
9461                } catch (RemoteException e) { /* can't happen */ }
9462            } else {
9463                // We were invoked via an active restore session, not by the Package
9464                // Manager, so start up the session timeout again.
9465                mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_SESSION_TIMEOUT,
9466                        TIMEOUT_RESTORE_INTERVAL);
9467            }
9468
9469            // Kick off any work that may be needed regarding app widget restores
9470            // TODO: http://b/22388012
9471            AppWidgetBackupBridge.restoreFinished(UserHandle.USER_SYSTEM);
9472
9473            // If this was a full-system restore, record the ancestral
9474            // dataset information
9475            if (mIsSystemRestore && mPmAgent != null) {
9476                mAncestralPackages = mPmAgent.getRestoredPackages();
9477                mAncestralToken = mToken;
9478                writeRestoreTokens();
9479            }
9480
9481            // done; we can finally release the wakelock and be legitimately done.
9482            Slog.i(TAG, "Restore complete.");
9483
9484            synchronized (mPendingRestores) {
9485                if (mPendingRestores.size() > 0) {
9486                    if (DEBUG) {
9487                        Slog.d(TAG, "Starting next pending restore.");
9488                    }
9489                    PerformUnifiedRestoreTask task = mPendingRestores.remove();
9490                    mBackupHandler.sendMessage(
9491                            mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, task));
9492
9493                } else {
9494                    mIsRestoreInProgress = false;
9495                    if (MORE_DEBUG) {
9496                        Slog.d(TAG, "No pending restores.");
9497                    }
9498                }
9499            }
9500
9501            mWakelock.release();
9502        }
9503
9504        void keyValueAgentErrorCleanup() {
9505            // If the agent fails restore, it might have put the app's data
9506            // into an incoherent state.  For consistency we wipe its data
9507            // again in this case before continuing with normal teardown
9508            clearApplicationDataSynchronous(mCurrentPackage.packageName);
9509            keyValueAgentCleanup();
9510        }
9511
9512        // TODO: clean up naming; this is now used at finish by both k/v and stream restores
9513        void keyValueAgentCleanup() {
9514            mBackupDataName.delete();
9515            mStageName.delete();
9516            try { if (mBackupData != null) mBackupData.close(); } catch (IOException e) {}
9517            try { if (mNewState != null) mNewState.close(); } catch (IOException e) {}
9518            mBackupData = mNewState = null;
9519
9520            // if everything went okay, remember the recorded state now
9521            //
9522            // !!! TODO: the restored data could be migrated on the server
9523            // side into the current dataset.  In that case the new state file
9524            // we just created would reflect the data already extant in the
9525            // backend, so there'd be nothing more to do.  Until that happens,
9526            // however, we need to make sure that we record the data to the
9527            // current backend dataset.  (Yes, this means shipping the data over
9528            // the wire in both directions.  That's bad, but consistency comes
9529            // first, then efficiency.)  Once we introduce server-side data
9530            // migration to the newly-restored device's dataset, we will change
9531            // the following from a discard of the newly-written state to the
9532            // "correct" operation of renaming into the canonical state blob.
9533            mNewStateName.delete();                      // TODO: remove; see above comment
9534            //mNewStateName.renameTo(mSavedStateName);   // TODO: replace with this
9535
9536            // If this wasn't the PM pseudopackage, tear down the agent side
9537            if (mCurrentPackage.applicationInfo != null) {
9538                // unbind and tidy up even on timeout or failure
9539                try {
9540                    mActivityManager.unbindBackupAgent(mCurrentPackage.applicationInfo);
9541
9542                    // The agent was probably running with a stub Application object,
9543                    // which isn't a valid run mode for the main app logic.  Shut
9544                    // down the app so that next time it's launched, it gets the
9545                    // usual full initialization.  Note that this is only done for
9546                    // full-system restores: when a single app has requested a restore,
9547                    // it is explicitly not killed following that operation.
9548                    //
9549                    // We execute this kill when these conditions hold:
9550                    //    1. it's not a system-uid process,
9551                    //    2. the app did not request its own restore (mTargetPackage == null), and either
9552                    //    3a. the app is a full-data target (TYPE_FULL_STREAM) or
9553                    //     b. the app does not state android:killAfterRestore="false" in its manifest
9554                    final int appFlags = mCurrentPackage.applicationInfo.flags;
9555                    final boolean killAfterRestore =
9556                            (mCurrentPackage.applicationInfo.uid >= Process.FIRST_APPLICATION_UID)
9557                            && ((mRestoreDescription.getDataType() == RestoreDescription.TYPE_FULL_STREAM)
9558                                    || ((appFlags & ApplicationInfo.FLAG_KILL_AFTER_RESTORE) != 0));
9559
9560                    if (mTargetPackage == null && killAfterRestore) {
9561                        if (DEBUG) Slog.d(TAG, "Restore complete, killing host process of "
9562                                + mCurrentPackage.applicationInfo.processName);
9563                        mActivityManager.killApplicationProcess(
9564                                mCurrentPackage.applicationInfo.processName,
9565                                mCurrentPackage.applicationInfo.uid);
9566                    }
9567                } catch (RemoteException e) {
9568                    // can't happen; we run in the same process as the activity manager
9569                }
9570            }
9571
9572            // The caller is responsible for reestablishing the state machine; our
9573            // responsibility here is to clear the decks for whatever comes next.
9574            mBackupHandler.removeMessages(MSG_RESTORE_OPERATION_TIMEOUT, this);
9575        }
9576
9577        @Override
9578        public void operationComplete(long unusedResult) {
9579            removeOperation(mEphemeralOpToken);
9580            if (MORE_DEBUG) {
9581                Slog.i(TAG, "operationComplete() during restore: target="
9582                        + mCurrentPackage.packageName
9583                        + " state=" + mState);
9584            }
9585
9586            final UnifiedRestoreState nextState;
9587            switch (mState) {
9588                case INITIAL:
9589                    // We've just (manually) restored the PMBA.  It doesn't need the
9590                    // additional restore-finished callback so we bypass that and go
9591                    // directly to running the queue.
9592                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
9593                    break;
9594
9595                case RESTORE_KEYVALUE:
9596                case RESTORE_FULL: {
9597                    // Okay, we've just heard back from the agent that it's done with
9598                    // the restore itself.  We now have to send the same agent its
9599                    // doRestoreFinished() callback, so roll into that state.
9600                    nextState = UnifiedRestoreState.RESTORE_FINISHED;
9601                    break;
9602                }
9603
9604                case RESTORE_FINISHED: {
9605                    // Okay, we're done with this package.  Tidy up and go on to the next
9606                    // app in the queue.
9607                    int size = (int) mBackupDataName.length();
9608                    EventLog.writeEvent(EventLogTags.RESTORE_PACKAGE,
9609                            mCurrentPackage.packageName, size);
9610
9611                    // Just go back to running the restore queue
9612                    keyValueAgentCleanup();
9613
9614                    // If there was widget state associated with this app, get the OS to
9615                    // incorporate it into current bookeeping and then pass that along to
9616                    // the app as part of the restore-time work.
9617                    if (mWidgetData != null) {
9618                        restoreWidgetData(mCurrentPackage.packageName, mWidgetData);
9619                    }
9620
9621                    nextState = UnifiedRestoreState.RUNNING_QUEUE;
9622                    break;
9623                }
9624
9625                default: {
9626                    // Some kind of horrible semantic error; we're in an unexpected state.
9627                    // Back off hard and wind up.
9628                    Slog.e(TAG, "Unexpected restore callback into state " + mState);
9629                    keyValueAgentErrorCleanup();
9630                    nextState = UnifiedRestoreState.FINAL;
9631                    break;
9632                }
9633            }
9634
9635            executeNextState(nextState);
9636        }
9637
9638        // A call to agent.doRestore() or agent.doRestoreFinished() has timed out
9639        @Override
9640        public void handleCancel(boolean cancelAll) {
9641            removeOperation(mEphemeralOpToken);
9642            Slog.e(TAG, "Timeout restoring application " + mCurrentPackage.packageName);
9643            mMonitor = monitorEvent(mMonitor,
9644                    BackupManagerMonitor.LOG_EVENT_ID_KEY_VALUE_RESTORE_TIMEOUT,
9645                    mCurrentPackage, BackupManagerMonitor.LOG_EVENT_CATEGORY_AGENT, null);
9646            EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
9647                    mCurrentPackage.packageName, "restore timeout");
9648            // Handle like an agent that threw on invocation: wipe it and go on to the next
9649            keyValueAgentErrorCleanup();
9650            executeNextState(UnifiedRestoreState.RUNNING_QUEUE);
9651        }
9652
9653        void executeNextState(UnifiedRestoreState nextState) {
9654            if (MORE_DEBUG) Slog.i(TAG, " => executing next step on "
9655                    + this + " nextState=" + nextState);
9656            mState = nextState;
9657            Message msg = mBackupHandler.obtainMessage(MSG_BACKUP_RESTORE_STEP, this);
9658            mBackupHandler.sendMessage(msg);
9659        }
9660
9661        // restore observer support
9662        void sendStartRestore(int numPackages) {
9663            if (mObserver != null) {
9664                try {
9665                    mObserver.restoreStarting(numPackages);
9666                } catch (RemoteException e) {
9667                    Slog.w(TAG, "Restore observer went away: startRestore");
9668                    mObserver = null;
9669                }
9670            }
9671        }
9672
9673        void sendOnRestorePackage(String name) {
9674            if (mObserver != null) {
9675                if (mObserver != null) {
9676                    try {
9677                        mObserver.onUpdate(mCount, name);
9678                    } catch (RemoteException e) {
9679                        Slog.d(TAG, "Restore observer died in onUpdate");
9680                        mObserver = null;
9681                    }
9682                }
9683            }
9684        }
9685
9686        void sendEndRestore() {
9687            if (mObserver != null) {
9688                try {
9689                    mObserver.restoreFinished(mStatus);
9690                } catch (RemoteException e) {
9691                    Slog.w(TAG, "Restore observer went away: endRestore");
9692                    mObserver = null;
9693                }
9694            }
9695        }
9696    }
9697
9698    class PerformClearTask implements Runnable {
9699        IBackupTransport mTransport;
9700        PackageInfo mPackage;
9701
9702        PerformClearTask(IBackupTransport transport, PackageInfo packageInfo) {
9703            mTransport = transport;
9704            mPackage = packageInfo;
9705        }
9706
9707        public void run() {
9708            try {
9709                // Clear the on-device backup state to ensure a full backup next time
9710                File stateDir = new File(mBaseStateDir, mTransport.transportDirName());
9711                File stateFile = new File(stateDir, mPackage.packageName);
9712                stateFile.delete();
9713
9714                // Tell the transport to remove all the persistent storage for the app
9715                // TODO - need to handle failures
9716                mTransport.clearBackupData(mPackage);
9717            } catch (Exception e) {
9718                Slog.e(TAG, "Transport threw clearing data for " + mPackage + ": " + e.getMessage());
9719            } finally {
9720                try {
9721                    // TODO - need to handle failures
9722                    mTransport.finishBackup();
9723                } catch (Exception e) {
9724                    // Nothing we can do here, alas
9725                    Slog.e(TAG, "Unable to mark clear operation finished: " + e.getMessage());
9726                }
9727
9728                // Last but not least, release the cpu
9729                mWakelock.release();
9730            }
9731        }
9732    }
9733
9734    class PerformInitializeTask implements Runnable {
9735        HashSet<String> mQueue;
9736
9737        PerformInitializeTask(HashSet<String> transportNames) {
9738            mQueue = transportNames;
9739        }
9740
9741        public void run() {
9742            try {
9743                for (String transportName : mQueue) {
9744                    IBackupTransport transport =
9745                            mTransportManager.getTransportBinder(transportName);
9746                    if (transport == null) {
9747                        Slog.e(TAG, "Requested init for " + transportName + " but not found");
9748                        continue;
9749                    }
9750
9751                    Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName);
9752                    EventLog.writeEvent(EventLogTags.BACKUP_START, transport.transportDirName());
9753                    long startRealtime = SystemClock.elapsedRealtime();
9754                    int status = transport.initializeDevice();
9755
9756                    if (status == BackupTransport.TRANSPORT_OK) {
9757                        status = transport.finishBackup();
9758                    }
9759
9760                    // Okay, the wipe really happened.  Clean up our local bookkeeping.
9761                    if (status == BackupTransport.TRANSPORT_OK) {
9762                        Slog.i(TAG, "Device init successful");
9763                        int millis = (int) (SystemClock.elapsedRealtime() - startRealtime);
9764                        EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE);
9765                        resetBackupState(new File(mBaseStateDir, transport.transportDirName()));
9766                        EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis);
9767                        synchronized (mQueueLock) {
9768                            recordInitPendingLocked(false, transportName);
9769                        }
9770                    } else {
9771                        // If this didn't work, requeue this one and try again
9772                        // after a suitable interval
9773                        Slog.e(TAG, "Transport error in initializeDevice()");
9774                        EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)");
9775                        synchronized (mQueueLock) {
9776                            recordInitPendingLocked(true, transportName);
9777                        }
9778                        // do this via another alarm to make sure of the wakelock states
9779                        long delay = transport.requestBackupTime();
9780                        Slog.w(TAG, "Init failed on " + transportName + " resched in " + delay);
9781                        mAlarmManager.set(AlarmManager.RTC_WAKEUP,
9782                                System.currentTimeMillis() + delay, mRunInitIntent);
9783                    }
9784                }
9785            } catch (Exception e) {
9786                Slog.e(TAG, "Unexpected error performing init", e);
9787            } finally {
9788                // Done; release the wakelock
9789                mWakelock.release();
9790            }
9791        }
9792    }
9793
9794    private void dataChangedImpl(String packageName) {
9795        HashSet<String> targets = dataChangedTargets(packageName);
9796        dataChangedImpl(packageName, targets);
9797    }
9798
9799    private void dataChangedImpl(String packageName, HashSet<String> targets) {
9800        // Record that we need a backup pass for the caller.  Since multiple callers
9801        // may share a uid, we need to note all candidates within that uid and schedule
9802        // a backup pass for each of them.
9803        if (targets == null) {
9804            Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'"
9805                   + " uid=" + Binder.getCallingUid());
9806            return;
9807        }
9808
9809        synchronized (mQueueLock) {
9810            // Note that this client has made data changes that need to be backed up
9811            if (targets.contains(packageName)) {
9812                // Add the caller to the set of pending backups.  If there is
9813                // one already there, then overwrite it, but no harm done.
9814                BackupRequest req = new BackupRequest(packageName);
9815                if (mPendingBackups.put(packageName, req) == null) {
9816                    if (MORE_DEBUG) Slog.d(TAG, "Now staging backup of " + packageName);
9817
9818                    // Journal this request in case of crash.  The put()
9819                    // operation returned null when this package was not already
9820                    // in the set; we want to avoid touching the disk redundantly.
9821                    writeToJournalLocked(packageName);
9822                }
9823            }
9824        }
9825
9826        // ...and schedule a backup pass if necessary
9827        KeyValueBackupJob.schedule(mContext);
9828    }
9829
9830    // Note: packageName is currently unused, but may be in the future
9831    private HashSet<String> dataChangedTargets(String packageName) {
9832        // If the caller does not hold the BACKUP permission, it can only request a
9833        // backup of its own data.
9834        if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
9835                Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
9836            synchronized (mBackupParticipants) {
9837                return mBackupParticipants.get(Binder.getCallingUid());
9838            }
9839        }
9840
9841        // a caller with full permission can ask to back up any participating app
9842        HashSet<String> targets = new HashSet<String>();
9843        if (PACKAGE_MANAGER_SENTINEL.equals(packageName)) {
9844            targets.add(PACKAGE_MANAGER_SENTINEL);
9845        } else {
9846            synchronized (mBackupParticipants) {
9847                int N = mBackupParticipants.size();
9848                for (int i = 0; i < N; i++) {
9849                    HashSet<String> s = mBackupParticipants.valueAt(i);
9850                    if (s != null) {
9851                        targets.addAll(s);
9852                    }
9853                }
9854            }
9855        }
9856        return targets;
9857    }
9858
9859    private void writeToJournalLocked(String str) {
9860        RandomAccessFile out = null;
9861        try {
9862            if (mJournal == null) mJournal = File.createTempFile("journal", null, mJournalDir);
9863            out = new RandomAccessFile(mJournal, "rws");
9864            out.seek(out.length());
9865            out.writeUTF(str);
9866        } catch (IOException e) {
9867            Slog.e(TAG, "Can't write " + str + " to backup journal", e);
9868            mJournal = null;
9869        } finally {
9870            try { if (out != null) out.close(); } catch (IOException e) {}
9871        }
9872    }
9873
9874    // ----- IBackupManager binder interface -----
9875
9876    @Override
9877    public void dataChanged(final String packageName) {
9878        final int callingUserHandle = UserHandle.getCallingUserId();
9879        if (callingUserHandle != UserHandle.USER_SYSTEM) {
9880            // TODO: http://b/22388012
9881            // App is running under a non-owner user profile.  For now, we do not back
9882            // up data from secondary user profiles.
9883            // TODO: backups for all user profiles although don't add backup for profiles
9884            // without adding admin control in DevicePolicyManager.
9885            if (MORE_DEBUG) {
9886                Slog.v(TAG, "dataChanged(" + packageName + ") ignored because it's user "
9887                        + callingUserHandle);
9888            }
9889            return;
9890        }
9891
9892        final HashSet<String> targets = dataChangedTargets(packageName);
9893        if (targets == null) {
9894            Slog.w(TAG, "dataChanged but no participant pkg='" + packageName + "'"
9895                   + " uid=" + Binder.getCallingUid());
9896            return;
9897        }
9898
9899        mBackupHandler.post(new Runnable() {
9900                public void run() {
9901                    dataChangedImpl(packageName, targets);
9902                }
9903            });
9904    }
9905
9906    // Clear the given package's backup data from the current transport
9907    @Override
9908    public void clearBackupData(String transportName, String packageName) {
9909        if (DEBUG) Slog.v(TAG, "clearBackupData() of " + packageName + " on " + transportName);
9910        PackageInfo info;
9911        try {
9912            info = mPackageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
9913        } catch (NameNotFoundException e) {
9914            Slog.d(TAG, "No such package '" + packageName + "' - not clearing backup data");
9915            return;
9916        }
9917
9918        // If the caller does not hold the BACKUP permission, it can only request a
9919        // wipe of its own backed-up data.
9920        HashSet<String> apps;
9921        if ((mContext.checkPermission(android.Manifest.permission.BACKUP, Binder.getCallingPid(),
9922                Binder.getCallingUid())) == PackageManager.PERMISSION_DENIED) {
9923            apps = mBackupParticipants.get(Binder.getCallingUid());
9924        } else {
9925            // a caller with full permission can ask to back up any participating app
9926            // !!! TODO: allow data-clear of ANY app?
9927            if (MORE_DEBUG) Slog.v(TAG, "Privileged caller, allowing clear of other apps");
9928            apps = new HashSet<String>();
9929            int N = mBackupParticipants.size();
9930            for (int i = 0; i < N; i++) {
9931                HashSet<String> s = mBackupParticipants.valueAt(i);
9932                if (s != null) {
9933                    apps.addAll(s);
9934                }
9935            }
9936        }
9937
9938        // Is the given app an available participant?
9939        if (apps.contains(packageName)) {
9940            // found it; fire off the clear request
9941            if (MORE_DEBUG) Slog.v(TAG, "Found the app - running clear process");
9942            mBackupHandler.removeMessages(MSG_RETRY_CLEAR);
9943            synchronized (mQueueLock) {
9944                final IBackupTransport transport =
9945                        mTransportManager.getTransportBinder(transportName);
9946                if (transport == null) {
9947                    // transport is currently unavailable -- make sure to retry
9948                    Message msg = mBackupHandler.obtainMessage(MSG_RETRY_CLEAR,
9949                            new ClearRetryParams(transportName, packageName));
9950                    mBackupHandler.sendMessageDelayed(msg, TRANSPORT_RETRY_INTERVAL);
9951                    return;
9952                }
9953                long oldId = Binder.clearCallingIdentity();
9954                mWakelock.acquire();
9955                Message msg = mBackupHandler.obtainMessage(MSG_RUN_CLEAR,
9956                        new ClearParams(transport, info));
9957                mBackupHandler.sendMessage(msg);
9958                Binder.restoreCallingIdentity(oldId);
9959            }
9960        }
9961    }
9962
9963    // Run a backup pass immediately for any applications that have declared
9964    // that they have pending updates.
9965    @Override
9966    public void backupNow() {
9967        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "backupNow");
9968
9969        final PowerSaveState result =
9970                mPowerManager.getPowerSaveState(ServiceType.KEYVALUE_BACKUP);
9971        if (result.batterySaverEnabled) {
9972            if (DEBUG) Slog.v(TAG, "Not running backup while in battery save mode");
9973            KeyValueBackupJob.schedule(mContext);   // try again in several hours
9974        } else {
9975            if (DEBUG) Slog.v(TAG, "Scheduling immediate backup pass");
9976            synchronized (mQueueLock) {
9977                // Fire the intent that kicks off the whole shebang...
9978                try {
9979                    mRunBackupIntent.send();
9980                } catch (PendingIntent.CanceledException e) {
9981                    // should never happen
9982                    Slog.e(TAG, "run-backup intent cancelled!");
9983                }
9984
9985                // ...and cancel any pending scheduled job, because we've just superseded it
9986                KeyValueBackupJob.cancel(mContext);
9987            }
9988        }
9989    }
9990
9991    boolean deviceIsProvisioned() {
9992        final ContentResolver resolver = mContext.getContentResolver();
9993        return (Settings.Global.getInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0) != 0);
9994    }
9995
9996    // Run a backup pass for the given packages, writing the resulting data stream
9997    // to the supplied file descriptor.  This method is synchronous and does not return
9998    // to the caller until the backup has been completed.
9999    //
10000    // This is the variant used by 'adb backup'; it requires on-screen confirmation
10001    // by the user because it can be used to offload data over untrusted USB.
10002    @Override
10003    public void adbBackup(ParcelFileDescriptor fd, boolean includeApks, boolean includeObbs,
10004            boolean includeShared, boolean doWidgets, boolean doAllApps, boolean includeSystem,
10005            boolean compress, boolean doKeyValue, String[] pkgList) {
10006        mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "adbBackup");
10007
10008        final int callingUserHandle = UserHandle.getCallingUserId();
10009        // TODO: http://b/22388012
10010        if (callingUserHandle != UserHandle.USER_SYSTEM) {
10011            throw new IllegalStateException("Backup supported only for the device owner");
10012        }
10013
10014        // Validate
10015        if (!doAllApps) {
10016            if (!includeShared) {
10017                // If we're backing up shared data (sdcard or equivalent), then we can run
10018                // without any supplied app names.  Otherwise, we'd be doing no work, so
10019                // report the error.
10020                if (pkgList == null || pkgList.length == 0) {
10021                    throw new IllegalArgumentException(
10022                            "Backup requested but neither shared nor any apps named");
10023                }
10024            }
10025        }
10026
10027        long oldId = Binder.clearCallingIdentity();
10028        try {
10029            // Doesn't make sense to do a full backup prior to setup
10030            if (!deviceIsProvisioned()) {
10031                Slog.i(TAG, "Backup not supported before setup");
10032                return;
10033            }
10034
10035            if (DEBUG) Slog.v(TAG, "Requesting backup: apks=" + includeApks + " obb=" + includeObbs
10036                    + " shared=" + includeShared + " all=" + doAllApps + " system="
10037                    + includeSystem + " includekeyvalue=" + doKeyValue + " pkgs=" + pkgList);
10038            Slog.i(TAG, "Beginning adb backup...");
10039
10040            AdbBackupParams params = new AdbBackupParams(fd, includeApks, includeObbs,
10041                    includeShared, doWidgets, doAllApps, includeSystem, compress, doKeyValue,
10042                    pkgList);
10043            final int token = generateRandomIntegerToken();
10044            synchronized (mAdbBackupRestoreConfirmations) {
10045                mAdbBackupRestoreConfirmations.put(token, params);
10046            }
10047
10048            // start up the confirmation UI
10049            if (DEBUG) Slog.d(TAG, "Starting backup confirmation UI, token=" + token);
10050            if (!startConfirmationUi(token, FullBackup.FULL_BACKUP_INTENT_ACTION)) {
10051                Slog.e(TAG, "Unable to launch backup confirmation UI");
10052                mAdbBackupRestoreConfirmations.delete(token);
10053                return;
10054            }
10055
10056            // make sure the screen is lit for the user interaction
10057            mPowerManager.userActivity(SystemClock.uptimeMillis(),
10058                    PowerManager.USER_ACTIVITY_EVENT_OTHER,
10059                    0);
10060
10061            // start the confirmation countdown
10062            startConfirmationTimeout(token, params);
10063
10064            // wait for the backup to be performed
10065            if (DEBUG) Slog.d(TAG, "Waiting for backup completion...");
10066            waitForCompletion(params);
10067        } finally {
10068            try {
10069                fd.close();
10070            } catch (IOException e) {
10071                // just eat it
10072            }
10073            Binder.restoreCallingIdentity(oldId);
10074            Slog.d(TAG, "Adb backup processing complete.");
10075        }
10076    }
10077
10078    @Override
10079    public void fullTransportBackup(String[] pkgNames) {
10080        mContext.enforceCallingPermission(android.Manifest.permission.BACKUP,
10081                "fullTransportBackup");
10082
10083        final int callingUserHandle = UserHandle.getCallingUserId();
10084        // TODO: http://b/22388012
10085        if (callingUserHandle != UserHandle.USER_SYSTEM) {
10086            throw new IllegalStateException("Restore supported only for the device owner");
10087        }
10088
10089        if (!fullBackupAllowable(mTransportManager.getCurrentTransportBinder())) {
10090            Slog.i(TAG, "Full backup not currently possible -- key/value backup not yet run?");
10091        } else {
10092            if (DEBUG) {
10093                Slog.d(TAG, "fullTransportBackup()");
10094            }
10095
10096            final long oldId = Binder.clearCallingIdentity();
10097            try {
10098                CountDownLatch latch = new CountDownLatch(1);
10099                PerformFullTransportBackupTask task = new PerformFullTransportBackupTask(null,
10100                        pkgNames, false, null, latch, null, null, false /* userInitiated */);
10101                // Acquiring wakelock for PerformFullTransportBackupTask before its start.
10102                mWakelock.acquire();
10103                (new Thread(task, "full-transport-master")).start();
10104                do {
10105                    try {
10106                        latch.await();
10107                        break;
10108                    } catch (InterruptedException e) {
10109                        // Just go back to waiting for the latch to indicate completion
10110                    }
10111                } while (true);
10112
10113                // We just ran a backup on these packages, so kick them to the end of the queue
10114                final long now = System.currentTimeMillis();
10115                for (String pkg : pkgNames) {
10116                    enqueueFullBackup(pkg, now);
10117                }
10118            } finally {
10119                Binder.restoreCallingIdentity(oldId);
10120            }
10121        }
10122
10123        if (DEBUG) {
10124            Slog.d(TAG, "Done with full transport backup.");
10125        }
10126    }
10127
10128    @Override
10129    public void adbRestore(ParcelFileDescriptor fd) {
10130        mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "adbRestore");
10131
10132        final int callingUserHandle = UserHandle.getCallingUserId();
10133        // TODO: http://b/22388012
10134        if (callingUserHandle != UserHandle.USER_SYSTEM) {
10135            throw new IllegalStateException("Restore supported only for the device owner");
10136        }
10137
10138        long oldId = Binder.clearCallingIdentity();
10139
10140        try {
10141            // Check whether the device has been provisioned -- we don't handle
10142            // full restores prior to completing the setup process.
10143            if (!deviceIsProvisioned()) {
10144                Slog.i(TAG, "Full restore not permitted before setup");
10145                return;
10146            }
10147
10148            Slog.i(TAG, "Beginning restore...");
10149
10150            AdbRestoreParams params = new AdbRestoreParams(fd);
10151            final int token = generateRandomIntegerToken();
10152            synchronized (mAdbBackupRestoreConfirmations) {
10153                mAdbBackupRestoreConfirmations.put(token, params);
10154            }
10155
10156            // start up the confirmation UI
10157            if (DEBUG) Slog.d(TAG, "Starting restore confirmation UI, token=" + token);
10158            if (!startConfirmationUi(token, FullBackup.FULL_RESTORE_INTENT_ACTION)) {
10159                Slog.e(TAG, "Unable to launch restore confirmation");
10160                mAdbBackupRestoreConfirmations.delete(token);
10161                return;
10162            }
10163
10164            // make sure the screen is lit for the user interaction
10165            mPowerManager.userActivity(SystemClock.uptimeMillis(),
10166                    PowerManager.USER_ACTIVITY_EVENT_OTHER,
10167                    0);
10168
10169            // start the confirmation countdown
10170            startConfirmationTimeout(token, params);
10171
10172            // wait for the restore to be performed
10173            if (DEBUG) Slog.d(TAG, "Waiting for restore completion...");
10174            waitForCompletion(params);
10175        } finally {
10176            try {
10177                fd.close();
10178            } catch (IOException e) {
10179                Slog.w(TAG, "Error trying to close fd after adb restore: " + e);
10180            }
10181            Binder.restoreCallingIdentity(oldId);
10182            Slog.i(TAG, "adb restore processing complete.");
10183        }
10184    }
10185
10186    boolean startConfirmationUi(int token, String action) {
10187        try {
10188            Intent confIntent = new Intent(action);
10189            confIntent.setClassName("com.android.backupconfirm",
10190                    "com.android.backupconfirm.BackupRestoreConfirmation");
10191            confIntent.putExtra(FullBackup.CONF_TOKEN_INTENT_EXTRA, token);
10192            confIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10193            mContext.startActivityAsUser(confIntent, UserHandle.SYSTEM);
10194        } catch (ActivityNotFoundException e) {
10195            return false;
10196        }
10197        return true;
10198    }
10199
10200    void startConfirmationTimeout(int token, AdbParams params) {
10201        if (MORE_DEBUG) Slog.d(TAG, "Posting conf timeout msg after "
10202                + TIMEOUT_FULL_CONFIRMATION + " millis");
10203        Message msg = mBackupHandler.obtainMessage(MSG_FULL_CONFIRMATION_TIMEOUT,
10204                token, 0, params);
10205        mBackupHandler.sendMessageDelayed(msg, TIMEOUT_FULL_CONFIRMATION);
10206    }
10207
10208    void waitForCompletion(AdbParams params) {
10209        synchronized (params.latch) {
10210            while (params.latch.get() == false) {
10211                try {
10212                    params.latch.wait();
10213                } catch (InterruptedException e) { /* never interrupted */ }
10214            }
10215        }
10216    }
10217
10218    void signalAdbBackupRestoreCompletion(AdbParams params) {
10219        synchronized (params.latch) {
10220            params.latch.set(true);
10221            params.latch.notifyAll();
10222        }
10223    }
10224
10225    // Confirm that the previously-requested full backup/restore operation can proceed.  This
10226    // is used to require a user-facing disclosure about the operation.
10227    @Override
10228    public void acknowledgeAdbBackupOrRestore(int token, boolean allow,
10229            String curPassword, String encPpassword, IFullBackupRestoreObserver observer) {
10230        if (DEBUG) Slog.d(TAG, "acknowledgeAdbBackupOrRestore : token=" + token
10231                + " allow=" + allow);
10232
10233        // TODO: possibly require not just this signature-only permission, but even
10234        // require that the specific designated confirmation-UI app uid is the caller?
10235        mContext.enforceCallingPermission(android.Manifest.permission.BACKUP, "acknowledgeAdbBackupOrRestore");
10236
10237        long oldId = Binder.clearCallingIdentity();
10238        try {
10239
10240            AdbParams params;
10241            synchronized (mAdbBackupRestoreConfirmations) {
10242                params = mAdbBackupRestoreConfirmations.get(token);
10243                if (params != null) {
10244                    mBackupHandler.removeMessages(MSG_FULL_CONFIRMATION_TIMEOUT, params);
10245                    mAdbBackupRestoreConfirmations.delete(token);
10246
10247                    if (allow) {
10248                        final int verb = params instanceof AdbBackupParams
10249                                ? MSG_RUN_ADB_BACKUP
10250                                : MSG_RUN_ADB_RESTORE;
10251
10252                        params.observer = observer;
10253                        params.curPassword = curPassword;
10254
10255                        params.encryptPassword = encPpassword;
10256
10257                        if (MORE_DEBUG) Slog.d(TAG, "Sending conf message with verb " + verb);
10258                        mWakelock.acquire();
10259                        Message msg = mBackupHandler.obtainMessage(verb, params);
10260                        mBackupHandler.sendMessage(msg);
10261                    } else {
10262                        Slog.w(TAG, "User rejected full backup/restore operation");
10263                        // indicate completion without having actually transferred any data
10264                        signalAdbBackupRestoreCompletion(params);
10265                    }
10266                } else {
10267                    Slog.w(TAG, "Attempted to ack full backup/restore with invalid token");
10268                }
10269            }
10270        } finally {
10271            Binder.restoreCallingIdentity(oldId);
10272        }
10273    }
10274
10275    private static boolean backupSettingMigrated(int userId) {
10276        File base = new File(Environment.getDataDirectory(), "backup");
10277        File enableFile = new File(base, BACKUP_ENABLE_FILE);
10278        return enableFile.exists();
10279    }
10280
10281    private static boolean readBackupEnableState(int userId) {
10282        File base = new File(Environment.getDataDirectory(), "backup");
10283        File enableFile = new File(base, BACKUP_ENABLE_FILE);
10284        if (enableFile.exists()) {
10285            try (FileInputStream fin = new FileInputStream(enableFile)) {
10286                int state = fin.read();
10287                return state != 0;
10288            } catch (IOException e) {
10289                // can't read the file; fall through to assume disabled
10290                Slog.e(TAG, "Cannot read enable state; assuming disabled");
10291            }
10292        } else {
10293            if (DEBUG) {
10294                Slog.i(TAG, "isBackupEnabled() => false due to absent settings file");
10295            }
10296        }
10297        return false;
10298    }
10299
10300    private static void writeBackupEnableState(boolean enable, int userId) {
10301        File base = new File(Environment.getDataDirectory(), "backup");
10302        File enableFile = new File(base, BACKUP_ENABLE_FILE);
10303        File stage = new File(base, BACKUP_ENABLE_FILE + "-stage");
10304        FileOutputStream fout = null;
10305        try {
10306            fout = new FileOutputStream(stage);
10307            fout.write(enable ? 1 : 0);
10308            fout.close();
10309            stage.renameTo(enableFile);
10310            // will be synced immediately by the try-with-resources call to close()
10311        } catch (IOException|RuntimeException e) {
10312            // Whoops; looks like we're doomed.  Roll everything out, disabled,
10313            // including the legacy state.
10314            Slog.e(TAG, "Unable to record backup enable state; reverting to disabled: "
10315                    + e.getMessage());
10316
10317            final ContentResolver r = sInstance.mContext.getContentResolver();
10318            Settings.Secure.putStringForUser(r,
10319                    Settings.Secure.BACKUP_ENABLED, null, userId);
10320            enableFile.delete();
10321            stage.delete();
10322        } finally {
10323            IoUtils.closeQuietly(fout);
10324        }
10325    }
10326
10327    // Enable/disable backups
10328    @Override
10329    public void setBackupEnabled(boolean enable) {
10330        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10331                "setBackupEnabled");
10332
10333        Slog.i(TAG, "Backup enabled => " + enable);
10334
10335        long oldId = Binder.clearCallingIdentity();
10336        try {
10337            boolean wasEnabled = mEnabled;
10338            synchronized (this) {
10339                writeBackupEnableState(enable, UserHandle.USER_SYSTEM);
10340                mEnabled = enable;
10341            }
10342
10343            synchronized (mQueueLock) {
10344                if (enable && !wasEnabled && mProvisioned) {
10345                    // if we've just been enabled, start scheduling backup passes
10346                    KeyValueBackupJob.schedule(mContext);
10347                    scheduleNextFullBackupJob(0);
10348                } else if (!enable) {
10349                    // No longer enabled, so stop running backups
10350                    if (MORE_DEBUG) Slog.i(TAG, "Opting out of backup");
10351
10352                    KeyValueBackupJob.cancel(mContext);
10353
10354                    // This also constitutes an opt-out, so we wipe any data for
10355                    // this device from the backend.  We start that process with
10356                    // an alarm in order to guarantee wakelock states.
10357                    if (wasEnabled && mProvisioned) {
10358                        // NOTE: we currently flush every registered transport, not just
10359                        // the currently-active one.
10360                        String[] allTransports = mTransportManager.getBoundTransportNames();
10361                        // build the set of transports for which we are posting an init
10362                        for (String transport : allTransports) {
10363                            recordInitPendingLocked(true, transport);
10364                        }
10365                        mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
10366                                mRunInitIntent);
10367                    }
10368                }
10369            }
10370        } finally {
10371            Binder.restoreCallingIdentity(oldId);
10372        }
10373    }
10374
10375    // Enable/disable automatic restore of app data at install time
10376    @Override
10377    public void setAutoRestore(boolean doAutoRestore) {
10378        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10379                "setAutoRestore");
10380
10381        Slog.i(TAG, "Auto restore => " + doAutoRestore);
10382
10383        final long oldId = Binder.clearCallingIdentity();
10384        try {
10385            synchronized (this) {
10386                Settings.Secure.putInt(mContext.getContentResolver(),
10387                        Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0);
10388                mAutoRestore = doAutoRestore;
10389            }
10390        } finally {
10391            Binder.restoreCallingIdentity(oldId);
10392        }
10393    }
10394
10395    // Mark the backup service as having been provisioned
10396    @Override
10397    public void setBackupProvisioned(boolean available) {
10398        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10399                "setBackupProvisioned");
10400        /*
10401         * This is now a no-op; provisioning is simply the device's own setup state.
10402         */
10403    }
10404
10405    // Report whether the backup mechanism is currently enabled
10406    @Override
10407    public boolean isBackupEnabled() {
10408        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "isBackupEnabled");
10409        return mEnabled;    // no need to synchronize just to read it
10410    }
10411
10412    // Report the name of the currently active transport
10413    @Override
10414    public String getCurrentTransport() {
10415        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10416                "getCurrentTransport");
10417        String currentTransport = mTransportManager.getCurrentTransportName();
10418        if (MORE_DEBUG) Slog.v(TAG, "... getCurrentTransport() returning " + currentTransport);
10419        return currentTransport;
10420    }
10421
10422    // Report all known, available backup transports
10423    @Override
10424    public String[] listAllTransports() {
10425        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP, "listAllTransports");
10426
10427        return mTransportManager.getBoundTransportNames();
10428    }
10429
10430    @Override
10431    public ComponentName[] listAllTransportComponents() {
10432        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10433                "listAllTransportComponents");
10434        return mTransportManager.getAllTransportCompenents();
10435    }
10436
10437    @Override
10438    public String[] getTransportWhitelist() {
10439        // No permission check, intentionally.
10440        Set<ComponentName> whitelistedComponents = mTransportManager.getTransportWhitelist();
10441        String[] whitelistedTransports = new String[whitelistedComponents.size()];
10442        int i = 0;
10443        for (ComponentName component : whitelistedComponents) {
10444            whitelistedTransports[i] = component.flattenToShortString();
10445            i++;
10446        }
10447        return whitelistedTransports;
10448    }
10449
10450    // Select which transport to use for the next backup operation.
10451    @Override
10452    public String selectBackupTransport(String transport) {
10453        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10454                "selectBackupTransport");
10455
10456        final long oldId = Binder.clearCallingIdentity();
10457        try {
10458            String prevTransport = mTransportManager.selectTransport(transport);
10459            Settings.Secure.putString(mContext.getContentResolver(),
10460                    Settings.Secure.BACKUP_TRANSPORT, transport);
10461            Slog.v(TAG, "selectBackupTransport() set " + mTransportManager.getCurrentTransportName()
10462                    + " returning " + prevTransport);
10463            return prevTransport;
10464        } finally {
10465            Binder.restoreCallingIdentity(oldId);
10466        }
10467    }
10468
10469    @Override
10470    public void selectBackupTransportAsync(final ComponentName transport,
10471            final ISelectBackupTransportCallback listener) {
10472        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10473                "selectBackupTransportAsync");
10474
10475        final long oldId = Binder.clearCallingIdentity();
10476
10477        Slog.v(TAG, "selectBackupTransportAsync() called with transport " +
10478                transport.flattenToShortString());
10479
10480        mTransportManager.ensureTransportReady(transport, new SelectBackupTransportCallback() {
10481            @Override
10482            public void onSuccess(String transportName) {
10483                mTransportManager.selectTransport(transportName);
10484                Settings.Secure.putString(mContext.getContentResolver(),
10485                        Settings.Secure.BACKUP_TRANSPORT,
10486                        mTransportManager.getCurrentTransportName());
10487                Slog.v(TAG, "Transport successfully selected: " + transport.flattenToShortString());
10488                try {
10489                    listener.onSuccess(transportName);
10490                } catch (RemoteException e) {
10491                    // Nothing to do here.
10492                }
10493            }
10494
10495            @Override
10496            public void onFailure(int reason) {
10497                Slog.v(TAG, "Failed to select transport: " + transport.flattenToShortString());
10498                try {
10499                    listener.onFailure(reason);
10500                } catch (RemoteException e) {
10501                    // Nothing to do here.
10502                }
10503            }
10504        });
10505
10506        Binder.restoreCallingIdentity(oldId);
10507    }
10508
10509    // Supply the configuration Intent for the given transport.  If the name is not one
10510    // of the available transports, or if the transport does not supply any configuration
10511    // UI, the method returns null.
10512    @Override
10513    public Intent getConfigurationIntent(String transportName) {
10514        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10515                "getConfigurationIntent");
10516
10517        final IBackupTransport transport = mTransportManager.getTransportBinder(transportName);
10518        if (transport != null) {
10519            try {
10520                final Intent intent = transport.configurationIntent();
10521                if (MORE_DEBUG) Slog.d(TAG, "getConfigurationIntent() returning config intent "
10522                        + intent);
10523                return intent;
10524            } catch (Exception e) {
10525                /* fall through to return null */
10526                Slog.e(TAG, "Unable to get configuration intent from transport: " + e.getMessage());
10527            }
10528        }
10529
10530        return null;
10531    }
10532
10533    // Supply the configuration summary string for the given transport.  If the name is
10534    // not one of the available transports, or if the transport does not supply any
10535    // summary / destination string, the method can return null.
10536    //
10537    // This string is used VERBATIM as the summary text of the relevant Settings item!
10538    @Override
10539    public String getDestinationString(String transportName) {
10540        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10541                "getDestinationString");
10542
10543        final IBackupTransport transport = mTransportManager.getTransportBinder(transportName);
10544        if (transport != null) {
10545            try {
10546                final String text = transport.currentDestinationString();
10547                if (MORE_DEBUG) Slog.d(TAG, "getDestinationString() returning " + text);
10548                return text;
10549            } catch (Exception e) {
10550                /* fall through to return null */
10551                Slog.e(TAG, "Unable to get string from transport: " + e.getMessage());
10552            }
10553        }
10554
10555        return null;
10556    }
10557
10558    // Supply the manage-data intent for the given transport.
10559    @Override
10560    public Intent getDataManagementIntent(String transportName) {
10561        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10562                "getDataManagementIntent");
10563
10564        final IBackupTransport transport = mTransportManager.getTransportBinder(transportName);
10565        if (transport != null) {
10566            try {
10567                final Intent intent = transport.dataManagementIntent();
10568                if (MORE_DEBUG) Slog.d(TAG, "getDataManagementIntent() returning intent "
10569                        + intent);
10570                return intent;
10571            } catch (Exception e) {
10572                /* fall through to return null */
10573                Slog.e(TAG, "Unable to get management intent from transport: " + e.getMessage());
10574            }
10575        }
10576
10577        return null;
10578    }
10579
10580    // Supply the menu label for affordances that fire the manage-data intent
10581    // for the given transport.
10582    @Override
10583    public String getDataManagementLabel(String transportName) {
10584        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10585                "getDataManagementLabel");
10586
10587        final IBackupTransport transport = mTransportManager.getTransportBinder(transportName);
10588        if (transport != null) {
10589            try {
10590                final String text = transport.dataManagementLabel();
10591                if (MORE_DEBUG) Slog.d(TAG, "getDataManagementLabel() returning " + text);
10592                return text;
10593            } catch (Exception e) {
10594                /* fall through to return null */
10595                Slog.e(TAG, "Unable to get management label from transport: " + e.getMessage());
10596            }
10597        }
10598
10599        return null;
10600    }
10601
10602    // Callback: a requested backup agent has been instantiated.  This should only
10603    // be called from the Activity Manager.
10604    @Override
10605    public void agentConnected(String packageName, IBinder agentBinder) {
10606        synchronized(mAgentConnectLock) {
10607            if (Binder.getCallingUid() == Process.SYSTEM_UID) {
10608                Slog.d(TAG, "agentConnected pkg=" + packageName + " agent=" + agentBinder);
10609                IBackupAgent agent = IBackupAgent.Stub.asInterface(agentBinder);
10610                mConnectedAgent = agent;
10611                mConnecting = false;
10612            } else {
10613                Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
10614                        + " claiming agent connected");
10615            }
10616            mAgentConnectLock.notifyAll();
10617        }
10618    }
10619
10620    // Callback: a backup agent has failed to come up, or has unexpectedly quit.
10621    // If the agent failed to come up in the first place, the agentBinder argument
10622    // will be null.  This should only be called from the Activity Manager.
10623    @Override
10624    public void agentDisconnected(String packageName) {
10625        // TODO: handle backup being interrupted
10626        synchronized(mAgentConnectLock) {
10627            if (Binder.getCallingUid() == Process.SYSTEM_UID) {
10628                mConnectedAgent = null;
10629                mConnecting = false;
10630            } else {
10631                Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
10632                        + " claiming agent disconnected");
10633            }
10634            mAgentConnectLock.notifyAll();
10635        }
10636    }
10637
10638    // An application being installed will need a restore pass, then the Package Manager
10639    // will need to be told when the restore is finished.
10640    @Override
10641    public void restoreAtInstall(String packageName, int token) {
10642        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10643            Slog.w(TAG, "Non-system process uid=" + Binder.getCallingUid()
10644                    + " attemping install-time restore");
10645            return;
10646        }
10647
10648        boolean skip = false;
10649
10650        long restoreSet = getAvailableRestoreToken(packageName);
10651        if (DEBUG) Slog.v(TAG, "restoreAtInstall pkg=" + packageName
10652                + " token=" + Integer.toHexString(token)
10653                + " restoreSet=" + Long.toHexString(restoreSet));
10654        if (restoreSet == 0) {
10655            if (MORE_DEBUG) Slog.i(TAG, "No restore set");
10656            skip = true;
10657        }
10658
10659        // Do we have a transport to fetch data for us?
10660        IBackupTransport transport = mTransportManager.getCurrentTransportBinder();
10661        if (transport == null) {
10662            if (DEBUG) Slog.w(TAG, "No transport");
10663            skip = true;
10664        }
10665
10666        if (!mAutoRestore) {
10667            if (DEBUG) {
10668                Slog.w(TAG, "Non-restorable state: auto=" + mAutoRestore);
10669            }
10670            skip = true;
10671        }
10672
10673        if (!skip) {
10674            try {
10675                // okay, we're going to attempt a restore of this package from this restore set.
10676                // The eventual message back into the Package Manager to run the post-install
10677                // steps for 'token' will be issued from the restore handling code.
10678
10679                // This can throw and so *must* happen before the wakelock is acquired
10680                String dirName = transport.transportDirName();
10681
10682                mWakelock.acquire();
10683                if (MORE_DEBUG) {
10684                    Slog.d(TAG, "Restore at install of " + packageName);
10685                }
10686                Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
10687                msg.obj = new RestoreParams(transport, dirName, null, null,
10688                        restoreSet, packageName, token);
10689                mBackupHandler.sendMessage(msg);
10690            } catch (Exception e) {
10691                // Calling into the transport broke; back off and proceed with the installation.
10692                Slog.e(TAG, "Unable to contact transport: " + e.getMessage());
10693                skip = true;
10694            }
10695        }
10696
10697        if (skip) {
10698            // Auto-restore disabled or no way to attempt a restore; just tell the Package
10699            // Manager to proceed with the post-install handling for this package.
10700            if (DEBUG) Slog.v(TAG, "Finishing install immediately");
10701            try {
10702                mPackageManagerBinder.finishPackageInstall(token, false);
10703            } catch (RemoteException e) { /* can't happen */ }
10704        }
10705    }
10706
10707    // Hand off a restore session
10708    @Override
10709    public IRestoreSession beginRestoreSession(String packageName, String transport) {
10710        if (DEBUG) Slog.v(TAG, "beginRestoreSession: pkg=" + packageName
10711                + " transport=" + transport);
10712
10713        boolean needPermission = true;
10714        if (transport == null) {
10715            transport = mTransportManager.getCurrentTransportName();
10716
10717            if (packageName != null) {
10718                PackageInfo app = null;
10719                try {
10720                    app = mPackageManager.getPackageInfo(packageName, 0);
10721                } catch (NameNotFoundException nnf) {
10722                    Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName);
10723                    throw new IllegalArgumentException("Package " + packageName + " not found");
10724                }
10725
10726                if (app.applicationInfo.uid == Binder.getCallingUid()) {
10727                    // So: using the current active transport, and the caller has asked
10728                    // that its own package will be restored.  In this narrow use case
10729                    // we do not require the caller to hold the permission.
10730                    needPermission = false;
10731                }
10732            }
10733        }
10734
10735        if (needPermission) {
10736            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10737                    "beginRestoreSession");
10738        } else {
10739            if (DEBUG) Slog.d(TAG, "restoring self on current transport; no permission needed");
10740        }
10741
10742        synchronized(this) {
10743            if (mActiveRestoreSession != null) {
10744                Slog.i(TAG, "Restore session requested but one already active");
10745                return null;
10746            }
10747            if (mBackupRunning) {
10748                Slog.i(TAG, "Restore session requested but currently running backups");
10749                return null;
10750            }
10751            mActiveRestoreSession = new ActiveRestoreSession(packageName, transport);
10752            mBackupHandler.sendEmptyMessageDelayed(MSG_RESTORE_SESSION_TIMEOUT,
10753                    TIMEOUT_RESTORE_INTERVAL);
10754        }
10755        return mActiveRestoreSession;
10756    }
10757
10758    void clearRestoreSession(ActiveRestoreSession currentSession) {
10759        synchronized(this) {
10760            if (currentSession != mActiveRestoreSession) {
10761                Slog.e(TAG, "ending non-current restore session");
10762            } else {
10763                if (DEBUG) Slog.v(TAG, "Clearing restore session and halting timeout");
10764                mActiveRestoreSession = null;
10765                mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
10766            }
10767        }
10768    }
10769
10770    // Note that a currently-active backup agent has notified us that it has
10771    // completed the given outstanding asynchronous backup/restore operation.
10772    @Override
10773    public void opComplete(int token, long result) {
10774        if (MORE_DEBUG) {
10775            Slog.v(TAG, "opComplete: " + Integer.toHexString(token) + " result=" + result);
10776        }
10777        Operation op = null;
10778        synchronized (mCurrentOpLock) {
10779            op = mCurrentOperations.get(token);
10780            if (op != null) {
10781                if (op.state == OP_TIMEOUT) {
10782                    // The operation already timed out, and this is a late response.  Tidy up
10783                    // and ignore it; we've already dealt with the timeout.
10784                    op = null;
10785                    mCurrentOperations.delete(token);
10786                } else if (op.state == OP_ACKNOWLEDGED) {
10787                    if (DEBUG) {
10788                        Slog.w(TAG, "Received duplicate ack for token=" +
10789                                Integer.toHexString(token));
10790                    }
10791                    op = null;
10792                    mCurrentOperations.remove(token);
10793                } else if (op.state == OP_PENDING) {
10794                    // Can't delete op from mCurrentOperations. waitUntilOperationComplete can be
10795                    // called after we we receive this call.
10796                    op.state = OP_ACKNOWLEDGED;
10797                }
10798            }
10799            mCurrentOpLock.notifyAll();
10800        }
10801
10802        // The completion callback, if any, is invoked on the handler
10803        if (op != null && op.callback != null) {
10804            Pair<BackupRestoreTask, Long> callbackAndResult = Pair.create(op.callback, result);
10805            Message msg = mBackupHandler.obtainMessage(MSG_OP_COMPLETE, callbackAndResult);
10806            mBackupHandler.sendMessage(msg);
10807        }
10808    }
10809
10810    @Override
10811    public boolean isAppEligibleForBackup(String packageName) {
10812        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10813                "isAppEligibleForBackup");
10814        try {
10815            PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName,
10816                    PackageManager.GET_SIGNATURES);
10817            if (!appIsEligibleForBackup(packageInfo.applicationInfo) ||
10818                    appIsStopped(packageInfo.applicationInfo) ||
10819                    appIsDisabled(packageInfo.applicationInfo, mPackageManager)) {
10820                return false;
10821            }
10822            IBackupTransport transport = mTransportManager.getCurrentTransportBinder();
10823            if (transport != null) {
10824                try {
10825                    return transport.isAppEligibleForBackup(packageInfo,
10826                        appGetsFullBackup(packageInfo));
10827                } catch (Exception e) {
10828                    Slog.e(TAG, "Unable to ask about eligibility: " + e.getMessage());
10829                }
10830            }
10831            // If transport is not present we couldn't tell that the package is not eligible.
10832            return true;
10833        } catch (NameNotFoundException e) {
10834            return false;
10835        }
10836    }
10837
10838    // ----- Restore session -----
10839
10840    class ActiveRestoreSession extends IRestoreSession.Stub {
10841        private static final String TAG = "RestoreSession";
10842
10843        private String mPackageName;
10844        private IBackupTransport mRestoreTransport = null;
10845        RestoreSet[] mRestoreSets = null;
10846        boolean mEnded = false;
10847        boolean mTimedOut = false;
10848
10849        ActiveRestoreSession(String packageName, String transport) {
10850            mPackageName = packageName;
10851            mRestoreTransport = mTransportManager.getTransportBinder(transport);
10852        }
10853
10854        public void markTimedOut() {
10855            mTimedOut = true;
10856        }
10857
10858        // --- Binder interface ---
10859        public synchronized int getAvailableRestoreSets(IRestoreObserver observer,
10860                IBackupManagerMonitor monitor) {
10861            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10862                    "getAvailableRestoreSets");
10863            if (observer == null) {
10864                throw new IllegalArgumentException("Observer must not be null");
10865            }
10866
10867            if (mEnded) {
10868                throw new IllegalStateException("Restore session already ended");
10869            }
10870
10871            if (mTimedOut) {
10872                Slog.i(TAG, "Session already timed out");
10873                return -1;
10874            }
10875
10876            long oldId = Binder.clearCallingIdentity();
10877            try {
10878                if (mRestoreTransport == null) {
10879                    Slog.w(TAG, "Null transport getting restore sets");
10880                    return -1;
10881                }
10882
10883                // We know we're doing legit work now, so halt the timeout
10884                // until we're done.  It gets started again when the result
10885                // comes in.
10886                mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
10887
10888                // spin off the transport request to our service thread
10889                mWakelock.acquire();
10890                Message msg = mBackupHandler.obtainMessage(MSG_RUN_GET_RESTORE_SETS,
10891                        new RestoreGetSetsParams(mRestoreTransport, this, observer,
10892                                monitor));
10893                mBackupHandler.sendMessage(msg);
10894                return 0;
10895            } catch (Exception e) {
10896                Slog.e(TAG, "Error in getAvailableRestoreSets", e);
10897                return -1;
10898            } finally {
10899                Binder.restoreCallingIdentity(oldId);
10900            }
10901        }
10902
10903        public synchronized int restoreAll(long token, IRestoreObserver observer,
10904                IBackupManagerMonitor monitor) {
10905            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10906                    "performRestore");
10907
10908            if (DEBUG) Slog.d(TAG, "restoreAll token=" + Long.toHexString(token)
10909                    + " observer=" + observer);
10910
10911            if (mEnded) {
10912                throw new IllegalStateException("Restore session already ended");
10913            }
10914
10915            if (mTimedOut) {
10916                Slog.i(TAG, "Session already timed out");
10917                return -1;
10918            }
10919
10920            if (mRestoreTransport == null || mRestoreSets == null) {
10921                Slog.e(TAG, "Ignoring restoreAll() with no restore set");
10922                return -1;
10923            }
10924
10925            if (mPackageName != null) {
10926                Slog.e(TAG, "Ignoring restoreAll() on single-package session");
10927                return -1;
10928            }
10929
10930            String dirName;
10931            try {
10932                dirName = mRestoreTransport.transportDirName();
10933            } catch (Exception e) {
10934                // Transport went AWOL; fail.
10935                Slog.e(TAG, "Unable to get transport dir for restore: " + e.getMessage());
10936                return -1;
10937            }
10938
10939            synchronized (mQueueLock) {
10940                for (int i = 0; i < mRestoreSets.length; i++) {
10941                    if (token == mRestoreSets[i].token) {
10942                        // Real work, so stop the session timeout until we finalize the restore
10943                        mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
10944
10945                        long oldId = Binder.clearCallingIdentity();
10946                        mWakelock.acquire();
10947                        if (MORE_DEBUG) {
10948                            Slog.d(TAG, "restoreAll() kicking off");
10949                        }
10950                        Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
10951                        msg.obj = new RestoreParams(mRestoreTransport, dirName,
10952                                observer, monitor, token);
10953                        mBackupHandler.sendMessage(msg);
10954                        Binder.restoreCallingIdentity(oldId);
10955                        return 0;
10956                    }
10957                }
10958            }
10959
10960            Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found");
10961            return -1;
10962        }
10963
10964        // Restores of more than a single package are treated as 'system' restores
10965        public synchronized int restoreSome(long token, IRestoreObserver observer,
10966                IBackupManagerMonitor monitor, String[] packages) {
10967            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
10968                    "performRestore");
10969
10970            if (DEBUG) {
10971                StringBuilder b = new StringBuilder(128);
10972                b.append("restoreSome token=");
10973                b.append(Long.toHexString(token));
10974                b.append(" observer=");
10975                b.append(observer.toString());
10976                b.append(" monitor=");
10977                if (monitor == null) {
10978                    b.append("null");
10979                } else {
10980                    b.append(monitor.toString());
10981                }
10982                b.append(" packages=");
10983                if (packages == null) {
10984                    b.append("null");
10985                } else {
10986                    b.append('{');
10987                    boolean first = true;
10988                    for (String s : packages) {
10989                        if (!first) {
10990                            b.append(", ");
10991                        } else first = false;
10992                        b.append(s);
10993                    }
10994                    b.append('}');
10995                }
10996                Slog.d(TAG, b.toString());
10997            }
10998
10999            if (mEnded) {
11000                throw new IllegalStateException("Restore session already ended");
11001            }
11002
11003            if (mTimedOut) {
11004                Slog.i(TAG, "Session already timed out");
11005                return -1;
11006            }
11007
11008            if (mRestoreTransport == null || mRestoreSets == null) {
11009                Slog.e(TAG, "Ignoring restoreAll() with no restore set");
11010                return -1;
11011            }
11012
11013            if (mPackageName != null) {
11014                Slog.e(TAG, "Ignoring restoreAll() on single-package session");
11015                return -1;
11016            }
11017
11018            String dirName;
11019            try {
11020                dirName = mRestoreTransport.transportDirName();
11021            } catch (Exception e) {
11022                // Transport went AWOL; fail.
11023                Slog.e(TAG, "Unable to get transport name for restoreSome: " + e.getMessage());
11024                return -1;
11025            }
11026
11027            synchronized (mQueueLock) {
11028                for (int i = 0; i < mRestoreSets.length; i++) {
11029                    if (token == mRestoreSets[i].token) {
11030                        // Stop the session timeout until we finalize the restore
11031                        mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
11032
11033                        long oldId = Binder.clearCallingIdentity();
11034                        mWakelock.acquire();
11035                        if (MORE_DEBUG) {
11036                            Slog.d(TAG, "restoreSome() of " + packages.length + " packages");
11037                        }
11038                        Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
11039                        msg.obj = new RestoreParams(mRestoreTransport, dirName, observer, monitor,
11040                                token, packages, packages.length > 1);
11041                        mBackupHandler.sendMessage(msg);
11042                        Binder.restoreCallingIdentity(oldId);
11043                        return 0;
11044                    }
11045                }
11046            }
11047
11048            Slog.w(TAG, "Restore token " + Long.toHexString(token) + " not found");
11049            return -1;
11050        }
11051
11052        public synchronized int restorePackage(String packageName, IRestoreObserver observer,
11053                IBackupManagerMonitor monitor) {
11054            if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer
11055                    + "monitor=" + monitor);
11056
11057            if (mEnded) {
11058                throw new IllegalStateException("Restore session already ended");
11059            }
11060
11061            if (mTimedOut) {
11062                Slog.i(TAG, "Session already timed out");
11063                return -1;
11064            }
11065
11066            if (mPackageName != null) {
11067                if (! mPackageName.equals(packageName)) {
11068                    Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName
11069                            + " on session for package " + mPackageName);
11070                    return -1;
11071                }
11072            }
11073
11074            PackageInfo app = null;
11075            try {
11076                app = mPackageManager.getPackageInfo(packageName, 0);
11077            } catch (NameNotFoundException nnf) {
11078                Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName);
11079                return -1;
11080            }
11081
11082            // If the caller is not privileged and is not coming from the target
11083            // app's uid, throw a permission exception back to the caller.
11084            int perm = mContext.checkPermission(android.Manifest.permission.BACKUP,
11085                    Binder.getCallingPid(), Binder.getCallingUid());
11086            if ((perm == PackageManager.PERMISSION_DENIED) &&
11087                    (app.applicationInfo.uid != Binder.getCallingUid())) {
11088                Slog.w(TAG, "restorePackage: bad packageName=" + packageName
11089                        + " or calling uid=" + Binder.getCallingUid());
11090                throw new SecurityException("No permission to restore other packages");
11091            }
11092
11093            // So far so good; we're allowed to try to restore this package.
11094            long oldId = Binder.clearCallingIdentity();
11095            try {
11096                // Check whether there is data for it in the current dataset, falling back
11097                // to the ancestral dataset if not.
11098                long token = getAvailableRestoreToken(packageName);
11099                if (DEBUG) Slog.v(TAG, "restorePackage pkg=" + packageName
11100                        + " token=" + Long.toHexString(token));
11101
11102                // If we didn't come up with a place to look -- no ancestral dataset and
11103                // the app has never been backed up from this device -- there's nothing
11104                // to do but return failure.
11105                if (token == 0) {
11106                    if (DEBUG) Slog.w(TAG, "No data available for this package; not restoring");
11107                    return -1;
11108                }
11109
11110                String dirName;
11111                try {
11112                    dirName = mRestoreTransport.transportDirName();
11113                } catch (Exception e) {
11114                    // Transport went AWOL; fail.
11115                    Slog.e(TAG, "Unable to get transport dir for restorePackage: " + e.getMessage());
11116                    return -1;
11117                }
11118
11119                // Stop the session timeout until we finalize the restore
11120                mBackupHandler.removeMessages(MSG_RESTORE_SESSION_TIMEOUT);
11121
11122                // Ready to go:  enqueue the restore request and claim success
11123                mWakelock.acquire();
11124                if (MORE_DEBUG) {
11125                    Slog.d(TAG, "restorePackage() : " + packageName);
11126                }
11127                Message msg = mBackupHandler.obtainMessage(MSG_RUN_RESTORE);
11128                msg.obj = new RestoreParams(mRestoreTransport, dirName, observer, monitor,
11129                        token, app);
11130                mBackupHandler.sendMessage(msg);
11131            } finally {
11132                Binder.restoreCallingIdentity(oldId);
11133            }
11134            return 0;
11135        }
11136
11137        // Posted to the handler to tear down a restore session in a cleanly synchronized way
11138        class EndRestoreRunnable implements Runnable {
11139            BackupManagerService mBackupManager;
11140            ActiveRestoreSession mSession;
11141
11142            EndRestoreRunnable(BackupManagerService manager, ActiveRestoreSession session) {
11143                mBackupManager = manager;
11144                mSession = session;
11145            }
11146
11147            public void run() {
11148                // clean up the session's bookkeeping
11149                synchronized (mSession) {
11150                    mSession.mRestoreTransport = null;
11151                    mSession.mEnded = true;
11152                }
11153
11154                // clean up the BackupManagerImpl side of the bookkeeping
11155                // and cancel any pending timeout message
11156                mBackupManager.clearRestoreSession(mSession);
11157            }
11158        }
11159
11160        public synchronized void endRestoreSession() {
11161            if (DEBUG) Slog.d(TAG, "endRestoreSession");
11162
11163            if (mTimedOut) {
11164                Slog.i(TAG, "Session already timed out");
11165                return;
11166            }
11167
11168            if (mEnded) {
11169                throw new IllegalStateException("Restore session already ended");
11170            }
11171
11172            mBackupHandler.post(new EndRestoreRunnable(BackupManagerService.this, this));
11173        }
11174    }
11175
11176    @Override
11177    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
11178        if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return;
11179
11180        long identityToken = Binder.clearCallingIdentity();
11181        try {
11182            if (args != null) {
11183                for (String arg : args) {
11184                    if ("-h".equals(arg)) {
11185                        pw.println("'dumpsys backup' optional arguments:");
11186                        pw.println("  -h       : this help text");
11187                        pw.println("  a[gents] : dump information about defined backup agents");
11188                        return;
11189                    } else if ("agents".startsWith(arg)) {
11190                        dumpAgents(pw);
11191                        return;
11192                    }
11193                }
11194            }
11195            dumpInternal(pw);
11196        } finally {
11197            Binder.restoreCallingIdentity(identityToken);
11198        }
11199    }
11200
11201    private void dumpAgents(PrintWriter pw) {
11202        List<PackageInfo> agentPackages = allAgentPackages();
11203        pw.println("Defined backup agents:");
11204        for (PackageInfo pkg : agentPackages) {
11205            pw.print("  ");
11206            pw.print(pkg.packageName); pw.println(':');
11207            pw.print("      "); pw.println(pkg.applicationInfo.backupAgentName);
11208        }
11209    }
11210
11211    private void dumpInternal(PrintWriter pw) {
11212        synchronized (mQueueLock) {
11213            pw.println("Backup Manager is " + (mEnabled ? "enabled" : "disabled")
11214                    + " / " + (!mProvisioned ? "not " : "") + "provisioned / "
11215                    + (this.mPendingInits.size() == 0 ? "not " : "") + "pending init");
11216            pw.println("Auto-restore is " + (mAutoRestore ? "enabled" : "disabled"));
11217            if (mBackupRunning) pw.println("Backup currently running");
11218            pw.println("Last backup pass started: " + mLastBackupPass
11219                    + " (now = " + System.currentTimeMillis() + ')');
11220            pw.println("  next scheduled: " + KeyValueBackupJob.nextScheduled());
11221
11222            pw.println("Transport whitelist:");
11223            for (ComponentName transport : mTransportManager.getTransportWhitelist()) {
11224                pw.print("    ");
11225                pw.println(transport.flattenToShortString());
11226            }
11227
11228            pw.println("Available transports:");
11229            final String[] transports = listAllTransports();
11230            if (transports != null) {
11231                for (String t : listAllTransports()) {
11232                    pw.println((t.equals(mTransportManager.getCurrentTransportName()) ? "  * " : "    ") + t);
11233                    try {
11234                        IBackupTransport transport = mTransportManager.getTransportBinder(t);
11235                        File dir = new File(mBaseStateDir, transport.transportDirName());
11236                        pw.println("       destination: " + transport.currentDestinationString());
11237                        pw.println("       intent: " + transport.configurationIntent());
11238                        for (File f : dir.listFiles()) {
11239                            pw.println("       " + f.getName() + " - " + f.length() + " state bytes");
11240                        }
11241                    } catch (Exception e) {
11242                        Slog.e(TAG, "Error in transport", e);
11243                        pw.println("        Error: " + e);
11244                    }
11245                }
11246            }
11247
11248            pw.println("Pending init: " + mPendingInits.size());
11249            for (String s : mPendingInits) {
11250                pw.println("    " + s);
11251            }
11252
11253            if (DEBUG_BACKUP_TRACE) {
11254                synchronized (mBackupTrace) {
11255                    if (!mBackupTrace.isEmpty()) {
11256                        pw.println("Most recent backup trace:");
11257                        for (String s : mBackupTrace) {
11258                            pw.println("   " + s);
11259                        }
11260                    }
11261                }
11262            }
11263
11264            pw.print("Ancestral: "); pw.println(Long.toHexString(mAncestralToken));
11265            pw.print("Current:   "); pw.println(Long.toHexString(mCurrentToken));
11266
11267            int N = mBackupParticipants.size();
11268            pw.println("Participants:");
11269            for (int i=0; i<N; i++) {
11270                int uid = mBackupParticipants.keyAt(i);
11271                pw.print("  uid: ");
11272                pw.println(uid);
11273                HashSet<String> participants = mBackupParticipants.valueAt(i);
11274                for (String app: participants) {
11275                    pw.println("    " + app);
11276                }
11277            }
11278
11279            pw.println("Ancestral packages: "
11280                    + (mAncestralPackages == null ? "none" : mAncestralPackages.size()));
11281            if (mAncestralPackages != null) {
11282                for (String pkg : mAncestralPackages) {
11283                    pw.println("    " + pkg);
11284                }
11285            }
11286
11287            pw.println("Ever backed up: " + mEverStoredApps.size());
11288            for (String pkg : mEverStoredApps) {
11289                pw.println("    " + pkg);
11290            }
11291
11292            pw.println("Pending key/value backup: " + mPendingBackups.size());
11293            for (BackupRequest req : mPendingBackups.values()) {
11294                pw.println("    " + req);
11295            }
11296
11297            pw.println("Full backup queue:" + mFullBackupQueue.size());
11298            for (FullBackupEntry entry : mFullBackupQueue) {
11299                pw.print("    "); pw.print(entry.lastBackup);
11300                pw.print(" : "); pw.println(entry.packageName);
11301            }
11302        }
11303    }
11304
11305    private static void sendBackupOnUpdate(IBackupObserver observer, String packageName,
11306            BackupProgress progress) {
11307        if (observer != null) {
11308            try {
11309                observer.onUpdate(packageName, progress);
11310            } catch (RemoteException e) {
11311                if (DEBUG) {
11312                    Slog.w(TAG, "Backup observer went away: onUpdate");
11313                }
11314            }
11315        }
11316    }
11317
11318    private static void sendBackupOnPackageResult(IBackupObserver observer, String packageName,
11319            int status) {
11320        if (observer != null) {
11321            try {
11322                observer.onResult(packageName, status);
11323            } catch (RemoteException e) {
11324                if (DEBUG) {
11325                    Slog.w(TAG, "Backup observer went away: onResult");
11326                }
11327            }
11328        }
11329    }
11330
11331    private static void sendBackupFinished(IBackupObserver observer, int status) {
11332        if (observer != null) {
11333            try {
11334                observer.backupFinished(status);
11335            } catch (RemoteException e) {
11336                if (DEBUG) {
11337                    Slog.w(TAG, "Backup observer went away: backupFinished");
11338                }
11339            }
11340        }
11341    }
11342
11343    private Bundle putMonitoringExtra(Bundle extras, String key, String value) {
11344        if (extras == null) {
11345            extras = new Bundle();
11346        }
11347        extras.putString(key, value);
11348        return extras;
11349    }
11350
11351    private Bundle putMonitoringExtra(Bundle extras, String key, int value) {
11352        if (extras == null) {
11353            extras = new Bundle();
11354        }
11355        extras.putInt(key, value);
11356        return extras;
11357    }
11358
11359    private Bundle putMonitoringExtra(Bundle extras, String key, long value) {
11360        if (extras == null) {
11361            extras = new Bundle();
11362        }
11363        extras.putLong(key, value);
11364        return extras;
11365    }
11366
11367
11368    private Bundle putMonitoringExtra(Bundle extras, String key, boolean value) {
11369        if (extras == null) {
11370            extras = new Bundle();
11371        }
11372        extras.putBoolean(key, value);
11373        return extras;
11374    }
11375
11376    private static IBackupManagerMonitor monitorEvent(IBackupManagerMonitor monitor, int id,
11377            PackageInfo pkg, int category, Bundle extras) {
11378        if (monitor != null) {
11379            try {
11380                Bundle bundle = new Bundle();
11381                bundle.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_ID, id);
11382                bundle.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_CATEGORY, category);
11383                if (pkg != null) {
11384                    bundle.putString(EXTRA_LOG_EVENT_PACKAGE_NAME,
11385                            pkg.packageName);
11386                    bundle.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_VERSION,
11387                            pkg.versionCode);
11388                }
11389                if (extras != null) {
11390                    bundle.putAll(extras);
11391                }
11392                monitor.onEvent(bundle);
11393                return monitor;
11394            } catch(RemoteException e) {
11395                if (DEBUG) {
11396                    Slog.w(TAG, "backup manager monitor went away");
11397                }
11398            }
11399        }
11400        return null;
11401    }
11402
11403    @Override
11404    public IBackupManager getBackupManagerBinder() {
11405        return mBackupManagerBinder;
11406    }
11407
11408}
11409