blob: c9d621dcd2d88358586d373e1eec2b909f92a044 [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 Tate9ff53a72014-06-03 17:20:07 -070037import java.io.BufferedOutputStream;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070038import java.io.File;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070039import java.io.FileInputStream;
Christopher Tate9ff53a72014-06-03 17:20:07 -070040import java.io.FileNotFoundException;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070041import java.io.FileOutputStream;
42import java.io.IOException;
Christopher Tateadfe8b82014-02-04 16:23:32 -080043import java.util.ArrayList;
Christopher Tate9ff53a72014-06-03 17:20:07 -070044import java.util.Arrays;
Christopher Tateadfe8b82014-02-04 16:23:32 -080045import java.util.Collections;
Christopher Tate9ff53a72014-06-03 17:20:07 -070046import java.util.HashSet;
47import java.util.List;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070048
Elliott Hughesf97c6332014-04-28 16:38:43 -070049import static android.system.OsConstants.*;
Christopher Tateb048c332014-02-21 12:50:21 -080050
Christopher Tate9bbc21a2009-06-10 20:23:25 -070051/**
52 * Backup transport for stashing stuff into a known location on disk, and
53 * later restoring from there. For testing only.
54 */
55
Christopher Tate74318c92014-05-15 19:03:44 -070056public class LocalTransport extends BackupTransport {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070057 private static final String TAG = "LocalTransport";
Christopher Tate2fdd4282009-06-12 15:20:04 -070058 private static final boolean DEBUG = true;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070059
Christopher Tate5cb400b2009-06-25 16:03:14 -070060 private static final String TRANSPORT_DIR_NAME
61 = "com.android.internal.backup.LocalTransport";
62
Chris Tatea8ddef32010-11-10 11:53:26 -080063 private static final String TRANSPORT_DESTINATION_STRING
64 = "Backing up to debug-only private cache";
65
Christopher Tateadfe8b82014-02-04 16:23:32 -080066 // The currently-active restore set always has the same (nonzero!) token
67 private static final long CURRENT_SET_TOKEN = 1;
Christopher Tate50c6df02010-01-29 12:48:20 -080068
Christopher Tate9bbc21a2009-06-10 20:23:25 -070069 private Context mContext;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070070 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Christopher Tateadfe8b82014-02-04 16:23:32 -080071 private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN));
Christopher Tate9ff53a72014-06-03 17:20:07 -070072 private File mCurrentSetIncrementalDir = new File(mCurrentSetDir, "_delta");
73 private File mCurrentSetFullDir = new File(mCurrentSetDir, "_full");
Christopher Tateadfe8b82014-02-04 16:23:32 -080074
Dan Egnorefe52642009-06-24 00:16:33 -070075 private PackageInfo[] mRestorePackages = null;
76 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tateadfe8b82014-02-04 16:23:32 -080077 private File mRestoreDataDir;
78 private long mRestoreToken;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070079
Christopher Tate9ff53a72014-06-03 17:20:07 -070080 // Additional bookkeeping for full backup
81 private String mFullTargetPackage;
82 private ParcelFileDescriptor mSocket;
83 private FileInputStream mSocketInputStream;
84 private BufferedOutputStream mFullBackupOutputStream;
85 private byte[] mFullBackupBuffer;
86
87 private File mFullRestoreSetDir;
88 private HashSet<String> mFullRestorePackages;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070089
90 public LocalTransport(Context context) {
91 mContext = context;
Christopher Tateadfe8b82014-02-04 16:23:32 -080092 mCurrentSetDir.mkdirs();
Christopher Tate9ff53a72014-06-03 17:20:07 -070093 mCurrentSetFullDir.mkdir();
94 mCurrentSetIncrementalDir.mkdir();
Christopher Tateadfe8b82014-02-04 16:23:32 -080095 if (!SELinux.restorecon(mCurrentSetDir)) {
96 Log.e(TAG, "SELinux restorecon failed for " + mCurrentSetDir);
rpcraigebab0ae2012-12-04 09:37:23 -050097 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -070098 }
99
Christopher Tatecefba582013-11-14 18:10:35 -0800100 public String name() {
101 return new ComponentName(mContext, this.getClass()).flattenToShortString();
102 }
103
Chris Tatea8ddef32010-11-10 11:53:26 -0800104 public Intent configurationIntent() {
105 // The local transport is not user-configurable
106 return null;
107 }
108
109 public String currentDestinationString() {
110 return TRANSPORT_DESTINATION_STRING;
111 }
Christopher Tate5cb400b2009-06-25 16:03:14 -0700112
Dan Egnor01445162009-09-21 17:04:05 -0700113 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -0700114 return TRANSPORT_DIR_NAME;
115 }
116
Dan Egnor01445162009-09-21 17:04:05 -0700117 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700118 // any time is a good time for local backup
119 return 0;
120 }
121
Dan Egnor01445162009-09-21 17:04:05 -0700122 public int initializeDevice() {
123 if (DEBUG) Log.v(TAG, "wiping all data");
Christopher Tateadfe8b82014-02-04 16:23:32 -0800124 deleteContents(mCurrentSetDir);
Christopher Tate4dd26352014-06-02 18:54:18 -0700125 return BackupTransport.TRANSPORT_OK;
Dan Egnor01445162009-09-21 17:04:05 -0700126 }
127
128 public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
Christopher Tateb048c332014-02-21 12:50:21 -0800129 if (DEBUG) {
130 try {
Elliott Hughesf97c6332014-04-28 16:38:43 -0700131 StructStat ss = Os.fstat(data.getFileDescriptor());
Christopher Tateb048c332014-02-21 12:50:21 -0800132 Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName
133 + " size=" + ss.st_size);
134 } catch (ErrnoException e) {
135 Log.w(TAG, "Unable to stat input file in performBackup() on "
136 + packageInfo.packageName);
137 }
138 }
Christopher Tate2fdd4282009-06-12 15:20:04 -0700139
Christopher Tate9ff53a72014-06-03 17:20:07 -0700140 File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700141 packageDir.mkdirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700142
Christopher Tate2fdd4282009-06-12 15:20:04 -0700143 // Each 'record' in the restore set is kept in its own file, named by
144 // the record key. Wind through the data file, extracting individual
145 // record operations and building a set of all the updates to apply
146 // in this update.
147 BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
148 try {
149 int bufSize = 512;
150 byte[] buf = new byte[bufSize];
151 while (changeSet.readNextHeader()) {
152 String key = changeSet.getKey();
Joe Onorato5d605dc2009-06-18 18:23:43 -0700153 String base64Key = new String(Base64.encode(key.getBytes()));
154 File entityFile = new File(packageDir, base64Key);
155
Christopher Tate2fdd4282009-06-12 15:20:04 -0700156 int dataSize = changeSet.getDataSize();
Christopher Tatee9190a22009-06-17 17:52:05 -0700157
Christopher Tatee9190a22009-06-17 17:52:05 -0700158 if (DEBUG) Log.v(TAG, "Got change set key=" + key + " size=" + dataSize
159 + " key64=" + base64Key);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700160
Joe Onorato5d605dc2009-06-18 18:23:43 -0700161 if (dataSize >= 0) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700162 if (entityFile.exists()) {
163 entityFile.delete();
164 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700165 FileOutputStream entity = new FileOutputStream(entityFile);
166
167 if (dataSize > bufSize) {
168 bufSize = dataSize;
169 buf = new byte[bufSize];
170 }
171 changeSet.readEntityData(buf, 0, dataSize);
Christopher Tateb048c332014-02-21 12:50:21 -0800172 if (DEBUG) {
173 try {
Elliott Hughesf97c6332014-04-28 16:38:43 -0700174 long cur = Os.lseek(data.getFileDescriptor(), 0, SEEK_CUR);
Christopher Tateb048c332014-02-21 12:50:21 -0800175 Log.v(TAG, " read entity data; new pos=" + cur);
176 }
177 catch (ErrnoException e) {
178 Log.w(TAG, "Unable to stat input file in performBackup() on "
179 + packageInfo.packageName);
180 }
181 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700182
183 try {
184 entity.write(buf, 0, dataSize);
185 } catch (IOException e) {
Dan Egnorefe52642009-06-24 00:16:33 -0700186 Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
Christopher Tate4dd26352014-06-02 18:54:18 -0700187 return BackupTransport.TRANSPORT_ERROR;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700188 } finally {
189 entity.close();
190 }
191 } else {
192 entityFile.delete();
Christopher Tate2fdd4282009-06-12 15:20:04 -0700193 }
194 }
Christopher Tate4dd26352014-06-02 18:54:18 -0700195 return BackupTransport.TRANSPORT_OK;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700196 } catch (IOException e) {
197 // oops, something went wrong. abort the operation and return error.
Dan Egnorefe52642009-06-24 00:16:33 -0700198 Log.v(TAG, "Exception reading backup input:", e);
Christopher Tate4dd26352014-06-02 18:54:18 -0700199 return BackupTransport.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700200 }
Dan Egnorefe52642009-06-24 00:16:33 -0700201 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700202
Christopher Tate25a747f2009-09-20 12:43:58 -0700203 // Deletes the contents but not the given directory
204 private void deleteContents(File dirname) {
205 File[] contents = dirname.listFiles();
206 if (contents != null) {
207 for (File f : contents) {
208 if (f.isDirectory()) {
209 // delete the directory's contents then fall through
210 // and delete the directory itself.
211 deleteContents(f);
212 }
213 f.delete();
214 }
215 }
216 }
217
Dan Egnor01445162009-09-21 17:04:05 -0700218 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700219 if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName);
220
Christopher Tate9ff53a72014-06-03 17:20:07 -0700221 File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName);
Christopher Tate0abf6a02012-03-23 17:45:15 -0700222 final File[] fileset = packageDir.listFiles();
223 if (fileset != null) {
224 for (File f : fileset) {
225 f.delete();
226 }
227 packageDir.delete();
Christopher Tateee0e78a2009-07-02 11:17:03 -0700228 }
Christopher Tate9ff53a72014-06-03 17:20:07 -0700229
230 packageDir = new File(mCurrentSetFullDir, packageInfo.packageName);
231 final File[] tarballs = packageDir.listFiles();
232 if (tarballs != null) {
233 for (File f : tarballs) {
234 f.delete();
235 }
236 packageDir.delete();
237 }
238
Christopher Tate4dd26352014-06-02 18:54:18 -0700239 return BackupTransport.TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700240 }
241
Dan Egnor01445162009-09-21 17:04:05 -0700242 public int finishBackup() {
Dan Egnorefe52642009-06-24 00:16:33 -0700243 if (DEBUG) Log.v(TAG, "finishBackup()");
Christopher Tate9ff53a72014-06-03 17:20:07 -0700244 if (mSocket != null) {
245 if (DEBUG) {
246 Log.v(TAG, "Concluding full backup of " + mFullTargetPackage);
247 }
248 try {
249 mFullBackupOutputStream.flush();
250 mFullBackupOutputStream.close();
251 mSocketInputStream = null;
252 mFullTargetPackage = null;
253 mSocket.close();
254 } catch (IOException e) {
255 return BackupTransport.TRANSPORT_ERROR;
256 } finally {
257 mSocket = null;
258 }
259 }
Christopher Tate4dd26352014-06-02 18:54:18 -0700260 return BackupTransport.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700261 }
262
Christopher Tate9ff53a72014-06-03 17:20:07 -0700263 // ------------------------------------------------------------------------------------
264 // Full backup handling
265 public long requestFullBackupTime() {
266 return 0;
267 }
268
269 public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket) {
270 if (mSocket != null) {
271 Log.e(TAG, "Attempt to initiate full backup while one is in progress");
272 return BackupTransport.TRANSPORT_ERROR;
273 }
274
275 if (DEBUG) {
276 Log.i(TAG, "performFullBackup : " + targetPackage);
277 }
278
279 // We know a priori that we run in the system process, so we need to make
280 // sure to dup() our own copy of the socket fd. Transports which run in
281 // their own processes must not do this.
282 try {
283 mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
284 mSocketInputStream = new FileInputStream(mSocket.getFileDescriptor());
285 } catch (IOException e) {
286 Log.e(TAG, "Unable to process socket for full backup");
287 return BackupTransport.TRANSPORT_ERROR;
288 }
289
290 mFullTargetPackage = targetPackage.packageName;
291 FileOutputStream tarstream;
292 try {
293 File tarball = new File(mCurrentSetFullDir, mFullTargetPackage);
294 tarstream = new FileOutputStream(tarball);
295 } catch (FileNotFoundException e) {
296 return BackupTransport.TRANSPORT_ERROR;
297 }
298 mFullBackupOutputStream = new BufferedOutputStream(tarstream);
299 mFullBackupBuffer = new byte[4096];
300
301 return BackupTransport.TRANSPORT_OK;
302 }
303
304 public int sendBackupData(int numBytes) {
305 if (mFullBackupBuffer == null) {
306 Log.w(TAG, "Attempted sendBackupData before performFullBackup");
307 return BackupTransport.TRANSPORT_ERROR;
308 }
309
310 if (numBytes > mFullBackupBuffer.length) {
311 mFullBackupBuffer = new byte[numBytes];
312 }
313 while (numBytes > 0) {
314 try {
315 int nRead = mSocketInputStream.read(mFullBackupBuffer, 0, numBytes);
316 if (nRead < 0) {
317 // Something went wrong if we expect data but saw EOD
318 Log.w(TAG, "Unexpected EOD; failing backup");
319 return BackupTransport.TRANSPORT_ERROR;
320 }
321 mFullBackupOutputStream.write(mFullBackupBuffer, 0, nRead);
322 numBytes -= nRead;
323 } catch (IOException e) {
324 Log.e(TAG, "Error handling backup data for " + mFullTargetPackage);
325 return BackupTransport.TRANSPORT_ERROR;
326 }
327 }
328 return BackupTransport.TRANSPORT_OK;
329 }
330
331 // ------------------------------------------------------------------------------------
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700332 // Restore handling
Christopher Tateadfe8b82014-02-04 16:23:32 -0800333 static final long[] POSSIBLE_SETS = { 2, 3, 4, 5, 6, 7, 8, 9 };
Christopher Tate74318c92014-05-15 19:03:44 -0700334 public RestoreSet[] getAvailableRestoreSets() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800335 long[] existing = new long[POSSIBLE_SETS.length + 1];
336 int num = 0;
337
Christopher Tate9ff53a72014-06-03 17:20:07 -0700338 // see which possible non-current sets exist...
Christopher Tateadfe8b82014-02-04 16:23:32 -0800339 for (long token : POSSIBLE_SETS) {
340 if ((new File(mDataDir, Long.toString(token))).exists()) {
341 existing[num++] = token;
342 }
343 }
Christopher Tate9ff53a72014-06-03 17:20:07 -0700344 // ...and always the currently-active set last
Christopher Tateadfe8b82014-02-04 16:23:32 -0800345 existing[num++] = CURRENT_SET_TOKEN;
346
347 RestoreSet[] available = new RestoreSet[num];
348 for (int i = 0; i < available.length; i++) {
349 available[i] = new RestoreSet("Local disk image", "flash", existing[i]);
350 }
351 return available;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700352 }
353
Christopher Tate50c6df02010-01-29 12:48:20 -0800354 public long getCurrentRestoreSet() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800355 // The current restore set always has the same token
356 return CURRENT_SET_TOKEN;
Christopher Tate50c6df02010-01-29 12:48:20 -0800357 }
358
Dan Egnor01445162009-09-21 17:04:05 -0700359 public int startRestore(long token, PackageInfo[] packages) {
Dan Egnorefe52642009-06-24 00:16:33 -0700360 if (DEBUG) Log.v(TAG, "start restore " + token);
361 mRestorePackages = packages;
362 mRestorePackage = -1;
Christopher Tateadfe8b82014-02-04 16:23:32 -0800363 mRestoreToken = token;
364 mRestoreDataDir = new File(mDataDir, Long.toString(token));
Christopher Tate4dd26352014-06-02 18:54:18 -0700365 return BackupTransport.TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700366 }
367
Dan Egnorefe52642009-06-24 00:16:33 -0700368 public String nextRestorePackage() {
369 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
370 while (++mRestorePackage < mRestorePackages.length) {
371 String name = mRestorePackages[mRestorePackage].packageName;
Christopher Tatea9b91862014-02-25 17:42:21 -0800372 // skip packages where we have a data dir but no actual contents
Christopher Tateadfe8b82014-02-04 16:23:32 -0800373 String[] contents = (new File(mRestoreDataDir, name)).list();
Christopher Tatea9b91862014-02-25 17:42:21 -0800374 if (contents != null && contents.length > 0) {
Dan Egnorefe52642009-06-24 00:16:33 -0700375 if (DEBUG) Log.v(TAG, " nextRestorePackage() = " + name);
376 return name;
377 }
378 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700379
Dan Egnorefe52642009-06-24 00:16:33 -0700380 if (DEBUG) Log.v(TAG, " no more packages to restore");
381 return "";
382 }
383
Dan Egnor01445162009-09-21 17:04:05 -0700384 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700385 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
386 if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called");
Christopher Tateadfe8b82014-02-04 16:23:32 -0800387 File packageDir = new File(mRestoreDataDir, mRestorePackages[mRestorePackage].packageName);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700388
Christopher Tate2fdd4282009-06-12 15:20:04 -0700389 // The restore set is the concatenation of the individual record blobs,
Christopher Tateadfe8b82014-02-04 16:23:32 -0800390 // each of which is a file in the package's directory. We return the
391 // data in lexical order sorted by key, so that apps which use synthetic
392 // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious
393 // order.
394 ArrayList<DecodedFilename> blobs = contentsByKey(packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700395 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Christopher Tateadfe8b82014-02-04 16:23:32 -0800396 Log.e(TAG, "No keys for package: " + packageDir);
Christopher Tate4dd26352014-06-02 18:54:18 -0700397 return BackupTransport.TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700398 }
Dan Egnorefe52642009-06-24 00:16:33 -0700399
400 // We expect at least some data if the directory exists in the first place
Christopher Tateadfe8b82014-02-04 16:23:32 -0800401 if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files");
Dan Egnorefe52642009-06-24 00:16:33 -0700402 BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
403 try {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800404 for (DecodedFilename keyEntry : blobs) {
405 File f = keyEntry.file;
Dan Egnorefe52642009-06-24 00:16:33 -0700406 FileInputStream in = new FileInputStream(f);
407 try {
408 int size = (int) f.length();
409 byte[] buf = new byte[size];
410 in.read(buf);
Christopher Tateadfe8b82014-02-04 16:23:32 -0800411 if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size);
412 out.writeEntityHeader(keyEntry.key, size);
Dan Egnorefe52642009-06-24 00:16:33 -0700413 out.writeEntityData(buf, size);
414 } finally {
415 in.close();
416 }
417 }
Christopher Tate4dd26352014-06-02 18:54:18 -0700418 return BackupTransport.TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700419 } catch (IOException e) {
420 Log.e(TAG, "Unable to read backup records", e);
Christopher Tate4dd26352014-06-02 18:54:18 -0700421 return BackupTransport.TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700422 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700423 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700424
Christopher Tateadfe8b82014-02-04 16:23:32 -0800425 static class DecodedFilename implements Comparable<DecodedFilename> {
426 public File file;
427 public String key;
428
429 public DecodedFilename(File f) {
430 file = f;
431 key = new String(Base64.decode(f.getName()));
432 }
433
434 @Override
435 public int compareTo(DecodedFilename other) {
436 // sorts into ascending lexical order by decoded key
437 return key.compareTo(other.key);
438 }
439 }
440
441 // Return a list of the files in the given directory, sorted lexically by
442 // the Base64-decoded file name, not by the on-disk filename
443 private ArrayList<DecodedFilename> contentsByKey(File dir) {
444 File[] allFiles = dir.listFiles();
445 if (allFiles == null || allFiles.length == 0) {
446 return null;
447 }
448
449 // Decode the filenames into keys then sort lexically by key
450 ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>();
451 for (File f : allFiles) {
452 contents.add(new DecodedFilename(f));
453 }
454 Collections.sort(contents);
455 return contents;
456 }
457
Dan Egnorefe52642009-06-24 00:16:33 -0700458 public void finishRestore() {
459 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate3a31a932009-06-22 15:10:30 -0700460 }
Christopher Tate9ff53a72014-06-03 17:20:07 -0700461
462 // ------------------------------------------------------------------------------------
463 // Full restore handling
464
465 public int prepareFullRestore(long token, String[] targetPackages) {
466 mRestoreDataDir = new File(mDataDir, Long.toString(token));
467 mFullRestoreSetDir = new File(mRestoreDataDir, "_full");
468 mFullRestorePackages = new HashSet<String>();
469 if (mFullRestoreSetDir.exists()) {
470 List<String> pkgs = Arrays.asList(mFullRestoreSetDir.list());
471 HashSet<String> available = new HashSet<String>(pkgs);
472
473 for (int i = 0; i < targetPackages.length; i++) {
474 if (available.contains(targetPackages[i])) {
475 mFullRestorePackages.add(targetPackages[i]);
476 }
477 }
478 }
479 return BackupTransport.TRANSPORT_OK;
480 }
481
482 /**
483 * Ask the transport what package's full data will be restored next. When all apps'
484 * data has been delivered, the transport should return {@code null} here.
485 * @return The package name of the next application whose data will be restored, or
486 * {@code null} if all available package has been delivered.
487 */
488 public String getNextFullRestorePackage() {
489 return null;
490 }
491
492 /**
493 * Ask the transport to provide data for the "current" package being restored. The
494 * transport then writes some data to the socket supplied to this call, and returns
495 * the number of bytes written. The system will then read that many bytes and
496 * stream them to the application's agent for restore, then will call this method again
497 * to receive the next chunk of the archive. This sequence will be repeated until the
498 * transport returns zero indicating that all of the package's data has been delivered
499 * (or returns a negative value indicating some sort of hard error condition at the
500 * transport level).
501 *
502 * <p>After this method returns zero, the system will then call
503 * {@link #getNextFullRestorePackage()} to begin the restore process for the next
504 * application, and the sequence begins again.
505 *
506 * @param socket The file descriptor that the transport will use for delivering the
507 * streamed archive.
508 * @return 0 when no more data for the current package is available. A positive value
509 * indicates the presence of that much data to be delivered to the app. A negative
510 * return value is treated as equivalent to {@link BackupTransport#TRANSPORT_ERROR},
511 * indicating a fatal error condition that precludes further restore operations
512 * on the current dataset.
513 */
514 public int getNextFullRestoreDataChunk(ParcelFileDescriptor socket) {
515 return 0;
516 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700517}