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