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