blob: 494bc7898e93d1ec06491f6f4e6e95ba9088b5ab [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 Tatecefba582013-11-14 18:10:35 -080022import android.content.ComponentName;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070023import android.content.Context;
Chris Tatea8ddef32010-11-10 11:53:26 -080024import android.content.Intent;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070025import android.content.pm.PackageInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.PackageManager.NameNotFoundException;
28import android.os.Environment;
29import android.os.ParcelFileDescriptor;
30import android.os.RemoteException;
rpcraigebab0ae2012-12-04 09:37:23 -050031import android.os.SELinux;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070032import android.util.Log;
33
Brian Carlstrom4140fae2011-01-24 16:17:43 -080034import com.android.org.bouncycastle.util.encoders.Base64;
Christopher Tatee9190a22009-06-17 17:52:05 -070035
Christopher Tate9bbc21a2009-06-10 20:23:25 -070036import java.io.File;
37import java.io.FileFilter;
38import java.io.FileInputStream;
39import java.io.FileOutputStream;
40import java.io.IOException;
41import java.util.ArrayList;
42
43/**
44 * Backup transport for stashing stuff into a known location on disk, and
45 * later restoring from there. For testing only.
46 */
47
48public class LocalTransport extends IBackupTransport.Stub {
49 private static final String TAG = "LocalTransport";
Christopher Tate2fdd4282009-06-12 15:20:04 -070050 private static final boolean DEBUG = true;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070051
Christopher Tate5cb400b2009-06-25 16:03:14 -070052 private static final String TRANSPORT_DIR_NAME
53 = "com.android.internal.backup.LocalTransport";
54
Chris Tatea8ddef32010-11-10 11:53:26 -080055 private static final String TRANSPORT_DESTINATION_STRING
56 = "Backing up to debug-only private cache";
57
Christopher Tate50c6df02010-01-29 12:48:20 -080058 // The single hardcoded restore set always has the same (nonzero!) token
59 private static final long RESTORE_TOKEN = 1;
60
Christopher Tate9bbc21a2009-06-10 20:23:25 -070061 private Context mContext;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070062 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Dan Egnorefe52642009-06-24 00:16:33 -070063 private PackageInfo[] mRestorePackages = null;
64 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tate9bbc21a2009-06-10 20:23:25 -070065
66
67 public LocalTransport(Context context) {
68 mContext = context;
rpcraigebab0ae2012-12-04 09:37:23 -050069 mDataDir.mkdirs();
70 if (!SELinux.restorecon(mDataDir)) {
71 Log.e(TAG, "SELinux restorecon failed for " + mDataDir);
72 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -070073 }
74
Christopher Tatecefba582013-11-14 18:10:35 -080075 public String name() {
76 return new ComponentName(mContext, this.getClass()).flattenToShortString();
77 }
78
Chris Tatea8ddef32010-11-10 11:53:26 -080079 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 Tate5cb400b2009-06-25 16:03:14 -070087
Dan Egnor01445162009-09-21 17:04:05 -070088 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -070089 return TRANSPORT_DIR_NAME;
90 }
91
Dan Egnor01445162009-09-21 17:04:05 -070092 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070093 // any time is a good time for local backup
94 return 0;
95 }
96
Dan Egnor01445162009-09-21 17:04:05 -070097 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 Tate2fdd4282009-06-12 15:20:04 -0700104 if (DEBUG) Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700105
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700106 File packageDir = new File(mDataDir, packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700107 packageDir.mkdirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700108
Christopher Tate2fdd4282009-06-12 15:20:04 -0700109 // Each 'record' in the restore set is kept in its own file, named by
110 // the record key. Wind through the data file, extracting individual
111 // record operations and building a set of all the updates to apply
112 // in this update.
113 BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
114 try {
115 int bufSize = 512;
116 byte[] buf = new byte[bufSize];
117 while (changeSet.readNextHeader()) {
118 String key = changeSet.getKey();
Joe Onorato5d605dc2009-06-18 18:23:43 -0700119 String base64Key = new String(Base64.encode(key.getBytes()));
120 File entityFile = new File(packageDir, base64Key);
121
Christopher Tate2fdd4282009-06-12 15:20:04 -0700122 int dataSize = changeSet.getDataSize();
Christopher Tatee9190a22009-06-17 17:52:05 -0700123
Christopher Tatee9190a22009-06-17 17:52:05 -0700124 if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize
125 + " key64=" + base64Key);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700126
Joe Onorato5d605dc2009-06-18 18:23:43 -0700127 if (dataSize >= 0) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700128 if (entityFile.exists()) {
129 entityFile.delete();
130 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700131 FileOutputStream entity = new FileOutputStream(entityFile);
132
133 if (dataSize > bufSize) {
134 bufSize = dataSize;
135 buf = new byte[bufSize];
136 }
137 changeSet.readEntityData(buf, 0, dataSize);
138 if (DEBUG) Log.v(TAG, " data size " + dataSize);
139
140 try {
141 entity.write(buf, 0, dataSize);
142 } catch (IOException e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700143 Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
Christopher Tated55e18a2009-09-21 10:12:59 -0700144 return BackupConstants.TRANSPORT_ERROR;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700145 } finally {
146 entity.close();
147 }
148 } else {
149 entityFile.delete();
Christopher Tate2fdd4282009-06-12 15:20:04 -0700150 }
151 }
Christopher Tated55e18a2009-09-21 10:12:59 -0700152 return BackupConstants.TRANSPORT_OK;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700153 } catch (IOException e) {
154 // oops, something went wrong. abort the operation and return error.
Dan Egnorefe52642009-06-24 00:16:33 -0700155 Log.v(TAG, "Exception reading backup input:", e);
Christopher Tated55e18a2009-09-21 10:12:59 -0700156 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700157 }
Dan Egnorefe52642009-06-24 00:16:33 -0700158 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700159
Christopher Tate25a747f2009-09-20 12:43:58 -0700160 // Deletes the contents but not the given directory
161 private void deleteContents(File dirname) {
162 File[] contents = dirname.listFiles();
163 if (contents != null) {
164 for (File f : contents) {
165 if (f.isDirectory()) {
166 // delete the directory's contents then fall through
167 // and delete the directory itself.
168 deleteContents(f);
169 }
170 f.delete();
171 }
172 }
173 }
174
Dan Egnor01445162009-09-21 17:04:05 -0700175 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700176 if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName);
177
178 File packageDir = new File(mDataDir, packageInfo.packageName);
Christopher Tate0abf6a02012-03-23 17:45:15 -0700179 final File[] fileset = packageDir.listFiles();
180 if (fileset != null) {
181 for (File f : fileset) {
182 f.delete();
183 }
184 packageDir.delete();
Christopher Tateee0e78a2009-07-02 11:17:03 -0700185 }
Dan Egnor01445162009-09-21 17:04:05 -0700186 return BackupConstants.TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700187 }
188
Dan Egnor01445162009-09-21 17:04:05 -0700189 public int finishBackup() {
Dan Egnorefe52642009-06-24 00:16:33 -0700190 if (DEBUG) Log.v(TAG, "finishBackup()");
Dan Egnor01445162009-09-21 17:04:05 -0700191 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700192 }
193
194 // Restore handling
195 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
196 // one hardcoded restore set
Christopher Tate50c6df02010-01-29 12:48:20 -0800197 RestoreSet set = new RestoreSet("Local disk image", "flash", RESTORE_TOKEN);
Christopher Tatef68eb502009-06-16 11:02:01 -0700198 RestoreSet[] array = { set };
199 return array;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700200 }
201
Christopher Tate50c6df02010-01-29 12:48:20 -0800202 public long getCurrentRestoreSet() {
203 // The hardcoded restore set always has the same token
204 return RESTORE_TOKEN;
205 }
206
Dan Egnor01445162009-09-21 17:04:05 -0700207 public int startRestore(long token, PackageInfo[] packages) {
Dan Egnorefe52642009-06-24 00:16:33 -0700208 if (DEBUG) Log.v(TAG, "start restore " + token);
209 mRestorePackages = packages;
210 mRestorePackage = -1;
Dan Egnor01445162009-09-21 17:04:05 -0700211 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700212 }
213
Dan Egnorefe52642009-06-24 00:16:33 -0700214 public String nextRestorePackage() {
215 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
216 while (++mRestorePackage < mRestorePackages.length) {
217 String name = mRestorePackages[mRestorePackage].packageName;
218 if (new File(mDataDir, name).isDirectory()) {
219 if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name);
220 return name;
221 }
222 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700223
Dan Egnorefe52642009-06-24 00:16:33 -0700224 if (DEBUG) Log.v(TAG, " no more packages to restore");
225 return "";
226 }
227
Dan Egnor01445162009-09-21 17:04:05 -0700228 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700229 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
230 if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called");
231 File packageDir = new File(mDataDir, mRestorePackages[mRestorePackage].packageName);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700232
Christopher Tate2fdd4282009-06-12 15:20:04 -0700233 // The restore set is the concatenation of the individual record blobs,
234 // each of which is a file in the package's directory
235 File[] blobs = packageDir.listFiles();
Dan Egnor01445162009-09-21 17:04:05 -0700236 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Dan Egnorefe52642009-06-24 00:16:33 -0700237 Log.e(TAG, "Error listing directory: " + packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700238 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700239 }
Dan Egnorefe52642009-06-24 00:16:33 -0700240
241 // We expect at least some data if the directory exists in the first place
242 if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.length + " key files");
243 BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
244 try {
245 for (File f : blobs) {
246 FileInputStream in = new FileInputStream(f);
247 try {
248 int size = (int) f.length();
249 byte[] buf = new byte[size];
250 in.read(buf);
251 String key = new String(Base64.decode(f.getName()));
252 if (DEBUG) Log.v(TAG, " ... key=" + key + " size=" + size);
253 out.writeEntityHeader(key, size);
254 out.writeEntityData(buf, size);
255 } finally {
256 in.close();
257 }
258 }
Dan Egnor01445162009-09-21 17:04:05 -0700259 return BackupConstants.TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700260 } catch (IOException e) {
261 Log.e(TAG, "Unable to read backup records", e);
Dan Egnor01445162009-09-21 17:04:05 -0700262 return BackupConstants.TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700263 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700264 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700265
Dan Egnorefe52642009-06-24 00:16:33 -0700266 public void finishRestore() {
267 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate3a31a932009-06-22 15:10:30 -0700268 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700269}