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