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