| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.backup; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.app.IBackupAgent; |
| 22 |
import android.app.QueuedWork; |
| 23 |
import android.app.backup.BackupManager.OperationType; |
| 24 |
import android.app.backup.FullBackup.BackupScheme.PathWithRequiredFlags; |
| 25 |
import android.content.Context; |
| 26 |
import android.content.ContextWrapper; |
| 27 |
import android.content.pm.ApplicationInfo; |
| 28 |
import android.os.Binder; |
| 29 |
import android.os.Handler; |
| 30 |
import android.os.IBinder; |
| 31 |
import android.os.Looper; |
| 32 |
import android.os.ParcelFileDescriptor; |
| 33 |
import android.os.Process; |
| 34 |
import android.os.RemoteException; |
| 35 |
import android.os.UserHandle; |
| 36 |
import android.system.ErrnoException; |
| 37 |
import android.system.Os; |
| 38 |
import android.system.OsConstants; |
| 39 |
import android.system.StructStat; |
| 40 |
import android.util.ArraySet; |
| 41 |
import android.util.Log; |
| 42 |
|
| 43 |
import com.android.internal.annotations.VisibleForTesting; |
| 44 |
|
| 45 |
import libcore.io.IoUtils; |
| 46 |
|
| 47 |
import org.xmlpull.v1.XmlPullParserException; |
| 48 |
|
| 49 |
import java.io.File; |
| 50 |
import java.io.FileOutputStream; |
| 51 |
import java.io.IOException; |
| 52 |
import java.lang.annotation.Retention; |
| 53 |
import java.lang.annotation.RetentionPolicy; |
| 54 |
import java.util.Collections; |
| 55 |
import java.util.HashSet; |
| 56 |
import java.util.LinkedList; |
| 57 |
import java.util.List; |
| 58 |
import java.util.Map; |
| 59 |
import java.util.Objects; |
| 60 |
import java.util.Set; |
| 61 |
import java.util.concurrent.CountDownLatch; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public abstract class BackupAgent extends ContextWrapper { |
| 137 |
private static final String TAG = "BackupAgent"; |
| 138 |
private static final boolean DEBUG = false; |
| 139 |
private static final int DEFAULT_OPERATION_TYPE = OperationType.BACKUP; |
| 140 |
|
| 141 |
|
| 142 |
public static final int RESULT_SUCCESS = 0; |
| 143 |
|
| 144 |
public static final int RESULT_ERROR = -1; |
| 145 |
|
| 146 |
|
| 147 |
public static final int TYPE_EOF = 0; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public static final int TYPE_FILE = 1; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static final int TYPE_DIRECTORY = 2; |
| 160 |
|
| 161 |
|
| 162 |
public static final int TYPE_SYMLINK = 3; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static final int FLAG_CLIENT_SIDE_ENCRYPTION_ENABLED = 1; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public static final int FLAG_DEVICE_TO_DEVICE_TRANSFER = 2; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public static final int FLAG_FAKE_CLIENT_SIDE_ENCRYPTION_ENABLED = 1 << 31; |
| 193 |
|
| 194 |
|
| 195 |
@Retention(RetentionPolicy.SOURCE) |
| 196 |
@IntDef(flag = true, value = { |
| 197 |
FLAG_CLIENT_SIDE_ENCRYPTION_ENABLED, |
| 198 |
FLAG_DEVICE_TO_DEVICE_TRANSFER, |
| 199 |
FLAG_FAKE_CLIENT_SIDE_ENCRYPTION_ENABLED |
| 200 |
}) |
| 201 |
public @interface BackupTransportFlags {} |
| 202 |
|
| 203 |
Handler mHandler = null; |
| 204 |
|
| 205 |
@Nullable private UserHandle mUser; |
| 206 |
|
| 207 |
|
| 208 |
@OperationType private volatile int mOperationType = DEFAULT_OPERATION_TYPE; |
| 209 |
|
| 210 |
Handler getHandler() { |
| 211 |
if (mHandler == null) { |
| 212 |
mHandler = new Handler(Looper.getMainLooper()); |
| 213 |
} |
| 214 |
return mHandler; |
| 215 |
} |
| 216 |
|
| 217 |
class SharedPrefsSynchronizer implements Runnable { |
| 218 |
public final CountDownLatch mLatch = new CountDownLatch(1); |
| 219 |
|
| 220 |
@Override |
| 221 |
public void run() { |
| 222 |
QueuedWork.waitToFinish(); |
| 223 |
mLatch.countDown(); |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
private void waitForSharedPrefs() { |
| 229 |
Handler h = getHandler(); |
| 230 |
final SharedPrefsSynchronizer s = new SharedPrefsSynchronizer(); |
| 231 |
h.postAtFrontOfQueue(s); |
| 232 |
try { |
| 233 |
s.mLatch.await(); |
| 234 |
} catch (InterruptedException e) { } |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
public BackupAgent() { |
| 239 |
super(null); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public void onCreate() { |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public void onCreate(UserHandle user) { |
| 255 |
onCreate(user, DEFAULT_OPERATION_TYPE); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public void onCreate(UserHandle user, @OperationType int operationType) { |
| 267 |
onCreate(); |
| 268 |
|
| 269 |
mUser = user; |
| 270 |
mOperationType = operationType; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public void onDestroy() { |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public abstract void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 313 |
ParcelFileDescriptor newState) throws IOException; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public abstract void onRestore(BackupDataInput data, int appVersionCode, |
| 347 |
ParcelFileDescriptor newState) throws IOException; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public void onRestore(BackupDataInput data, long appVersionCode, |
| 355 |
ParcelFileDescriptor newState) |
| 356 |
throws IOException { |
| 357 |
onRestore(data, (int) appVersionCode, newState); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public void onRestore(BackupDataInput data, long appVersionCode, |
| 376 |
ParcelFileDescriptor newState, |
| 377 |
Set<String> excludedKeys) |
| 378 |
throws IOException { |
| 379 |
onRestore(data, appVersionCode, newState); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public void onFullBackup(FullBackupDataOutput data) throws IOException { |
| 416 |
FullBackup.BackupScheme backupScheme = FullBackup.getBackupScheme(this, |
| 417 |
mOperationType); |
| 418 |
if (!backupScheme.isFullBackupEnabled(data.getTransportFlags())) { |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
IncludeExcludeRules includeExcludeRules; |
| 423 |
try { |
| 424 |
includeExcludeRules = getIncludeExcludeRules(backupScheme); |
| 425 |
} catch (IOException | XmlPullParserException e) { |
| 426 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 427 |
Log.v(FullBackup.TAG_XML_PARSER, |
| 428 |
"Exception trying to parse fullBackupContent xml file!" |
| 429 |
+ " Aborting full backup.", e); |
| 430 |
} |
| 431 |
return; |
| 432 |
} |
| 433 |
Map<String, Set<PathWithRequiredFlags>> manifestIncludeMap |
| 434 |
= includeExcludeRules.getIncludeMap(); |
| 435 |
Set<PathWithRequiredFlags> manifestExcludeSet |
| 436 |
= includeExcludeRules.getExcludeSet(); |
| 437 |
|
| 438 |
final String packageName = getPackageName(); |
| 439 |
final ApplicationInfo appInfo = getApplicationInfo(); |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
final Context ceContext = createCredentialProtectedStorageContext(); |
| 444 |
final String rootDir = ceContext.getDataDir().getCanonicalPath(); |
| 445 |
final String filesDir = ceContext.getFilesDir().getCanonicalPath(); |
| 446 |
final String databaseDir = ceContext.getDatabasePath("foo").getParentFile() |
| 447 |
.getCanonicalPath(); |
| 448 |
final String sharedPrefsDir = ceContext.getSharedPreferencesPath("foo").getParentFile() |
| 449 |
.getCanonicalPath(); |
| 450 |
|
| 451 |
final Context deContext = createDeviceProtectedStorageContext(); |
| 452 |
final String deviceRootDir = deContext.getDataDir().getCanonicalPath(); |
| 453 |
final String deviceFilesDir = deContext.getFilesDir().getCanonicalPath(); |
| 454 |
final String deviceDatabaseDir = deContext.getDatabasePath("foo").getParentFile() |
| 455 |
.getCanonicalPath(); |
| 456 |
final String deviceSharedPrefsDir = deContext.getSharedPreferencesPath("foo") |
| 457 |
.getParentFile().getCanonicalPath(); |
| 458 |
|
| 459 |
final String libDir = (appInfo.nativeLibraryDir != null) |
| 460 |
? new File(appInfo.nativeLibraryDir).getCanonicalPath() |
| 461 |
: null; |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
final ArraySet<String> traversalExcludeSet = new ArraySet<String>(); |
| 467 |
|
| 468 |
|
| 469 |
traversalExcludeSet.add(filesDir); |
| 470 |
traversalExcludeSet.add(databaseDir); |
| 471 |
traversalExcludeSet.add(sharedPrefsDir); |
| 472 |
|
| 473 |
traversalExcludeSet.add(deviceFilesDir); |
| 474 |
traversalExcludeSet.add(deviceDatabaseDir); |
| 475 |
traversalExcludeSet.add(deviceSharedPrefsDir); |
| 476 |
|
| 477 |
if (libDir != null) { |
| 478 |
traversalExcludeSet.add(libDir); |
| 479 |
} |
| 480 |
|
| 481 |
Set<String> extraExcludedDirs = getExtraExcludeDirsIfAny(ceContext); |
| 482 |
Set<String> extraExcludedDeviceDirs = getExtraExcludeDirsIfAny(deContext); |
| 483 |
traversalExcludeSet.addAll(extraExcludedDirs); |
| 484 |
traversalExcludeSet.addAll(extraExcludedDeviceDirs); |
| 485 |
|
| 486 |
|
| 487 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 488 |
packageName, FullBackup.ROOT_TREE_TOKEN, manifestIncludeMap, |
| 489 |
manifestExcludeSet, traversalExcludeSet, data); |
| 490 |
traversalExcludeSet.add(rootDir); |
| 491 |
|
| 492 |
traversalExcludeSet.addAll(extraExcludedDirs); |
| 493 |
|
| 494 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 495 |
packageName, FullBackup.DEVICE_ROOT_TREE_TOKEN, manifestIncludeMap, |
| 496 |
manifestExcludeSet, traversalExcludeSet, data); |
| 497 |
traversalExcludeSet.add(deviceRootDir); |
| 498 |
|
| 499 |
traversalExcludeSet.addAll(extraExcludedDeviceDirs); |
| 500 |
|
| 501 |
|
| 502 |
traversalExcludeSet.remove(filesDir); |
| 503 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 504 |
packageName, FullBackup.FILES_TREE_TOKEN, manifestIncludeMap, |
| 505 |
manifestExcludeSet, traversalExcludeSet, data); |
| 506 |
traversalExcludeSet.add(filesDir); |
| 507 |
|
| 508 |
traversalExcludeSet.remove(deviceFilesDir); |
| 509 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 510 |
packageName, FullBackup.DEVICE_FILES_TREE_TOKEN, manifestIncludeMap, |
| 511 |
manifestExcludeSet, traversalExcludeSet, data); |
| 512 |
traversalExcludeSet.add(deviceFilesDir); |
| 513 |
|
| 514 |
|
| 515 |
traversalExcludeSet.remove(databaseDir); |
| 516 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 517 |
packageName, FullBackup.DATABASE_TREE_TOKEN, manifestIncludeMap, |
| 518 |
manifestExcludeSet, traversalExcludeSet, data); |
| 519 |
traversalExcludeSet.add(databaseDir); |
| 520 |
|
| 521 |
traversalExcludeSet.remove(deviceDatabaseDir); |
| 522 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 523 |
packageName, FullBackup.DEVICE_DATABASE_TREE_TOKEN, manifestIncludeMap, |
| 524 |
manifestExcludeSet, traversalExcludeSet, data); |
| 525 |
traversalExcludeSet.add(deviceDatabaseDir); |
| 526 |
|
| 527 |
|
| 528 |
traversalExcludeSet.remove(sharedPrefsDir); |
| 529 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 530 |
packageName, FullBackup.SHAREDPREFS_TREE_TOKEN, manifestIncludeMap, |
| 531 |
manifestExcludeSet, traversalExcludeSet, data); |
| 532 |
traversalExcludeSet.add(sharedPrefsDir); |
| 533 |
|
| 534 |
traversalExcludeSet.remove(deviceSharedPrefsDir); |
| 535 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 536 |
packageName, FullBackup.DEVICE_SHAREDPREFS_TREE_TOKEN, manifestIncludeMap, |
| 537 |
manifestExcludeSet, traversalExcludeSet, data); |
| 538 |
traversalExcludeSet.add(deviceSharedPrefsDir); |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
if (Process.myUid() != Process.SYSTEM_UID) { |
| 548 |
File efLocation = getExternalFilesDir(null); |
| 549 |
if (efLocation != null) { |
| 550 |
applyXmlFiltersAndDoFullBackupForDomain( |
| 551 |
packageName, FullBackup.MANAGED_EXTERNAL_TREE_TOKEN, manifestIncludeMap, |
| 552 |
manifestExcludeSet, traversalExcludeSet, data); |
| 553 |
} |
| 554 |
|
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
private Set<String> getExtraExcludeDirsIfAny(Context context) throws IOException { |
| 559 |
Set<String> excludedDirs = new HashSet<>(); |
| 560 |
excludedDirs.add(context.getCacheDir().getCanonicalPath()); |
| 561 |
excludedDirs.add(context.getCodeCacheDir().getCanonicalPath()); |
| 562 |
excludedDirs.add(context.getNoBackupFilesDir().getCanonicalPath()); |
| 563 |
return Collections.unmodifiableSet(excludedDirs); |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
@VisibleForTesting |
| 568 |
public IncludeExcludeRules getIncludeExcludeRules(FullBackup.BackupScheme backupScheme) |
| 569 |
throws IOException, XmlPullParserException { |
| 570 |
Map<String, Set<PathWithRequiredFlags>> manifestIncludeMap; |
| 571 |
ArraySet<PathWithRequiredFlags> manifestExcludeSet; |
| 572 |
|
| 573 |
manifestIncludeMap = |
| 574 |
backupScheme.maybeParseAndGetCanonicalIncludePaths(); |
| 575 |
manifestExcludeSet = backupScheme.maybeParseAndGetCanonicalExcludePaths(); |
| 576 |
|
| 577 |
return new IncludeExcludeRules(manifestIncludeMap, manifestExcludeSet); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public void onQuotaExceeded(long backupDataBytes, long quotaBytes) { |
| 608 |
} |
| 609 |
|
| 610 |
private int getBackupUserId() { |
| 611 |
return mUser == null ? super.getUserId() : mUser.getIdentifier(); |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
private void applyXmlFiltersAndDoFullBackupForDomain(String packageName, String domainToken, |
| 621 |
Map<String, Set<PathWithRequiredFlags>> includeMap, |
| 622 |
Set<PathWithRequiredFlags> filterSet, ArraySet<String> traversalExcludeSet, |
| 623 |
FullBackupDataOutput data) throws IOException { |
| 624 |
if (includeMap == null || includeMap.size() == 0) { |
| 625 |
|
| 626 |
fullBackupFileTree(packageName, domainToken, |
| 627 |
FullBackup.getBackupScheme(this, mOperationType) |
| 628 |
.tokenToDirectoryPath(domainToken), |
| 629 |
filterSet, traversalExcludeSet, data); |
| 630 |
} else if (includeMap.get(domainToken) != null) { |
| 631 |
|
| 632 |
|
| 633 |
for (PathWithRequiredFlags includeFile : includeMap.get(domainToken)) { |
| 634 |
if (areIncludeRequiredTransportFlagsSatisfied(includeFile.getRequiredFlags(), |
| 635 |
data.getTransportFlags())) { |
| 636 |
fullBackupFileTree(packageName, domainToken, includeFile.getPath(), filterSet, |
| 637 |
traversalExcludeSet, data); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
private boolean areIncludeRequiredTransportFlagsSatisfied(int includeFlags, |
| 644 |
int transportFlags) { |
| 645 |
|
| 646 |
return (transportFlags & includeFlags) == includeFlags; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public final void fullBackupFile(File file, FullBackupDataOutput output) { |
| 666 |
|
| 667 |
final String rootDir; |
| 668 |
final String filesDir; |
| 669 |
final String nbFilesDir; |
| 670 |
final String dbDir; |
| 671 |
final String spDir; |
| 672 |
final String cacheDir; |
| 673 |
final String codeCacheDir; |
| 674 |
final String deviceRootDir; |
| 675 |
final String deviceFilesDir; |
| 676 |
final String deviceNbFilesDir; |
| 677 |
final String deviceDbDir; |
| 678 |
final String deviceSpDir; |
| 679 |
final String deviceCacheDir; |
| 680 |
final String deviceCodeCacheDir; |
| 681 |
final String libDir; |
| 682 |
|
| 683 |
String efDir = null; |
| 684 |
String filePath; |
| 685 |
|
| 686 |
ApplicationInfo appInfo = getApplicationInfo(); |
| 687 |
|
| 688 |
try { |
| 689 |
|
| 690 |
|
| 691 |
final Context ceContext = createCredentialProtectedStorageContext(); |
| 692 |
rootDir = ceContext.getDataDir().getCanonicalPath(); |
| 693 |
filesDir = ceContext.getFilesDir().getCanonicalPath(); |
| 694 |
nbFilesDir = ceContext.getNoBackupFilesDir().getCanonicalPath(); |
| 695 |
dbDir = ceContext.getDatabasePath("foo").getParentFile().getCanonicalPath(); |
| 696 |
spDir = ceContext.getSharedPreferencesPath("foo").getParentFile().getCanonicalPath(); |
| 697 |
cacheDir = ceContext.getCacheDir().getCanonicalPath(); |
| 698 |
codeCacheDir = ceContext.getCodeCacheDir().getCanonicalPath(); |
| 699 |
|
| 700 |
final Context deContext = createDeviceProtectedStorageContext(); |
| 701 |
deviceRootDir = deContext.getDataDir().getCanonicalPath(); |
| 702 |
deviceFilesDir = deContext.getFilesDir().getCanonicalPath(); |
| 703 |
deviceNbFilesDir = deContext.getNoBackupFilesDir().getCanonicalPath(); |
| 704 |
deviceDbDir = deContext.getDatabasePath("foo").getParentFile().getCanonicalPath(); |
| 705 |
deviceSpDir = deContext.getSharedPreferencesPath("foo").getParentFile() |
| 706 |
.getCanonicalPath(); |
| 707 |
deviceCacheDir = deContext.getCacheDir().getCanonicalPath(); |
| 708 |
deviceCodeCacheDir = deContext.getCodeCacheDir().getCanonicalPath(); |
| 709 |
|
| 710 |
libDir = (appInfo.nativeLibraryDir == null) |
| 711 |
? null |
| 712 |
: new File(appInfo.nativeLibraryDir).getCanonicalPath(); |
| 713 |
|
| 714 |
|
| 715 |
if (Process.myUid() != Process.SYSTEM_UID) { |
| 716 |
File efLocation = getExternalFilesDir(null); |
| 717 |
if (efLocation != null) { |
| 718 |
efDir = efLocation.getCanonicalPath(); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
filePath = file.getCanonicalPath(); |
| 726 |
} catch (IOException e) { |
| 727 |
Log.w(TAG, "Unable to obtain canonical paths"); |
| 728 |
return; |
| 729 |
} |
| 730 |
|
| 731 |
if (filePath.startsWith(cacheDir) |
| 732 |
|| filePath.startsWith(codeCacheDir) |
| 733 |
|| filePath.startsWith(nbFilesDir) |
| 734 |
|| filePath.startsWith(deviceCacheDir) |
| 735 |
|| filePath.startsWith(deviceCodeCacheDir) |
| 736 |
|| filePath.startsWith(deviceNbFilesDir) |
| 737 |
|| filePath.startsWith(libDir)) { |
| 738 |
Log.w(TAG, "lib, cache, code_cache, and no_backup files are not backed up"); |
| 739 |
return; |
| 740 |
} |
| 741 |
|
| 742 |
final String domain; |
| 743 |
String rootpath = null; |
| 744 |
if (filePath.startsWith(dbDir)) { |
| 745 |
domain = FullBackup.DATABASE_TREE_TOKEN; |
| 746 |
rootpath = dbDir; |
| 747 |
} else if (filePath.startsWith(spDir)) { |
| 748 |
domain = FullBackup.SHAREDPREFS_TREE_TOKEN; |
| 749 |
rootpath = spDir; |
| 750 |
} else if (filePath.startsWith(filesDir)) { |
| 751 |
domain = FullBackup.FILES_TREE_TOKEN; |
| 752 |
rootpath = filesDir; |
| 753 |
} else if (filePath.startsWith(rootDir)) { |
| 754 |
domain = FullBackup.ROOT_TREE_TOKEN; |
| 755 |
rootpath = rootDir; |
| 756 |
} else if (filePath.startsWith(deviceDbDir)) { |
| 757 |
domain = FullBackup.DEVICE_DATABASE_TREE_TOKEN; |
| 758 |
rootpath = deviceDbDir; |
| 759 |
} else if (filePath.startsWith(deviceSpDir)) { |
| 760 |
domain = FullBackup.DEVICE_SHAREDPREFS_TREE_TOKEN; |
| 761 |
rootpath = deviceSpDir; |
| 762 |
} else if (filePath.startsWith(deviceFilesDir)) { |
| 763 |
domain = FullBackup.DEVICE_FILES_TREE_TOKEN; |
| 764 |
rootpath = deviceFilesDir; |
| 765 |
} else if (filePath.startsWith(deviceRootDir)) { |
| 766 |
domain = FullBackup.DEVICE_ROOT_TREE_TOKEN; |
| 767 |
rootpath = deviceRootDir; |
| 768 |
} else if ((efDir != null) && filePath.startsWith(efDir)) { |
| 769 |
domain = FullBackup.MANAGED_EXTERNAL_TREE_TOKEN; |
| 770 |
rootpath = efDir; |
| 771 |
} else { |
| 772 |
Log.w(TAG, "File " + filePath + " is in an unsupported location; skipping"); |
| 773 |
return; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
if (DEBUG) Log.i(TAG, "backupFile() of " + filePath + " => domain=" + domain |
| 780 |
+ " rootpath=" + rootpath); |
| 781 |
|
| 782 |
FullBackup.backupToTar(getPackageName(), domain, null, rootpath, filePath, output); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
protected final void fullBackupFileTree(String packageName, String domain, String startingPath, |
| 795 |
Set<PathWithRequiredFlags> manifestExcludes, |
| 796 |
ArraySet<String> systemExcludes, |
| 797 |
FullBackupDataOutput output) { |
| 798 |
|
| 799 |
String domainPath = FullBackup.getBackupScheme(this, mOperationType) |
| 800 |
.tokenToDirectoryPath(domain); |
| 801 |
if (domainPath == null) { |
| 802 |
|
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
File rootFile = new File(startingPath); |
| 807 |
if (rootFile.exists()) { |
| 808 |
LinkedList<File> scanQueue = new LinkedList<File>(); |
| 809 |
scanQueue.add(rootFile); |
| 810 |
|
| 811 |
while (scanQueue.size() > 0) { |
| 812 |
File file = scanQueue.remove(0); |
| 813 |
String filePath; |
| 814 |
try { |
| 815 |
|
| 816 |
StructStat stat = Os.lstat(file.getPath()); |
| 817 |
if (!OsConstants.S_ISREG(stat.st_mode) |
| 818 |
&& !OsConstants.S_ISDIR(stat.st_mode)) { |
| 819 |
if (DEBUG) Log.i(TAG, "Not a file/dir (skipping)!: " + file); |
| 820 |
continue; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
filePath = file.getCanonicalPath(); |
| 825 |
|
| 826 |
|
| 827 |
if (manifestExcludes != null |
| 828 |
&& manifestExcludesContainFilePath(manifestExcludes, filePath)) { |
| 829 |
continue; |
| 830 |
} |
| 831 |
if (systemExcludes != null && systemExcludes.contains(filePath)) { |
| 832 |
continue; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
if (OsConstants.S_ISDIR(stat.st_mode)) { |
| 837 |
File[] contents = file.listFiles(); |
| 838 |
if (contents != null) { |
| 839 |
for (File entry : contents) { |
| 840 |
scanQueue.add(0, entry); |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
} catch (IOException e) { |
| 845 |
if (DEBUG) Log.w(TAG, "Error canonicalizing path of " + file); |
| 846 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 847 |
Log.v(FullBackup.TAG_XML_PARSER, "Error canonicalizing path of " + file); |
| 848 |
} |
| 849 |
continue; |
| 850 |
} catch (ErrnoException e) { |
| 851 |
if (DEBUG) Log.w(TAG, "Error scanning file " + file + " : " + e); |
| 852 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 853 |
Log.v(FullBackup.TAG_XML_PARSER, "Error scanning file " + file + " : " + e); |
| 854 |
} |
| 855 |
continue; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
FullBackup.backupToTar(packageName, domain, null, domainPath, filePath, output); |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
private boolean manifestExcludesContainFilePath( |
| 865 |
Set<PathWithRequiredFlags> manifestExcludes, String filePath) { |
| 866 |
for (PathWithRequiredFlags exclude : manifestExcludes) { |
| 867 |
String excludePath = exclude.getPath(); |
| 868 |
if (excludePath != null && excludePath.equals(filePath)) { |
| 869 |
return true; |
| 870 |
} |
| 871 |
} |
| 872 |
return false; |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
public void onRestoreFile(ParcelFileDescriptor data, long size, |
| 902 |
File destination, int type, long mode, long mtime) |
| 903 |
throws IOException { |
| 904 |
|
| 905 |
final boolean accept = isFileEligibleForRestore(destination); |
| 906 |
|
| 907 |
FullBackup.restoreFile(data, size, type, mode, mtime, accept ? destination : null); |
| 908 |
} |
| 909 |
|
| 910 |
private boolean isFileEligibleForRestore(File destination) throws IOException { |
| 911 |
FullBackup.BackupScheme bs = FullBackup.getBackupScheme(this, mOperationType); |
| 912 |
if (!bs.isFullRestoreEnabled()) { |
| 913 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 914 |
Log.v(FullBackup.TAG_XML_PARSER, |
| 915 |
"onRestoreFile \"" + destination.getCanonicalPath() |
| 916 |
+ "\" : fullBackupContent not enabled for " + getPackageName()); |
| 917 |
} |
| 918 |
return false; |
| 919 |
} |
| 920 |
|
| 921 |
Map<String, Set<PathWithRequiredFlags>> includes = null; |
| 922 |
ArraySet<PathWithRequiredFlags> excludes = null; |
| 923 |
final String destinationCanonicalPath = destination.getCanonicalPath(); |
| 924 |
try { |
| 925 |
includes = bs.maybeParseAndGetCanonicalIncludePaths(); |
| 926 |
excludes = bs.maybeParseAndGetCanonicalExcludePaths(); |
| 927 |
} catch (XmlPullParserException e) { |
| 928 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 929 |
Log.v(FullBackup.TAG_XML_PARSER, |
| 930 |
"onRestoreFile \"" + destinationCanonicalPath |
| 931 |
+ "\" : Exception trying to parse fullBackupContent xml file!" |
| 932 |
+ " Aborting onRestoreFile.", e); |
| 933 |
} |
| 934 |
return false; |
| 935 |
} |
| 936 |
|
| 937 |
if (excludes != null && |
| 938 |
BackupUtils.isFileSpecifiedInPathList(destination, excludes)) { |
| 939 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 940 |
Log.v(FullBackup.TAG_XML_PARSER, |
| 941 |
"onRestoreFile: \"" + destinationCanonicalPath + "\": listed in" |
| 942 |
+ " excludes; skipping."); |
| 943 |
} |
| 944 |
return false; |
| 945 |
} |
| 946 |
|
| 947 |
if (includes != null && !includes.isEmpty()) { |
| 948 |
|
| 949 |
|
| 950 |
boolean explicitlyIncluded = false; |
| 951 |
for (Set<PathWithRequiredFlags> domainIncludes : includes.values()) { |
| 952 |
explicitlyIncluded |= |
| 953 |
BackupUtils.isFileSpecifiedInPathList(destination, domainIncludes); |
| 954 |
if (explicitlyIncluded) { |
| 955 |
break; |
| 956 |
} |
| 957 |
} |
| 958 |
if (!explicitlyIncluded) { |
| 959 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 960 |
Log.v(FullBackup.TAG_XML_PARSER, |
| 961 |
"onRestoreFile: Trying to restore \"" |
| 962 |
+ destinationCanonicalPath + "\" but it isn't specified" |
| 963 |
+ " in the included files; skipping."); |
| 964 |
} |
| 965 |
return false; |
| 966 |
} |
| 967 |
} |
| 968 |
return true; |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
protected void onRestoreFile(ParcelFileDescriptor data, long size, |
| 977 |
int type, String domain, String path, long mode, long mtime) |
| 978 |
throws IOException { |
| 979 |
String basePath = null; |
| 980 |
|
| 981 |
if (DEBUG) Log.d(TAG, "onRestoreFile() size=" + size + " type=" + type |
| 982 |
+ " domain=" + domain + " relpath=" + path + " mode=" + mode |
| 983 |
+ " mtime=" + mtime); |
| 984 |
|
| 985 |
basePath = FullBackup.getBackupScheme(this, mOperationType).tokenToDirectoryPath( |
| 986 |
domain); |
| 987 |
if (domain.equals(FullBackup.MANAGED_EXTERNAL_TREE_TOKEN)) { |
| 988 |
mode = -1; |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
if (basePath != null) { |
| 993 |
|
| 994 |
File outFile = new File(basePath, path); |
| 995 |
String outPath = outFile.getCanonicalPath(); |
| 996 |
if (outPath.startsWith(basePath + File.separatorChar)) { |
| 997 |
if (DEBUG) Log.i(TAG, "[" + domain + " : " + path + "] mapped to " + outPath); |
| 998 |
onRestoreFile(data, size, outFile, type, mode, mtime); |
| 999 |
return; |
| 1000 |
} else { |
| 1001 |
|
| 1002 |
if (DEBUG) { |
| 1003 |
Log.e(TAG, "Cross-domain restore attempt: " + outPath); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
if (DEBUG) Log.i(TAG, "[ skipping file " + path + "]"); |
| 1012 |
FullBackup.restoreFile(data, size, type, mode, mtime, null); |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
public void onRestoreFinished() { |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
public final IBinder onBind() { |
| 1033 |
return mBinder; |
| 1034 |
} |
| 1035 |
|
| 1036 |
private final IBinder mBinder = new BackupServiceBinder().asBinder(); |
| 1037 |
|
| 1038 |
|
| 1039 |
public void attach(Context context) { |
| 1040 |
attachBaseContext(context); |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
private class BackupServiceBinder extends IBackupAgent.Stub { |
| 1045 |
private static final String TAG = "BackupServiceBinder"; |
| 1046 |
|
| 1047 |
@Override |
| 1048 |
public void doBackup( |
| 1049 |
ParcelFileDescriptor oldState, |
| 1050 |
ParcelFileDescriptor data, |
| 1051 |
ParcelFileDescriptor newState, |
| 1052 |
long quotaBytes, |
| 1053 |
IBackupCallback callbackBinder, |
| 1054 |
int transportFlags) throws RemoteException { |
| 1055 |
if (DEBUG) Log.v(TAG, "doBackup() invoked"); |
| 1056 |
|
| 1057 |
BackupDataOutput output = new BackupDataOutput( |
| 1058 |
data.getFileDescriptor(), quotaBytes, transportFlags); |
| 1059 |
|
| 1060 |
long result = RESULT_ERROR; |
| 1061 |
|
| 1062 |
|
| 1063 |
final long ident = Binder.clearCallingIdentity(); |
| 1064 |
try { |
| 1065 |
BackupAgent.this.onBackup(oldState, output, newState); |
| 1066 |
result = RESULT_SUCCESS; |
| 1067 |
} catch (IOException ex) { |
| 1068 |
Log.d(TAG, "onBackup (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1069 |
throw new RuntimeException(ex); |
| 1070 |
} catch (RuntimeException ex) { |
| 1071 |
Log.d(TAG, "onBackup (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1072 |
throw ex; |
| 1073 |
} finally { |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
waitForSharedPrefs(); |
| 1078 |
|
| 1079 |
Binder.restoreCallingIdentity(ident); |
| 1080 |
try { |
| 1081 |
callbackBinder.operationComplete(result); |
| 1082 |
} catch (RemoteException e) { |
| 1083 |
|
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
if (Binder.getCallingPid() != Process.myPid()) { |
| 1088 |
IoUtils.closeQuietly(oldState); |
| 1089 |
IoUtils.closeQuietly(data); |
| 1090 |
IoUtils.closeQuietly(newState); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
@Override |
| 1096 |
public void doRestore(ParcelFileDescriptor data, long appVersionCode, |
| 1097 |
ParcelFileDescriptor newState, int token, IBackupManager callbackBinder) |
| 1098 |
throws RemoteException { |
| 1099 |
doRestoreInternal(data, appVersionCode, newState, token, callbackBinder, |
| 1100 |
null); |
| 1101 |
} |
| 1102 |
|
| 1103 |
@Override |
| 1104 |
public void doRestoreWithExcludedKeys(ParcelFileDescriptor data, long appVersionCode, |
| 1105 |
ParcelFileDescriptor newState, int token, IBackupManager callbackBinder, |
| 1106 |
List<String> excludedKeys) throws RemoteException { |
| 1107 |
doRestoreInternal(data, appVersionCode, newState, token, callbackBinder, excludedKeys); |
| 1108 |
} |
| 1109 |
|
| 1110 |
private void doRestoreInternal(ParcelFileDescriptor data, long appVersionCode, |
| 1111 |
ParcelFileDescriptor newState, int token, IBackupManager callbackBinder, |
| 1112 |
List<String> excludedKeys) throws RemoteException { |
| 1113 |
if (DEBUG) Log.v(TAG, "doRestore() invoked"); |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
waitForSharedPrefs(); |
| 1118 |
|
| 1119 |
BackupDataInput input = new BackupDataInput(data.getFileDescriptor()); |
| 1120 |
|
| 1121 |
|
| 1122 |
final long ident = Binder.clearCallingIdentity(); |
| 1123 |
try { |
| 1124 |
BackupAgent.this.onRestore(input, appVersionCode, newState, |
| 1125 |
excludedKeys != null ? new HashSet<>(excludedKeys) |
| 1126 |
: Collections.emptySet()); |
| 1127 |
} catch (IOException ex) { |
| 1128 |
Log.d(TAG, "onRestore (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1129 |
throw new RuntimeException(ex); |
| 1130 |
} catch (RuntimeException ex) { |
| 1131 |
Log.d(TAG, "onRestore (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1132 |
throw ex; |
| 1133 |
} finally { |
| 1134 |
|
| 1135 |
reloadSharedPreferences(); |
| 1136 |
|
| 1137 |
Binder.restoreCallingIdentity(ident); |
| 1138 |
try { |
| 1139 |
callbackBinder.opCompleteForUser(getBackupUserId(), token, 0); |
| 1140 |
} catch (RemoteException e) { |
| 1141 |
|
| 1142 |
} |
| 1143 |
|
| 1144 |
if (Binder.getCallingPid() != Process.myPid()) { |
| 1145 |
IoUtils.closeQuietly(data); |
| 1146 |
IoUtils.closeQuietly(newState); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
@Override |
| 1152 |
public void doFullBackup(ParcelFileDescriptor data, |
| 1153 |
long quotaBytes, int token, IBackupManager callbackBinder, int transportFlags) { |
| 1154 |
if (DEBUG) Log.v(TAG, "doFullBackup() invoked"); |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
waitForSharedPrefs(); |
| 1159 |
|
| 1160 |
|
| 1161 |
final long ident = Binder.clearCallingIdentity(); |
| 1162 |
try { |
| 1163 |
BackupAgent.this.onFullBackup(new FullBackupDataOutput( |
| 1164 |
data, quotaBytes, transportFlags)); |
| 1165 |
} catch (IOException ex) { |
| 1166 |
Log.d(TAG, "onFullBackup (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1167 |
throw new RuntimeException(ex); |
| 1168 |
} catch (RuntimeException ex) { |
| 1169 |
Log.d(TAG, "onFullBackup (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1170 |
throw ex; |
| 1171 |
} finally { |
| 1172 |
|
| 1173 |
waitForSharedPrefs(); |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
try { |
| 1178 |
FileOutputStream out = new FileOutputStream(data.getFileDescriptor()); |
| 1179 |
byte[] buf = new byte[4]; |
| 1180 |
out.write(buf); |
| 1181 |
} catch (IOException e) { |
| 1182 |
Log.e(TAG, "Unable to finalize backup stream!"); |
| 1183 |
} |
| 1184 |
|
| 1185 |
Binder.restoreCallingIdentity(ident); |
| 1186 |
try { |
| 1187 |
callbackBinder.opCompleteForUser(getBackupUserId(), token, 0); |
| 1188 |
} catch (RemoteException e) { |
| 1189 |
|
| 1190 |
} |
| 1191 |
|
| 1192 |
if (Binder.getCallingPid() != Process.myPid()) { |
| 1193 |
IoUtils.closeQuietly(data); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
public void doMeasureFullBackup(long quotaBytes, int token, IBackupManager callbackBinder, |
| 1199 |
int transportFlags) { |
| 1200 |
FullBackupDataOutput measureOutput = |
| 1201 |
new FullBackupDataOutput(quotaBytes, transportFlags); |
| 1202 |
|
| 1203 |
waitForSharedPrefs(); |
| 1204 |
|
| 1205 |
|
| 1206 |
final long ident = Binder.clearCallingIdentity(); |
| 1207 |
try { |
| 1208 |
BackupAgent.this.onFullBackup(measureOutput); |
| 1209 |
} catch (IOException ex) { |
| 1210 |
Log.d(TAG, "onFullBackup[M] (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1211 |
throw new RuntimeException(ex); |
| 1212 |
} catch (RuntimeException ex) { |
| 1213 |
Log.d(TAG, "onFullBackup[M] (" + BackupAgent.this.getClass().getName() + ") threw", ex); |
| 1214 |
throw ex; |
| 1215 |
} finally { |
| 1216 |
Binder.restoreCallingIdentity(ident); |
| 1217 |
try { |
| 1218 |
callbackBinder.opCompleteForUser(getBackupUserId(), token, |
| 1219 |
measureOutput.getSize()); |
| 1220 |
} catch (RemoteException e) { |
| 1221 |
|
| 1222 |
} |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
@Override |
| 1227 |
public void doRestoreFile(ParcelFileDescriptor data, long size, |
| 1228 |
int type, String domain, String path, long mode, long mtime, |
| 1229 |
int token, IBackupManager callbackBinder) throws RemoteException { |
| 1230 |
final long ident = Binder.clearCallingIdentity(); |
| 1231 |
try { |
| 1232 |
BackupAgent.this.onRestoreFile(data, size, type, domain, path, mode, mtime); |
| 1233 |
} catch (IOException e) { |
| 1234 |
Log.d(TAG, "onRestoreFile (" + BackupAgent.this.getClass().getName() + ") threw", e); |
| 1235 |
throw new RuntimeException(e); |
| 1236 |
} finally { |
| 1237 |
|
| 1238 |
waitForSharedPrefs(); |
| 1239 |
|
| 1240 |
reloadSharedPreferences(); |
| 1241 |
|
| 1242 |
Binder.restoreCallingIdentity(ident); |
| 1243 |
try { |
| 1244 |
callbackBinder.opCompleteForUser(getBackupUserId(), token, 0); |
| 1245 |
} catch (RemoteException e) { |
| 1246 |
|
| 1247 |
} |
| 1248 |
|
| 1249 |
if (Binder.getCallingPid() != Process.myPid()) { |
| 1250 |
IoUtils.closeQuietly(data); |
| 1251 |
} |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
@Override |
| 1256 |
public void doRestoreFinished(int token, IBackupManager callbackBinder) { |
| 1257 |
final long ident = Binder.clearCallingIdentity(); |
| 1258 |
try { |
| 1259 |
BackupAgent.this.onRestoreFinished(); |
| 1260 |
} catch (Exception e) { |
| 1261 |
Log.d(TAG, "onRestoreFinished (" + BackupAgent.this.getClass().getName() + ") threw", e); |
| 1262 |
throw e; |
| 1263 |
} finally { |
| 1264 |
|
| 1265 |
waitForSharedPrefs(); |
| 1266 |
|
| 1267 |
Binder.restoreCallingIdentity(ident); |
| 1268 |
try { |
| 1269 |
callbackBinder.opCompleteForUser(getBackupUserId(), token, 0); |
| 1270 |
} catch (RemoteException e) { |
| 1271 |
|
| 1272 |
} |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
@Override |
| 1277 |
public void fail(String message) { |
| 1278 |
getHandler().post(new FailRunnable(message)); |
| 1279 |
} |
| 1280 |
|
| 1281 |
@Override |
| 1282 |
public void doQuotaExceeded( |
| 1283 |
long backupDataBytes, |
| 1284 |
long quotaBytes, |
| 1285 |
IBackupCallback callbackBinder) { |
| 1286 |
long result = RESULT_ERROR; |
| 1287 |
|
| 1288 |
|
| 1289 |
final long ident = Binder.clearCallingIdentity(); |
| 1290 |
try { |
| 1291 |
BackupAgent.this.onQuotaExceeded(backupDataBytes, quotaBytes); |
| 1292 |
result = RESULT_SUCCESS; |
| 1293 |
} catch (Exception e) { |
| 1294 |
Log.d(TAG, "onQuotaExceeded(" + BackupAgent.this.getClass().getName() + ") threw", |
| 1295 |
e); |
| 1296 |
throw e; |
| 1297 |
} finally { |
| 1298 |
waitForSharedPrefs(); |
| 1299 |
Binder.restoreCallingIdentity(ident); |
| 1300 |
|
| 1301 |
try { |
| 1302 |
callbackBinder.operationComplete(result); |
| 1303 |
} catch (RemoteException e) { |
| 1304 |
|
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
static class FailRunnable implements Runnable { |
| 1311 |
private String mMessage; |
| 1312 |
|
| 1313 |
FailRunnable(String message) { |
| 1314 |
mMessage = message; |
| 1315 |
} |
| 1316 |
|
| 1317 |
@Override |
| 1318 |
public void run() { |
| 1319 |
throw new IllegalStateException(mMessage); |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
@VisibleForTesting |
| 1325 |
public static class IncludeExcludeRules { |
| 1326 |
private final Map<String, Set<PathWithRequiredFlags>> mManifestIncludeMap; |
| 1327 |
private final Set<PathWithRequiredFlags> mManifestExcludeSet; |
| 1328 |
|
| 1329 |
|
| 1330 |
public IncludeExcludeRules( |
| 1331 |
Map<String, Set<PathWithRequiredFlags>> manifestIncludeMap, |
| 1332 |
Set<PathWithRequiredFlags> manifestExcludeSet) { |
| 1333 |
mManifestIncludeMap = manifestIncludeMap; |
| 1334 |
mManifestExcludeSet = manifestExcludeSet; |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
@VisibleForTesting |
| 1339 |
public static IncludeExcludeRules emptyRules() { |
| 1340 |
return new IncludeExcludeRules(Collections.emptyMap(), new ArraySet<>()); |
| 1341 |
} |
| 1342 |
|
| 1343 |
private Map<String, Set<PathWithRequiredFlags>> getIncludeMap() { |
| 1344 |
return mManifestIncludeMap; |
| 1345 |
} |
| 1346 |
|
| 1347 |
private Set<PathWithRequiredFlags> getExcludeSet() { |
| 1348 |
return mManifestExcludeSet; |
| 1349 |
} |
| 1350 |
|
| 1351 |
|
| 1352 |
@Override |
| 1353 |
public int hashCode() { |
| 1354 |
return Objects.hash(mManifestIncludeMap, mManifestExcludeSet); |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
@Override |
| 1359 |
public boolean equals(@Nullable Object object) { |
| 1360 |
if (this == object) { |
| 1361 |
return true; |
| 1362 |
} |
| 1363 |
if (object == null || getClass() != object.getClass()) { |
| 1364 |
return false; |
| 1365 |
} |
| 1366 |
IncludeExcludeRules that = (IncludeExcludeRules) object; |
| 1367 |
return Objects.equals(mManifestIncludeMap, that.mManifestIncludeMap) && |
| 1368 |
Objects.equals(mManifestExcludeSet, that.mManifestExcludeSet); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
} |