Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 17 | package com.android.internal.backup; |
| 18 | |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 19 | import android.app.backup.BackupAgent; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 20 | import android.app.backup.BackupDataInput; |
| 21 | import android.app.backup.BackupDataOutput; |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 22 | import android.app.backup.BackupTransport; |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 23 | import android.app.backup.RestoreDescription; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 24 | import android.app.backup.RestoreSet; |
Christopher Tate | cefba58 | 2013-11-14 18:10:35 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageInfo; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 29 | import android.os.Environment; |
| 30 | import android.os.ParcelFileDescriptor; |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 31 | import android.system.ErrnoException; |
| 32 | import android.system.Os; |
| 33 | import android.system.StructStat; |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 34 | import android.util.ArrayMap; |
Bernardo Rufino | 7a6e032 | 2018-07-17 10:17:43 +0100 | [diff] [blame] | 35 | import android.util.Base64; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | |
Christopher Tate | 824392b | 2014-11-14 18:27:36 -0800 | [diff] [blame] | 38 | import libcore.io.IoUtils; |
| 39 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 40 | import java.io.BufferedOutputStream; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 41 | import java.io.File; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 42 | import java.io.FileInputStream; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 43 | import java.io.FileNotFoundException; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 44 | import java.io.FileOutputStream; |
| 45 | import java.io.IOException; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 46 | import java.util.ArrayList; |
| 47 | import java.util.Collections; |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 48 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Backup transport for stashing stuff into a known location on disk, and |
| 51 | * later restoring from there. For testing only. |
| 52 | */ |
| 53 | |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 54 | public class LocalTransport extends BackupTransport { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 55 | private static final String TAG = "LocalTransport"; |
Ed Heyl | a50cd8d | 2014-07-14 23:42:04 -0700 | [diff] [blame] | 56 | private static final boolean DEBUG = false; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 57 | |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 58 | private static final String TRANSPORT_DIR_NAME |
| 59 | = "com.android.internal.backup.LocalTransport"; |
| 60 | |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 61 | private static final String TRANSPORT_DESTINATION_STRING |
| 62 | = "Backing up to debug-only private cache"; |
| 63 | |
Christopher Tate | 9679410 | 2014-07-27 20:21:55 -0700 | [diff] [blame] | 64 | private static final String TRANSPORT_DATA_MANAGEMENT_LABEL |
| 65 | = ""; |
| 66 | |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 67 | private static final String INCREMENTAL_DIR = "_delta"; |
| 68 | private static final String FULL_DATA_DIR = "_full"; |
| 69 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 70 | // The currently-active restore set always has the same (nonzero!) token |
| 71 | private static final long CURRENT_SET_TOKEN = 1; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 72 | |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 73 | // Size quotas at reasonable values, similar to the current cloud-storage limits |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 74 | private static final long FULL_BACKUP_SIZE_QUOTA = 25 * 1024 * 1024; |
Shreyas Basarge | b6e73c9 | 2017-01-31 20:13:43 +0000 | [diff] [blame] | 75 | private static final long KEY_VALUE_BACKUP_SIZE_QUOTA = 5 * 1024 * 1024; |
| 76 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 77 | private Context mContext; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 78 | private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 79 | private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN)); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 80 | private File mCurrentSetIncrementalDir = new File(mCurrentSetDir, INCREMENTAL_DIR); |
| 81 | private File mCurrentSetFullDir = new File(mCurrentSetDir, FULL_DATA_DIR); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 82 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 83 | private PackageInfo[] mRestorePackages = null; |
| 84 | private int mRestorePackage = -1; // Index into mRestorePackages |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 85 | private int mRestoreType; |
| 86 | private File mRestoreSetDir; |
| 87 | private File mRestoreSetIncrementalDir; |
| 88 | private File mRestoreSetFullDir; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 89 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 90 | // Additional bookkeeping for full backup |
| 91 | private String mFullTargetPackage; |
| 92 | private ParcelFileDescriptor mSocket; |
| 93 | private FileInputStream mSocketInputStream; |
| 94 | private BufferedOutputStream mFullBackupOutputStream; |
| 95 | private byte[] mFullBackupBuffer; |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 96 | private long mFullBackupSize; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 97 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 98 | private FileInputStream mCurFullRestoreStream; |
| 99 | private FileOutputStream mFullRestoreSocketStream; |
| 100 | private byte[] mFullRestoreBuffer; |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 101 | private final LocalTransportParameters mParameters; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 102 | |
Christopher Tate | de2826b | 2015-03-12 18:53:31 -0700 | [diff] [blame] | 103 | private void makeDataDirs() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 104 | mCurrentSetDir.mkdirs(); |
Christopher Tate | de2826b | 2015-03-12 18:53:31 -0700 | [diff] [blame] | 105 | mCurrentSetFullDir.mkdir(); |
| 106 | mCurrentSetIncrementalDir.mkdir(); |
| 107 | } |
| 108 | |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 109 | public LocalTransport(Context context, LocalTransportParameters parameters) { |
Christopher Tate | de2826b | 2015-03-12 18:53:31 -0700 | [diff] [blame] | 110 | mContext = context; |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 111 | mParameters = parameters; |
Christopher Tate | de2826b | 2015-03-12 18:53:31 -0700 | [diff] [blame] | 112 | makeDataDirs(); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 115 | LocalTransportParameters getParameters() { |
| 116 | return mParameters; |
| 117 | } |
| 118 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 119 | @Override |
Christopher Tate | cefba58 | 2013-11-14 18:10:35 -0800 | [diff] [blame] | 120 | public String name() { |
| 121 | return new ComponentName(mContext, this.getClass()).flattenToShortString(); |
| 122 | } |
| 123 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 124 | @Override |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 125 | public Intent configurationIntent() { |
| 126 | // The local transport is not user-configurable |
| 127 | return null; |
| 128 | } |
| 129 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 130 | @Override |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 131 | public String currentDestinationString() { |
| 132 | return TRANSPORT_DESTINATION_STRING; |
| 133 | } |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 134 | |
Christopher Tate | 9679410 | 2014-07-27 20:21:55 -0700 | [diff] [blame] | 135 | public Intent dataManagementIntent() { |
| 136 | // The local transport does not present a data-management UI |
| 137 | // TODO: consider adding simple UI to wipe the archives entirely, |
| 138 | // for cleaning up the cache partition. |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | public String dataManagementLabel() { |
| 143 | return TRANSPORT_DATA_MANAGEMENT_LABEL; |
| 144 | } |
| 145 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 146 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 147 | public String transportDirName() { |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 148 | return TRANSPORT_DIR_NAME; |
| 149 | } |
| 150 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 151 | @Override |
Bernardo Rufino | eaa78b9 | 2018-01-26 11:25:37 +0000 | [diff] [blame] | 152 | public int getTransportFlags() { |
| 153 | int flags = super.getTransportFlags(); |
| 154 | // Testing for a fake flag and having it set as a boolean in settings prevents anyone from |
| 155 | // using this it to pull data from the agent |
| 156 | if (mParameters.isFakeEncryptionFlag()) { |
| 157 | flags |= BackupAgent.FLAG_FAKE_CLIENT_SIDE_ENCRYPTION_ENABLED; |
| 158 | } |
| 159 | return flags; |
| 160 | } |
| 161 | |
| 162 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 163 | public long requestBackupTime() { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 164 | // any time is a good time for local backup |
| 165 | return 0; |
| 166 | } |
| 167 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 168 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 169 | public int initializeDevice() { |
| 170 | if (DEBUG) Log.v(TAG, "wiping all data"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 171 | deleteContents(mCurrentSetDir); |
Christopher Tate | de2826b | 2015-03-12 18:53:31 -0700 | [diff] [blame] | 172 | makeDataDirs(); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 173 | return TRANSPORT_OK; |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 176 | // Encapsulation of a single k/v element change |
| 177 | private class KVOperation { |
| 178 | final String key; // Element filename, not the raw key, for efficiency |
| 179 | final byte[] value; // null when this is a deletion operation |
| 180 | |
| 181 | KVOperation(String k, byte[] v) { |
| 182 | key = k; |
| 183 | value = v; |
| 184 | } |
| 185 | } |
| 186 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 187 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 188 | public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) { |
Anton Philippov | d903074 | 2018-03-26 19:12:08 +0100 | [diff] [blame] | 189 | return performBackup(packageInfo, data, /*flags=*/ 0); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data, int flags) { |
| 194 | boolean isIncremental = (flags & FLAG_INCREMENTAL) != 0; |
| 195 | boolean isNonIncremental = (flags & FLAG_NON_INCREMENTAL) != 0; |
| 196 | |
| 197 | if (isIncremental) { |
| 198 | Log.i(TAG, "Performing incremental backup for " + packageInfo.packageName); |
| 199 | } else if (isNonIncremental) { |
| 200 | Log.i(TAG, "Performing non-incremental backup for " + packageInfo.packageName); |
| 201 | } else { |
| 202 | Log.i(TAG, "Performing backup for " + packageInfo.packageName); |
| 203 | } |
| 204 | |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 205 | if (DEBUG) { |
| 206 | try { |
Anton Philippov | d903074 | 2018-03-26 19:12:08 +0100 | [diff] [blame] | 207 | StructStat ss = Os.fstat(data.getFileDescriptor()); |
| 208 | Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName |
| 209 | + " size=" + ss.st_size + " flags=" + flags); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 210 | } catch (ErrnoException e) { |
| 211 | Log.w(TAG, "Unable to stat input file in performBackup() on " |
| 212 | + packageInfo.packageName); |
| 213 | } |
| 214 | } |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 215 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 216 | File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName); |
Anton Philippov | d903074 | 2018-03-26 19:12:08 +0100 | [diff] [blame] | 217 | boolean hasDataForPackage = !packageDir.mkdirs(); |
| 218 | |
| 219 | if (isIncremental) { |
| 220 | if (mParameters.isNonIncrementalOnly() || !hasDataForPackage) { |
| 221 | if (mParameters.isNonIncrementalOnly()) { |
| 222 | Log.w(TAG, "Transport is in non-incremental only mode."); |
| 223 | |
| 224 | } else { |
| 225 | Log.w(TAG, |
| 226 | "Requested incremental, but transport currently stores no data for the " |
| 227 | + "package, requesting non-incremental retry."); |
| 228 | } |
| 229 | return TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED; |
| 230 | } |
| 231 | } |
| 232 | if (isNonIncremental && hasDataForPackage) { |
| 233 | Log.w(TAG, "Requested non-incremental, deleting existing data."); |
| 234 | clearBackupData(packageInfo); |
| 235 | packageDir.mkdirs(); |
| 236 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 237 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 238 | // Each 'record' in the restore set is kept in its own file, named by |
| 239 | // the record key. Wind through the data file, extracting individual |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 240 | // record operations and building a list of all the updates to apply |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 241 | // in this update. |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 242 | final ArrayList<KVOperation> changeOps; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 243 | try { |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 244 | changeOps = parseBackupStream(data); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 245 | } catch (IOException e) { |
| 246 | // oops, something went wrong. abort the operation and return error. |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 247 | Log.v(TAG, "Exception reading backup input", e); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 248 | return TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 249 | } |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 250 | |
| 251 | // Okay, now we've parsed out the delta's individual operations. We need to measure |
| 252 | // the effect against what we already have in the datastore to detect quota overrun. |
| 253 | // So, we first need to tally up the current in-datastore size per key. |
| 254 | final ArrayMap<String, Integer> datastore = new ArrayMap<>(); |
| 255 | int totalSize = parseKeySizes(packageDir, datastore); |
| 256 | |
| 257 | // ... and now figure out the datastore size that will result from applying the |
| 258 | // sequence of delta operations |
| 259 | if (DEBUG) { |
| 260 | if (changeOps.size() > 0) { |
| 261 | Log.v(TAG, "Calculating delta size impact"); |
| 262 | } else { |
| 263 | Log.v(TAG, "No operations in backup stream, so no size change"); |
| 264 | } |
| 265 | } |
| 266 | int updatedSize = totalSize; |
| 267 | for (KVOperation op : changeOps) { |
| 268 | // Deduct the size of the key we're about to replace, if any |
| 269 | final Integer curSize = datastore.get(op.key); |
| 270 | if (curSize != null) { |
| 271 | updatedSize -= curSize.intValue(); |
| 272 | if (DEBUG && op.value == null) { |
| 273 | Log.v(TAG, " delete " + op.key + ", updated total " + updatedSize); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // And add back the size of the value we're about to store, if any |
| 278 | if (op.value != null) { |
| 279 | updatedSize += op.value.length; |
| 280 | if (DEBUG) { |
| 281 | Log.v(TAG, ((curSize == null) ? " new " : " replace ") |
| 282 | + op.key + ", updated total " + updatedSize); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // If our final size is over quota, report the failure |
| 288 | if (updatedSize > KEY_VALUE_BACKUP_SIZE_QUOTA) { |
| 289 | if (DEBUG) { |
| 290 | Log.i(TAG, "New datastore size " + updatedSize |
| 291 | + " exceeds quota " + KEY_VALUE_BACKUP_SIZE_QUOTA); |
| 292 | } |
| 293 | return TRANSPORT_QUOTA_EXCEEDED; |
| 294 | } |
| 295 | |
| 296 | // No problem with storage size, so go ahead and apply the delta operations |
| 297 | // (in the order that the app provided them) |
| 298 | for (KVOperation op : changeOps) { |
| 299 | File element = new File(packageDir, op.key); |
| 300 | |
| 301 | // this is either a deletion or a rewrite-from-zero, so we can just remove |
| 302 | // the existing file and proceed in either case. |
| 303 | element.delete(); |
| 304 | |
| 305 | // if this wasn't a deletion, put the new data in place |
| 306 | if (op.value != null) { |
| 307 | try (FileOutputStream out = new FileOutputStream(element)) { |
| 308 | out.write(op.value, 0, op.value.length); |
| 309 | } catch (IOException e) { |
| 310 | Log.e(TAG, "Unable to update key file " + element); |
| 311 | return TRANSPORT_ERROR; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | return TRANSPORT_OK; |
| 316 | } |
| 317 | |
| 318 | // Parses a backup stream into individual key/value operations |
| 319 | private ArrayList<KVOperation> parseBackupStream(ParcelFileDescriptor data) |
| 320 | throws IOException { |
| 321 | ArrayList<KVOperation> changeOps = new ArrayList<>(); |
| 322 | BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor()); |
| 323 | while (changeSet.readNextHeader()) { |
| 324 | String key = changeSet.getKey(); |
Bernardo Rufino | 7a6e032 | 2018-07-17 10:17:43 +0100 | [diff] [blame] | 325 | String base64Key = new String(Base64.encode(key.getBytes(), Base64.NO_WRAP)); |
Christopher Tate | c8a9d42 | 2017-06-23 12:48:26 -0700 | [diff] [blame] | 326 | int dataSize = changeSet.getDataSize(); |
| 327 | if (DEBUG) { |
| 328 | Log.v(TAG, " Delta operation key " + key + " size " + dataSize |
| 329 | + " key64 " + base64Key); |
| 330 | } |
| 331 | |
| 332 | byte[] buf = (dataSize >= 0) ? new byte[dataSize] : null; |
| 333 | if (dataSize >= 0) { |
| 334 | changeSet.readEntityData(buf, 0, dataSize); |
| 335 | } |
| 336 | changeOps.add(new KVOperation(base64Key, buf)); |
| 337 | } |
| 338 | return changeOps; |
| 339 | } |
| 340 | |
| 341 | // Reads the given datastore directory, building a table of the value size of each |
| 342 | // keyed element, and returning the summed total. |
| 343 | private int parseKeySizes(File packageDir, ArrayMap<String, Integer> datastore) { |
| 344 | int totalSize = 0; |
| 345 | final String[] elements = packageDir.list(); |
| 346 | if (elements != null) { |
| 347 | if (DEBUG) { |
| 348 | Log.v(TAG, "Existing datastore contents:"); |
| 349 | } |
| 350 | for (String file : elements) { |
| 351 | File element = new File(packageDir, file); |
| 352 | String key = file; // filename |
| 353 | int size = (int) element.length(); |
| 354 | totalSize += size; |
| 355 | if (DEBUG) { |
| 356 | Log.v(TAG, " key " + key + " size " + size); |
| 357 | } |
| 358 | datastore.put(key, size); |
| 359 | } |
| 360 | if (DEBUG) { |
| 361 | Log.v(TAG, " TOTAL: " + totalSize); |
| 362 | } |
| 363 | } else { |
| 364 | if (DEBUG) { |
| 365 | Log.v(TAG, "No existing data for this package"); |
| 366 | } |
| 367 | } |
| 368 | return totalSize; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 369 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 370 | |
Christopher Tate | 25a747f | 2009-09-20 12:43:58 -0700 | [diff] [blame] | 371 | // Deletes the contents but not the given directory |
| 372 | private void deleteContents(File dirname) { |
| 373 | File[] contents = dirname.listFiles(); |
| 374 | if (contents != null) { |
| 375 | for (File f : contents) { |
| 376 | if (f.isDirectory()) { |
| 377 | // delete the directory's contents then fall through |
| 378 | // and delete the directory itself. |
| 379 | deleteContents(f); |
| 380 | } |
| 381 | f.delete(); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 386 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 387 | public int clearBackupData(PackageInfo packageInfo) { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 388 | if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName); |
| 389 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 390 | File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName); |
Christopher Tate | 0abf6a0 | 2012-03-23 17:45:15 -0700 | [diff] [blame] | 391 | final File[] fileset = packageDir.listFiles(); |
| 392 | if (fileset != null) { |
| 393 | for (File f : fileset) { |
| 394 | f.delete(); |
| 395 | } |
| 396 | packageDir.delete(); |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 397 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 398 | |
| 399 | packageDir = new File(mCurrentSetFullDir, packageInfo.packageName); |
| 400 | final File[] tarballs = packageDir.listFiles(); |
| 401 | if (tarballs != null) { |
| 402 | for (File f : tarballs) { |
| 403 | f.delete(); |
| 404 | } |
| 405 | packageDir.delete(); |
| 406 | } |
| 407 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 408 | return TRANSPORT_OK; |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 411 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 412 | public int finishBackup() { |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 413 | if (DEBUG) Log.v(TAG, "finishBackup() of " + mFullTargetPackage); |
| 414 | return tearDownFullBackup(); |
| 415 | } |
| 416 | |
| 417 | // ------------------------------------------------------------------------------------ |
| 418 | // Full backup handling |
| 419 | |
| 420 | private int tearDownFullBackup() { |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 421 | if (mSocket != null) { |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 422 | try { |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 423 | if (mFullBackupOutputStream != null) { |
| 424 | mFullBackupOutputStream.flush(); |
| 425 | mFullBackupOutputStream.close(); |
| 426 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 427 | mSocketInputStream = null; |
| 428 | mFullTargetPackage = null; |
| 429 | mSocket.close(); |
| 430 | } catch (IOException e) { |
Christopher Tate | 89101f7 | 2014-07-17 19:09:00 -0700 | [diff] [blame] | 431 | if (DEBUG) { |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 432 | Log.w(TAG, "Exception caught in tearDownFullBackup()", e); |
Christopher Tate | 89101f7 | 2014-07-17 19:09:00 -0700 | [diff] [blame] | 433 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 434 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 435 | } finally { |
| 436 | mSocket = null; |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 437 | mFullBackupOutputStream = null; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 440 | return TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 443 | private File tarballFile(String pkgName) { |
| 444 | return new File(mCurrentSetFullDir, pkgName); |
| 445 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 446 | |
| 447 | @Override |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 448 | public long requestFullBackupTime() { |
| 449 | return 0; |
| 450 | } |
| 451 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 452 | @Override |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 453 | public int checkFullBackupSize(long size) { |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 454 | int result = TRANSPORT_OK; |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 455 | // Decline zero-size "backups" |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 456 | if (size <= 0) { |
| 457 | result = TRANSPORT_PACKAGE_REJECTED; |
| 458 | } else if (size > FULL_BACKUP_SIZE_QUOTA) { |
| 459 | result = TRANSPORT_QUOTA_EXCEEDED; |
| 460 | } |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 461 | if (result != TRANSPORT_OK) { |
| 462 | if (DEBUG) { |
| 463 | Log.v(TAG, "Declining backup of size " + size); |
| 464 | } |
| 465 | } |
| 466 | return result; |
| 467 | } |
| 468 | |
| 469 | @Override |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 470 | public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket) { |
| 471 | if (mSocket != null) { |
| 472 | Log.e(TAG, "Attempt to initiate full backup while one is in progress"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 473 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (DEBUG) { |
| 477 | Log.i(TAG, "performFullBackup : " + targetPackage); |
| 478 | } |
| 479 | |
| 480 | // We know a priori that we run in the system process, so we need to make |
| 481 | // sure to dup() our own copy of the socket fd. Transports which run in |
| 482 | // their own processes must not do this. |
| 483 | try { |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 484 | mFullBackupSize = 0; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 485 | mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 486 | mSocketInputStream = new FileInputStream(mSocket.getFileDescriptor()); |
| 487 | } catch (IOException e) { |
| 488 | Log.e(TAG, "Unable to process socket for full backup"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 489 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | mFullTargetPackage = targetPackage.packageName; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 493 | mFullBackupBuffer = new byte[4096]; |
| 494 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 495 | return TRANSPORT_OK; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 498 | @Override |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 499 | public int sendBackupData(final int numBytes) { |
| 500 | if (mSocket == null) { |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 501 | Log.w(TAG, "Attempted sendBackupData before performFullBackup"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 502 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 505 | mFullBackupSize += numBytes; |
| 506 | if (mFullBackupSize > FULL_BACKUP_SIZE_QUOTA) { |
| 507 | return TRANSPORT_QUOTA_EXCEEDED; |
| 508 | } |
| 509 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 510 | if (numBytes > mFullBackupBuffer.length) { |
| 511 | mFullBackupBuffer = new byte[numBytes]; |
| 512 | } |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 513 | |
| 514 | if (mFullBackupOutputStream == null) { |
| 515 | FileOutputStream tarstream; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 516 | try { |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 517 | File tarball = tarballFile(mFullTargetPackage); |
| 518 | tarstream = new FileOutputStream(tarball); |
| 519 | } catch (FileNotFoundException e) { |
| 520 | return TRANSPORT_ERROR; |
| 521 | } |
| 522 | mFullBackupOutputStream = new BufferedOutputStream(tarstream); |
| 523 | } |
| 524 | |
| 525 | int bytesLeft = numBytes; |
| 526 | while (bytesLeft > 0) { |
| 527 | try { |
| 528 | int nRead = mSocketInputStream.read(mFullBackupBuffer, 0, bytesLeft); |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 529 | if (nRead < 0) { |
| 530 | // Something went wrong if we expect data but saw EOD |
| 531 | Log.w(TAG, "Unexpected EOD; failing backup"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 532 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 533 | } |
| 534 | mFullBackupOutputStream.write(mFullBackupBuffer, 0, nRead); |
Christopher Tate | 9310e42 | 2015-04-10 11:17:14 -0700 | [diff] [blame] | 535 | bytesLeft -= nRead; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 536 | } catch (IOException e) { |
| 537 | Log.e(TAG, "Error handling backup data for " + mFullTargetPackage); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 538 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
Christopher Tate | 77a2d78d | 2015-03-03 16:19:44 -0800 | [diff] [blame] | 541 | if (DEBUG) { |
| 542 | Log.v(TAG, " stored " + numBytes + " of data"); |
| 543 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 544 | return TRANSPORT_OK; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 547 | // For now we can't roll back, so just tear everything down. |
| 548 | @Override |
| 549 | public void cancelFullBackup() { |
| 550 | if (DEBUG) { |
| 551 | Log.i(TAG, "Canceling full backup of " + mFullTargetPackage); |
| 552 | } |
| 553 | File archive = tarballFile(mFullTargetPackage); |
| 554 | tearDownFullBackup(); |
| 555 | if (archive.exists()) { |
| 556 | archive.delete(); |
| 557 | } |
| 558 | } |
| 559 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 560 | // ------------------------------------------------------------------------------------ |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 561 | // Restore handling |
Christopher Tate | 51fea57 | 2014-06-23 17:01:06 -0700 | [diff] [blame] | 562 | static final long[] POSSIBLE_SETS = { 2, 3, 4, 5, 6, 7, 8, 9 }; |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 563 | |
| 564 | @Override |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 565 | public RestoreSet[] getAvailableRestoreSets() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 566 | long[] existing = new long[POSSIBLE_SETS.length + 1]; |
| 567 | int num = 0; |
| 568 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 569 | // see which possible non-current sets exist... |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 570 | for (long token : POSSIBLE_SETS) { |
| 571 | if ((new File(mDataDir, Long.toString(token))).exists()) { |
| 572 | existing[num++] = token; |
| 573 | } |
| 574 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 575 | // ...and always the currently-active set last |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 576 | existing[num++] = CURRENT_SET_TOKEN; |
| 577 | |
| 578 | RestoreSet[] available = new RestoreSet[num]; |
| 579 | for (int i = 0; i < available.length; i++) { |
| 580 | available[i] = new RestoreSet("Local disk image", "flash", existing[i]); |
| 581 | } |
| 582 | return available; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 585 | @Override |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 586 | public long getCurrentRestoreSet() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 587 | // The current restore set always has the same token |
| 588 | return CURRENT_SET_TOKEN; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 591 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 592 | public int startRestore(long token, PackageInfo[] packages) { |
Christopher Tate | 51fea57 | 2014-06-23 17:01:06 -0700 | [diff] [blame] | 593 | if (DEBUG) Log.v(TAG, "start restore " + token + " : " + packages.length |
| 594 | + " matching packages"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 595 | mRestorePackages = packages; |
| 596 | mRestorePackage = -1; |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 597 | mRestoreSetDir = new File(mDataDir, Long.toString(token)); |
| 598 | mRestoreSetIncrementalDir = new File(mRestoreSetDir, INCREMENTAL_DIR); |
| 599 | mRestoreSetFullDir = new File(mRestoreSetDir, FULL_DATA_DIR); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 600 | return TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 603 | @Override |
| 604 | public RestoreDescription nextRestorePackage() { |
Christopher Tate | 77a2d78d | 2015-03-03 16:19:44 -0800 | [diff] [blame] | 605 | if (DEBUG) { |
| 606 | Log.v(TAG, "nextRestorePackage() : mRestorePackage=" + mRestorePackage |
| 607 | + " length=" + mRestorePackages.length); |
| 608 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 609 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 610 | |
| 611 | boolean found = false; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 612 | while (++mRestorePackage < mRestorePackages.length) { |
| 613 | String name = mRestorePackages[mRestorePackage].packageName; |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 614 | |
| 615 | // If we have key/value data for this package, deliver that |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 616 | // skip packages where we have a data dir but no actual contents |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 617 | String[] contents = (new File(mRestoreSetIncrementalDir, name)).list(); |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 618 | if (contents != null && contents.length > 0) { |
Christopher Tate | 77a2d78d | 2015-03-03 16:19:44 -0800 | [diff] [blame] | 619 | if (DEBUG) { |
| 620 | Log.v(TAG, " nextRestorePackage(TYPE_KEY_VALUE) @ " |
| 621 | + mRestorePackage + " = " + name); |
| 622 | } |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 623 | mRestoreType = RestoreDescription.TYPE_KEY_VALUE; |
| 624 | found = true; |
| 625 | } |
| 626 | |
| 627 | if (!found) { |
| 628 | // No key/value data; check for [non-empty] full data |
| 629 | File maybeFullData = new File(mRestoreSetFullDir, name); |
| 630 | if (maybeFullData.length() > 0) { |
Christopher Tate | 77a2d78d | 2015-03-03 16:19:44 -0800 | [diff] [blame] | 631 | if (DEBUG) { |
| 632 | Log.v(TAG, " nextRestorePackage(TYPE_FULL_STREAM) @ " |
| 633 | + mRestorePackage + " = " + name); |
| 634 | } |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 635 | mRestoreType = RestoreDescription.TYPE_FULL_STREAM; |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 636 | mCurFullRestoreStream = null; // ensure starting from the ground state |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 637 | found = true; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (found) { |
| 642 | return new RestoreDescription(name, mRestoreType); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 643 | } |
Christopher Tate | 77a2d78d | 2015-03-03 16:19:44 -0800 | [diff] [blame] | 644 | |
| 645 | if (DEBUG) { |
| 646 | Log.v(TAG, " ... package @ " + mRestorePackage + " = " + name |
| 647 | + " has no data; skipping"); |
| 648 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 649 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 650 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 651 | if (DEBUG) Log.v(TAG, " no more packages to restore"); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 652 | return RestoreDescription.NO_MORE_PACKAGES; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 655 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 656 | public int getRestoreData(ParcelFileDescriptor outFd) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 657 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 658 | if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called"); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 659 | if (mRestoreType != RestoreDescription.TYPE_KEY_VALUE) { |
| 660 | throw new IllegalStateException("getRestoreData(fd) for non-key/value dataset"); |
| 661 | } |
Christopher Tate | 51fea57 | 2014-06-23 17:01:06 -0700 | [diff] [blame] | 662 | File packageDir = new File(mRestoreSetIncrementalDir, |
| 663 | mRestorePackages[mRestorePackage].packageName); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 664 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 665 | // The restore set is the concatenation of the individual record blobs, |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 666 | // each of which is a file in the package's directory. We return the |
| 667 | // data in lexical order sorted by key, so that apps which use synthetic |
| 668 | // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious |
| 669 | // order. |
| 670 | ArrayList<DecodedFilename> blobs = contentsByKey(packageDir); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 671 | if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 672 | Log.e(TAG, "No keys for package: " + packageDir); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 673 | return TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 674 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 675 | |
| 676 | // We expect at least some data if the directory exists in the first place |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 677 | if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 678 | BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor()); |
| 679 | try { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 680 | for (DecodedFilename keyEntry : blobs) { |
| 681 | File f = keyEntry.file; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 682 | FileInputStream in = new FileInputStream(f); |
| 683 | try { |
| 684 | int size = (int) f.length(); |
| 685 | byte[] buf = new byte[size]; |
| 686 | in.read(buf); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 687 | if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size); |
| 688 | out.writeEntityHeader(keyEntry.key, size); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 689 | out.writeEntityData(buf, size); |
| 690 | } finally { |
| 691 | in.close(); |
| 692 | } |
| 693 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 694 | return TRANSPORT_OK; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 695 | } catch (IOException e) { |
| 696 | Log.e(TAG, "Unable to read backup records", e); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 697 | return TRANSPORT_ERROR; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 698 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 699 | } |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 700 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 701 | static class DecodedFilename implements Comparable<DecodedFilename> { |
| 702 | public File file; |
| 703 | public String key; |
| 704 | |
| 705 | public DecodedFilename(File f) { |
| 706 | file = f; |
Bernardo Rufino | 7a6e032 | 2018-07-17 10:17:43 +0100 | [diff] [blame] | 707 | key = new String(Base64.decode(f.getName(), Base64.DEFAULT)); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | @Override |
| 711 | public int compareTo(DecodedFilename other) { |
| 712 | // sorts into ascending lexical order by decoded key |
| 713 | return key.compareTo(other.key); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // Return a list of the files in the given directory, sorted lexically by |
| 718 | // the Base64-decoded file name, not by the on-disk filename |
| 719 | private ArrayList<DecodedFilename> contentsByKey(File dir) { |
| 720 | File[] allFiles = dir.listFiles(); |
| 721 | if (allFiles == null || allFiles.length == 0) { |
| 722 | return null; |
| 723 | } |
| 724 | |
| 725 | // Decode the filenames into keys then sort lexically by key |
| 726 | ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>(); |
| 727 | for (File f : allFiles) { |
| 728 | contents.add(new DecodedFilename(f)); |
| 729 | } |
| 730 | Collections.sort(contents); |
| 731 | return contents; |
| 732 | } |
| 733 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 734 | @Override |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 735 | public void finishRestore() { |
| 736 | if (DEBUG) Log.v(TAG, "finishRestore()"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 737 | if (mRestoreType == RestoreDescription.TYPE_FULL_STREAM) { |
| 738 | resetFullRestoreState(); |
| 739 | } |
| 740 | mRestoreType = 0; |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 741 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 742 | |
| 743 | // ------------------------------------------------------------------------------------ |
| 744 | // Full restore handling |
| 745 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 746 | private void resetFullRestoreState() { |
Christopher Tate | 824392b | 2014-11-14 18:27:36 -0800 | [diff] [blame] | 747 | IoUtils.closeQuietly(mCurFullRestoreStream); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 748 | mCurFullRestoreStream = null; |
| 749 | mFullRestoreSocketStream = null; |
| 750 | mFullRestoreBuffer = null; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Ask the transport to provide data for the "current" package being restored. The |
| 755 | * transport then writes some data to the socket supplied to this call, and returns |
| 756 | * the number of bytes written. The system will then read that many bytes and |
| 757 | * stream them to the application's agent for restore, then will call this method again |
| 758 | * to receive the next chunk of the archive. This sequence will be repeated until the |
| 759 | * transport returns zero indicating that all of the package's data has been delivered |
| 760 | * (or returns a negative value indicating some sort of hard error condition at the |
| 761 | * transport level). |
| 762 | * |
| 763 | * <p>After this method returns zero, the system will then call |
| 764 | * {@link #getNextFullRestorePackage()} to begin the restore process for the next |
| 765 | * application, and the sequence begins again. |
| 766 | * |
| 767 | * @param socket The file descriptor that the transport will use for delivering the |
| 768 | * streamed archive. |
| 769 | * @return 0 when no more data for the current package is available. A positive value |
| 770 | * indicates the presence of that much data to be delivered to the app. A negative |
| 771 | * return value is treated as equivalent to {@link BackupTransport#TRANSPORT_ERROR}, |
| 772 | * indicating a fatal error condition that precludes further restore operations |
| 773 | * on the current dataset. |
| 774 | */ |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 775 | @Override |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 776 | public int getNextFullRestoreDataChunk(ParcelFileDescriptor socket) { |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 777 | if (mRestoreType != RestoreDescription.TYPE_FULL_STREAM) { |
| 778 | throw new IllegalStateException("Asked for full restore data for non-stream package"); |
| 779 | } |
| 780 | |
| 781 | // first chunk? |
| 782 | if (mCurFullRestoreStream == null) { |
| 783 | final String name = mRestorePackages[mRestorePackage].packageName; |
| 784 | if (DEBUG) Log.i(TAG, "Starting full restore of " + name); |
| 785 | File dataset = new File(mRestoreSetFullDir, name); |
| 786 | try { |
| 787 | mCurFullRestoreStream = new FileInputStream(dataset); |
| 788 | } catch (IOException e) { |
| 789 | // If we can't open the target package's tarball, we return the single-package |
| 790 | // error code and let the caller go on to the next package. |
| 791 | Log.e(TAG, "Unable to read archive for " + name); |
| 792 | return TRANSPORT_PACKAGE_REJECTED; |
| 793 | } |
| 794 | mFullRestoreSocketStream = new FileOutputStream(socket.getFileDescriptor()); |
Christopher Tate | 89101f7 | 2014-07-17 19:09:00 -0700 | [diff] [blame] | 795 | mFullRestoreBuffer = new byte[2*1024]; |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | int nRead; |
| 799 | try { |
| 800 | nRead = mCurFullRestoreStream.read(mFullRestoreBuffer); |
| 801 | if (nRead < 0) { |
| 802 | // EOF: tell the caller we're done |
| 803 | nRead = NO_MORE_DATA; |
| 804 | } else if (nRead == 0) { |
| 805 | // This shouldn't happen when reading a FileInputStream; we should always |
| 806 | // get either a positive nonzero byte count or -1. Log the situation and |
| 807 | // treat it as EOF. |
| 808 | Log.w(TAG, "read() of archive file returned 0; treating as EOF"); |
| 809 | nRead = NO_MORE_DATA; |
| 810 | } else { |
| 811 | if (DEBUG) { |
| 812 | Log.i(TAG, " delivering restore chunk: " + nRead); |
| 813 | } |
| 814 | mFullRestoreSocketStream.write(mFullRestoreBuffer, 0, nRead); |
| 815 | } |
| 816 | } catch (IOException e) { |
| 817 | return TRANSPORT_ERROR; // Hard error accessing the file; shouldn't happen |
| 818 | } finally { |
| 819 | // Most transports will need to explicitly close 'socket' here, but this transport |
| 820 | // is in the same process as the caller so it can leave it up to the backup manager |
| 821 | // to manage both socket fds. |
| 822 | } |
| 823 | |
| 824 | return nRead; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 825 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 826 | |
| 827 | /** |
| 828 | * If the OS encounters an error while processing {@link RestoreDescription#TYPE_FULL_STREAM} |
| 829 | * data for restore, it will invoke this method to tell the transport that it should |
| 830 | * abandon the data download for the current package. The OS will then either call |
| 831 | * {@link #nextRestorePackage()} again to move on to restoring the next package in the |
| 832 | * set being iterated over, or will call {@link #finishRestore()} to shut down the restore |
| 833 | * operation. |
| 834 | * |
| 835 | * @return {@link #TRANSPORT_OK} if the transport was successful in shutting down the |
| 836 | * current stream cleanly, or {@link #TRANSPORT_ERROR} to indicate a serious |
| 837 | * transport-level failure. If the transport reports an error here, the entire restore |
| 838 | * operation will immediately be finished with no further attempts to restore app data. |
| 839 | */ |
| 840 | @Override |
| 841 | public int abortFullRestore() { |
| 842 | if (mRestoreType != RestoreDescription.TYPE_FULL_STREAM) { |
| 843 | throw new IllegalStateException("abortFullRestore() but not currently restoring"); |
| 844 | } |
| 845 | resetFullRestoreState(); |
| 846 | mRestoreType = 0; |
| 847 | return TRANSPORT_OK; |
| 848 | } |
| 849 | |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 850 | @Override |
| 851 | public long getBackupQuota(String packageName, boolean isFullBackup) { |
Shreyas Basarge | b6e73c9 | 2017-01-31 20:13:43 +0000 | [diff] [blame] | 852 | return isFullBackup ? FULL_BACKUP_SIZE_QUOTA : KEY_VALUE_BACKUP_SIZE_QUOTA; |
Sergey Poromov | 872d3b6 | 2016-01-12 15:48:08 +0100 | [diff] [blame] | 853 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 854 | } |