blob: eb2d1fe3e79a6269e16ea045ae5a2e74720dba73 [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;
rpcraigebab0ae2012-12-04 09:37:23 -050030import android.os.SELinux;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070031import android.util.Log;
32
Brian Carlstrom4140fae2011-01-24 16:17:43 -080033import com.android.org.bouncycastle.util.encoders.Base64;
Christopher Tatee9190a22009-06-17 17:52:05 -070034
Christopher Tate9bbc21a2009-06-10 20:23:25 -070035import java.io.File;
36import java.io.FileFilter;
37import java.io.FileInputStream;
38import java.io.FileOutputStream;
39import java.io.IOException;
40import java.util.ArrayList;
41
42/**
43 * Backup transport for stashing stuff into a known location on disk, and
44 * later restoring from there. For testing only.
45 */
46
47public class LocalTransport extends IBackupTransport.Stub {
48 private static final String TAG = "LocalTransport";
Christopher Tate2fdd4282009-06-12 15:20:04 -070049 private static final boolean DEBUG = true;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070050
Christopher Tate5cb400b2009-06-25 16:03:14 -070051 private static final String TRANSPORT_DIR_NAME
52 = "com.android.internal.backup.LocalTransport";
53
Chris Tatea8ddef32010-11-10 11:53:26 -080054 private static final String TRANSPORT_DESTINATION_STRING
55 = "Backing up to debug-only private cache";
56
Christopher Tate50c6df02010-01-29 12:48:20 -080057 // The single hardcoded restore set always has the same (nonzero!) token
58 private static final long RESTORE_TOKEN = 1;
59
Christopher Tate9bbc21a2009-06-10 20:23:25 -070060 private Context mContext;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070061 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Dan Egnorefe52642009-06-24 00:16:33 -070062 private PackageInfo[] mRestorePackages = null;
63 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tate9bbc21a2009-06-10 20:23:25 -070064
65
66 public LocalTransport(Context context) {
67 mContext = context;
rpcraigebab0ae2012-12-04 09:37:23 -050068 mDataDir.mkdirs();
69 if (!SELinux.restorecon(mDataDir)) {
70 Log.e(TAG, "SELinux restorecon failed for " + mDataDir);
71 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -070072 }
73
Chris Tatea8ddef32010-11-10 11:53:26 -080074 public Intent configurationIntent() {
75 // The local transport is not user-configurable
76 return null;
77 }
78
79 public String currentDestinationString() {
80 return TRANSPORT_DESTINATION_STRING;
81 }
Christopher Tate5cb400b2009-06-25 16:03:14 -070082
Dan Egnor01445162009-09-21 17:04:05 -070083 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -070084 return TRANSPORT_DIR_NAME;
85 }
86
Dan Egnor01445162009-09-21 17:04:05 -070087 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070088 // any time is a good time for local backup
89 return 0;
90 }
91
Dan Egnor01445162009-09-21 17:04:05 -070092 public int initializeDevice() {
93 if (DEBUG) Log.v(TAG, "wiping all data");
94 deleteContents(mDataDir);
95 return BackupConstants.TRANSPORT_OK;
96 }
97
98 public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
Christopher Tate2fdd4282009-06-12 15:20:04 -070099 if (DEBUG) Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700100
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700101 File packageDir = new File(mDataDir, packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700102 packageDir.mkdirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700103
Christopher Tate2fdd4282009-06-12 15:20:04 -0700104 // Each 'record' in the restore set is kept in its own file, named by
105 // the record key. Wind through the data file, extracting individual
106 // record operations and building a set of all the updates to apply
107 // in this update.
108 BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
109 try {
110 int bufSize = 512;
111 byte[] buf = new byte[bufSize];
112 while (changeSet.readNextHeader()) {
113 String key = changeSet.getKey();
Joe Onorato5d605dc2009-06-18 18:23:43 -0700114 String base64Key = new String(Base64.encode(key.getBytes()));
115 File entityFile = new File(packageDir, base64Key);
116
Christopher Tate2fdd4282009-06-12 15:20:04 -0700117 int dataSize = changeSet.getDataSize();
Christopher Tatee9190a22009-06-17 17:52:05 -0700118
Christopher Tatee9190a22009-06-17 17:52:05 -0700119 if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize
120 + " key64=" + base64Key);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700121
Joe Onorato5d605dc2009-06-18 18:23:43 -0700122 if (dataSize >= 0) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700123 if (entityFile.exists()) {
124 entityFile.delete();
125 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700126 FileOutputStream entity = new FileOutputStream(entityFile);
127
128 if (dataSize > bufSize) {
129 bufSize = dataSize;
130 buf = new byte[bufSize];
131 }
132 changeSet.readEntityData(buf, 0, dataSize);
133 if (DEBUG) Log.v(TAG, " data size " + dataSize);
134
135 try {
136 entity.write(buf, 0, dataSize);
137 } catch (IOException e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700138 Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
Christopher Tated55e18a2009-09-21 10:12:59 -0700139 return BackupConstants.TRANSPORT_ERROR;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700140 } finally {
141 entity.close();
142 }
143 } else {
144 entityFile.delete();
Christopher Tate2fdd4282009-06-12 15:20:04 -0700145 }
146 }
Christopher Tated55e18a2009-09-21 10:12:59 -0700147 return BackupConstants.TRANSPORT_OK;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700148 } catch (IOException e) {
149 // oops, something went wrong. abort the operation and return error.
Dan Egnorefe52642009-06-24 00:16:33 -0700150 Log.v(TAG, "Exception reading backup input:", e);
Christopher Tated55e18a2009-09-21 10:12:59 -0700151 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700152 }
Dan Egnorefe52642009-06-24 00:16:33 -0700153 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700154
Christopher Tate25a747f2009-09-20 12:43:58 -0700155 // Deletes the contents but not the given directory
156 private void deleteContents(File dirname) {
157 File[] contents = dirname.listFiles();
158 if (contents != null) {
159 for (File f : contents) {
160 if (f.isDirectory()) {
161 // delete the directory's contents then fall through
162 // and delete the directory itself.
163 deleteContents(f);
164 }
165 f.delete();
166 }
167 }
168 }
169
Dan Egnor01445162009-09-21 17:04:05 -0700170 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700171 if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName);
172
173 File packageDir = new File(mDataDir, packageInfo.packageName);
Christopher Tate0abf6a02012-03-23 17:45:15 -0700174 final File[] fileset = packageDir.listFiles();
175 if (fileset != null) {
176 for (File f : fileset) {
177 f.delete();
178 }
179 packageDir.delete();
Christopher Tateee0e78a2009-07-02 11:17:03 -0700180 }
Dan Egnor01445162009-09-21 17:04:05 -0700181 return BackupConstants.TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700182 }
183
Dan Egnor01445162009-09-21 17:04:05 -0700184 public int finishBackup() {
Dan Egnorefe52642009-06-24 00:16:33 -0700185 if (DEBUG) Log.v(TAG, "finishBackup()");
Dan Egnor01445162009-09-21 17:04:05 -0700186 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700187 }
188
189 // Restore handling
190 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
191 // one hardcoded restore set
Christopher Tate50c6df02010-01-29 12:48:20 -0800192 RestoreSet set = new RestoreSet("Local disk image", "flash", RESTORE_TOKEN);
Christopher Tatef68eb502009-06-16 11:02:01 -0700193 RestoreSet[] array = { set };
194 return array;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700195 }
196
Christopher Tate50c6df02010-01-29 12:48:20 -0800197 public long getCurrentRestoreSet() {
198 // The hardcoded restore set always has the same token
199 return RESTORE_TOKEN;
200 }
201
Dan Egnor01445162009-09-21 17:04:05 -0700202 public int startRestore(long token, PackageInfo[] packages) {
Dan Egnorefe52642009-06-24 00:16:33 -0700203 if (DEBUG) Log.v(TAG, "start restore " + token);
204 mRestorePackages = packages;
205 mRestorePackage = -1;
Dan Egnor01445162009-09-21 17:04:05 -0700206 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700207 }
208
Dan Egnorefe52642009-06-24 00:16:33 -0700209 public String nextRestorePackage() {
210 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
211 while (++mRestorePackage < mRestorePackages.length) {
212 String name = mRestorePackages[mRestorePackage].packageName;
213 if (new File(mDataDir, name).isDirectory()) {
214 if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name);
215 return name;
216 }
217 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700218
Dan Egnorefe52642009-06-24 00:16:33 -0700219 if (DEBUG) Log.v(TAG, " no more packages to restore");
220 return "";
221 }
222
Dan Egnor01445162009-09-21 17:04:05 -0700223 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700224 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
225 if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called");
226 File packageDir = new File(mDataDir, mRestorePackages[mRestorePackage].packageName);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700227
Christopher Tate2fdd4282009-06-12 15:20:04 -0700228 // The restore set is the concatenation of the individual record blobs,
229 // each of which is a file in the package's directory
230 File[] blobs = packageDir.listFiles();
Dan Egnor01445162009-09-21 17:04:05 -0700231 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Dan Egnorefe52642009-06-24 00:16:33 -0700232 Log.e(TAG, "Error listing directory: " + packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700233 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700234 }
Dan Egnorefe52642009-06-24 00:16:33 -0700235
236 // We expect at least some data if the directory exists in the first place
237 if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.length + " key files");
238 BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
239 try {
240 for (File f : blobs) {
241 FileInputStream in = new FileInputStream(f);
242 try {
243 int size = (int) f.length();
244 byte[] buf = new byte[size];
245 in.read(buf);
246 String key = new String(Base64.decode(f.getName()));
247 if (DEBUG) Log.v(TAG, " ... key=" + key + " size=" + size);
248 out.writeEntityHeader(key, size);
249 out.writeEntityData(buf, size);
250 } finally {
251 in.close();
252 }
253 }
Dan Egnor01445162009-09-21 17:04:05 -0700254 return BackupConstants.TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700255 } catch (IOException e) {
256 Log.e(TAG, "Unable to read backup records", e);
Dan Egnor01445162009-09-21 17:04:05 -0700257 return BackupConstants.TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700258 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700259 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700260
Dan Egnorefe52642009-06-24 00:16:33 -0700261 public void finishRestore() {
262 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate3a31a932009-06-22 15:10:30 -0700263 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700264}