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