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