blob: 9e4b606122c950abe5d6cf673666e6f1dbbf7139 [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;
23import android.content.pm.PackageInfo;
24import android.content.pm.PackageManager;
25import android.content.pm.PackageManager.NameNotFoundException;
26import android.os.Environment;
27import android.os.ParcelFileDescriptor;
28import android.os.RemoteException;
29import android.util.Log;
30
Christopher Tatee9190a22009-06-17 17:52:05 -070031import org.bouncycastle.util.encoders.Base64;
32
Christopher Tate9bbc21a2009-06-10 20:23:25 -070033import java.io.File;
34import java.io.FileFilter;
35import java.io.FileInputStream;
36import java.io.FileOutputStream;
37import java.io.IOException;
38import 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
45public class LocalTransport extends IBackupTransport.Stub {
46 private static final String TAG = "LocalTransport";
Christopher Tate2fdd4282009-06-12 15:20:04 -070047 private static final boolean DEBUG = true;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070048
Christopher Tate5cb400b2009-06-25 16:03:14 -070049 private static final String TRANSPORT_DIR_NAME
50 = "com.android.internal.backup.LocalTransport";
51
Christopher Tate50c6df02010-01-29 12:48:20 -080052 // The single hardcoded restore set always has the same (nonzero!) token
53 private static final long RESTORE_TOKEN = 1;
54
Christopher Tate9bbc21a2009-06-10 20:23:25 -070055 private Context mContext;
56 private PackageManager mPackageManager;
57 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Dan Egnorefe52642009-06-24 00:16:33 -070058 private PackageInfo[] mRestorePackages = null;
59 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tate9bbc21a2009-06-10 20:23:25 -070060
61
62 public LocalTransport(Context context) {
63 mContext = context;
64 mPackageManager = context.getPackageManager();
65 }
66
Christopher Tate5cb400b2009-06-25 16:03:14 -070067
Dan Egnor01445162009-09-21 17:04:05 -070068 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -070069 return TRANSPORT_DIR_NAME;
70 }
71
Dan Egnor01445162009-09-21 17:04:05 -070072 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070073 // any time is a good time for local backup
74 return 0;
75 }
76
Dan Egnor01445162009-09-21 17:04:05 -070077 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 Tate2fdd4282009-06-12 15:20:04 -070084 if (DEBUG) Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -070085
Christopher Tate9bbc21a2009-06-10 20:23:25 -070086 File packageDir = new File(mDataDir, packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -070087 packageDir.mkdirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -070088
Christopher Tate2fdd4282009-06-12 15:20:04 -070089 // 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 Onorato5d605dc2009-06-18 18:23:43 -070099 String base64Key = new String(Base64.encode(key.getBytes()));
100 File entityFile = new File(packageDir, base64Key);
101
Christopher Tate2fdd4282009-06-12 15:20:04 -0700102 int dataSize = changeSet.getDataSize();
Christopher Tatee9190a22009-06-17 17:52:05 -0700103
Christopher Tatee9190a22009-06-17 17:52:05 -0700104 if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize
105 + " key64=" + base64Key);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700106
Joe Onorato5d605dc2009-06-18 18:23:43 -0700107 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 Egnorefe52642009-06-24 00:16:33 -0700120 Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
Christopher Tated55e18a2009-09-21 10:12:59 -0700121 return BackupConstants.TRANSPORT_ERROR;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700122 } finally {
123 entity.close();
124 }
125 } else {
126 entityFile.delete();
Christopher Tate2fdd4282009-06-12 15:20:04 -0700127 }
128 }
Christopher Tated55e18a2009-09-21 10:12:59 -0700129 return BackupConstants.TRANSPORT_OK;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700130 } catch (IOException e) {
131 // oops, something went wrong. abort the operation and return error.
Dan Egnorefe52642009-06-24 00:16:33 -0700132 Log.v(TAG, "Exception reading backup input:", e);
Christopher Tated55e18a2009-09-21 10:12:59 -0700133 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700134 }
Dan Egnorefe52642009-06-24 00:16:33 -0700135 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700136
Christopher Tate25a747f2009-09-20 12:43:58 -0700137 // 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 Egnor01445162009-09-21 17:04:05 -0700152 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700153 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 Egnor01445162009-09-21 17:04:05 -0700160 return BackupConstants.TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700161 }
162
Dan Egnor01445162009-09-21 17:04:05 -0700163 public int finishBackup() {
Dan Egnorefe52642009-06-24 00:16:33 -0700164 if (DEBUG) Log.v(TAG, "finishBackup()");
Dan Egnor01445162009-09-21 17:04:05 -0700165 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700166 }
167
168 // Restore handling
169 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
170 // one hardcoded restore set
Christopher Tate50c6df02010-01-29 12:48:20 -0800171 RestoreSet set = new RestoreSet("Local disk image", "flash", RESTORE_TOKEN);
Christopher Tatef68eb502009-06-16 11:02:01 -0700172 RestoreSet[] array = { set };
173 return array;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700174 }
175
Christopher Tate50c6df02010-01-29 12:48:20 -0800176 public long getCurrentRestoreSet() {
177 // The hardcoded restore set always has the same token
178 return RESTORE_TOKEN;
179 }
180
Dan Egnor01445162009-09-21 17:04:05 -0700181 public int startRestore(long token, PackageInfo[] packages) {
Dan Egnorefe52642009-06-24 00:16:33 -0700182 if (DEBUG) Log.v(TAG, "start restore " + token);
183 mRestorePackages = packages;
184 mRestorePackage = -1;
Dan Egnor01445162009-09-21 17:04:05 -0700185 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700186 }
187
Dan Egnorefe52642009-06-24 00:16:33 -0700188 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 Tate9bbc21a2009-06-10 20:23:25 -0700197
Dan Egnorefe52642009-06-24 00:16:33 -0700198 if (DEBUG) Log.v(TAG, " no more packages to restore");
199 return "";
200 }
201
Dan Egnor01445162009-09-21 17:04:05 -0700202 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700203 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 Tate9bbc21a2009-06-10 20:23:25 -0700206
Christopher Tate2fdd4282009-06-12 15:20:04 -0700207 // 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 Egnor01445162009-09-21 17:04:05 -0700210 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Dan Egnorefe52642009-06-24 00:16:33 -0700211 Log.e(TAG, "Error listing directory: " + packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700212 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700213 }
Dan Egnorefe52642009-06-24 00:16:33 -0700214
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 Egnor01445162009-09-21 17:04:05 -0700233 return BackupConstants.TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700234 } catch (IOException e) {
235 Log.e(TAG, "Unable to read backup records", e);
Dan Egnor01445162009-09-21 17:04:05 -0700236 return BackupConstants.TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700237 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700238 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700239
Dan Egnorefe52642009-06-24 00:16:33 -0700240 public void finishRestore() {
241 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate3a31a932009-06-22 15:10:30 -0700242 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700243}