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