blob: 1bfad05a2d59215fd592067ad3bb1e2e89c7d870 [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;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070026import android.os.Environment;
27import android.os.ParcelFileDescriptor;
rpcraigebab0ae2012-12-04 09:37:23 -050028import android.os.SELinux;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070029import android.util.Log;
30
Brian Carlstrom4140fae2011-01-24 16:17:43 -080031import com.android.org.bouncycastle.util.encoders.Base64;
Christopher Tatee9190a22009-06-17 17:52:05 -070032
Christopher Tate9bbc21a2009-06-10 20:23:25 -070033import java.io.File;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070034import java.io.FileInputStream;
35import java.io.FileOutputStream;
36import java.io.IOException;
Christopher Tateadfe8b82014-02-04 16:23:32 -080037import java.util.ArrayList;
38import java.util.Collections;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070039
Christopher Tateb048c332014-02-21 12:50:21 -080040import libcore.io.ErrnoException;
41import libcore.io.Libcore;
42import libcore.io.StructStat;
43import static libcore.io.OsConstants.*;
44
Christopher Tate9bbc21a2009-06-10 20:23:25 -070045/**
46 * Backup transport for stashing stuff into a known location on disk, and
47 * later restoring from there. For testing only.
48 */
49
50public class LocalTransport extends IBackupTransport.Stub {
51 private static final String TAG = "LocalTransport";
Christopher Tate2fdd4282009-06-12 15:20:04 -070052 private static final boolean DEBUG = true;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070053
Christopher Tate5cb400b2009-06-25 16:03:14 -070054 private static final String TRANSPORT_DIR_NAME
55 = "com.android.internal.backup.LocalTransport";
56
Chris Tatea8ddef32010-11-10 11:53:26 -080057 private static final String TRANSPORT_DESTINATION_STRING
58 = "Backing up to debug-only private cache";
59
Christopher Tateadfe8b82014-02-04 16:23:32 -080060 // The currently-active restore set always has the same (nonzero!) token
61 private static final long CURRENT_SET_TOKEN = 1;
Christopher Tate50c6df02010-01-29 12:48:20 -080062
Christopher Tate9bbc21a2009-06-10 20:23:25 -070063 private Context mContext;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070064 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Christopher Tateadfe8b82014-02-04 16:23:32 -080065 private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN));
66
Dan Egnorefe52642009-06-24 00:16:33 -070067 private PackageInfo[] mRestorePackages = null;
68 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tateadfe8b82014-02-04 16:23:32 -080069 private File mRestoreDataDir;
70 private long mRestoreToken;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070071
72
73 public LocalTransport(Context context) {
74 mContext = context;
Christopher Tateadfe8b82014-02-04 16:23:32 -080075 mCurrentSetDir.mkdirs();
76 if (!SELinux.restorecon(mCurrentSetDir)) {
77 Log.e(TAG, "SELinux restorecon failed for " + mCurrentSetDir);
rpcraigebab0ae2012-12-04 09:37:23 -050078 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -070079 }
80
Christopher Tatecefba582013-11-14 18:10:35 -080081 public String name() {
82 return new ComponentName(mContext, this.getClass()).flattenToShortString();
83 }
84
Chris Tatea8ddef32010-11-10 11:53:26 -080085 public Intent configurationIntent() {
86 // The local transport is not user-configurable
87 return null;
88 }
89
90 public String currentDestinationString() {
91 return TRANSPORT_DESTINATION_STRING;
92 }
Christopher Tate5cb400b2009-06-25 16:03:14 -070093
Dan Egnor01445162009-09-21 17:04:05 -070094 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -070095 return TRANSPORT_DIR_NAME;
96 }
97
Dan Egnor01445162009-09-21 17:04:05 -070098 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070099 // any time is a good time for local backup
100 return 0;
101 }
102
Dan Egnor01445162009-09-21 17:04:05 -0700103 public int initializeDevice() {
104 if (DEBUG) Log.v(TAG, "wiping all data");
Christopher Tateadfe8b82014-02-04 16:23:32 -0800105 deleteContents(mCurrentSetDir);
Dan Egnor01445162009-09-21 17:04:05 -0700106 return BackupConstants.TRANSPORT_OK;
107 }
108
109 public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
Christopher Tateb048c332014-02-21 12:50:21 -0800110 if (DEBUG) {
111 try {
112 StructStat ss = Libcore.os.fstat(data.getFileDescriptor());
113 Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName
114 + " size=" + ss.st_size);
115 } catch (ErrnoException e) {
116 Log.w(TAG, "Unable to stat input file in performBackup() on "
117 + packageInfo.packageName);
118 }
119 }
Christopher Tate2fdd4282009-06-12 15:20:04 -0700120
Christopher Tateadfe8b82014-02-04 16:23:32 -0800121 File packageDir = new File(mCurrentSetDir, packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700122 packageDir.mkdirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700123
Christopher Tate2fdd4282009-06-12 15:20:04 -0700124 // Each 'record' in the restore set is kept in its own file, named by
125 // the record key. Wind through the data file, extracting individual
126 // record operations and building a set of all the updates to apply
127 // in this update.
128 BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
129 try {
130 int bufSize = 512;
131 byte[] buf = new byte[bufSize];
132 while (changeSet.readNextHeader()) {
133 String key = changeSet.getKey();
Joe Onorato5d605dc2009-06-18 18:23:43 -0700134 String base64Key = new String(Base64.encode(key.getBytes()));
135 File entityFile = new File(packageDir, base64Key);
136
Christopher Tate2fdd4282009-06-12 15:20:04 -0700137 int dataSize = changeSet.getDataSize();
Christopher Tatee9190a22009-06-17 17:52:05 -0700138
Christopher Tatee9190a22009-06-17 17:52:05 -0700139 if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize
140 + " key64=" + base64Key);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700141
Joe Onorato5d605dc2009-06-18 18:23:43 -0700142 if (dataSize >= 0) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700143 if (entityFile.exists()) {
144 entityFile.delete();
145 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700146 FileOutputStream entity = new FileOutputStream(entityFile);
147
148 if (dataSize > bufSize) {
149 bufSize = dataSize;
150 buf = new byte[bufSize];
151 }
152 changeSet.readEntityData(buf, 0, dataSize);
Christopher Tateb048c332014-02-21 12:50:21 -0800153 if (DEBUG) {
154 try {
155 long cur = Libcore.os.lseek(data.getFileDescriptor(), 0, SEEK_CUR);
156 Log.v(TAG, " read entity data; new pos=" + cur);
157 }
158 catch (ErrnoException e) {
159 Log.w(TAG, "Unable to stat input file in performBackup() on "
160 + packageInfo.packageName);
161 }
162 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700163
164 try {
165 entity.write(buf, 0, dataSize);
166 } catch (IOException e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700167 Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
Christopher Tated55e18a2009-09-21 10:12:59 -0700168 return BackupConstants.TRANSPORT_ERROR;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700169 } finally {
170 entity.close();
171 }
172 } else {
173 entityFile.delete();
Christopher Tate2fdd4282009-06-12 15:20:04 -0700174 }
175 }
Christopher Tated55e18a2009-09-21 10:12:59 -0700176 return BackupConstants.TRANSPORT_OK;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700177 } catch (IOException e) {
178 // oops, something went wrong. abort the operation and return error.
Dan Egnorefe52642009-06-24 00:16:33 -0700179 Log.v(TAG, "Exception reading backup input:", e);
Christopher Tated55e18a2009-09-21 10:12:59 -0700180 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700181 }
Dan Egnorefe52642009-06-24 00:16:33 -0700182 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700183
Christopher Tate25a747f2009-09-20 12:43:58 -0700184 // Deletes the contents but not the given directory
185 private void deleteContents(File dirname) {
186 File[] contents = dirname.listFiles();
187 if (contents != null) {
188 for (File f : contents) {
189 if (f.isDirectory()) {
190 // delete the directory's contents then fall through
191 // and delete the directory itself.
192 deleteContents(f);
193 }
194 f.delete();
195 }
196 }
197 }
198
Dan Egnor01445162009-09-21 17:04:05 -0700199 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700200 if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName);
201
Christopher Tateadfe8b82014-02-04 16:23:32 -0800202 File packageDir = new File(mCurrentSetDir, packageInfo.packageName);
Christopher Tate0abf6a02012-03-23 17:45:15 -0700203 final File[] fileset = packageDir.listFiles();
204 if (fileset != null) {
205 for (File f : fileset) {
206 f.delete();
207 }
208 packageDir.delete();
Christopher Tateee0e78a2009-07-02 11:17:03 -0700209 }
Dan Egnor01445162009-09-21 17:04:05 -0700210 return BackupConstants.TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700211 }
212
Dan Egnor01445162009-09-21 17:04:05 -0700213 public int finishBackup() {
Dan Egnorefe52642009-06-24 00:16:33 -0700214 if (DEBUG) Log.v(TAG, "finishBackup()");
Dan Egnor01445162009-09-21 17:04:05 -0700215 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700216 }
217
218 // Restore handling
Christopher Tateadfe8b82014-02-04 16:23:32 -0800219 static final long[] POSSIBLE_SETS = { 2, 3, 4, 5, 6, 7, 8, 9 };
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700220 public RestoreSet[] getAvailableRestoreSets() throws android.os.RemoteException {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800221 long[] existing = new long[POSSIBLE_SETS.length + 1];
222 int num = 0;
223
224 // see which possible non-current sets exist, then put the current set at the end
225 for (long token : POSSIBLE_SETS) {
226 if ((new File(mDataDir, Long.toString(token))).exists()) {
227 existing[num++] = token;
228 }
229 }
230 // and always the currently-active set last
231 existing[num++] = CURRENT_SET_TOKEN;
232
233 RestoreSet[] available = new RestoreSet[num];
234 for (int i = 0; i < available.length; i++) {
235 available[i] = new RestoreSet("Local disk image", "flash", existing[i]);
236 }
237 return available;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700238 }
239
Christopher Tate50c6df02010-01-29 12:48:20 -0800240 public long getCurrentRestoreSet() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800241 // The current restore set always has the same token
242 return CURRENT_SET_TOKEN;
Christopher Tate50c6df02010-01-29 12:48:20 -0800243 }
244
Dan Egnor01445162009-09-21 17:04:05 -0700245 public int startRestore(long token, PackageInfo[] packages) {
Dan Egnorefe52642009-06-24 00:16:33 -0700246 if (DEBUG) Log.v(TAG, "start restore " + token);
247 mRestorePackages = packages;
248 mRestorePackage = -1;
Christopher Tateadfe8b82014-02-04 16:23:32 -0800249 mRestoreToken = token;
250 mRestoreDataDir = new File(mDataDir, Long.toString(token));
Dan Egnor01445162009-09-21 17:04:05 -0700251 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700252 }
253
Dan Egnorefe52642009-06-24 00:16:33 -0700254 public String nextRestorePackage() {
255 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
256 while (++mRestorePackage < mRestorePackages.length) {
257 String name = mRestorePackages[mRestorePackage].packageName;
Christopher Tatea9b91862014-02-25 17:42:21 -0800258 // skip packages where we have a data dir but no actual contents
Christopher Tateadfe8b82014-02-04 16:23:32 -0800259 String[] contents = (new File(mRestoreDataDir, name)).list();
Christopher Tatea9b91862014-02-25 17:42:21 -0800260 if (contents != null && contents.length > 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700261 if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name);
262 return name;
263 }
264 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700265
Dan Egnorefe52642009-06-24 00:16:33 -0700266 if (DEBUG) Log.v(TAG, " no more packages to restore");
267 return "";
268 }
269
Dan Egnor01445162009-09-21 17:04:05 -0700270 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700271 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
272 if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called");
Christopher Tateadfe8b82014-02-04 16:23:32 -0800273 File packageDir = new File(mRestoreDataDir, mRestorePackages[mRestorePackage].packageName);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700274
Christopher Tate2fdd4282009-06-12 15:20:04 -0700275 // The restore set is the concatenation of the individual record blobs,
Christopher Tateadfe8b82014-02-04 16:23:32 -0800276 // each of which is a file in the package's directory. We return the
277 // data in lexical order sorted by key, so that apps which use synthetic
278 // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious
279 // order.
280 ArrayList<DecodedFilename> blobs = contentsByKey(packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700281 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Christopher Tateadfe8b82014-02-04 16:23:32 -0800282 Log.e(TAG, "No keys for package: " + packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700283 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700284 }
Dan Egnorefe52642009-06-24 00:16:33 -0700285
286 // We expect at least some data if the directory exists in the first place
Christopher Tateadfe8b82014-02-04 16:23:32 -0800287 if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files");
Dan Egnorefe52642009-06-24 00:16:33 -0700288 BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
289 try {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800290 for (DecodedFilename keyEntry : blobs) {
291 File f = keyEntry.file;
Dan Egnorefe52642009-06-24 00:16:33 -0700292 FileInputStream in = new FileInputStream(f);
293 try {
294 int size = (int) f.length();
295 byte[] buf = new byte[size];
296 in.read(buf);
Christopher Tateadfe8b82014-02-04 16:23:32 -0800297 if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size);
298 out.writeEntityHeader(keyEntry.key, size);
Dan Egnorefe52642009-06-24 00:16:33 -0700299 out.writeEntityData(buf, size);
300 } finally {
301 in.close();
302 }
303 }
Dan Egnor01445162009-09-21 17:04:05 -0700304 return BackupConstants.TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700305 } catch (IOException e) {
306 Log.e(TAG, "Unable to read backup records", e);
Dan Egnor01445162009-09-21 17:04:05 -0700307 return BackupConstants.TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700308 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700309 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700310
Christopher Tateadfe8b82014-02-04 16:23:32 -0800311 static class DecodedFilename implements Comparable<DecodedFilename> {
312 public File file;
313 public String key;
314
315 public DecodedFilename(File f) {
316 file = f;
317 key = new String(Base64.decode(f.getName()));
318 }
319
320 @Override
321 public int compareTo(DecodedFilename other) {
322 // sorts into ascending lexical order by decoded key
323 return key.compareTo(other.key);
324 }
325 }
326
327 // Return a list of the files in the given directory, sorted lexically by
328 // the Base64-decoded file name, not by the on-disk filename
329 private ArrayList<DecodedFilename> contentsByKey(File dir) {
330 File[] allFiles = dir.listFiles();
331 if (allFiles == null || allFiles.length == 0) {
332 return null;
333 }
334
335 // Decode the filenames into keys then sort lexically by key
336 ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>();
337 for (File f : allFiles) {
338 contents.add(new DecodedFilename(f));
339 }
340 Collections.sort(contents);
341 return contents;
342 }
343
Dan Egnorefe52642009-06-24 00:16:33 -0700344 public void finishRestore() {
345 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate3a31a932009-06-22 15:10:30 -0700346 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700347}