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