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