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 | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 22 | import android.app.backup.RestoreSet; |
Christopher Tate | cefba58 | 2013-11-14 18:10:35 -0800 | [diff] [blame] | 23 | import android.content.ComponentName; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageInfo; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 27 | import android.os.Environment; |
| 28 | import android.os.ParcelFileDescriptor; |
rpcraig | ebab0ae | 2012-12-04 09:37:23 -0500 | [diff] [blame] | 29 | import android.os.SELinux; |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 30 | import android.system.ErrnoException; |
| 31 | import android.system.Os; |
| 32 | import android.system.StructStat; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
Brian Carlstrom | 4140fae | 2011-01-24 16:17:43 -0800 | [diff] [blame] | 35 | import com.android.org.bouncycastle.util.encoders.Base64; |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 36 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 37 | import java.io.BufferedOutputStream; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 38 | import java.io.File; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 39 | import java.io.FileInputStream; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 40 | import java.io.FileNotFoundException; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 41 | import java.io.FileOutputStream; |
| 42 | import java.io.IOException; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 43 | import java.util.ArrayList; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 44 | import java.util.Arrays; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 45 | import java.util.Collections; |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 46 | import java.util.HashSet; |
| 47 | import java.util.List; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 49 | import static android.system.OsConstants.*; |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 50 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Backup transport for stashing stuff into a known location on disk, and |
| 53 | * later restoring from there. For testing only. |
| 54 | */ |
| 55 | |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 56 | public class LocalTransport extends BackupTransport { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 57 | private static final String TAG = "LocalTransport"; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 58 | private static final boolean DEBUG = true; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 59 | |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 60 | private static final String TRANSPORT_DIR_NAME |
| 61 | = "com.android.internal.backup.LocalTransport"; |
| 62 | |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 63 | private static final String TRANSPORT_DESTINATION_STRING |
| 64 | = "Backing up to debug-only private cache"; |
| 65 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 66 | // The currently-active restore set always has the same (nonzero!) token |
| 67 | private static final long CURRENT_SET_TOKEN = 1; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 68 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 69 | private Context mContext; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 70 | private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 71 | private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN)); |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 72 | private File mCurrentSetIncrementalDir = new File(mCurrentSetDir, "_delta"); |
| 73 | private File mCurrentSetFullDir = new File(mCurrentSetDir, "_full"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 74 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 75 | private PackageInfo[] mRestorePackages = null; |
| 76 | private int mRestorePackage = -1; // Index into mRestorePackages |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 77 | private File mRestoreDataDir; |
| 78 | private long mRestoreToken; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 79 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 80 | // Additional bookkeeping for full backup |
| 81 | private String mFullTargetPackage; |
| 82 | private ParcelFileDescriptor mSocket; |
| 83 | private FileInputStream mSocketInputStream; |
| 84 | private BufferedOutputStream mFullBackupOutputStream; |
| 85 | private byte[] mFullBackupBuffer; |
| 86 | |
| 87 | private File mFullRestoreSetDir; |
| 88 | private HashSet<String> mFullRestorePackages; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 89 | |
| 90 | public LocalTransport(Context context) { |
| 91 | mContext = context; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 92 | mCurrentSetDir.mkdirs(); |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 93 | mCurrentSetFullDir.mkdir(); |
| 94 | mCurrentSetIncrementalDir.mkdir(); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 95 | if (!SELinux.restorecon(mCurrentSetDir)) { |
| 96 | Log.e(TAG, "SELinux restorecon failed for " + mCurrentSetDir); |
rpcraig | ebab0ae | 2012-12-04 09:37:23 -0500 | [diff] [blame] | 97 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Christopher Tate | cefba58 | 2013-11-14 18:10:35 -0800 | [diff] [blame] | 100 | public String name() { |
| 101 | return new ComponentName(mContext, this.getClass()).flattenToShortString(); |
| 102 | } |
| 103 | |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 104 | public Intent configurationIntent() { |
| 105 | // The local transport is not user-configurable |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | public String currentDestinationString() { |
| 110 | return TRANSPORT_DESTINATION_STRING; |
| 111 | } |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 112 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 113 | public String transportDirName() { |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 114 | return TRANSPORT_DIR_NAME; |
| 115 | } |
| 116 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 117 | public long requestBackupTime() { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 118 | // any time is a good time for local backup |
| 119 | return 0; |
| 120 | } |
| 121 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 122 | public int initializeDevice() { |
| 123 | if (DEBUG) Log.v(TAG, "wiping all data"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 124 | deleteContents(mCurrentSetDir); |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 125 | return BackupTransport.TRANSPORT_OK; |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) { |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 129 | if (DEBUG) { |
| 130 | try { |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 131 | StructStat ss = Os.fstat(data.getFileDescriptor()); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 132 | Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName |
| 133 | + " size=" + ss.st_size); |
| 134 | } catch (ErrnoException e) { |
| 135 | Log.w(TAG, "Unable to stat input file in performBackup() on " |
| 136 | + packageInfo.packageName); |
| 137 | } |
| 138 | } |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 139 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 140 | File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 141 | packageDir.mkdirs(); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 142 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 143 | // Each 'record' in the restore set is kept in its own file, named by |
| 144 | // the record key. Wind through the data file, extracting individual |
| 145 | // record operations and building a set of all the updates to apply |
| 146 | // in this update. |
| 147 | BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor()); |
| 148 | try { |
| 149 | int bufSize = 512; |
| 150 | byte[] buf = new byte[bufSize]; |
| 151 | while (changeSet.readNextHeader()) { |
| 152 | String key = changeSet.getKey(); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 153 | String base64Key = new String(Base64.encode(key.getBytes())); |
| 154 | File entityFile = new File(packageDir, base64Key); |
| 155 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 156 | int dataSize = changeSet.getDataSize(); |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 157 | |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 158 | if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize |
| 159 | + " key64=" + base64Key); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 160 | |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 161 | if (dataSize >= 0) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 162 | if (entityFile.exists()) { |
| 163 | entityFile.delete(); |
| 164 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 165 | FileOutputStream entity = new FileOutputStream(entityFile); |
| 166 | |
| 167 | if (dataSize > bufSize) { |
| 168 | bufSize = dataSize; |
| 169 | buf = new byte[bufSize]; |
| 170 | } |
| 171 | changeSet.readEntityData(buf, 0, dataSize); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 172 | if (DEBUG) { |
| 173 | try { |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 174 | long cur = Os.lseek(data.getFileDescriptor(), 0, SEEK_CUR); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 175 | Log.v(TAG, " read entity data; new pos=" + cur); |
| 176 | } |
| 177 | catch (ErrnoException e) { |
| 178 | Log.w(TAG, "Unable to stat input file in performBackup() on " |
| 179 | + packageInfo.packageName); |
| 180 | } |
| 181 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 182 | |
| 183 | try { |
| 184 | entity.write(buf, 0, dataSize); |
| 185 | } catch (IOException e) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 186 | Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath()); |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 187 | return BackupTransport.TRANSPORT_ERROR; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 188 | } finally { |
| 189 | entity.close(); |
| 190 | } |
| 191 | } else { |
| 192 | entityFile.delete(); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 195 | return BackupTransport.TRANSPORT_OK; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 196 | } catch (IOException e) { |
| 197 | // oops, something went wrong. abort the operation and return error. |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 198 | Log.v(TAG, "Exception reading backup input:", e); |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 199 | return BackupTransport.TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 200 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 201 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 202 | |
Christopher Tate | 25a747f | 2009-09-20 12:43:58 -0700 | [diff] [blame] | 203 | // Deletes the contents but not the given directory |
| 204 | private void deleteContents(File dirname) { |
| 205 | File[] contents = dirname.listFiles(); |
| 206 | if (contents != null) { |
| 207 | for (File f : contents) { |
| 208 | if (f.isDirectory()) { |
| 209 | // delete the directory's contents then fall through |
| 210 | // and delete the directory itself. |
| 211 | deleteContents(f); |
| 212 | } |
| 213 | f.delete(); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 218 | public int clearBackupData(PackageInfo packageInfo) { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 219 | if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName); |
| 220 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 221 | File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName); |
Christopher Tate | 0abf6a0 | 2012-03-23 17:45:15 -0700 | [diff] [blame] | 222 | final File[] fileset = packageDir.listFiles(); |
| 223 | if (fileset != null) { |
| 224 | for (File f : fileset) { |
| 225 | f.delete(); |
| 226 | } |
| 227 | packageDir.delete(); |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 228 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 229 | |
| 230 | packageDir = new File(mCurrentSetFullDir, packageInfo.packageName); |
| 231 | final File[] tarballs = packageDir.listFiles(); |
| 232 | if (tarballs != null) { |
| 233 | for (File f : tarballs) { |
| 234 | f.delete(); |
| 235 | } |
| 236 | packageDir.delete(); |
| 237 | } |
| 238 | |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 239 | return BackupTransport.TRANSPORT_OK; |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 242 | public int finishBackup() { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 243 | if (DEBUG) Log.v(TAG, "finishBackup()"); |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 244 | if (mSocket != null) { |
| 245 | if (DEBUG) { |
| 246 | Log.v(TAG, "Concluding full backup of " + mFullTargetPackage); |
| 247 | } |
| 248 | try { |
| 249 | mFullBackupOutputStream.flush(); |
| 250 | mFullBackupOutputStream.close(); |
| 251 | mSocketInputStream = null; |
| 252 | mFullTargetPackage = null; |
| 253 | mSocket.close(); |
| 254 | } catch (IOException e) { |
| 255 | return BackupTransport.TRANSPORT_ERROR; |
| 256 | } finally { |
| 257 | mSocket = null; |
| 258 | } |
| 259 | } |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 260 | return BackupTransport.TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 263 | // ------------------------------------------------------------------------------------ |
| 264 | // Full backup handling |
| 265 | public long requestFullBackupTime() { |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket) { |
| 270 | if (mSocket != null) { |
| 271 | Log.e(TAG, "Attempt to initiate full backup while one is in progress"); |
| 272 | return BackupTransport.TRANSPORT_ERROR; |
| 273 | } |
| 274 | |
| 275 | if (DEBUG) { |
| 276 | Log.i(TAG, "performFullBackup : " + targetPackage); |
| 277 | } |
| 278 | |
| 279 | // We know a priori that we run in the system process, so we need to make |
| 280 | // sure to dup() our own copy of the socket fd. Transports which run in |
| 281 | // their own processes must not do this. |
| 282 | try { |
| 283 | mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 284 | mSocketInputStream = new FileInputStream(mSocket.getFileDescriptor()); |
| 285 | } catch (IOException e) { |
| 286 | Log.e(TAG, "Unable to process socket for full backup"); |
| 287 | return BackupTransport.TRANSPORT_ERROR; |
| 288 | } |
| 289 | |
| 290 | mFullTargetPackage = targetPackage.packageName; |
| 291 | FileOutputStream tarstream; |
| 292 | try { |
| 293 | File tarball = new File(mCurrentSetFullDir, mFullTargetPackage); |
| 294 | tarstream = new FileOutputStream(tarball); |
| 295 | } catch (FileNotFoundException e) { |
| 296 | return BackupTransport.TRANSPORT_ERROR; |
| 297 | } |
| 298 | mFullBackupOutputStream = new BufferedOutputStream(tarstream); |
| 299 | mFullBackupBuffer = new byte[4096]; |
| 300 | |
| 301 | return BackupTransport.TRANSPORT_OK; |
| 302 | } |
| 303 | |
| 304 | public int sendBackupData(int numBytes) { |
| 305 | if (mFullBackupBuffer == null) { |
| 306 | Log.w(TAG, "Attempted sendBackupData before performFullBackup"); |
| 307 | return BackupTransport.TRANSPORT_ERROR; |
| 308 | } |
| 309 | |
| 310 | if (numBytes > mFullBackupBuffer.length) { |
| 311 | mFullBackupBuffer = new byte[numBytes]; |
| 312 | } |
| 313 | while (numBytes > 0) { |
| 314 | try { |
| 315 | int nRead = mSocketInputStream.read(mFullBackupBuffer, 0, numBytes); |
| 316 | if (nRead < 0) { |
| 317 | // Something went wrong if we expect data but saw EOD |
| 318 | Log.w(TAG, "Unexpected EOD; failing backup"); |
| 319 | return BackupTransport.TRANSPORT_ERROR; |
| 320 | } |
| 321 | mFullBackupOutputStream.write(mFullBackupBuffer, 0, nRead); |
| 322 | numBytes -= nRead; |
| 323 | } catch (IOException e) { |
| 324 | Log.e(TAG, "Error handling backup data for " + mFullTargetPackage); |
| 325 | return BackupTransport.TRANSPORT_ERROR; |
| 326 | } |
| 327 | } |
| 328 | return BackupTransport.TRANSPORT_OK; |
| 329 | } |
| 330 | |
| 331 | // ------------------------------------------------------------------------------------ |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 332 | // Restore handling |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 333 | static final long[] POSSIBLE_SETS = { 2, 3, 4, 5, 6, 7, 8, 9 }; |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame] | 334 | public RestoreSet[] getAvailableRestoreSets() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 335 | long[] existing = new long[POSSIBLE_SETS.length + 1]; |
| 336 | int num = 0; |
| 337 | |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 338 | // see which possible non-current sets exist... |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 339 | for (long token : POSSIBLE_SETS) { |
| 340 | if ((new File(mDataDir, Long.toString(token))).exists()) { |
| 341 | existing[num++] = token; |
| 342 | } |
| 343 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 344 | // ...and always the currently-active set last |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 345 | existing[num++] = CURRENT_SET_TOKEN; |
| 346 | |
| 347 | RestoreSet[] available = new RestoreSet[num]; |
| 348 | for (int i = 0; i < available.length; i++) { |
| 349 | available[i] = new RestoreSet("Local disk image", "flash", existing[i]); |
| 350 | } |
| 351 | return available; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 354 | public long getCurrentRestoreSet() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 355 | // The current restore set always has the same token |
| 356 | return CURRENT_SET_TOKEN; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 359 | public int startRestore(long token, PackageInfo[] packages) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 360 | if (DEBUG) Log.v(TAG, "start restore " + token); |
| 361 | mRestorePackages = packages; |
| 362 | mRestorePackage = -1; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 363 | mRestoreToken = token; |
| 364 | mRestoreDataDir = new File(mDataDir, Long.toString(token)); |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 365 | return BackupTransport.TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 368 | public String nextRestorePackage() { |
| 369 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 370 | while (++mRestorePackage < mRestorePackages.length) { |
| 371 | String name = mRestorePackages[mRestorePackage].packageName; |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 372 | // skip packages where we have a data dir but no actual contents |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 373 | String[] contents = (new File(mRestoreDataDir, name)).list(); |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 374 | if (contents != null && contents.length > 0) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 375 | if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name); |
| 376 | return name; |
| 377 | } |
| 378 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 379 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 380 | if (DEBUG) Log.v(TAG, " no more packages to restore"); |
| 381 | return ""; |
| 382 | } |
| 383 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 384 | public int getRestoreData(ParcelFileDescriptor outFd) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 385 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 386 | if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 387 | File packageDir = new File(mRestoreDataDir, mRestorePackages[mRestorePackage].packageName); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 388 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 389 | // The restore set is the concatenation of the individual record blobs, |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 390 | // each of which is a file in the package's directory. We return the |
| 391 | // data in lexical order sorted by key, so that apps which use synthetic |
| 392 | // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious |
| 393 | // order. |
| 394 | ArrayList<DecodedFilename> blobs = contentsByKey(packageDir); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 395 | 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] | 396 | Log.e(TAG, "No keys for package: " + packageDir); |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 397 | return BackupTransport.TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 398 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 399 | |
| 400 | // 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] | 401 | if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 402 | BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor()); |
| 403 | try { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 404 | for (DecodedFilename keyEntry : blobs) { |
| 405 | File f = keyEntry.file; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 406 | FileInputStream in = new FileInputStream(f); |
| 407 | try { |
| 408 | int size = (int) f.length(); |
| 409 | byte[] buf = new byte[size]; |
| 410 | in.read(buf); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 411 | if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size); |
| 412 | out.writeEntityHeader(keyEntry.key, size); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 413 | out.writeEntityData(buf, size); |
| 414 | } finally { |
| 415 | in.close(); |
| 416 | } |
| 417 | } |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 418 | return BackupTransport.TRANSPORT_OK; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 419 | } catch (IOException e) { |
| 420 | Log.e(TAG, "Unable to read backup records", e); |
Christopher Tate | 4dd2635 | 2014-06-02 18:54:18 -0700 | [diff] [blame] | 421 | return BackupTransport.TRANSPORT_ERROR; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 422 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 423 | } |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 424 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 425 | static class DecodedFilename implements Comparable<DecodedFilename> { |
| 426 | public File file; |
| 427 | public String key; |
| 428 | |
| 429 | public DecodedFilename(File f) { |
| 430 | file = f; |
| 431 | key = new String(Base64.decode(f.getName())); |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public int compareTo(DecodedFilename other) { |
| 436 | // sorts into ascending lexical order by decoded key |
| 437 | return key.compareTo(other.key); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // Return a list of the files in the given directory, sorted lexically by |
| 442 | // the Base64-decoded file name, not by the on-disk filename |
| 443 | private ArrayList<DecodedFilename> contentsByKey(File dir) { |
| 444 | File[] allFiles = dir.listFiles(); |
| 445 | if (allFiles == null || allFiles.length == 0) { |
| 446 | return null; |
| 447 | } |
| 448 | |
| 449 | // Decode the filenames into keys then sort lexically by key |
| 450 | ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>(); |
| 451 | for (File f : allFiles) { |
| 452 | contents.add(new DecodedFilename(f)); |
| 453 | } |
| 454 | Collections.sort(contents); |
| 455 | return contents; |
| 456 | } |
| 457 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 458 | public void finishRestore() { |
| 459 | if (DEBUG) Log.v(TAG, "finishRestore()"); |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 460 | } |
Christopher Tate | 9ff53a7 | 2014-06-03 17:20:07 -0700 | [diff] [blame^] | 461 | |
| 462 | // ------------------------------------------------------------------------------------ |
| 463 | // Full restore handling |
| 464 | |
| 465 | public int prepareFullRestore(long token, String[] targetPackages) { |
| 466 | mRestoreDataDir = new File(mDataDir, Long.toString(token)); |
| 467 | mFullRestoreSetDir = new File(mRestoreDataDir, "_full"); |
| 468 | mFullRestorePackages = new HashSet<String>(); |
| 469 | if (mFullRestoreSetDir.exists()) { |
| 470 | List<String> pkgs = Arrays.asList(mFullRestoreSetDir.list()); |
| 471 | HashSet<String> available = new HashSet<String>(pkgs); |
| 472 | |
| 473 | for (int i = 0; i < targetPackages.length; i++) { |
| 474 | if (available.contains(targetPackages[i])) { |
| 475 | mFullRestorePackages.add(targetPackages[i]); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | return BackupTransport.TRANSPORT_OK; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Ask the transport what package's full data will be restored next. When all apps' |
| 484 | * data has been delivered, the transport should return {@code null} here. |
| 485 | * @return The package name of the next application whose data will be restored, or |
| 486 | * {@code null} if all available package has been delivered. |
| 487 | */ |
| 488 | public String getNextFullRestorePackage() { |
| 489 | return null; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Ask the transport to provide data for the "current" package being restored. The |
| 494 | * transport then writes some data to the socket supplied to this call, and returns |
| 495 | * the number of bytes written. The system will then read that many bytes and |
| 496 | * stream them to the application's agent for restore, then will call this method again |
| 497 | * to receive the next chunk of the archive. This sequence will be repeated until the |
| 498 | * transport returns zero indicating that all of the package's data has been delivered |
| 499 | * (or returns a negative value indicating some sort of hard error condition at the |
| 500 | * transport level). |
| 501 | * |
| 502 | * <p>After this method returns zero, the system will then call |
| 503 | * {@link #getNextFullRestorePackage()} to begin the restore process for the next |
| 504 | * application, and the sequence begins again. |
| 505 | * |
| 506 | * @param socket The file descriptor that the transport will use for delivering the |
| 507 | * streamed archive. |
| 508 | * @return 0 when no more data for the current package is available. A positive value |
| 509 | * indicates the presence of that much data to be delivered to the app. A negative |
| 510 | * return value is treated as equivalent to {@link BackupTransport#TRANSPORT_ERROR}, |
| 511 | * indicating a fatal error condition that precludes further restore operations |
| 512 | * on the current dataset. |
| 513 | */ |
| 514 | public int getNextFullRestoreDataChunk(ParcelFileDescriptor socket) { |
| 515 | return 0; |
| 516 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 517 | } |