blob: e7c394870920ba6b3105a176ab0c317a97f48842 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
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 Tate9bbc21a2009-06-10 20:23:25 -070017package com.android.internal.backup;
18
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.BackupDataInput;
20import android.app.backup.BackupDataOutput;
21import android.app.backup.RestoreSet;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070022import android.content.Context;
Chris Tatea8ddef32010-11-10 11:53:26 -080023import android.content.Intent;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070024import android.content.pm.PackageInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.os.Environment;
28import android.os.ParcelFileDescriptor;
29import android.os.RemoteException;
30import android.util.Log;
31
Brian Carlstrom4140fae2011-01-24 16:17:43 -080032import com.android.org.bouncycastle.util.encoders.Base64;
Christopher Tatee9190a22009-06-17 17:52:05 -070033
Christopher Tate9bbc21a2009-06-10 20:23:25 -070034import java.io.File;
35import java.io.FileFilter;
36import java.io.FileInputStream;
37import java.io.FileOutputStream;
38import java.io.IOException;
39import java.util.ArrayList;
40
41/**
42 * Backup transport for stashing stuff into a known location on disk, and
43 * later restoring from there. For testing only.
44 */
45
46public class LocalTransport extends IBackupTransport.Stub {
47 private static final String TAG = "LocalTransport";
Christopher Tate2fdd4282009-06-12 15:20:04 -070048 private static final boolean DEBUG = true;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070049
Christopher Tate5cb400b2009-06-25 16:03:14 -070050 private static final String TRANSPORT_DIR_NAME
51 = "com.android.internal.backup.LocalTransport";
52
Chris Tatea8ddef32010-11-10 11:53:26 -080053 private static final String TRANSPORT_DESTINATION_STRING
54 = "Backing up to debug-only private cache";
55
Christopher Tate50c6df02010-01-29 12:48:20 -080056 // The single hardcoded restore set always has the same (nonzero!) token
57 private static final long RESTORE_TOKEN = 1;
58
Christopher Tate9bbc21a2009-06-10 20:23:25 -070059 private Context mContext;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070060 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Dan Egnorefe52642009-06-24 00:16:33 -070061 private PackageInfo[] mRestorePackages = null;
62 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tate9bbc21a2009-06-10 20:23:25 -070063
64
65 public LocalTransport(Context context) {
66 mContext = context;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070067 }
68
Chris Tatea8ddef32010-11-10 11:53:26 -080069 public Intent configurationIntent() {
70 // The local transport is not user-configurable
71 return null;
72 }
73
74 public String currentDestinationString() {
75 return TRANSPORT_DESTINATION_STRING;
76 }
Christopher Tate5cb400b2009-06-25 16:03:14 -070077
Dan Egnor01445162009-09-21 17:04:05 -070078 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -070079 return TRANSPORT_DIR_NAME;
80 }
81
Dan Egnor01445162009-09-21 17:04:05 -070082 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070083 // any time is a good time for local backup
84 return 0;
85 }
86
Dan Egnor01445162009-09-21 17:04:05 -070087 public int initializeDevice() {
88 if (DEBUG) Log.v(TAG, "wiping all data");
89 deleteContents(mDataDir);
90 return BackupConstants.TRANSPORT_OK;
91 }
92
93 public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070094 if (DEBUG) Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -070095
Christopher Tate9bbc21a2009-06-10 20:23:25 -070096 File packageDir = new File(mDataDir, packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -070097 packageDir.mkdirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -070098
Christopher Tate2fdd4282009-06-12 15:20:04 -070099 // Each 'record' in the restore set is kept in its own file, named by
100 // the record key. Wind through the data file, extracting individual
101 // record operations and building a set of all the updates to apply
102 // in this update.
103 BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
104 try {
105 int bufSize = 512;
106 byte[] buf = new byte[bufSize];
107 while (changeSet.readNextHeader()) {
108 String key = changeSet.getKey();
Joe Onorato5d605dc2009-06-18 18:23:43 -0700109 String base64Key = new String(Base64.encode(key.getBytes()));
110 File entityFile = new File(packageDir, base64Key);
111
Christopher Tate2fdd4282009-06-12 15:20:04 -0700112 int dataSize = changeSet.getDataSize();
Christopher Tatee9190a22009-06-17 17:52:05 -0700113
Christopher Tatee9190a22009-06-17 17:52:05 -0700114 if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize
115 + " key64=" + base64Key);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700116
Joe Onorato5d605dc2009-06-18 18:23:43 -0700117 if (dataSize >= 0) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700118 if (entityFile.exists()) {
119 entityFile.delete();
120 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700121 FileOutputStream entity = new FileOutputStream(entityFile);
122
123 if (dataSize > bufSize) {
124 bufSize = dataSize;
125 buf = new byte[bufSize];
126 }
127 changeSet.readEntityData(buf, 0, dataSize);
128 if (DEBUG) Log.v(TAG, " data size " + dataSize);
129
130 try {
131 entity.write(buf, 0, dataSize);
132 } catch (IOException e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700133 Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
Christopher Tated55e18a2009-09-21 10:12:59 -0700134 return BackupConstants.TRANSPORT_ERROR;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700135 } finally {
136 entity.close();
137 }
138 } else {
139 entityFile.delete();
Christopher Tate2fdd4282009-06-12 15:20:04 -0700140 }
141 }
Christopher Tated55e18a2009-09-21 10:12:59 -0700142 return BackupConstants.TRANSPORT_OK;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700143 } catch (IOException e) {
144 // oops, something went wrong. abort the operation and return error.
Dan Egnorefe52642009-06-24 00:16:33 -0700145 Log.v(TAG, "Exception reading backup input:", e);
Christopher Tated55e18a2009-09-21 10:12:59 -0700146 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700147 }
Dan Egnorefe52642009-06-24 00:16:33 -0700148 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700149
Christopher Tate25a747f2009-09-20 12:43:58 -0700150 // Deletes the contents but not the given directory
151 private void deleteContents(File dirname) {
152 File[] contents = dirname.listFiles();
153 if (contents != null) {
154 for (File f : contents) {
155 if (f.isDirectory()) {
156 // delete the directory's contents then fall through
157 // and delete the directory itself.
158 deleteContents(f);
159 }
160 f.delete();
161 }
162 }
163 }
164
Dan Egnor01445162009-09-21 17:04:05 -0700165 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700166 if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName);
167
168 File packageDir = new File(mDataDir, packageInfo.packageName);
169 for (File f : packageDir.listFiles()) {
170 f.delete();
171 }
172 packageDir.delete();
Dan Egnor01445162009-09-21 17:04:05 -0700173 return BackupConstants.TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700174 }
175
Dan Egnor01445162009-09-21 17:04:05 -0700176 public int finishBackup() {
Dan Egnorefe52642009-06-24 00:16:33 -0700177 if (DEBUG) Log.v(TAG, "finishBackup()");
Dan Egnor01445162009-09-21 17:04:05 -0700178 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700179 }
180
181 // Restore handling
182 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
183 // one hardcoded restore set
Christopher Tate50c6df02010-01-29 12:48:20 -0800184 RestoreSet set = new RestoreSet("Local disk image", "flash", RESTORE_TOKEN);
Christopher Tatef68eb502009-06-16 11:02:01 -0700185 RestoreSet[] array = { set };
186 return array;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700187 }
188
Christopher Tate50c6df02010-01-29 12:48:20 -0800189 public long getCurrentRestoreSet() {
190 // The hardcoded restore set always has the same token
191 return RESTORE_TOKEN;
192 }
193
Dan Egnor01445162009-09-21 17:04:05 -0700194 public int startRestore(long token, PackageInfo[] packages) {
Dan Egnorefe52642009-06-24 00:16:33 -0700195 if (DEBUG) Log.v(TAG, "start restore " + token);
196 mRestorePackages = packages;
197 mRestorePackage = -1;
Dan Egnor01445162009-09-21 17:04:05 -0700198 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700199 }
200
Dan Egnorefe52642009-06-24 00:16:33 -0700201 public String nextRestorePackage() {
202 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
203 while (++mRestorePackage < mRestorePackages.length) {
204 String name = mRestorePackages[mRestorePackage].packageName;
205 if (new File(mDataDir, name).isDirectory()) {
206 if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name);
207 return name;
208 }
209 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700210
Dan Egnorefe52642009-06-24 00:16:33 -0700211 if (DEBUG) Log.v(TAG, " no more packages to restore");
212 return "";
213 }
214
Dan Egnor01445162009-09-21 17:04:05 -0700215 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700216 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
217 if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called");
218 File packageDir = new File(mDataDir, mRestorePackages[mRestorePackage].packageName);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700219
Christopher Tate2fdd4282009-06-12 15:20:04 -0700220 // The restore set is the concatenation of the individual record blobs,
221 // each of which is a file in the package's directory
222 File[] blobs = packageDir.listFiles();
Dan Egnor01445162009-09-21 17:04:05 -0700223 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Dan Egnorefe52642009-06-24 00:16:33 -0700224 Log.e(TAG, "Error listing directory: " + packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700225 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700226 }
Dan Egnorefe52642009-06-24 00:16:33 -0700227
228 // We expect at least some data if the directory exists in the first place
229 if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.length + " key files");
230 BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
231 try {
232 for (File f : blobs) {
233 FileInputStream in = new FileInputStream(f);
234 try {
235 int size = (int) f.length();
236 byte[] buf = new byte[size];
237 in.read(buf);
238 String key = new String(Base64.decode(f.getName()));
239 if (DEBUG) Log.v(TAG, " ... key=" + key + " size=" + size);
240 out.writeEntityHeader(key, size);
241 out.writeEntityData(buf, size);
242 } finally {
243 in.close();
244 }
245 }
Dan Egnor01445162009-09-21 17:04:05 -0700246 return BackupConstants.TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700247 } catch (IOException e) {
248 Log.e(TAG, "Unable to read backup records", e);
Dan Egnor01445162009-09-21 17:04:05 -0700249 return BackupConstants.TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700250 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700251 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700252
Dan Egnorefe52642009-06-24 00:16:33 -0700253 public void finishRestore() {
254 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate3a31a932009-06-22 15:10:30 -0700255 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700256}