blob: f2b29ef69e256469b9de5b420b6a9ac710c9e99c [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;
Christopher Tate74318c92014-05-15 19:03:44 -070021import android.app.backup.BackupTransport;
Christopher Tate45281862010-03-05 15:46:30 -080022import android.app.backup.RestoreSet;
Christopher Tatecefba582013-11-14 18:10:35 -080023import android.content.ComponentName;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070024import android.content.Context;
Chris Tatea8ddef32010-11-10 11:53:26 -080025import android.content.Intent;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070026import android.content.pm.PackageInfo;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070027import android.os.Environment;
28import android.os.ParcelFileDescriptor;
rpcraigebab0ae2012-12-04 09:37:23 -050029import android.os.SELinux;
Elliott Hughesf97c6332014-04-28 16:38:43 -070030import android.system.ErrnoException;
31import android.system.Os;
32import android.system.StructStat;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070033import android.util.Log;
34
Brian Carlstrom4140fae2011-01-24 16:17:43 -080035import com.android.org.bouncycastle.util.encoders.Base64;
Christopher Tatee9190a22009-06-17 17:52:05 -070036
Christopher Tate9bbc21a2009-06-10 20:23:25 -070037import java.io.File;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070038import java.io.FileInputStream;
39import java.io.FileOutputStream;
40import java.io.IOException;
Christopher Tateadfe8b82014-02-04 16:23:32 -080041import java.util.ArrayList;
42import java.util.Collections;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070043
Elliott Hughesf97c6332014-04-28 16:38:43 -070044import static android.system.OsConstants.*;
Christopher Tateb048c332014-02-21 12:50:21 -080045
Christopher Tate9bbc21a2009-06-10 20:23:25 -070046/**
47 * Backup transport for stashing stuff into a known location on disk, and
48 * later restoring from there. For testing only.
49 */
50
Christopher Tate74318c92014-05-15 19:03:44 -070051public class LocalTransport extends BackupTransport {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070052 private static final String TAG = "LocalTransport";
Christopher Tate2fdd4282009-06-12 15:20:04 -070053 private static final boolean DEBUG = true;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070054
Christopher Tate5cb400b2009-06-25 16:03:14 -070055 private static final String TRANSPORT_DIR_NAME
56 = "com.android.internal.backup.LocalTransport";
57
Chris Tatea8ddef32010-11-10 11:53:26 -080058 private static final String TRANSPORT_DESTINATION_STRING
59 = "Backing up to debug-only private cache";
60
Christopher Tateadfe8b82014-02-04 16:23:32 -080061 // The currently-active restore set always has the same (nonzero!) token
62 private static final long CURRENT_SET_TOKEN = 1;
Christopher Tate50c6df02010-01-29 12:48:20 -080063
Christopher Tate9bbc21a2009-06-10 20:23:25 -070064 private Context mContext;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070065 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Christopher Tateadfe8b82014-02-04 16:23:32 -080066 private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN));
67
Dan Egnorefe52642009-06-24 00:16:33 -070068 private PackageInfo[] mRestorePackages = null;
69 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tateadfe8b82014-02-04 16:23:32 -080070 private File mRestoreDataDir;
71 private long mRestoreToken;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070072
73
74 public LocalTransport(Context context) {
75 mContext = context;
Christopher Tateadfe8b82014-02-04 16:23:32 -080076 mCurrentSetDir.mkdirs();
77 if (!SELinux.restorecon(mCurrentSetDir)) {
78 Log.e(TAG, "SELinux restorecon failed for " + mCurrentSetDir);
rpcraigebab0ae2012-12-04 09:37:23 -050079 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -070080 }
81
Christopher Tatecefba582013-11-14 18:10:35 -080082 public String name() {
83 return new ComponentName(mContext, this.getClass()).flattenToShortString();
84 }
85
Chris Tatea8ddef32010-11-10 11:53:26 -080086 public Intent configurationIntent() {
87 // The local transport is not user-configurable
88 return null;
89 }
90
91 public String currentDestinationString() {
92 return TRANSPORT_DESTINATION_STRING;
93 }
Christopher Tate5cb400b2009-06-25 16:03:14 -070094
Dan Egnor01445162009-09-21 17:04:05 -070095 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -070096 return TRANSPORT_DIR_NAME;
97 }
98
Dan Egnor01445162009-09-21 17:04:05 -070099 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700100 // any time is a good time for local backup
101 return 0;
102 }
103
Dan Egnor01445162009-09-21 17:04:05 -0700104 public int initializeDevice() {
105 if (DEBUG) Log.v(TAG, "wiping all data");
Christopher Tateadfe8b82014-02-04 16:23:32 -0800106 deleteContents(mCurrentSetDir);
Dan Egnor01445162009-09-21 17:04:05 -0700107 return BackupConstants.TRANSPORT_OK;
108 }
109
110 public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
Christopher Tateb048c332014-02-21 12:50:21 -0800111 if (DEBUG) {
112 try {
Elliott Hughesf97c6332014-04-28 16:38:43 -0700113 StructStat ss = Os.fstat(data.getFileDescriptor());
Christopher Tateb048c332014-02-21 12:50:21 -0800114 Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName
115 + " size=" + ss.st_size);
116 } catch (ErrnoException e) {
117 Log.w(TAG, "Unable to stat input file in performBackup() on "
118 + packageInfo.packageName);
119 }
120 }
Christopher Tate2fdd4282009-06-12 15:20:04 -0700121
Christopher Tateadfe8b82014-02-04 16:23:32 -0800122 File packageDir = new File(mCurrentSetDir, packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700123 packageDir.mkdirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700124
Christopher Tate2fdd4282009-06-12 15:20:04 -0700125 // Each 'record' in the restore set is kept in its own file, named by
126 // the record key. Wind through the data file, extracting individual
127 // record operations and building a set of all the updates to apply
128 // in this update.
129 BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
130 try {
131 int bufSize = 512;
132 byte[] buf = new byte[bufSize];
133 while (changeSet.readNextHeader()) {
134 String key = changeSet.getKey();
Joe Onorato5d605dc2009-06-18 18:23:43 -0700135 String base64Key = new String(Base64.encode(key.getBytes()));
136 File entityFile = new File(packageDir, base64Key);
137
Christopher Tate2fdd4282009-06-12 15:20:04 -0700138 int dataSize = changeSet.getDataSize();
Christopher Tatee9190a22009-06-17 17:52:05 -0700139
Christopher Tatee9190a22009-06-17 17:52:05 -0700140 if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize
141 + " key64=" + base64Key);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700142
Joe Onorato5d605dc2009-06-18 18:23:43 -0700143 if (dataSize >= 0) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700144 if (entityFile.exists()) {
145 entityFile.delete();
146 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700147 FileOutputStream entity = new FileOutputStream(entityFile);
148
149 if (dataSize > bufSize) {
150 bufSize = dataSize;
151 buf = new byte[bufSize];
152 }
153 changeSet.readEntityData(buf, 0, dataSize);
Christopher Tateb048c332014-02-21 12:50:21 -0800154 if (DEBUG) {
155 try {
Elliott Hughesf97c6332014-04-28 16:38:43 -0700156 long cur = Os.lseek(data.getFileDescriptor(), 0, SEEK_CUR);
Christopher Tateb048c332014-02-21 12:50:21 -0800157 Log.v(TAG, " read entity data; new pos=" + cur);
158 }
159 catch (ErrnoException e) {
160 Log.w(TAG, "Unable to stat input file in performBackup() on "
161 + packageInfo.packageName);
162 }
163 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700164
165 try {
166 entity.write(buf, 0, dataSize);
167 } catch (IOException e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700168 Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
Christopher Tated55e18a2009-09-21 10:12:59 -0700169 return BackupConstants.TRANSPORT_ERROR;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700170 } finally {
171 entity.close();
172 }
173 } else {
174 entityFile.delete();
Christopher Tate2fdd4282009-06-12 15:20:04 -0700175 }
176 }
Christopher Tated55e18a2009-09-21 10:12:59 -0700177 return BackupConstants.TRANSPORT_OK;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700178 } catch (IOException e) {
179 // oops, something went wrong. abort the operation and return error.
Dan Egnorefe52642009-06-24 00:16:33 -0700180 Log.v(TAG, "Exception reading backup input:", e);
Christopher Tated55e18a2009-09-21 10:12:59 -0700181 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700182 }
Dan Egnorefe52642009-06-24 00:16:33 -0700183 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700184
Christopher Tate25a747f2009-09-20 12:43:58 -0700185 // Deletes the contents but not the given directory
186 private void deleteContents(File dirname) {
187 File[] contents = dirname.listFiles();
188 if (contents != null) {
189 for (File f : contents) {
190 if (f.isDirectory()) {
191 // delete the directory's contents then fall through
192 // and delete the directory itself.
193 deleteContents(f);
194 }
195 f.delete();
196 }
197 }
198 }
199
Dan Egnor01445162009-09-21 17:04:05 -0700200 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700201 if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName);
202
Christopher Tateadfe8b82014-02-04 16:23:32 -0800203 File packageDir = new File(mCurrentSetDir, packageInfo.packageName);
Christopher Tate0abf6a02012-03-23 17:45:15 -0700204 final File[] fileset = packageDir.listFiles();
205 if (fileset != null) {
206 for (File f : fileset) {
207 f.delete();
208 }
209 packageDir.delete();
Christopher Tateee0e78a2009-07-02 11:17:03 -0700210 }
Dan Egnor01445162009-09-21 17:04:05 -0700211 return BackupConstants.TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700212 }
213
Dan Egnor01445162009-09-21 17:04:05 -0700214 public int finishBackup() {
Dan Egnorefe52642009-06-24 00:16:33 -0700215 if (DEBUG) Log.v(TAG, "finishBackup()");
Dan Egnor01445162009-09-21 17:04:05 -0700216 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700217 }
218
219 // Restore handling
Christopher Tateadfe8b82014-02-04 16:23:32 -0800220 static final long[] POSSIBLE_SETS = { 2, 3, 4, 5, 6, 7, 8, 9 };
Christopher Tate74318c92014-05-15 19:03:44 -0700221 public RestoreSet[] getAvailableRestoreSets() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800222 long[] existing = new long[POSSIBLE_SETS.length + 1];
223 int num = 0;
224
225 // see which possible non-current sets exist, then put the current set at the end
226 for (long token : POSSIBLE_SETS) {
227 if ((new File(mDataDir, Long.toString(token))).exists()) {
228 existing[num++] = token;
229 }
230 }
231 // and always the currently-active set last
232 existing[num++] = CURRENT_SET_TOKEN;
233
234 RestoreSet[] available = new RestoreSet[num];
235 for (int i = 0; i < available.length; i++) {
236 available[i] = new RestoreSet("Local disk image", "flash", existing[i]);
237 }
238 return available;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700239 }
240
Christopher Tate50c6df02010-01-29 12:48:20 -0800241 public long getCurrentRestoreSet() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800242 // The current restore set always has the same token
243 return CURRENT_SET_TOKEN;
Christopher Tate50c6df02010-01-29 12:48:20 -0800244 }
245
Dan Egnor01445162009-09-21 17:04:05 -0700246 public int startRestore(long token, PackageInfo[] packages) {
Dan Egnorefe52642009-06-24 00:16:33 -0700247 if (DEBUG) Log.v(TAG, "start restore " + token);
248 mRestorePackages = packages;
249 mRestorePackage = -1;
Christopher Tateadfe8b82014-02-04 16:23:32 -0800250 mRestoreToken = token;
251 mRestoreDataDir = new File(mDataDir, Long.toString(token));
Dan Egnor01445162009-09-21 17:04:05 -0700252 return BackupConstants.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700253 }
254
Dan Egnorefe52642009-06-24 00:16:33 -0700255 public String nextRestorePackage() {
256 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
257 while (++mRestorePackage < mRestorePackages.length) {
258 String name = mRestorePackages[mRestorePackage].packageName;
Christopher Tatea9b91862014-02-25 17:42:21 -0800259 // skip packages where we have a data dir but no actual contents
Christopher Tateadfe8b82014-02-04 16:23:32 -0800260 String[] contents = (new File(mRestoreDataDir, name)).list();
Christopher Tatea9b91862014-02-25 17:42:21 -0800261 if (contents != null && contents.length > 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700262 if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name);
263 return name;
264 }
265 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700266
Dan Egnorefe52642009-06-24 00:16:33 -0700267 if (DEBUG) Log.v(TAG, " no more packages to restore");
268 return "";
269 }
270
Dan Egnor01445162009-09-21 17:04:05 -0700271 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700272 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
273 if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called");
Christopher Tateadfe8b82014-02-04 16:23:32 -0800274 File packageDir = new File(mRestoreDataDir, mRestorePackages[mRestorePackage].packageName);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700275
Christopher Tate2fdd4282009-06-12 15:20:04 -0700276 // The restore set is the concatenation of the individual record blobs,
Christopher Tateadfe8b82014-02-04 16:23:32 -0800277 // each of which is a file in the package's directory. We return the
278 // data in lexical order sorted by key, so that apps which use synthetic
279 // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious
280 // order.
281 ArrayList<DecodedFilename> blobs = contentsByKey(packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700282 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Christopher Tateadfe8b82014-02-04 16:23:32 -0800283 Log.e(TAG, "No keys for package: " + packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700284 return BackupConstants.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700285 }
Dan Egnorefe52642009-06-24 00:16:33 -0700286
287 // We expect at least some data if the directory exists in the first place
Christopher Tateadfe8b82014-02-04 16:23:32 -0800288 if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files");
Dan Egnorefe52642009-06-24 00:16:33 -0700289 BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
290 try {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800291 for (DecodedFilename keyEntry : blobs) {
292 File f = keyEntry.file;
Dan Egnorefe52642009-06-24 00:16:33 -0700293 FileInputStream in = new FileInputStream(f);
294 try {
295 int size = (int) f.length();
296 byte[] buf = new byte[size];
297 in.read(buf);
Christopher Tateadfe8b82014-02-04 16:23:32 -0800298 if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size);
299 out.writeEntityHeader(keyEntry.key, size);
Dan Egnorefe52642009-06-24 00:16:33 -0700300 out.writeEntityData(buf, size);
301 } finally {
302 in.close();
303 }
304 }
Dan Egnor01445162009-09-21 17:04:05 -0700305 return BackupConstants.TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700306 } catch (IOException e) {
307 Log.e(TAG, "Unable to read backup records", e);
Dan Egnor01445162009-09-21 17:04:05 -0700308 return BackupConstants.TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700309 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700310 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700311
Christopher Tateadfe8b82014-02-04 16:23:32 -0800312 static class DecodedFilename implements Comparable<DecodedFilename> {
313 public File file;
314 public String key;
315
316 public DecodedFilename(File f) {
317 file = f;
318 key = new String(Base64.decode(f.getName()));
319 }
320
321 @Override
322 public int compareTo(DecodedFilename other) {
323 // sorts into ascending lexical order by decoded key
324 return key.compareTo(other.key);
325 }
326 }
327
328 // Return a list of the files in the given directory, sorted lexically by
329 // the Base64-decoded file name, not by the on-disk filename
330 private ArrayList<DecodedFilename> contentsByKey(File dir) {
331 File[] allFiles = dir.listFiles();
332 if (allFiles == null || allFiles.length == 0) {
333 return null;
334 }
335
336 // Decode the filenames into keys then sort lexically by key
337 ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>();
338 for (File f : allFiles) {
339 contents.add(new DecodedFilename(f));
340 }
341 Collections.sort(contents);
342 return contents;
343 }
344
Dan Egnorefe52642009-06-24 00:16:33 -0700345 public void finishRestore() {
346 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate3a31a932009-06-22 15:10:30 -0700347 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700348}