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 | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 37 | import java.io.File; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 38 | import java.io.FileInputStream; |
| 39 | import java.io.FileOutputStream; |
| 40 | import java.io.IOException; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.Collections; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 44 | import static android.system.OsConstants.*; |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 45 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Backup transport for stashing stuff into a known location on disk, and |
| 48 | * later restoring from there. For testing only. |
| 49 | */ |
| 50 | |
Christopher Tate | 74318c9 | 2014-05-15 19:03:44 -0700 | [diff] [blame^] | 51 | public class LocalTransport extends BackupTransport { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 52 | private static final String TAG = "LocalTransport"; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 53 | private static final boolean DEBUG = true; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 54 | |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 55 | private static final String TRANSPORT_DIR_NAME |
| 56 | = "com.android.internal.backup.LocalTransport"; |
| 57 | |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 58 | private static final String TRANSPORT_DESTINATION_STRING |
| 59 | = "Backing up to debug-only private cache"; |
| 60 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 61 | // The currently-active restore set always has the same (nonzero!) token |
| 62 | private static final long CURRENT_SET_TOKEN = 1; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 63 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 64 | private Context mContext; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 65 | private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 66 | private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN)); |
| 67 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 68 | private PackageInfo[] mRestorePackages = null; |
| 69 | private int mRestorePackage = -1; // Index into mRestorePackages |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 70 | private File mRestoreDataDir; |
| 71 | private long mRestoreToken; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 72 | |
| 73 | |
| 74 | public LocalTransport(Context context) { |
| 75 | mContext = context; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 76 | mCurrentSetDir.mkdirs(); |
| 77 | if (!SELinux.restorecon(mCurrentSetDir)) { |
| 78 | Log.e(TAG, "SELinux restorecon failed for " + mCurrentSetDir); |
rpcraig | ebab0ae | 2012-12-04 09:37:23 -0500 | [diff] [blame] | 79 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Christopher Tate | cefba58 | 2013-11-14 18:10:35 -0800 | [diff] [blame] | 82 | public String name() { |
| 83 | return new ComponentName(mContext, this.getClass()).flattenToShortString(); |
| 84 | } |
| 85 | |
Chris Tate | a8ddef3 | 2010-11-10 11:53:26 -0800 | [diff] [blame] | 86 | public Intent configurationIntent() { |
| 87 | // The local transport is not user-configurable |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | public String currentDestinationString() { |
| 92 | return TRANSPORT_DESTINATION_STRING; |
| 93 | } |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 94 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 95 | public String transportDirName() { |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 96 | return TRANSPORT_DIR_NAME; |
| 97 | } |
| 98 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 99 | public long requestBackupTime() { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 100 | // any time is a good time for local backup |
| 101 | return 0; |
| 102 | } |
| 103 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 104 | public int initializeDevice() { |
| 105 | if (DEBUG) Log.v(TAG, "wiping all data"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 106 | deleteContents(mCurrentSetDir); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 107 | return BackupConstants.TRANSPORT_OK; |
| 108 | } |
| 109 | |
| 110 | public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) { |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 111 | if (DEBUG) { |
| 112 | try { |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 113 | StructStat ss = Os.fstat(data.getFileDescriptor()); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 114 | Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName |
| 115 | + " size=" + ss.st_size); |
| 116 | } catch (ErrnoException e) { |
| 117 | Log.w(TAG, "Unable to stat input file in performBackup() on " |
| 118 | + packageInfo.packageName); |
| 119 | } |
| 120 | } |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 121 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 122 | File packageDir = new File(mCurrentSetDir, packageInfo.packageName); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 123 | packageDir.mkdirs(); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 124 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 125 | // Each 'record' in the restore set is kept in its own file, named by |
| 126 | // the record key. Wind through the data file, extracting individual |
| 127 | // record operations and building a set of all the updates to apply |
| 128 | // in this update. |
| 129 | BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor()); |
| 130 | try { |
| 131 | int bufSize = 512; |
| 132 | byte[] buf = new byte[bufSize]; |
| 133 | while (changeSet.readNextHeader()) { |
| 134 | String key = changeSet.getKey(); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 135 | String base64Key = new String(Base64.encode(key.getBytes())); |
| 136 | File entityFile = new File(packageDir, base64Key); |
| 137 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 138 | int dataSize = changeSet.getDataSize(); |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 139 | |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 140 | if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize |
| 141 | + " key64=" + base64Key); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 142 | |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 143 | if (dataSize >= 0) { |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 144 | if (entityFile.exists()) { |
| 145 | entityFile.delete(); |
| 146 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 147 | FileOutputStream entity = new FileOutputStream(entityFile); |
| 148 | |
| 149 | if (dataSize > bufSize) { |
| 150 | bufSize = dataSize; |
| 151 | buf = new byte[bufSize]; |
| 152 | } |
| 153 | changeSet.readEntityData(buf, 0, dataSize); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 154 | if (DEBUG) { |
| 155 | try { |
Elliott Hughes | f97c633 | 2014-04-28 16:38:43 -0700 | [diff] [blame] | 156 | long cur = Os.lseek(data.getFileDescriptor(), 0, SEEK_CUR); |
Christopher Tate | b048c33 | 2014-02-21 12:50:21 -0800 | [diff] [blame] | 157 | Log.v(TAG, " read entity data; new pos=" + cur); |
| 158 | } |
| 159 | catch (ErrnoException e) { |
| 160 | Log.w(TAG, "Unable to stat input file in performBackup() on " |
| 161 | + packageInfo.packageName); |
| 162 | } |
| 163 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 164 | |
| 165 | try { |
| 166 | entity.write(buf, 0, dataSize); |
| 167 | } catch (IOException e) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 168 | Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath()); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 169 | return BackupConstants.TRANSPORT_ERROR; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 170 | } finally { |
| 171 | entity.close(); |
| 172 | } |
| 173 | } else { |
| 174 | entityFile.delete(); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 177 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 178 | } catch (IOException e) { |
| 179 | // oops, something went wrong. abort the operation and return error. |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 180 | Log.v(TAG, "Exception reading backup input:", e); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 181 | return BackupConstants.TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 182 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 183 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 184 | |
Christopher Tate | 25a747f | 2009-09-20 12:43:58 -0700 | [diff] [blame] | 185 | // Deletes the contents but not the given directory |
| 186 | private void deleteContents(File dirname) { |
| 187 | File[] contents = dirname.listFiles(); |
| 188 | if (contents != null) { |
| 189 | for (File f : contents) { |
| 190 | if (f.isDirectory()) { |
| 191 | // delete the directory's contents then fall through |
| 192 | // and delete the directory itself. |
| 193 | deleteContents(f); |
| 194 | } |
| 195 | f.delete(); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 200 | public int clearBackupData(PackageInfo packageInfo) { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 201 | if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName); |
| 202 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 203 | File packageDir = new File(mCurrentSetDir, packageInfo.packageName); |
Christopher Tate | 0abf6a0 | 2012-03-23 17:45:15 -0700 | [diff] [blame] | 204 | final File[] fileset = packageDir.listFiles(); |
| 205 | if (fileset != null) { |
| 206 | for (File f : fileset) { |
| 207 | f.delete(); |
| 208 | } |
| 209 | packageDir.delete(); |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 210 | } |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 211 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 214 | public int finishBackup() { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 215 | if (DEBUG) Log.v(TAG, "finishBackup()"); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 216 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Restore handling |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 220 | 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^] | 221 | public RestoreSet[] getAvailableRestoreSets() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 222 | long[] existing = new long[POSSIBLE_SETS.length + 1]; |
| 223 | int num = 0; |
| 224 | |
| 225 | // see which possible non-current sets exist, then put the current set at the end |
| 226 | for (long token : POSSIBLE_SETS) { |
| 227 | if ((new File(mDataDir, Long.toString(token))).exists()) { |
| 228 | existing[num++] = token; |
| 229 | } |
| 230 | } |
| 231 | // and always the currently-active set last |
| 232 | existing[num++] = CURRENT_SET_TOKEN; |
| 233 | |
| 234 | RestoreSet[] available = new RestoreSet[num]; |
| 235 | for (int i = 0; i < available.length; i++) { |
| 236 | available[i] = new RestoreSet("Local disk image", "flash", existing[i]); |
| 237 | } |
| 238 | return available; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 241 | public long getCurrentRestoreSet() { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 242 | // The current restore set always has the same token |
| 243 | return CURRENT_SET_TOKEN; |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 246 | public int startRestore(long token, PackageInfo[] packages) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 247 | if (DEBUG) Log.v(TAG, "start restore " + token); |
| 248 | mRestorePackages = packages; |
| 249 | mRestorePackage = -1; |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 250 | mRestoreToken = token; |
| 251 | mRestoreDataDir = new File(mDataDir, Long.toString(token)); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 252 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 255 | public String nextRestorePackage() { |
| 256 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 257 | while (++mRestorePackage < mRestorePackages.length) { |
| 258 | String name = mRestorePackages[mRestorePackage].packageName; |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 259 | // skip packages where we have a data dir but no actual contents |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 260 | String[] contents = (new File(mRestoreDataDir, name)).list(); |
Christopher Tate | a9b9186 | 2014-02-25 17:42:21 -0800 | [diff] [blame] | 261 | if (contents != null && contents.length > 0) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 262 | if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name); |
| 263 | return name; |
| 264 | } |
| 265 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 266 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 267 | if (DEBUG) Log.v(TAG, " no more packages to restore"); |
| 268 | return ""; |
| 269 | } |
| 270 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 271 | public int getRestoreData(ParcelFileDescriptor outFd) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 272 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 273 | if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called"); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 274 | File packageDir = new File(mRestoreDataDir, mRestorePackages[mRestorePackage].packageName); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 275 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 276 | // The restore set is the concatenation of the individual record blobs, |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 277 | // each of which is a file in the package's directory. We return the |
| 278 | // data in lexical order sorted by key, so that apps which use synthetic |
| 279 | // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious |
| 280 | // order. |
| 281 | ArrayList<DecodedFilename> blobs = contentsByKey(packageDir); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 282 | 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] | 283 | Log.e(TAG, "No keys for package: " + packageDir); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 284 | return BackupConstants.TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 285 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 286 | |
| 287 | // 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] | 288 | if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 289 | BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor()); |
| 290 | try { |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 291 | for (DecodedFilename keyEntry : blobs) { |
| 292 | File f = keyEntry.file; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 293 | FileInputStream in = new FileInputStream(f); |
| 294 | try { |
| 295 | int size = (int) f.length(); |
| 296 | byte[] buf = new byte[size]; |
| 297 | in.read(buf); |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 298 | if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size); |
| 299 | out.writeEntityHeader(keyEntry.key, size); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 300 | out.writeEntityData(buf, size); |
| 301 | } finally { |
| 302 | in.close(); |
| 303 | } |
| 304 | } |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 305 | return BackupConstants.TRANSPORT_OK; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 306 | } catch (IOException e) { |
| 307 | Log.e(TAG, "Unable to read backup records", e); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 308 | return BackupConstants.TRANSPORT_ERROR; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 309 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 310 | } |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 311 | |
Christopher Tate | adfe8b8 | 2014-02-04 16:23:32 -0800 | [diff] [blame] | 312 | static class DecodedFilename implements Comparable<DecodedFilename> { |
| 313 | public File file; |
| 314 | public String key; |
| 315 | |
| 316 | public DecodedFilename(File f) { |
| 317 | file = f; |
| 318 | key = new String(Base64.decode(f.getName())); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public int compareTo(DecodedFilename other) { |
| 323 | // sorts into ascending lexical order by decoded key |
| 324 | return key.compareTo(other.key); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // Return a list of the files in the given directory, sorted lexically by |
| 329 | // the Base64-decoded file name, not by the on-disk filename |
| 330 | private ArrayList<DecodedFilename> contentsByKey(File dir) { |
| 331 | File[] allFiles = dir.listFiles(); |
| 332 | if (allFiles == null || allFiles.length == 0) { |
| 333 | return null; |
| 334 | } |
| 335 | |
| 336 | // Decode the filenames into keys then sort lexically by key |
| 337 | ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>(); |
| 338 | for (File f : allFiles) { |
| 339 | contents.add(new DecodedFilename(f)); |
| 340 | } |
| 341 | Collections.sort(contents); |
| 342 | return contents; |
| 343 | } |
| 344 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 345 | public void finishRestore() { |
| 346 | if (DEBUG) Log.v(TAG, "finishRestore()"); |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 347 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 348 | } |