blob: d0f02725b1a0193b71cce0ffd192c1949225c02f [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
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +000019import android.app.backup.BackupAgent;
Christopher Tate45281862010-03-05 15:46:30 -080020import android.app.backup.BackupDataInput;
21import android.app.backup.BackupDataOutput;
Christopher Tate74318c92014-05-15 19:03:44 -070022import android.app.backup.BackupTransport;
Christopher Tate6a49dd02014-06-16 18:49:25 -070023import android.app.backup.RestoreDescription;
Christopher Tate45281862010-03-05 15:46:30 -080024import android.app.backup.RestoreSet;
Christopher Tatecefba582013-11-14 18:10:35 -080025import android.content.ComponentName;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070026import android.content.Context;
Chris Tatea8ddef32010-11-10 11:53:26 -080027import android.content.Intent;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070028import android.content.pm.PackageInfo;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070029import android.os.Environment;
30import android.os.ParcelFileDescriptor;
Elliott Hughesf97c6332014-04-28 16:38:43 -070031import android.system.ErrnoException;
32import android.system.Os;
33import android.system.StructStat;
Christopher Tatec8a9d422017-06-23 12:48:26 -070034import android.util.ArrayMap;
Bernardo Rufino7a6e0322018-07-17 10:17:43 +010035import android.util.Base64;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070036import android.util.Log;
37
Christopher Tate824392b2014-11-14 18:27:36 -080038import libcore.io.IoUtils;
39
Christopher Tate9ff53a72014-06-03 17:20:07 -070040import java.io.BufferedOutputStream;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070041import java.io.File;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070042import java.io.FileInputStream;
Christopher Tate9ff53a72014-06-03 17:20:07 -070043import java.io.FileNotFoundException;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070044import java.io.FileOutputStream;
45import java.io.IOException;
Christopher Tateadfe8b82014-02-04 16:23:32 -080046import java.util.ArrayList;
47import java.util.Collections;
Christopher Tateb048c332014-02-21 12:50:21 -080048
Christopher Tate9bbc21a2009-06-10 20:23:25 -070049/**
50 * Backup transport for stashing stuff into a known location on disk, and
51 * later restoring from there. For testing only.
52 */
53
Christopher Tate74318c92014-05-15 19:03:44 -070054public class LocalTransport extends BackupTransport {
Christopher Tate9bbc21a2009-06-10 20:23:25 -070055 private static final String TAG = "LocalTransport";
Ed Heyla50cd8d2014-07-14 23:42:04 -070056 private static final boolean DEBUG = false;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070057
Christopher Tate5cb400b2009-06-25 16:03:14 -070058 private static final String TRANSPORT_DIR_NAME
59 = "com.android.internal.backup.LocalTransport";
60
Chris Tatea8ddef32010-11-10 11:53:26 -080061 private static final String TRANSPORT_DESTINATION_STRING
62 = "Backing up to debug-only private cache";
63
Christopher Tate96794102014-07-27 20:21:55 -070064 private static final String TRANSPORT_DATA_MANAGEMENT_LABEL
65 = "";
66
Christopher Tate6a49dd02014-06-16 18:49:25 -070067 private static final String INCREMENTAL_DIR = "_delta";
68 private static final String FULL_DATA_DIR = "_full";
69
Christopher Tateadfe8b82014-02-04 16:23:32 -080070 // The currently-active restore set always has the same (nonzero!) token
71 private static final long CURRENT_SET_TOKEN = 1;
Christopher Tate50c6df02010-01-29 12:48:20 -080072
Christopher Tatec8a9d422017-06-23 12:48:26 -070073 // Size quotas at reasonable values, similar to the current cloud-storage limits
Sergey Poromov872d3b62016-01-12 15:48:08 +010074 private static final long FULL_BACKUP_SIZE_QUOTA = 25 * 1024 * 1024;
Shreyas Basargeb6e73c92017-01-31 20:13:43 +000075 private static final long KEY_VALUE_BACKUP_SIZE_QUOTA = 5 * 1024 * 1024;
76
Christopher Tate9bbc21a2009-06-10 20:23:25 -070077 private Context mContext;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070078 private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
Christopher Tateadfe8b82014-02-04 16:23:32 -080079 private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN));
Christopher Tate6a49dd02014-06-16 18:49:25 -070080 private File mCurrentSetIncrementalDir = new File(mCurrentSetDir, INCREMENTAL_DIR);
81 private File mCurrentSetFullDir = new File(mCurrentSetDir, FULL_DATA_DIR);
Christopher Tateadfe8b82014-02-04 16:23:32 -080082
Dan Egnorefe52642009-06-24 00:16:33 -070083 private PackageInfo[] mRestorePackages = null;
84 private int mRestorePackage = -1; // Index into mRestorePackages
Christopher Tate6a49dd02014-06-16 18:49:25 -070085 private int mRestoreType;
86 private File mRestoreSetDir;
87 private File mRestoreSetIncrementalDir;
88 private File mRestoreSetFullDir;
Christopher Tate9bbc21a2009-06-10 20:23:25 -070089
Christopher Tate9ff53a72014-06-03 17:20:07 -070090 // Additional bookkeeping for full backup
91 private String mFullTargetPackage;
92 private ParcelFileDescriptor mSocket;
93 private FileInputStream mSocketInputStream;
94 private BufferedOutputStream mFullBackupOutputStream;
95 private byte[] mFullBackupBuffer;
Sergey Poromov872d3b62016-01-12 15:48:08 +010096 private long mFullBackupSize;
Christopher Tate9ff53a72014-06-03 17:20:07 -070097
Christopher Tate5a009f92014-06-19 14:53:18 -070098 private FileInputStream mCurFullRestoreStream;
99 private FileOutputStream mFullRestoreSocketStream;
100 private byte[] mFullRestoreBuffer;
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +0000101 private final LocalTransportParameters mParameters;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700102
Christopher Tatede2826b2015-03-12 18:53:31 -0700103 private void makeDataDirs() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800104 mCurrentSetDir.mkdirs();
Christopher Tatede2826b2015-03-12 18:53:31 -0700105 mCurrentSetFullDir.mkdir();
106 mCurrentSetIncrementalDir.mkdir();
107 }
108
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +0000109 public LocalTransport(Context context, LocalTransportParameters parameters) {
Christopher Tatede2826b2015-03-12 18:53:31 -0700110 mContext = context;
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +0000111 mParameters = parameters;
Christopher Tatede2826b2015-03-12 18:53:31 -0700112 makeDataDirs();
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700113 }
114
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +0000115 LocalTransportParameters getParameters() {
116 return mParameters;
117 }
118
Christopher Tate5a009f92014-06-19 14:53:18 -0700119 @Override
Christopher Tatecefba582013-11-14 18:10:35 -0800120 public String name() {
121 return new ComponentName(mContext, this.getClass()).flattenToShortString();
122 }
123
Christopher Tate5a009f92014-06-19 14:53:18 -0700124 @Override
Chris Tatea8ddef32010-11-10 11:53:26 -0800125 public Intent configurationIntent() {
126 // The local transport is not user-configurable
127 return null;
128 }
129
Christopher Tate5a009f92014-06-19 14:53:18 -0700130 @Override
Chris Tatea8ddef32010-11-10 11:53:26 -0800131 public String currentDestinationString() {
132 return TRANSPORT_DESTINATION_STRING;
133 }
Christopher Tate5cb400b2009-06-25 16:03:14 -0700134
Christopher Tate96794102014-07-27 20:21:55 -0700135 public Intent dataManagementIntent() {
136 // The local transport does not present a data-management UI
137 // TODO: consider adding simple UI to wipe the archives entirely,
138 // for cleaning up the cache partition.
139 return null;
140 }
141
142 public String dataManagementLabel() {
143 return TRANSPORT_DATA_MANAGEMENT_LABEL;
144 }
145
Christopher Tate5a009f92014-06-19 14:53:18 -0700146 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700147 public String transportDirName() {
Christopher Tate5cb400b2009-06-25 16:03:14 -0700148 return TRANSPORT_DIR_NAME;
149 }
150
Christopher Tate5a009f92014-06-19 14:53:18 -0700151 @Override
Bernardo Rufinoeaa78b92018-01-26 11:25:37 +0000152 public int getTransportFlags() {
153 int flags = super.getTransportFlags();
154 // Testing for a fake flag and having it set as a boolean in settings prevents anyone from
155 // using this it to pull data from the agent
156 if (mParameters.isFakeEncryptionFlag()) {
157 flags |= BackupAgent.FLAG_FAKE_CLIENT_SIDE_ENCRYPTION_ENABLED;
158 }
159 return flags;
160 }
161
162 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700163 public long requestBackupTime() {
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700164 // any time is a good time for local backup
165 return 0;
166 }
167
Christopher Tate5a009f92014-06-19 14:53:18 -0700168 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700169 public int initializeDevice() {
170 if (DEBUG) Log.v(TAG, "wiping all data");
Christopher Tateadfe8b82014-02-04 16:23:32 -0800171 deleteContents(mCurrentSetDir);
Christopher Tatede2826b2015-03-12 18:53:31 -0700172 makeDataDirs();
Christopher Tate5a009f92014-06-19 14:53:18 -0700173 return TRANSPORT_OK;
Dan Egnor01445162009-09-21 17:04:05 -0700174 }
175
Christopher Tatec8a9d422017-06-23 12:48:26 -0700176 // Encapsulation of a single k/v element change
177 private class KVOperation {
178 final String key; // Element filename, not the raw key, for efficiency
179 final byte[] value; // null when this is a deletion operation
180
181 KVOperation(String k, byte[] v) {
182 key = k;
183 value = v;
184 }
185 }
186
Christopher Tate5a009f92014-06-19 14:53:18 -0700187 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700188 public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
Anton Philippovd9030742018-03-26 19:12:08 +0100189 return performBackup(packageInfo, data, /*flags=*/ 0);
190 }
191
192 @Override
193 public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data, int flags) {
194 boolean isIncremental = (flags & FLAG_INCREMENTAL) != 0;
195 boolean isNonIncremental = (flags & FLAG_NON_INCREMENTAL) != 0;
196
197 if (isIncremental) {
198 Log.i(TAG, "Performing incremental backup for " + packageInfo.packageName);
199 } else if (isNonIncremental) {
200 Log.i(TAG, "Performing non-incremental backup for " + packageInfo.packageName);
201 } else {
202 Log.i(TAG, "Performing backup for " + packageInfo.packageName);
203 }
204
Christopher Tateb048c332014-02-21 12:50:21 -0800205 if (DEBUG) {
206 try {
Anton Philippovd9030742018-03-26 19:12:08 +0100207 StructStat ss = Os.fstat(data.getFileDescriptor());
208 Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName
209 + " size=" + ss.st_size + " flags=" + flags);
Christopher Tateb048c332014-02-21 12:50:21 -0800210 } catch (ErrnoException e) {
211 Log.w(TAG, "Unable to stat input file in performBackup() on "
212 + packageInfo.packageName);
213 }
214 }
Christopher Tate2fdd4282009-06-12 15:20:04 -0700215
Christopher Tate9ff53a72014-06-03 17:20:07 -0700216 File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName);
Anton Philippovd9030742018-03-26 19:12:08 +0100217 boolean hasDataForPackage = !packageDir.mkdirs();
218
219 if (isIncremental) {
220 if (mParameters.isNonIncrementalOnly() || !hasDataForPackage) {
221 if (mParameters.isNonIncrementalOnly()) {
222 Log.w(TAG, "Transport is in non-incremental only mode.");
223
224 } else {
225 Log.w(TAG,
226 "Requested incremental, but transport currently stores no data for the "
227 + "package, requesting non-incremental retry.");
228 }
229 return TRANSPORT_NON_INCREMENTAL_BACKUP_REQUIRED;
230 }
231 }
232 if (isNonIncremental && hasDataForPackage) {
233 Log.w(TAG, "Requested non-incremental, deleting existing data.");
234 clearBackupData(packageInfo);
235 packageDir.mkdirs();
236 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700237
Christopher Tate2fdd4282009-06-12 15:20:04 -0700238 // Each 'record' in the restore set is kept in its own file, named by
239 // the record key. Wind through the data file, extracting individual
Christopher Tatec8a9d422017-06-23 12:48:26 -0700240 // record operations and building a list of all the updates to apply
Christopher Tate2fdd4282009-06-12 15:20:04 -0700241 // in this update.
Christopher Tatec8a9d422017-06-23 12:48:26 -0700242 final ArrayList<KVOperation> changeOps;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700243 try {
Christopher Tatec8a9d422017-06-23 12:48:26 -0700244 changeOps = parseBackupStream(data);
Christopher Tate2fdd4282009-06-12 15:20:04 -0700245 } catch (IOException e) {
246 // oops, something went wrong. abort the operation and return error.
Christopher Tatec8a9d422017-06-23 12:48:26 -0700247 Log.v(TAG, "Exception reading backup input", e);
Christopher Tate5a009f92014-06-19 14:53:18 -0700248 return TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700249 }
Christopher Tatec8a9d422017-06-23 12:48:26 -0700250
251 // Okay, now we've parsed out the delta's individual operations. We need to measure
252 // the effect against what we already have in the datastore to detect quota overrun.
253 // So, we first need to tally up the current in-datastore size per key.
254 final ArrayMap<String, Integer> datastore = new ArrayMap<>();
255 int totalSize = parseKeySizes(packageDir, datastore);
256
257 // ... and now figure out the datastore size that will result from applying the
258 // sequence of delta operations
259 if (DEBUG) {
260 if (changeOps.size() > 0) {
261 Log.v(TAG, "Calculating delta size impact");
262 } else {
263 Log.v(TAG, "No operations in backup stream, so no size change");
264 }
265 }
266 int updatedSize = totalSize;
267 for (KVOperation op : changeOps) {
268 // Deduct the size of the key we're about to replace, if any
269 final Integer curSize = datastore.get(op.key);
270 if (curSize != null) {
271 updatedSize -= curSize.intValue();
272 if (DEBUG && op.value == null) {
273 Log.v(TAG, " delete " + op.key + ", updated total " + updatedSize);
274 }
275 }
276
277 // And add back the size of the value we're about to store, if any
278 if (op.value != null) {
279 updatedSize += op.value.length;
280 if (DEBUG) {
281 Log.v(TAG, ((curSize == null) ? " new " : " replace ")
282 + op.key + ", updated total " + updatedSize);
283 }
284 }
285 }
286
287 // If our final size is over quota, report the failure
288 if (updatedSize > KEY_VALUE_BACKUP_SIZE_QUOTA) {
289 if (DEBUG) {
290 Log.i(TAG, "New datastore size " + updatedSize
291 + " exceeds quota " + KEY_VALUE_BACKUP_SIZE_QUOTA);
292 }
293 return TRANSPORT_QUOTA_EXCEEDED;
294 }
295
296 // No problem with storage size, so go ahead and apply the delta operations
297 // (in the order that the app provided them)
298 for (KVOperation op : changeOps) {
299 File element = new File(packageDir, op.key);
300
301 // this is either a deletion or a rewrite-from-zero, so we can just remove
302 // the existing file and proceed in either case.
303 element.delete();
304
305 // if this wasn't a deletion, put the new data in place
306 if (op.value != null) {
307 try (FileOutputStream out = new FileOutputStream(element)) {
308 out.write(op.value, 0, op.value.length);
309 } catch (IOException e) {
310 Log.e(TAG, "Unable to update key file " + element);
311 return TRANSPORT_ERROR;
312 }
313 }
314 }
315 return TRANSPORT_OK;
316 }
317
318 // Parses a backup stream into individual key/value operations
319 private ArrayList<KVOperation> parseBackupStream(ParcelFileDescriptor data)
320 throws IOException {
321 ArrayList<KVOperation> changeOps = new ArrayList<>();
322 BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
323 while (changeSet.readNextHeader()) {
324 String key = changeSet.getKey();
Bernardo Rufino7a6e0322018-07-17 10:17:43 +0100325 String base64Key = new String(Base64.encode(key.getBytes(), Base64.NO_WRAP));
Christopher Tatec8a9d422017-06-23 12:48:26 -0700326 int dataSize = changeSet.getDataSize();
327 if (DEBUG) {
328 Log.v(TAG, " Delta operation key " + key + " size " + dataSize
329 + " key64 " + base64Key);
330 }
331
332 byte[] buf = (dataSize >= 0) ? new byte[dataSize] : null;
333 if (dataSize >= 0) {
334 changeSet.readEntityData(buf, 0, dataSize);
335 }
336 changeOps.add(new KVOperation(base64Key, buf));
337 }
338 return changeOps;
339 }
340
341 // Reads the given datastore directory, building a table of the value size of each
342 // keyed element, and returning the summed total.
343 private int parseKeySizes(File packageDir, ArrayMap<String, Integer> datastore) {
344 int totalSize = 0;
345 final String[] elements = packageDir.list();
346 if (elements != null) {
347 if (DEBUG) {
348 Log.v(TAG, "Existing datastore contents:");
349 }
350 for (String file : elements) {
351 File element = new File(packageDir, file);
352 String key = file; // filename
353 int size = (int) element.length();
354 totalSize += size;
355 if (DEBUG) {
356 Log.v(TAG, " key " + key + " size " + size);
357 }
358 datastore.put(key, size);
359 }
360 if (DEBUG) {
361 Log.v(TAG, " TOTAL: " + totalSize);
362 }
363 } else {
364 if (DEBUG) {
365 Log.v(TAG, "No existing data for this package");
366 }
367 }
368 return totalSize;
Dan Egnorefe52642009-06-24 00:16:33 -0700369 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700370
Christopher Tate25a747f2009-09-20 12:43:58 -0700371 // Deletes the contents but not the given directory
372 private void deleteContents(File dirname) {
373 File[] contents = dirname.listFiles();
374 if (contents != null) {
375 for (File f : contents) {
376 if (f.isDirectory()) {
377 // delete the directory's contents then fall through
378 // and delete the directory itself.
379 deleteContents(f);
380 }
381 f.delete();
382 }
383 }
384 }
385
Christopher Tate5a009f92014-06-19 14:53:18 -0700386 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700387 public int clearBackupData(PackageInfo packageInfo) {
Christopher Tateee0e78a2009-07-02 11:17:03 -0700388 if (DEBUG) Log.v(TAG, "clearBackupData() pkg=" + packageInfo.packageName);
389
Christopher Tate9ff53a72014-06-03 17:20:07 -0700390 File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName);
Christopher Tate0abf6a02012-03-23 17:45:15 -0700391 final File[] fileset = packageDir.listFiles();
392 if (fileset != null) {
393 for (File f : fileset) {
394 f.delete();
395 }
396 packageDir.delete();
Christopher Tateee0e78a2009-07-02 11:17:03 -0700397 }
Christopher Tate9ff53a72014-06-03 17:20:07 -0700398
399 packageDir = new File(mCurrentSetFullDir, packageInfo.packageName);
400 final File[] tarballs = packageDir.listFiles();
401 if (tarballs != null) {
402 for (File f : tarballs) {
403 f.delete();
404 }
405 packageDir.delete();
406 }
407
Christopher Tate5a009f92014-06-19 14:53:18 -0700408 return TRANSPORT_OK;
Christopher Tateee0e78a2009-07-02 11:17:03 -0700409 }
410
Christopher Tate5a009f92014-06-19 14:53:18 -0700411 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700412 public int finishBackup() {
Christopher Tatee0792642014-08-07 14:19:50 -0700413 if (DEBUG) Log.v(TAG, "finishBackup() of " + mFullTargetPackage);
414 return tearDownFullBackup();
415 }
416
417 // ------------------------------------------------------------------------------------
418 // Full backup handling
419
420 private int tearDownFullBackup() {
Christopher Tate9ff53a72014-06-03 17:20:07 -0700421 if (mSocket != null) {
Christopher Tate9ff53a72014-06-03 17:20:07 -0700422 try {
Christopher Tate9310e422015-04-10 11:17:14 -0700423 if (mFullBackupOutputStream != null) {
424 mFullBackupOutputStream.flush();
425 mFullBackupOutputStream.close();
426 }
Christopher Tate9ff53a72014-06-03 17:20:07 -0700427 mSocketInputStream = null;
428 mFullTargetPackage = null;
429 mSocket.close();
430 } catch (IOException e) {
Christopher Tate89101f72014-07-17 19:09:00 -0700431 if (DEBUG) {
Christopher Tatee0792642014-08-07 14:19:50 -0700432 Log.w(TAG, "Exception caught in tearDownFullBackup()", e);
Christopher Tate89101f72014-07-17 19:09:00 -0700433 }
Christopher Tate5a009f92014-06-19 14:53:18 -0700434 return TRANSPORT_ERROR;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700435 } finally {
436 mSocket = null;
Christopher Tate9310e422015-04-10 11:17:14 -0700437 mFullBackupOutputStream = null;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700438 }
439 }
Christopher Tate5a009f92014-06-19 14:53:18 -0700440 return TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700441 }
442
Christopher Tatee0792642014-08-07 14:19:50 -0700443 private File tarballFile(String pkgName) {
444 return new File(mCurrentSetFullDir, pkgName);
445 }
Christopher Tate5a009f92014-06-19 14:53:18 -0700446
447 @Override
Christopher Tate9ff53a72014-06-03 17:20:07 -0700448 public long requestFullBackupTime() {
449 return 0;
450 }
451
Christopher Tate5a009f92014-06-19 14:53:18 -0700452 @Override
Christopher Tate9310e422015-04-10 11:17:14 -0700453 public int checkFullBackupSize(long size) {
Sergey Poromov872d3b62016-01-12 15:48:08 +0100454 int result = TRANSPORT_OK;
Christopher Tate9310e422015-04-10 11:17:14 -0700455 // Decline zero-size "backups"
Sergey Poromov872d3b62016-01-12 15:48:08 +0100456 if (size <= 0) {
457 result = TRANSPORT_PACKAGE_REJECTED;
458 } else if (size > FULL_BACKUP_SIZE_QUOTA) {
459 result = TRANSPORT_QUOTA_EXCEEDED;
460 }
Christopher Tate9310e422015-04-10 11:17:14 -0700461 if (result != TRANSPORT_OK) {
462 if (DEBUG) {
463 Log.v(TAG, "Declining backup of size " + size);
464 }
465 }
466 return result;
467 }
468
469 @Override
Christopher Tate9ff53a72014-06-03 17:20:07 -0700470 public int performFullBackup(PackageInfo targetPackage, ParcelFileDescriptor socket) {
471 if (mSocket != null) {
472 Log.e(TAG, "Attempt to initiate full backup while one is in progress");
Christopher Tate5a009f92014-06-19 14:53:18 -0700473 return TRANSPORT_ERROR;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700474 }
475
476 if (DEBUG) {
477 Log.i(TAG, "performFullBackup : " + targetPackage);
478 }
479
480 // We know a priori that we run in the system process, so we need to make
481 // sure to dup() our own copy of the socket fd. Transports which run in
482 // their own processes must not do this.
483 try {
Sergey Poromov872d3b62016-01-12 15:48:08 +0100484 mFullBackupSize = 0;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700485 mSocket = ParcelFileDescriptor.dup(socket.getFileDescriptor());
486 mSocketInputStream = new FileInputStream(mSocket.getFileDescriptor());
487 } catch (IOException e) {
488 Log.e(TAG, "Unable to process socket for full backup");
Christopher Tate5a009f92014-06-19 14:53:18 -0700489 return TRANSPORT_ERROR;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700490 }
491
492 mFullTargetPackage = targetPackage.packageName;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700493 mFullBackupBuffer = new byte[4096];
494
Christopher Tate5a009f92014-06-19 14:53:18 -0700495 return TRANSPORT_OK;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700496 }
497
Christopher Tate5a009f92014-06-19 14:53:18 -0700498 @Override
Christopher Tate9310e422015-04-10 11:17:14 -0700499 public int sendBackupData(final int numBytes) {
500 if (mSocket == null) {
Christopher Tate9ff53a72014-06-03 17:20:07 -0700501 Log.w(TAG, "Attempted sendBackupData before performFullBackup");
Christopher Tate5a009f92014-06-19 14:53:18 -0700502 return TRANSPORT_ERROR;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700503 }
504
Sergey Poromov872d3b62016-01-12 15:48:08 +0100505 mFullBackupSize += numBytes;
506 if (mFullBackupSize > FULL_BACKUP_SIZE_QUOTA) {
507 return TRANSPORT_QUOTA_EXCEEDED;
508 }
509
Christopher Tate9ff53a72014-06-03 17:20:07 -0700510 if (numBytes > mFullBackupBuffer.length) {
511 mFullBackupBuffer = new byte[numBytes];
512 }
Christopher Tate9310e422015-04-10 11:17:14 -0700513
514 if (mFullBackupOutputStream == null) {
515 FileOutputStream tarstream;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700516 try {
Christopher Tate9310e422015-04-10 11:17:14 -0700517 File tarball = tarballFile(mFullTargetPackage);
518 tarstream = new FileOutputStream(tarball);
519 } catch (FileNotFoundException e) {
520 return TRANSPORT_ERROR;
521 }
522 mFullBackupOutputStream = new BufferedOutputStream(tarstream);
523 }
524
525 int bytesLeft = numBytes;
526 while (bytesLeft > 0) {
527 try {
528 int nRead = mSocketInputStream.read(mFullBackupBuffer, 0, bytesLeft);
Christopher Tate9ff53a72014-06-03 17:20:07 -0700529 if (nRead < 0) {
530 // Something went wrong if we expect data but saw EOD
531 Log.w(TAG, "Unexpected EOD; failing backup");
Christopher Tate5a009f92014-06-19 14:53:18 -0700532 return TRANSPORT_ERROR;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700533 }
534 mFullBackupOutputStream.write(mFullBackupBuffer, 0, nRead);
Christopher Tate9310e422015-04-10 11:17:14 -0700535 bytesLeft -= nRead;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700536 } catch (IOException e) {
537 Log.e(TAG, "Error handling backup data for " + mFullTargetPackage);
Christopher Tate5a009f92014-06-19 14:53:18 -0700538 return TRANSPORT_ERROR;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700539 }
540 }
Christopher Tate77a2d78d2015-03-03 16:19:44 -0800541 if (DEBUG) {
542 Log.v(TAG, " stored " + numBytes + " of data");
543 }
Christopher Tate5a009f92014-06-19 14:53:18 -0700544 return TRANSPORT_OK;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700545 }
546
Christopher Tatee0792642014-08-07 14:19:50 -0700547 // For now we can't roll back, so just tear everything down.
548 @Override
549 public void cancelFullBackup() {
550 if (DEBUG) {
551 Log.i(TAG, "Canceling full backup of " + mFullTargetPackage);
552 }
553 File archive = tarballFile(mFullTargetPackage);
554 tearDownFullBackup();
555 if (archive.exists()) {
556 archive.delete();
557 }
558 }
559
Christopher Tate9ff53a72014-06-03 17:20:07 -0700560 // ------------------------------------------------------------------------------------
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700561 // Restore handling
Christopher Tate51fea572014-06-23 17:01:06 -0700562 static final long[] POSSIBLE_SETS = { 2, 3, 4, 5, 6, 7, 8, 9 };
Christopher Tate5a009f92014-06-19 14:53:18 -0700563
564 @Override
Christopher Tate74318c92014-05-15 19:03:44 -0700565 public RestoreSet[] getAvailableRestoreSets() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800566 long[] existing = new long[POSSIBLE_SETS.length + 1];
567 int num = 0;
568
Christopher Tate9ff53a72014-06-03 17:20:07 -0700569 // see which possible non-current sets exist...
Christopher Tateadfe8b82014-02-04 16:23:32 -0800570 for (long token : POSSIBLE_SETS) {
571 if ((new File(mDataDir, Long.toString(token))).exists()) {
572 existing[num++] = token;
573 }
574 }
Christopher Tate9ff53a72014-06-03 17:20:07 -0700575 // ...and always the currently-active set last
Christopher Tateadfe8b82014-02-04 16:23:32 -0800576 existing[num++] = CURRENT_SET_TOKEN;
577
578 RestoreSet[] available = new RestoreSet[num];
579 for (int i = 0; i < available.length; i++) {
580 available[i] = new RestoreSet("Local disk image", "flash", existing[i]);
581 }
582 return available;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700583 }
584
Christopher Tate5a009f92014-06-19 14:53:18 -0700585 @Override
Christopher Tate50c6df02010-01-29 12:48:20 -0800586 public long getCurrentRestoreSet() {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800587 // The current restore set always has the same token
588 return CURRENT_SET_TOKEN;
Christopher Tate50c6df02010-01-29 12:48:20 -0800589 }
590
Christopher Tate5a009f92014-06-19 14:53:18 -0700591 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700592 public int startRestore(long token, PackageInfo[] packages) {
Christopher Tate51fea572014-06-23 17:01:06 -0700593 if (DEBUG) Log.v(TAG, "start restore " + token + " : " + packages.length
594 + " matching packages");
Dan Egnorefe52642009-06-24 00:16:33 -0700595 mRestorePackages = packages;
596 mRestorePackage = -1;
Christopher Tate6a49dd02014-06-16 18:49:25 -0700597 mRestoreSetDir = new File(mDataDir, Long.toString(token));
598 mRestoreSetIncrementalDir = new File(mRestoreSetDir, INCREMENTAL_DIR);
599 mRestoreSetFullDir = new File(mRestoreSetDir, FULL_DATA_DIR);
Christopher Tate5a009f92014-06-19 14:53:18 -0700600 return TRANSPORT_OK;
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700601 }
602
Christopher Tate6a49dd02014-06-16 18:49:25 -0700603 @Override
604 public RestoreDescription nextRestorePackage() {
Christopher Tate77a2d78d2015-03-03 16:19:44 -0800605 if (DEBUG) {
606 Log.v(TAG, "nextRestorePackage() : mRestorePackage=" + mRestorePackage
607 + " length=" + mRestorePackages.length);
608 }
Dan Egnorefe52642009-06-24 00:16:33 -0700609 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
Christopher Tate6a49dd02014-06-16 18:49:25 -0700610
611 boolean found = false;
Dan Egnorefe52642009-06-24 00:16:33 -0700612 while (++mRestorePackage < mRestorePackages.length) {
613 String name = mRestorePackages[mRestorePackage].packageName;
Christopher Tate6a49dd02014-06-16 18:49:25 -0700614
615 // If we have key/value data for this package, deliver that
Christopher Tatea9b91862014-02-25 17:42:21 -0800616 // skip packages where we have a data dir but no actual contents
Christopher Tate6a49dd02014-06-16 18:49:25 -0700617 String[] contents = (new File(mRestoreSetIncrementalDir, name)).list();
Christopher Tatea9b91862014-02-25 17:42:21 -0800618 if (contents != null && contents.length > 0) {
Christopher Tate77a2d78d2015-03-03 16:19:44 -0800619 if (DEBUG) {
620 Log.v(TAG, " nextRestorePackage(TYPE_KEY_VALUE) @ "
621 + mRestorePackage + " = " + name);
622 }
Christopher Tate6a49dd02014-06-16 18:49:25 -0700623 mRestoreType = RestoreDescription.TYPE_KEY_VALUE;
624 found = true;
625 }
626
627 if (!found) {
628 // No key/value data; check for [non-empty] full data
629 File maybeFullData = new File(mRestoreSetFullDir, name);
630 if (maybeFullData.length() > 0) {
Christopher Tate77a2d78d2015-03-03 16:19:44 -0800631 if (DEBUG) {
632 Log.v(TAG, " nextRestorePackage(TYPE_FULL_STREAM) @ "
633 + mRestorePackage + " = " + name);
634 }
Christopher Tate6a49dd02014-06-16 18:49:25 -0700635 mRestoreType = RestoreDescription.TYPE_FULL_STREAM;
Christopher Tate5a009f92014-06-19 14:53:18 -0700636 mCurFullRestoreStream = null; // ensure starting from the ground state
Christopher Tate6a49dd02014-06-16 18:49:25 -0700637 found = true;
638 }
639 }
640
641 if (found) {
642 return new RestoreDescription(name, mRestoreType);
Dan Egnorefe52642009-06-24 00:16:33 -0700643 }
Christopher Tate77a2d78d2015-03-03 16:19:44 -0800644
645 if (DEBUG) {
646 Log.v(TAG, " ... package @ " + mRestorePackage + " = " + name
647 + " has no data; skipping");
648 }
Dan Egnorefe52642009-06-24 00:16:33 -0700649 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700650
Dan Egnorefe52642009-06-24 00:16:33 -0700651 if (DEBUG) Log.v(TAG, " no more packages to restore");
Christopher Tate6a49dd02014-06-16 18:49:25 -0700652 return RestoreDescription.NO_MORE_PACKAGES;
Dan Egnorefe52642009-06-24 00:16:33 -0700653 }
654
Christopher Tate5a009f92014-06-19 14:53:18 -0700655 @Override
Dan Egnor01445162009-09-21 17:04:05 -0700656 public int getRestoreData(ParcelFileDescriptor outFd) {
Dan Egnorefe52642009-06-24 00:16:33 -0700657 if (mRestorePackages == null) throw new IllegalStateException("startRestore not called");
658 if (mRestorePackage < 0) throw new IllegalStateException("nextRestorePackage not called");
Christopher Tate6a49dd02014-06-16 18:49:25 -0700659 if (mRestoreType != RestoreDescription.TYPE_KEY_VALUE) {
660 throw new IllegalStateException("getRestoreData(fd) for non-key/value dataset");
661 }
Christopher Tate51fea572014-06-23 17:01:06 -0700662 File packageDir = new File(mRestoreSetIncrementalDir,
663 mRestorePackages[mRestorePackage].packageName);
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700664
Christopher Tate2fdd4282009-06-12 15:20:04 -0700665 // The restore set is the concatenation of the individual record blobs,
Christopher Tateadfe8b82014-02-04 16:23:32 -0800666 // each of which is a file in the package's directory. We return the
667 // data in lexical order sorted by key, so that apps which use synthetic
668 // keys like BLOB_1, BLOB_2, etc will see the date in the most obvious
669 // order.
670 ArrayList<DecodedFilename> blobs = contentsByKey(packageDir);
Dan Egnor01445162009-09-21 17:04:05 -0700671 if (blobs == null) { // nextRestorePackage() ensures the dir exists, so this is an error
Christopher Tateadfe8b82014-02-04 16:23:32 -0800672 Log.e(TAG, "No keys for package: " + packageDir);
Christopher Tate5a009f92014-06-19 14:53:18 -0700673 return TRANSPORT_ERROR;
Christopher Tate2fdd4282009-06-12 15:20:04 -0700674 }
Dan Egnorefe52642009-06-24 00:16:33 -0700675
676 // We expect at least some data if the directory exists in the first place
Christopher Tateadfe8b82014-02-04 16:23:32 -0800677 if (DEBUG) Log.v(TAG, " getRestoreData() found " + blobs.size() + " key files");
Dan Egnorefe52642009-06-24 00:16:33 -0700678 BackupDataOutput out = new BackupDataOutput(outFd.getFileDescriptor());
679 try {
Christopher Tateadfe8b82014-02-04 16:23:32 -0800680 for (DecodedFilename keyEntry : blobs) {
681 File f = keyEntry.file;
Dan Egnorefe52642009-06-24 00:16:33 -0700682 FileInputStream in = new FileInputStream(f);
683 try {
684 int size = (int) f.length();
685 byte[] buf = new byte[size];
686 in.read(buf);
Christopher Tateadfe8b82014-02-04 16:23:32 -0800687 if (DEBUG) Log.v(TAG, " ... key=" + keyEntry.key + " size=" + size);
688 out.writeEntityHeader(keyEntry.key, size);
Dan Egnorefe52642009-06-24 00:16:33 -0700689 out.writeEntityData(buf, size);
690 } finally {
691 in.close();
692 }
693 }
Christopher Tate5a009f92014-06-19 14:53:18 -0700694 return TRANSPORT_OK;
Dan Egnorefe52642009-06-24 00:16:33 -0700695 } catch (IOException e) {
696 Log.e(TAG, "Unable to read backup records", e);
Christopher Tate5a009f92014-06-19 14:53:18 -0700697 return TRANSPORT_ERROR;
Dan Egnorefe52642009-06-24 00:16:33 -0700698 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700699 }
Christopher Tate3a31a932009-06-22 15:10:30 -0700700
Christopher Tateadfe8b82014-02-04 16:23:32 -0800701 static class DecodedFilename implements Comparable<DecodedFilename> {
702 public File file;
703 public String key;
704
705 public DecodedFilename(File f) {
706 file = f;
Bernardo Rufino7a6e0322018-07-17 10:17:43 +0100707 key = new String(Base64.decode(f.getName(), Base64.DEFAULT));
Christopher Tateadfe8b82014-02-04 16:23:32 -0800708 }
709
710 @Override
711 public int compareTo(DecodedFilename other) {
712 // sorts into ascending lexical order by decoded key
713 return key.compareTo(other.key);
714 }
715 }
716
717 // Return a list of the files in the given directory, sorted lexically by
718 // the Base64-decoded file name, not by the on-disk filename
719 private ArrayList<DecodedFilename> contentsByKey(File dir) {
720 File[] allFiles = dir.listFiles();
721 if (allFiles == null || allFiles.length == 0) {
722 return null;
723 }
724
725 // Decode the filenames into keys then sort lexically by key
726 ArrayList<DecodedFilename> contents = new ArrayList<DecodedFilename>();
727 for (File f : allFiles) {
728 contents.add(new DecodedFilename(f));
729 }
730 Collections.sort(contents);
731 return contents;
732 }
733
Christopher Tate5a009f92014-06-19 14:53:18 -0700734 @Override
Dan Egnorefe52642009-06-24 00:16:33 -0700735 public void finishRestore() {
736 if (DEBUG) Log.v(TAG, "finishRestore()");
Christopher Tate5a009f92014-06-19 14:53:18 -0700737 if (mRestoreType == RestoreDescription.TYPE_FULL_STREAM) {
738 resetFullRestoreState();
739 }
740 mRestoreType = 0;
Christopher Tate3a31a932009-06-22 15:10:30 -0700741 }
Christopher Tate9ff53a72014-06-03 17:20:07 -0700742
743 // ------------------------------------------------------------------------------------
744 // Full restore handling
745
Christopher Tate5a009f92014-06-19 14:53:18 -0700746 private void resetFullRestoreState() {
Christopher Tate824392b2014-11-14 18:27:36 -0800747 IoUtils.closeQuietly(mCurFullRestoreStream);
Christopher Tate5a009f92014-06-19 14:53:18 -0700748 mCurFullRestoreStream = null;
749 mFullRestoreSocketStream = null;
750 mFullRestoreBuffer = null;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700751 }
752
753 /**
754 * Ask the transport to provide data for the "current" package being restored. The
755 * transport then writes some data to the socket supplied to this call, and returns
756 * the number of bytes written. The system will then read that many bytes and
757 * stream them to the application's agent for restore, then will call this method again
758 * to receive the next chunk of the archive. This sequence will be repeated until the
759 * transport returns zero indicating that all of the package's data has been delivered
760 * (or returns a negative value indicating some sort of hard error condition at the
761 * transport level).
762 *
763 * <p>After this method returns zero, the system will then call
764 * {@link #getNextFullRestorePackage()} to begin the restore process for the next
765 * application, and the sequence begins again.
766 *
767 * @param socket The file descriptor that the transport will use for delivering the
768 * streamed archive.
769 * @return 0 when no more data for the current package is available. A positive value
770 * indicates the presence of that much data to be delivered to the app. A negative
771 * return value is treated as equivalent to {@link BackupTransport#TRANSPORT_ERROR},
772 * indicating a fatal error condition that precludes further restore operations
773 * on the current dataset.
774 */
Christopher Tate5a009f92014-06-19 14:53:18 -0700775 @Override
Christopher Tate9ff53a72014-06-03 17:20:07 -0700776 public int getNextFullRestoreDataChunk(ParcelFileDescriptor socket) {
Christopher Tate5a009f92014-06-19 14:53:18 -0700777 if (mRestoreType != RestoreDescription.TYPE_FULL_STREAM) {
778 throw new IllegalStateException("Asked for full restore data for non-stream package");
779 }
780
781 // first chunk?
782 if (mCurFullRestoreStream == null) {
783 final String name = mRestorePackages[mRestorePackage].packageName;
784 if (DEBUG) Log.i(TAG, "Starting full restore of " + name);
785 File dataset = new File(mRestoreSetFullDir, name);
786 try {
787 mCurFullRestoreStream = new FileInputStream(dataset);
788 } catch (IOException e) {
789 // If we can't open the target package's tarball, we return the single-package
790 // error code and let the caller go on to the next package.
791 Log.e(TAG, "Unable to read archive for " + name);
792 return TRANSPORT_PACKAGE_REJECTED;
793 }
794 mFullRestoreSocketStream = new FileOutputStream(socket.getFileDescriptor());
Christopher Tate89101f72014-07-17 19:09:00 -0700795 mFullRestoreBuffer = new byte[2*1024];
Christopher Tate5a009f92014-06-19 14:53:18 -0700796 }
797
798 int nRead;
799 try {
800 nRead = mCurFullRestoreStream.read(mFullRestoreBuffer);
801 if (nRead < 0) {
802 // EOF: tell the caller we're done
803 nRead = NO_MORE_DATA;
804 } else if (nRead == 0) {
805 // This shouldn't happen when reading a FileInputStream; we should always
806 // get either a positive nonzero byte count or -1. Log the situation and
807 // treat it as EOF.
808 Log.w(TAG, "read() of archive file returned 0; treating as EOF");
809 nRead = NO_MORE_DATA;
810 } else {
811 if (DEBUG) {
812 Log.i(TAG, " delivering restore chunk: " + nRead);
813 }
814 mFullRestoreSocketStream.write(mFullRestoreBuffer, 0, nRead);
815 }
816 } catch (IOException e) {
817 return TRANSPORT_ERROR; // Hard error accessing the file; shouldn't happen
818 } finally {
819 // Most transports will need to explicitly close 'socket' here, but this transport
820 // is in the same process as the caller so it can leave it up to the backup manager
821 // to manage both socket fds.
822 }
823
824 return nRead;
Christopher Tate9ff53a72014-06-03 17:20:07 -0700825 }
Christopher Tate5a009f92014-06-19 14:53:18 -0700826
827 /**
828 * If the OS encounters an error while processing {@link RestoreDescription#TYPE_FULL_STREAM}
829 * data for restore, it will invoke this method to tell the transport that it should
830 * abandon the data download for the current package. The OS will then either call
831 * {@link #nextRestorePackage()} again to move on to restoring the next package in the
832 * set being iterated over, or will call {@link #finishRestore()} to shut down the restore
833 * operation.
834 *
835 * @return {@link #TRANSPORT_OK} if the transport was successful in shutting down the
836 * current stream cleanly, or {@link #TRANSPORT_ERROR} to indicate a serious
837 * transport-level failure. If the transport reports an error here, the entire restore
838 * operation will immediately be finished with no further attempts to restore app data.
839 */
840 @Override
841 public int abortFullRestore() {
842 if (mRestoreType != RestoreDescription.TYPE_FULL_STREAM) {
843 throw new IllegalStateException("abortFullRestore() but not currently restoring");
844 }
845 resetFullRestoreState();
846 mRestoreType = 0;
847 return TRANSPORT_OK;
848 }
849
Sergey Poromov872d3b62016-01-12 15:48:08 +0100850 @Override
851 public long getBackupQuota(String packageName, boolean isFullBackup) {
Shreyas Basargeb6e73c92017-01-31 20:13:43 +0000852 return isFullBackup ? FULL_BACKUP_SIZE_QUOTA : KEY_VALUE_BACKUP_SIZE_QUOTA;
Sergey Poromov872d3b62016-01-12 15:48:08 +0100853 }
Christopher Tate9bbc21a2009-06-10 20:23:25 -0700854}