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 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 19 | import android.app.backup.BackupDataInput; |
| 20 | import android.app.backup.BackupDataOutput; |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 21 | import android.app.backup.BackupTransport; |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 22 | import android.app.backup.RestoreDescription; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 23 | import android.app.backup.RestoreSet; |
Christopher Tate | cefba58 | 2013-11-14 18:10:35 -0800 | [diff] [blame] | 24 | import android.content.ComponentName; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 26 | import android.content.Intent; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageInfo; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 28 | import android.os.Environment; |
| 29 | import android.os.ParcelFileDescriptor; |
rpcraig | ebab0ae | 2012-12-04 09:37:23 -0500 | [diff] [blame] | 30 | import android.os.SELinux; |
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 | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | |
Brian Carlstrom | 4140fae | 2011-01-24 16:17:43 -0800 | [diff] [blame] | 36 | import com.android.org.bouncycastle.util.encoders.Base64; |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 37 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 38 | import java.io.BufferedOutputStream; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 39 | import java.io.File; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 40 | import java.io.FileInputStream; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 41 | import java.io.FileNotFoundException; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 42 | import java.io.FileOutputStream; |
| 43 | import java.io.IOException; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 44 | import java.util.ArrayList; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 45 | import java.util.Arrays; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 46 | import java.util.Collections; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 47 | import java.util.HashSet; |
| 48 | import java.util.List; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 50 | import static android.system.OsConstants.*; |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 51 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Backup transport for stashing stuff into a known location on disk, and |
| 54 | * later restoring from there. For testing only. |
| 55 | */ |
| 56 | |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 57 | public class LocalTransport extends BackupTransport { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 58 | private static final String TAG = "LocalTransport"; |
Ed Heyl | a50cd8d | 2014-07-14 23:42:04 -0700 | [diff] [blame] | 59 | private static final boolean DEBUG = false; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 60 | |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 61 | private static final String TRANSPORT_DIR_NAME |
| 62 | = "com.android.internal.backup.LocalTransport"; |
| 63 | |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 64 | private static final String TRANSPORT_DESTINATION_STRING |
| 65 | = "Backing up to debug-only private cache"; |
| 66 | |
Christopher Tate | 9679410 | 2014-07-27 20:21:55 -0700 | [diff] [blame] | 67 | private static final String TRANSPORT_DATA_MANAGEMENT_LABEL |
| 68 | = ""; |
| 69 | |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 70 | private static final String INCREMENTAL_DIR = "_delta"; |
| 71 | private static final String FULL_DATA_DIR = "_full"; |
| 72 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 73 | // The currently-active restore set always has the same (nonzero!) token |
| 74 | private static final long CURRENT_SET_TOKEN = 1; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 75 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 76 | private Context mContext; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 77 | private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 78 | private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN)); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 79 | private File mCurrentSetIncrementalDir = new File(mCurrentSetDir, INCREMENTAL_DIR); |
| 80 | private File mCurrentSetFullDir = new File(mCurrentSetDir, FULL_DATA_DIR); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 81 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 82 | private PackageInfo[] mRestorePackages = null; |
| 83 | private int mRestorePackage = -1; // Index into mRestorePackages |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 84 | private int mRestoreType; |
| 85 | private File mRestoreSetDir; |
| 86 | private File mRestoreSetIncrementalDir; |
| 87 | private File mRestoreSetFullDir; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 88 | private long mRestoreToken; |
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; |
| 96 | |
| 97 | private File mFullRestoreSetDir; |
| 98 | private HashSet<String> mFullRestorePackages; |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 99 | private FileInputStream mCurFullRestoreStream; |
| 100 | private FileOutputStream mFullRestoreSocketStream; |
| 101 | private byte[] mFullRestoreBuffer; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 102 | |
| 103 | public LocalTransport(Context context) { |
| 104 | mContext = context; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 105 | mCurrentSetDir.mkdirs(); |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 106 | mCurrentSetFullDir.mkdir(); |
| 107 | mCurrentSetIncrementalDir.mkdir(); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 108 | if (!SELinux.restorecon(mCurrentSetDir)) { |
| 109 | Log.e(TAG, "SELinux restorecon failed for " + mCurrentSetDir); |
rpcraig | ebab0ae | 2012-12-04 09:37:23 -0500 | [diff] [blame] | 110 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 113 | @Override |
Christopher Tate | cefba58 | 2013-11-14 18:10:35 -0800 | [diff] [blame] | 114 | public String name() { |
| 115 | return new ComponentName(mContext, this.getClass()).flattenToShortString(); |
| 116 | } |
| 117 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 118 | @Override |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 119 | public Intent configurationIntent() { |
| 120 | // The local transport is not user-configurable |
| 121 | return null; |
| 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 String currentDestinationString() { |
| 126 | return TRANSPORT_DESTINATION_STRING; |
| 127 | } |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 128 | |
Christopher Tate | 9679410 | 2014-07-27 20:21:55 -0700 | [diff] [blame] | 129 | public Intent dataManagementIntent() { |
| 130 | // The local transport does not present a data-management UI |
| 131 | // TODO: consider adding simple UI to wipe the archives entirely, |
| 132 | // for cleaning up the cache partition. |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | public String dataManagementLabel() { |
| 137 | return TRANSPORT_DATA_MANAGEMENT_LABEL; |
| 138 | } |
| 139 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 140 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 141 | public String transportDirName() { |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 142 | return TRANSPORT_DIR_NAME; |
| 143 | } |
| 144 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 145 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 146 | public long requestBackupTime() { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 147 | // any time is a good time for local backup |
| 148 | return 0; |
| 149 | } |
| 150 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 151 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 152 | public int initializeDevice() { |
| 153 | if (DEBUG) Log.v(TAG, "wiping all data"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 154 | deleteContents(mCurrentSetDir); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 155 | return TRANSPORT_OK; |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 158 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 159 | public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) { |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 160 | if (DEBUG) { |
| 161 | try { |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 162 | StructStat ss = Os.fstat(data.getFileDescriptor()); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 163 | Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName |
| 164 | + " size=" + ss.st_size); |
| 165 | } catch (ErrnoException e) { |
| 166 | Log.w(TAG, "Unable to stat input file in performBackup() on " |
| 167 | + packageInfo.packageName); |
| 168 | } |
| 169 | } |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 170 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 171 | File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 172 | packageDir.mkdirs(); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 173 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 174 | // Each 'record' in the restore set is kept in its own file, named by |
| 175 | // the record key. Wind through the data file, extracting individual |
| 176 | // record operations and building a set of all the updates to apply |
| 177 | // in this update. |
| 178 | BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor()); |
| 179 | try { |
| 180 | int bufSize = 512; |
| 181 | byte[] buf = new byte[bufSize]; |
| 182 | while (changeSet.readNextHeader()) { |
| 183 | String key = changeSet.getKey(); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 184 | String base64Key = new String(Base64.encode(key.getBytes())); |
| 185 | File entityFile = new File(packageDir, base64Key); |
| 186 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 187 | int dataSize = changeSet.getDataSize(); |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 188 | |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 189 | if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize |
| 190 | + " key64=" + base64Key); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 191 | |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 192 | if (dataSize >= 0) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 193 | if (entityFile.exists()) { |
| 194 | entityFile.delete(); |
| 195 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 196 | FileOutputStream entity = new FileOutputStream(entityFile); |
| 197 | |
| 198 | if (dataSize > bufSize) { |
| 199 | bufSize = dataSize; |
| 200 | buf = new byte[bufSize]; |
| 201 | } |
| 202 | changeSet.readEntityData(buf, 0, dataSize); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 203 | if (DEBUG) { |
| 204 | try { |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 205 | long cur = Os.lseek(data.getFileDescriptor(), 0, SEEK_CUR); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 206 | Log.v(TAG, " read entity data; new pos=" + cur); |
| 207 | } |
| 208 | catch (ErrnoException e) { |
| 209 | Log.w(TAG, "Unable to stat input file in performBackup() on " |
| 210 | + packageInfo.packageName); |
| 211 | } |
| 212 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 213 | |
| 214 | try { |
| 215 | entity.write(buf, 0, dataSize); |
| 216 | } catch (IOException e) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 217 | Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath()); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 218 | return TRANSPORT_ERROR; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 219 | } finally { |
| 220 | entity.close(); |
| 221 | } |
| 222 | } else { |
| 223 | entityFile.delete(); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 226 | return TRANSPORT_OK; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 227 | } catch (IOException e) { |
| 228 | // oops, something went wrong. abort the operation and return error. |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 229 | Log.v(TAG, "Exception reading backup input:", e); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 230 | return TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 231 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 232 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 233 | |
Christopher Tate | 25a747f | 2009-09-20 12:43:58 -0700 | [diff] [blame] | 234 | // Deletes the contents but not the given directory |
| 235 | private void deleteContents(File dirname) { |
| 236 | File[] contents = dirname.listFiles(); |
| 237 | if (contents != null) { |
| 238 | for (File f : contents) { |
| 239 | if (f.isDirectory()) { |
| 240 | // delete the directory's contents then fall through |
| 241 | // and delete the directory itself. |
| 242 | deleteContents(f); |
| 243 | } |
| 244 | f.delete(); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 249 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 250 | public int clearBackupData(PackageInfo packageInfo) { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 251 | if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName); |
| 252 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 253 | File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName); |
Christopher Tate | 0abf6a0 | 2012-03-23 17:45:15 -0700 | [diff] [blame] | 254 | final File[] fileset = packageDir.listFiles(); |
| 255 | if (fileset != null) { |
| 256 | for (File f : fileset) { |
| 257 | f.delete(); |
| 258 | } |
| 259 | packageDir.delete(); |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 260 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 261 | |
| 262 | packageDir = new File(mCurrentSetFullDir, packageInfo.packageName); |
| 263 | final File[] tarballs = packageDir.listFiles(); |
| 264 | if (tarballs != null) { |
| 265 | for (File f : tarballs) { |
| 266 | f.delete(); |
| 267 | } |
| 268 | packageDir.delete(); |
| 269 | } |
| 270 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 271 | return TRANSPORT_OK; |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 274 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 275 | public int finishBackup() { |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 276 | if (DEBUG) Log.v(TAG, "finishBackup() of " + mFullTargetPackage); |
| 277 | return tearDownFullBackup(); |
| 278 | } |
| 279 | |
| 280 | // ------------------------------------------------------------------------------------ |
| 281 | // Full backup handling |
| 282 | |
| 283 | private int tearDownFullBackup() { |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 284 | if (mSocket != null) { |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 285 | try { |
| 286 | mFullBackupOutputStream.flush(); |
| 287 | mFullBackupOutputStream.close(); |
| 288 | mSocketInputStream = null; |
| 289 | mFullTargetPackage = null; |
| 290 | mSocket.close(); |
| 291 | } catch (IOException e) { |
Christopher Tate | 89101f7 | 2014-07-17 19:09:00 -0700 | [diff] [blame] | 292 | if (DEBUG) { |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 293 | Log.w(TAG, "Exception caught in tearDownFullBackup()", e); |
Christopher Tate | 89101f7 | 2014-07-17 19:09:00 -0700 | [diff] [blame] | 294 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 295 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 296 | } finally { |
| 297 | mSocket = null; |
| 298 | } |
| 299 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 300 | return TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 303 | private File tarballFile(String pkgName) { |
| 304 | return new File(mCurrentSetFullDir, pkgName); |
| 305 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 306 | |
| 307 | @Override |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 308 | public long requestFullBackupTime() { |
| 309 | return 0; |
| 310 | } |
| 311 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 312 | @Override |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 313 | public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket) { |
| 314 | if (mSocket != null) { |
| 315 | 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] | 316 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (DEBUG) { |
| 320 | Log.i(TAG, "performFullBackup : " + targetPackage); |
| 321 | } |
| 322 | |
| 323 | // We know a priori that we run in the system process, so we need to make |
| 324 | // sure to dup() our own copy of the socket fd. Transports which run in |
| 325 | // their own processes must not do this. |
| 326 | try { |
| 327 | mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 328 | mSocketInputStream = new FileInputStream(mSocket.getFileDescriptor()); |
| 329 | } catch (IOException e) { |
| 330 | Log.e(TAG, "Unable to process socket for full backup"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 331 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | mFullTargetPackage = targetPackage.packageName; |
| 335 | FileOutputStream tarstream; |
| 336 | try { |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 337 | File tarball = tarballFile(mFullTargetPackage); |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 338 | tarstream = new FileOutputStream(tarball); |
| 339 | } catch (FileNotFoundException e) { |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 340 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 341 | } |
| 342 | mFullBackupOutputStream = new BufferedOutputStream(tarstream); |
| 343 | mFullBackupBuffer = new byte[4096]; |
| 344 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 345 | return TRANSPORT_OK; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 348 | @Override |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 349 | public int sendBackupData(int numBytes) { |
| 350 | if (mFullBackupBuffer == null) { |
| 351 | Log.w(TAG, "Attempted sendBackupData before performFullBackup"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 352 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (numBytes > mFullBackupBuffer.length) { |
| 356 | mFullBackupBuffer = new byte[numBytes]; |
| 357 | } |
| 358 | while (numBytes > 0) { |
| 359 | try { |
| 360 | int nRead = mSocketInputStream.read(mFullBackupBuffer, 0, numBytes); |
| 361 | if (nRead < 0) { |
| 362 | // Something went wrong if we expect data but saw EOD |
| 363 | Log.w(TAG, "Unexpected EOD; failing backup"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 364 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 365 | } |
| 366 | mFullBackupOutputStream.write(mFullBackupBuffer, 0, nRead); |
| 367 | numBytes -= nRead; |
| 368 | } catch (IOException e) { |
| 369 | Log.e(TAG, "Error handling backup data for " + mFullTargetPackage); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 370 | return TRANSPORT_ERROR; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 373 | return TRANSPORT_OK; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Christopher Tate | e079264 | 2014-08-07 14:19:50 -0700 | [diff] [blame] | 376 | // For now we can't roll back, so just tear everything down. |
| 377 | @Override |
| 378 | public void cancelFullBackup() { |
| 379 | if (DEBUG) { |
| 380 | Log.i(TAG, "Canceling full backup of " + mFullTargetPackage); |
| 381 | } |
| 382 | File archive = tarballFile(mFullTargetPackage); |
| 383 | tearDownFullBackup(); |
| 384 | if (archive.exists()) { |
| 385 | archive.delete(); |
| 386 | } |
| 387 | } |
| 388 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 389 | // ------------------------------------------------------------------------------------ |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 390 | // Restore handling |
Christopher Tate | 51fea57 | 2014-06-23 17:01:06 -0700 | [diff] [blame] | 391 | 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] | 392 | |
| 393 | @Override |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 394 | public RestoreSet[] getAvailableRestoreSets() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 395 | long[] existing = new long[POSSIBLE_SETS.length + 1]; |
| 396 | int num = 0; |
| 397 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 398 | // see which possible non-current sets exist... |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 399 | for (long token : POSSIBLE_SETS) { |
| 400 | if ((new File(mDataDir, Long.toString(token))).exists()) { |
| 401 | existing[num++] = token; |
| 402 | } |
| 403 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 404 | // ...and always the currently-active set last |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 405 | existing[num++] = CURRENT_SET_TOKEN; |
| 406 | |
| 407 | RestoreSet[] available = new RestoreSet[num]; |
| 408 | for (int i = 0; i < available.length; i++) { |
| 409 | available[i] = new RestoreSet("Local disk image", "flash", existing[i]); |
| 410 | } |
| 411 | return available; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 414 | @Override |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 415 | public long getCurrentRestoreSet() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 416 | // The current restore set always has the same token |
| 417 | return CURRENT_SET_TOKEN; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 420 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 421 | public int startRestore(long token, PackageInfo[] packages) { |
Christopher Tate | 51fea57 | 2014-06-23 17:01:06 -0700 | [diff] [blame] | 422 | if (DEBUG) Log.v(TAG, "start restore " + token + " : " + packages.length |
| 423 | + " matching packages"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 424 | mRestorePackages = packages; |
| 425 | mRestorePackage = -1; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 426 | mRestoreToken = token; |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 427 | mRestoreSetDir = new File(mDataDir, Long.toString(token)); |
| 428 | mRestoreSetIncrementalDir = new File(mRestoreSetDir, INCREMENTAL_DIR); |
| 429 | mRestoreSetFullDir = new File(mRestoreSetDir, FULL_DATA_DIR); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 430 | return TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 433 | @Override |
| 434 | public RestoreDescription nextRestorePackage() { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 435 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 436 | |
| 437 | boolean found = false; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 438 | while (++mRestorePackage < mRestorePackages.length) { |
| 439 | String name = mRestorePackages[mRestorePackage].packageName; |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 440 | |
| 441 | // If we have key/value data for this package, deliver that |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 442 | // skip packages where we have a data dir but no actual contents |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 443 | String[] contents = (new File(mRestoreSetIncrementalDir, name)).list(); |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 444 | if (contents != null && contents.length > 0) { |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 445 | if (DEBUG) Log.v(TAG, " nextRestorePackage(TYPE_KEY_VALUE) = " + name); |
| 446 | mRestoreType = RestoreDescription.TYPE_KEY_VALUE; |
| 447 | found = true; |
| 448 | } |
| 449 | |
| 450 | if (!found) { |
| 451 | // No key/value data; check for [non-empty] full data |
| 452 | File maybeFullData = new File(mRestoreSetFullDir, name); |
| 453 | if (maybeFullData.length() > 0) { |
| 454 | if (DEBUG) Log.v(TAG, " nextRestorePackage(TYPE_FULL_STREAM) = " + name); |
| 455 | mRestoreType = RestoreDescription.TYPE_FULL_STREAM; |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 456 | mCurFullRestoreStream = null; // ensure starting from the ground state |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 457 | found = true; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (found) { |
| 462 | return new RestoreDescription(name, mRestoreType); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 465 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 466 | if (DEBUG) Log.v(TAG, " no more packages to restore"); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 467 | return RestoreDescription.NO_MORE_PACKAGES; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 470 | @Override |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 471 | public int getRestoreData(ParcelFileDescriptor outFd) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 472 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 473 | if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called"); |
Christopher Tate | 6a49dd0 | 2014-06-16 18:49:25 -0700 | [diff] [blame] | 474 | if (mRestoreType != RestoreDescription.TYPE_KEY_VALUE) { |
| 475 | throw new IllegalStateException("getRestoreData(fd) for non-key/value dataset"); |
| 476 | } |
Christopher Tate | 51fea57 | 2014-06-23 17:01:06 -0700 | [diff] [blame] | 477 | File packageDir = new File(mRestoreSetIncrementalDir, |
| 478 | mRestorePackages[mRestorePackage].packageName); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 479 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 480 | // The restore set is the concatenation of the individual record blobs, |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 481 | // each of which is a file in the package's directory. We return the |
| 482 | // data in lexical order sorted by key, so that apps which use synthetic |
| 483 | // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious |
| 484 | // order. |
| 485 | ArrayList<DecodedFilename> blobs = contentsByKey(packageDir); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 486 | 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] | 487 | Log.e(TAG, "No keys for package: " + packageDir); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 488 | return TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 489 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 490 | |
| 491 | // 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] | 492 | if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 493 | BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor()); |
| 494 | try { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 495 | for (DecodedFilename keyEntry : blobs) { |
| 496 | File f = keyEntry.file; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 497 | FileInputStream in = new FileInputStream(f); |
| 498 | try { |
| 499 | int size = (int) f.length(); |
| 500 | byte[] buf = new byte[size]; |
| 501 | in.read(buf); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 502 | if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size); |
| 503 | out.writeEntityHeader(keyEntry.key, size); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 504 | out.writeEntityData(buf, size); |
| 505 | } finally { |
| 506 | in.close(); |
| 507 | } |
| 508 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 509 | return TRANSPORT_OK; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 510 | } catch (IOException e) { |
| 511 | Log.e(TAG, "Unable to read backup records", e); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 512 | return TRANSPORT_ERROR; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 513 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 514 | } |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 515 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 516 | static class DecodedFilename implements Comparable<DecodedFilename> { |
| 517 | public File file; |
| 518 | public String key; |
| 519 | |
| 520 | public DecodedFilename(File f) { |
| 521 | file = f; |
| 522 | key = new String(Base64.decode(f.getName())); |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public int compareTo(DecodedFilename other) { |
| 527 | // sorts into ascending lexical order by decoded key |
| 528 | return key.compareTo(other.key); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // Return a list of the files in the given directory, sorted lexically by |
| 533 | // the Base64-decoded file name, not by the on-disk filename |
| 534 | private ArrayList<DecodedFilename> contentsByKey(File dir) { |
| 535 | File[] allFiles = dir.listFiles(); |
| 536 | if (allFiles == null || allFiles.length == 0) { |
| 537 | return null; |
| 538 | } |
| 539 | |
| 540 | // Decode the filenames into keys then sort lexically by key |
| 541 | ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>(); |
| 542 | for (File f : allFiles) { |
| 543 | contents.add(new DecodedFilename(f)); |
| 544 | } |
| 545 | Collections.sort(contents); |
| 546 | return contents; |
| 547 | } |
| 548 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 549 | @Override |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 550 | public void finishRestore() { |
| 551 | if (DEBUG) Log.v(TAG, "finishRestore()"); |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 552 | if (mRestoreType == RestoreDescription.TYPE_FULL_STREAM) { |
| 553 | resetFullRestoreState(); |
| 554 | } |
| 555 | mRestoreType = 0; |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 556 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 557 | |
| 558 | // ------------------------------------------------------------------------------------ |
| 559 | // Full restore handling |
| 560 | |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 561 | private void resetFullRestoreState() { |
| 562 | try { |
| 563 | mCurFullRestoreStream.close(); |
| 564 | } catch (IOException e) { |
| 565 | Log.w(TAG, "Unable to close full restore input stream"); |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 566 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 567 | mCurFullRestoreStream = null; |
| 568 | mFullRestoreSocketStream = null; |
| 569 | mFullRestoreBuffer = null; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Ask the transport to provide data for the "current" package being restored. The |
| 574 | * transport then writes some data to the socket supplied to this call, and returns |
| 575 | * the number of bytes written. The system will then read that many bytes and |
| 576 | * stream them to the application's agent for restore, then will call this method again |
| 577 | * to receive the next chunk of the archive. This sequence will be repeated until the |
| 578 | * transport returns zero indicating that all of the package's data has been delivered |
| 579 | * (or returns a negative value indicating some sort of hard error condition at the |
| 580 | * transport level). |
| 581 | * |
| 582 | * <p>After this method returns zero, the system will then call |
| 583 | * {@link #getNextFullRestorePackage()} to begin the restore process for the next |
| 584 | * application, and the sequence begins again. |
| 585 | * |
| 586 | * @param socket The file descriptor that the transport will use for delivering the |
| 587 | * streamed archive. |
| 588 | * @return 0 when no more data for the current package is available. A positive value |
| 589 | * indicates the presence of that much data to be delivered to the app. A negative |
| 590 | * return value is treated as equivalent to {@link BackupTransport#TRANSPORT_ERROR}, |
| 591 | * indicating a fatal error condition that precludes further restore operations |
| 592 | * on the current dataset. |
| 593 | */ |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 594 | @Override |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 595 | public int getNextFullRestoreDataChunk(ParcelFileDescriptor socket) { |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 596 | if (mRestoreType != RestoreDescription.TYPE_FULL_STREAM) { |
| 597 | throw new IllegalStateException("Asked for full restore data for non-stream package"); |
| 598 | } |
| 599 | |
| 600 | // first chunk? |
| 601 | if (mCurFullRestoreStream == null) { |
| 602 | final String name = mRestorePackages[mRestorePackage].packageName; |
| 603 | if (DEBUG) Log.i(TAG, "Starting full restore of " + name); |
| 604 | File dataset = new File(mRestoreSetFullDir, name); |
| 605 | try { |
| 606 | mCurFullRestoreStream = new FileInputStream(dataset); |
| 607 | } catch (IOException e) { |
| 608 | // If we can't open the target package's tarball, we return the single-package |
| 609 | // error code and let the caller go on to the next package. |
| 610 | Log.e(TAG, "Unable to read archive for " + name); |
| 611 | return TRANSPORT_PACKAGE_REJECTED; |
| 612 | } |
| 613 | mFullRestoreSocketStream = new FileOutputStream(socket.getFileDescriptor()); |
Christopher Tate | 89101f7 | 2014-07-17 19:09:00 -0700 | [diff] [blame] | 614 | mFullRestoreBuffer = new byte[2*1024]; |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | int nRead; |
| 618 | try { |
| 619 | nRead = mCurFullRestoreStream.read(mFullRestoreBuffer); |
| 620 | if (nRead < 0) { |
| 621 | // EOF: tell the caller we're done |
| 622 | nRead = NO_MORE_DATA; |
| 623 | } else if (nRead == 0) { |
| 624 | // This shouldn't happen when reading a FileInputStream; we should always |
| 625 | // get either a positive nonzero byte count or -1. Log the situation and |
| 626 | // treat it as EOF. |
| 627 | Log.w(TAG, "read() of archive file returned 0; treating as EOF"); |
| 628 | nRead = NO_MORE_DATA; |
| 629 | } else { |
| 630 | if (DEBUG) { |
| 631 | Log.i(TAG, " delivering restore chunk: " + nRead); |
| 632 | } |
| 633 | mFullRestoreSocketStream.write(mFullRestoreBuffer, 0, nRead); |
| 634 | } |
| 635 | } catch (IOException e) { |
| 636 | return TRANSPORT_ERROR; // Hard error accessing the file; shouldn't happen |
| 637 | } finally { |
| 638 | // Most transports will need to explicitly close 'socket' here, but this transport |
| 639 | // is in the same process as the caller so it can leave it up to the backup manager |
| 640 | // to manage both socket fds. |
| 641 | } |
| 642 | |
| 643 | return nRead; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame] | 644 | } |
Christopher Tate | 5a009f9 | 2014-06-19 14:53:18 -0700 | [diff] [blame] | 645 | |
| 646 | /** |
| 647 | * If the OS encounters an error while processing {@link RestoreDescription#TYPE_FULL_STREAM} |
| 648 | * data for restore, it will invoke this method to tell the transport that it should |
| 649 | * abandon the data download for the current package. The OS will then either call |
| 650 | * {@link #nextRestorePackage()} again to move on to restoring the next package in the |
| 651 | * set being iterated over, or will call {@link #finishRestore()} to shut down the restore |
| 652 | * operation. |
| 653 | * |
| 654 | * @return {@link #TRANSPORT_OK} if the transport was successful in shutting down the |
| 655 | * current stream cleanly, or {@link #TRANSPORT_ERROR} to indicate a serious |
| 656 | * transport-level failure. If the transport reports an error here, the entire restore |
| 657 | * operation will immediately be finished with no further attempts to restore app data. |
| 658 | */ |
| 659 | @Override |
| 660 | public int abortFullRestore() { |
| 661 | if (mRestoreType != RestoreDescription.TYPE_FULL_STREAM) { |
| 662 | throw new IllegalStateException("abortFullRestore() but not currently restoring"); |
| 663 | } |
| 664 | resetFullRestoreState(); |
| 665 | mRestoreType = 0; |
| 666 | return TRANSPORT_OK; |
| 667 | } |
| 668 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 669 | } |