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 | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.pm.PackageInfo; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.PackageManager.NameNotFoundException; |
| 26 | import android.os.Environment; |
| 27 | import android.os.ParcelFileDescriptor; |
| 28 | import android.os.RemoteException; |
| 29 | import android.util.Log; |
| 30 | |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 31 | import org.bouncycastle.util.encoders.Base64; |
| 32 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 33 | import java.io.File; |
| 34 | import java.io.FileFilter; |
| 35 | import java.io.FileInputStream; |
| 36 | import java.io.FileOutputStream; |
| 37 | import java.io.IOException; |
| 38 | import java.util.ArrayList; |
| 39 | |
| 40 | /** |
| 41 | * Backup transport for stashing stuff into a known location on disk, and |
| 42 | * later restoring from there. For testing only. |
| 43 | */ |
| 44 | |
| 45 | public class LocalTransport extends IBackupTransport.Stub { |
| 46 | private static final String TAG = "LocalTransport"; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 47 | private static final boolean DEBUG = true; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 48 | |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 49 | private static final String TRANSPORT_DIR_NAME |
| 50 | = "com.android.internal.backup.LocalTransport"; |
| 51 | |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 52 | // The single hardcoded restore set always has the same (nonzero!) token |
| 53 | private static final long RESTORE_TOKEN = 1; |
| 54 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 55 | private Context mContext; |
| 56 | private PackageManager mPackageManager; |
| 57 | private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup"); |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 58 | private PackageInfo[] mRestorePackages = null; |
| 59 | private int mRestorePackage = -1; // Index into mRestorePackages |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 60 | |
| 61 | |
| 62 | public LocalTransport(Context context) { |
| 63 | mContext = context; |
| 64 | mPackageManager = context.getPackageManager(); |
| 65 | } |
| 66 | |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 67 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 68 | public String transportDirName() { |
Christopher Tate | 5cb400b | 2009-06-25 16:03:14 -0700 | [diff] [blame] | 69 | return TRANSPORT_DIR_NAME; |
| 70 | } |
| 71 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 72 | public long requestBackupTime() { |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 73 | // any time is a good time for local backup |
| 74 | return 0; |
| 75 | } |
| 76 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 77 | public int initializeDevice() { |
| 78 | if (DEBUG) Log.v(TAG, "wiping all data"); |
| 79 | deleteContents(mDataDir); |
| 80 | return BackupConstants.TRANSPORT_OK; |
| 81 | } |
| 82 | |
| 83 | public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) { |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 84 | if (DEBUG) Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 85 | |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 86 | File packageDir = new File(mDataDir, packageInfo.packageName); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 87 | packageDir.mkdirs(); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 88 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 89 | // Each 'record' in the restore set is kept in its own file, named by |
| 90 | // the record key. Wind through the data file, extracting individual |
| 91 | // record operations and building a set of all the updates to apply |
| 92 | // in this update. |
| 93 | BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor()); |
| 94 | try { |
| 95 | int bufSize = 512; |
| 96 | byte[] buf = new byte[bufSize]; |
| 97 | while (changeSet.readNextHeader()) { |
| 98 | String key = changeSet.getKey(); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 99 | String base64Key = new String(Base64.encode(key.getBytes())); |
| 100 | File entityFile = new File(packageDir, base64Key); |
| 101 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 102 | int dataSize = changeSet.getDataSize(); |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 103 | |
Christopher Tate | e9190a2 | 2009-06-17 17:52:05 -0700 | [diff] [blame] | 104 | if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize |
| 105 | + " key64=" + base64Key); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 106 | |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 107 | if (dataSize >= 0) { |
| 108 | FileOutputStream entity = new FileOutputStream(entityFile); |
| 109 | |
| 110 | if (dataSize > bufSize) { |
| 111 | bufSize = dataSize; |
| 112 | buf = new byte[bufSize]; |
| 113 | } |
| 114 | changeSet.readEntityData(buf, 0, dataSize); |
| 115 | if (DEBUG) Log.v(TAG, " data size " + dataSize); |
| 116 | |
| 117 | try { |
| 118 | entity.write(buf, 0, dataSize); |
| 119 | } catch (IOException e) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 120 | Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath()); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 121 | return BackupConstants.TRANSPORT_ERROR; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 122 | } finally { |
| 123 | entity.close(); |
| 124 | } |
| 125 | } else { |
| 126 | entityFile.delete(); |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 129 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 130 | } catch (IOException e) { |
| 131 | // oops, something went wrong. abort the operation and return error. |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 132 | Log.v(TAG, "Exception reading backup input:", e); |
Christopher Tate | d55e18a | 2009-09-21 10:12:59 -0700 | [diff] [blame] | 133 | return BackupConstants.TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 134 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 135 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 136 | |
Christopher Tate | 25a747f | 2009-09-20 12:43:58 -0700 | [diff] [blame] | 137 | // Deletes the contents but not the given directory |
| 138 | private void deleteContents(File dirname) { |
| 139 | File[] contents = dirname.listFiles(); |
| 140 | if (contents != null) { |
| 141 | for (File f : contents) { |
| 142 | if (f.isDirectory()) { |
| 143 | // delete the directory's contents then fall through |
| 144 | // and delete the directory itself. |
| 145 | deleteContents(f); |
| 146 | } |
| 147 | f.delete(); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 152 | public int clearBackupData(PackageInfo packageInfo) { |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 153 | if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName); |
| 154 | |
| 155 | File packageDir = new File(mDataDir, packageInfo.packageName); |
| 156 | for (File f : packageDir.listFiles()) { |
| 157 | f.delete(); |
| 158 | } |
| 159 | packageDir.delete(); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 160 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | ee0e78a | 2009-07-02 11:17:03 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 163 | public int finishBackup() { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 164 | if (DEBUG) Log.v(TAG, "finishBackup()"); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 165 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Restore handling |
| 169 | public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException { |
| 170 | // one hardcoded restore set |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 171 | RestoreSet set = new RestoreSet("Local disk image", "flash", RESTORE_TOKEN); |
Christopher Tate | f68eb50 | 2009-06-16 11:02:01 -0700 | [diff] [blame] | 172 | RestoreSet[] array = { set }; |
| 173 | return array; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Christopher Tate | 50c6df0 | 2010-01-29 12:48:20 -0800 | [diff] [blame] | 176 | public long getCurrentRestoreSet() { |
| 177 | // The hardcoded restore set always has the same token |
| 178 | return RESTORE_TOKEN; |
| 179 | } |
| 180 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 181 | public int startRestore(long token, PackageInfo[] packages) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 182 | if (DEBUG) Log.v(TAG, "start restore " + token); |
| 183 | mRestorePackages = packages; |
| 184 | mRestorePackage = -1; |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 185 | return BackupConstants.TRANSPORT_OK; |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 188 | public String nextRestorePackage() { |
| 189 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 190 | while (++mRestorePackage < mRestorePackages.length) { |
| 191 | String name = mRestorePackages[mRestorePackage].packageName; |
| 192 | if (new File(mDataDir, name).isDirectory()) { |
| 193 | if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name); |
| 194 | return name; |
| 195 | } |
| 196 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 197 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 198 | if (DEBUG) Log.v(TAG, " no more packages to restore"); |
| 199 | return ""; |
| 200 | } |
| 201 | |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 202 | public int getRestoreData(ParcelFileDescriptor outFd) { |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 203 | if (mRestorePackages == null) throw new IllegalStateException("startRestore not called"); |
| 204 | if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called"); |
| 205 | File packageDir = new File(mDataDir, mRestorePackages[mRestorePackage].packageName); |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 206 | |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 207 | // The restore set is the concatenation of the individual record blobs, |
| 208 | // each of which is a file in the package's directory |
| 209 | File[] blobs = packageDir.listFiles(); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 210 | if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 211 | Log.e(TAG, "Error listing directory: " + packageDir); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 212 | return BackupConstants.TRANSPORT_ERROR; |
Christopher Tate | 2fdd428 | 2009-06-12 15:20:04 -0700 | [diff] [blame] | 213 | } |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 214 | |
| 215 | // We expect at least some data if the directory exists in the first place |
| 216 | if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.length + " key files"); |
| 217 | BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor()); |
| 218 | try { |
| 219 | for (File f : blobs) { |
| 220 | FileInputStream in = new FileInputStream(f); |
| 221 | try { |
| 222 | int size = (int) f.length(); |
| 223 | byte[] buf = new byte[size]; |
| 224 | in.read(buf); |
| 225 | String key = new String(Base64.decode(f.getName())); |
| 226 | if (DEBUG) Log.v(TAG, " ... key=" + key + " size=" + size); |
| 227 | out.writeEntityHeader(key, size); |
| 228 | out.writeEntityData(buf, size); |
| 229 | } finally { |
| 230 | in.close(); |
| 231 | } |
| 232 | } |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 233 | return BackupConstants.TRANSPORT_OK; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 234 | } catch (IOException e) { |
| 235 | Log.e(TAG, "Unable to read backup records", e); |
Dan Egnor | 0144516 | 2009-09-21 17:04:05 -0700 | [diff] [blame] | 236 | return BackupConstants.TRANSPORT_ERROR; |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 237 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 238 | } |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 239 | |
Dan Egnor | efe5264 | 2009-06-24 00:16:33 -0700 | [diff] [blame] | 240 | public void finishRestore() { |
| 241 | if (DEBUG) Log.v(TAG, "finishRestore()"); |
Christopher Tate | 3a31a93 | 2009-06-22 15:10:30 -0700 | [diff] [blame] | 242 | } |
Christopher Tate | 9bbc21a | 2009-06-10 20:23:25 -0700 | [diff] [blame] | 243 | } |