| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.backup; |
| 18 |
|
| 19 |
import static android.app.backup.BackupManager.OperationType; |
| 20 |
|
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.StringDef; |
| 23 |
import android.app.compat.CompatChanges; |
| 24 |
import android.compat.annotation.ChangeId; |
| 25 |
import android.compat.annotation.EnabledSince; |
| 26 |
import android.compat.annotation.Overridable; |
| 27 |
import android.compat.annotation.UnsupportedAppUsage; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.pm.ApplicationInfo; |
| 30 |
import android.content.pm.PackageManager; |
| 31 |
import android.content.res.XmlResourceParser; |
| 32 |
import android.os.Build; |
| 33 |
import android.os.ParcelFileDescriptor; |
| 34 |
import android.os.Process; |
| 35 |
import android.os.storage.StorageManager; |
| 36 |
import android.os.storage.StorageVolume; |
| 37 |
import android.system.ErrnoException; |
| 38 |
import android.system.Os; |
| 39 |
import android.text.TextUtils; |
| 40 |
import android.util.ArrayMap; |
| 41 |
import android.util.ArraySet; |
| 42 |
import android.util.Log; |
| 43 |
import android.util.Slog; |
| 44 |
|
| 45 |
import com.android.internal.annotations.VisibleForTesting; |
| 46 |
|
| 47 |
import org.xmlpull.v1.XmlPullParser; |
| 48 |
import org.xmlpull.v1.XmlPullParserException; |
| 49 |
|
| 50 |
import java.io.File; |
| 51 |
import java.io.FileInputStream; |
| 52 |
import java.io.FileOutputStream; |
| 53 |
import java.io.IOException; |
| 54 |
import java.util.Map; |
| 55 |
import java.util.Objects; |
| 56 |
import java.util.Optional; |
| 57 |
import java.util.Set; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public class FullBackup { |
| 67 |
static final String TAG = "FullBackup"; |
| 68 |
|
| 69 |
static final String TAG_XML_PARSER = "BackupXmlParserLogging"; |
| 70 |
|
| 71 |
public static final String APK_TREE_TOKEN = "a"; |
| 72 |
public static final String OBB_TREE_TOKEN = "obb"; |
| 73 |
public static final String KEY_VALUE_DATA_TOKEN = "k"; |
| 74 |
|
| 75 |
public static final String ROOT_TREE_TOKEN = "r"; |
| 76 |
public static final String FILES_TREE_TOKEN = "f"; |
| 77 |
public static final String NO_BACKUP_TREE_TOKEN = "nb"; |
| 78 |
public static final String DATABASE_TREE_TOKEN = "db"; |
| 79 |
public static final String SHAREDPREFS_TREE_TOKEN = "sp"; |
| 80 |
public static final String CACHE_TREE_TOKEN = "c"; |
| 81 |
|
| 82 |
public static final String DEVICE_ROOT_TREE_TOKEN = "d_r"; |
| 83 |
public static final String DEVICE_FILES_TREE_TOKEN = "d_f"; |
| 84 |
public static final String DEVICE_NO_BACKUP_TREE_TOKEN = "d_nb"; |
| 85 |
public static final String DEVICE_DATABASE_TREE_TOKEN = "d_db"; |
| 86 |
public static final String DEVICE_SHAREDPREFS_TREE_TOKEN = "d_sp"; |
| 87 |
public static final String DEVICE_CACHE_TREE_TOKEN = "d_c"; |
| 88 |
|
| 89 |
public static final String MANAGED_EXTERNAL_TREE_TOKEN = "ef"; |
| 90 |
public static final String SHARED_STORAGE_TOKEN = "shared"; |
| 91 |
|
| 92 |
public static final String APPS_PREFIX = "apps/"; |
| 93 |
public static final String SHARED_PREFIX = SHARED_STORAGE_TOKEN + "/"; |
| 94 |
|
| 95 |
public static final String FULL_BACKUP_INTENT_ACTION = "fullback"; |
| 96 |
public static final String FULL_RESTORE_INTENT_ACTION = "fullrest"; |
| 97 |
public static final String CONF_TOKEN_INTENT_EXTRA = "conftoken"; |
| 98 |
|
| 99 |
public static final String FLAG_REQUIRED_CLIENT_SIDE_ENCRYPTION = "clientSideEncryption"; |
| 100 |
public static final String FLAG_REQUIRED_DEVICE_TO_DEVICE_TRANSFER = "deviceToDeviceTransfer"; |
| 101 |
public static final String FLAG_REQUIRED_FAKE_CLIENT_SIDE_ENCRYPTION = |
| 102 |
"fakeClientSideEncryption"; |
| 103 |
private static final String FLAG_DISABLE_IF_NO_ENCRYPTION_CAPABILITIES |
| 104 |
= "disableIfNoEncryptionCapabilities"; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
@ChangeId |
| 111 |
@Overridable |
| 112 |
@EnabledSince(targetSdkVersion = Build.VERSION_CODES.S) |
| 113 |
private static final long IGNORE_FULL_BACKUP_CONTENT_IN_D2D = 180523564L; |
| 114 |
|
| 115 |
@StringDef({ |
| 116 |
ConfigSection.CLOUD_BACKUP, |
| 117 |
ConfigSection.DEVICE_TRANSFER |
| 118 |
}) |
| 119 |
@interface ConfigSection { |
| 120 |
String CLOUD_BACKUP = "cloud-backup"; |
| 121 |
String DEVICE_TRANSFER = "device-transfer"; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private static class BackupSchemeId { |
| 129 |
final String mPackageName; |
| 130 |
@OperationType final int mOperationType; |
| 131 |
|
| 132 |
BackupSchemeId(String packageName, @OperationType int operationType) { |
| 133 |
mPackageName = packageName; |
| 134 |
mOperationType = operationType; |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public int hashCode() { |
| 139 |
return Objects.hash(mPackageName, mOperationType); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public boolean equals(@Nullable Object object) { |
| 144 |
if (this == object) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
if (object == null || getClass() != object.getClass()) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
BackupSchemeId that = (BackupSchemeId) object; |
| 151 |
return Objects.equals(mPackageName, that.mPackageName) && |
| 152 |
Objects.equals(mOperationType, that.mOperationType); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@UnsupportedAppUsage |
| 160 |
static public native int backupToTar(String packageName, String domain, |
| 161 |
String linkdomain, String rootpath, String path, FullBackupDataOutput output); |
| 162 |
|
| 163 |
private static final Map<BackupSchemeId, BackupScheme> kPackageBackupSchemeMap = |
| 164 |
new ArrayMap<>(); |
| 165 |
|
| 166 |
static synchronized BackupScheme getBackupScheme(Context context, |
| 167 |
@OperationType int operationType) { |
| 168 |
BackupSchemeId backupSchemeId = new BackupSchemeId(context.getPackageName(), operationType); |
| 169 |
BackupScheme backupSchemeForPackage = |
| 170 |
kPackageBackupSchemeMap.get(backupSchemeId); |
| 171 |
if (backupSchemeForPackage == null) { |
| 172 |
backupSchemeForPackage = new BackupScheme(context, operationType); |
| 173 |
kPackageBackupSchemeMap.put(backupSchemeId, backupSchemeForPackage); |
| 174 |
} |
| 175 |
return backupSchemeForPackage; |
| 176 |
} |
| 177 |
|
| 178 |
public static BackupScheme getBackupSchemeForTest(Context context) { |
| 179 |
BackupScheme testing = new BackupScheme(context, OperationType.BACKUP); |
| 180 |
testing.mExcludes = new ArraySet(); |
| 181 |
testing.mIncludes = new ArrayMap(); |
| 182 |
return testing; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static public void restoreFile(ParcelFileDescriptor data, |
| 216 |
long size, int type, long mode, long mtime, File outFile) throws IOException { |
| 217 |
if (type == BackupAgent.TYPE_DIRECTORY) { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
if (outFile != null) outFile.mkdirs(); |
| 222 |
} else { |
| 223 |
FileOutputStream out = null; |
| 224 |
|
| 225 |
|
| 226 |
try { |
| 227 |
if (outFile != null) { |
| 228 |
File parent = outFile.getParentFile(); |
| 229 |
if (!parent.exists()) { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
parent.mkdirs(); |
| 235 |
} |
| 236 |
out = new FileOutputStream(outFile); |
| 237 |
} |
| 238 |
} catch (IOException e) { |
| 239 |
Log.e(TAG, "Unable to create/open file " + outFile.getPath(), e); |
| 240 |
} |
| 241 |
|
| 242 |
byte[] buffer = new byte[32 * 1024]; |
| 243 |
final long origSize = size; |
| 244 |
FileInputStream in = new FileInputStream(data.getFileDescriptor()); |
| 245 |
while (size > 0) { |
| 246 |
int toRead = (size > buffer.length) ? buffer.length : (int)size; |
| 247 |
int got = in.read(buffer, 0, toRead); |
| 248 |
if (got <= 0) { |
| 249 |
Log.w(TAG, "Incomplete read: expected " + size + " but got " |
| 250 |
+ (origSize - size)); |
| 251 |
break; |
| 252 |
} |
| 253 |
if (out != null) { |
| 254 |
try { |
| 255 |
out.write(buffer, 0, got); |
| 256 |
} catch (IOException e) { |
| 257 |
|
| 258 |
|
| 259 |
Log.e(TAG, "Unable to write to file " + outFile.getPath(), e); |
| 260 |
out.close(); |
| 261 |
out = null; |
| 262 |
outFile.delete(); |
| 263 |
} |
| 264 |
} |
| 265 |
size -= got; |
| 266 |
} |
| 267 |
if (out != null) out.close(); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
if (mode >= 0 && outFile != null) { |
| 272 |
try { |
| 273 |
|
| 274 |
mode &= 0700; |
| 275 |
Os.chmod(outFile.getPath(), (int)mode); |
| 276 |
} catch (ErrnoException e) { |
| 277 |
e.rethrowAsIOException(); |
| 278 |
} |
| 279 |
outFile.setLastModified(mtime); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
@VisibleForTesting |
| 284 |
public static class BackupScheme { |
| 285 |
private final File FILES_DIR; |
| 286 |
private final File DATABASE_DIR; |
| 287 |
private final File ROOT_DIR; |
| 288 |
private final File SHAREDPREF_DIR; |
| 289 |
private final File CACHE_DIR; |
| 290 |
private final File NOBACKUP_DIR; |
| 291 |
|
| 292 |
private final File DEVICE_FILES_DIR; |
| 293 |
private final File DEVICE_DATABASE_DIR; |
| 294 |
private final File DEVICE_ROOT_DIR; |
| 295 |
private final File DEVICE_SHAREDPREF_DIR; |
| 296 |
private final File DEVICE_CACHE_DIR; |
| 297 |
private final File DEVICE_NOBACKUP_DIR; |
| 298 |
|
| 299 |
private final File EXTERNAL_DIR; |
| 300 |
|
| 301 |
private final static String TAG_INCLUDE = "include"; |
| 302 |
private final static String TAG_EXCLUDE = "exclude"; |
| 303 |
|
| 304 |
final int mDataExtractionRules; |
| 305 |
final int mFullBackupContent; |
| 306 |
@OperationType final int mOperationType; |
| 307 |
final PackageManager mPackageManager; |
| 308 |
final StorageManager mStorageManager; |
| 309 |
final String mPackageName; |
| 310 |
|
| 311 |
|
| 312 |
private StorageVolume[] mVolumes = null; |
| 313 |
|
| 314 |
|
| 315 |
@Nullable private Integer mRequiredTransportFlags; |
| 316 |
@Nullable private Boolean mIsUsingNewScheme; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
String tokenToDirectoryPath(String domainToken) { |
| 322 |
try { |
| 323 |
if (domainToken.equals(FullBackup.FILES_TREE_TOKEN)) { |
| 324 |
return FILES_DIR.getCanonicalPath(); |
| 325 |
} else if (domainToken.equals(FullBackup.DATABASE_TREE_TOKEN)) { |
| 326 |
return DATABASE_DIR.getCanonicalPath(); |
| 327 |
} else if (domainToken.equals(FullBackup.ROOT_TREE_TOKEN)) { |
| 328 |
return ROOT_DIR.getCanonicalPath(); |
| 329 |
} else if (domainToken.equals(FullBackup.SHAREDPREFS_TREE_TOKEN)) { |
| 330 |
return SHAREDPREF_DIR.getCanonicalPath(); |
| 331 |
} else if (domainToken.equals(FullBackup.CACHE_TREE_TOKEN)) { |
| 332 |
return CACHE_DIR.getCanonicalPath(); |
| 333 |
} else if (domainToken.equals(FullBackup.NO_BACKUP_TREE_TOKEN)) { |
| 334 |
return NOBACKUP_DIR.getCanonicalPath(); |
| 335 |
} else if (domainToken.equals(FullBackup.DEVICE_FILES_TREE_TOKEN)) { |
| 336 |
return DEVICE_FILES_DIR.getCanonicalPath(); |
| 337 |
} else if (domainToken.equals(FullBackup.DEVICE_DATABASE_TREE_TOKEN)) { |
| 338 |
return DEVICE_DATABASE_DIR.getCanonicalPath(); |
| 339 |
} else if (domainToken.equals(FullBackup.DEVICE_ROOT_TREE_TOKEN)) { |
| 340 |
return DEVICE_ROOT_DIR.getCanonicalPath(); |
| 341 |
} else if (domainToken.equals(FullBackup.DEVICE_SHAREDPREFS_TREE_TOKEN)) { |
| 342 |
return DEVICE_SHAREDPREF_DIR.getCanonicalPath(); |
| 343 |
} else if (domainToken.equals(FullBackup.DEVICE_CACHE_TREE_TOKEN)) { |
| 344 |
return DEVICE_CACHE_DIR.getCanonicalPath(); |
| 345 |
} else if (domainToken.equals(FullBackup.DEVICE_NO_BACKUP_TREE_TOKEN)) { |
| 346 |
return DEVICE_NOBACKUP_DIR.getCanonicalPath(); |
| 347 |
} else if (domainToken.equals(FullBackup.MANAGED_EXTERNAL_TREE_TOKEN)) { |
| 348 |
if (EXTERNAL_DIR != null) { |
| 349 |
return EXTERNAL_DIR.getCanonicalPath(); |
| 350 |
} else { |
| 351 |
return null; |
| 352 |
} |
| 353 |
} else if (domainToken.startsWith(FullBackup.SHARED_PREFIX)) { |
| 354 |
return sharedDomainToPath(domainToken); |
| 355 |
} |
| 356 |
|
| 357 |
Log.i(TAG, "Unrecognized domain " + domainToken); |
| 358 |
return null; |
| 359 |
} catch (Exception e) { |
| 360 |
Log.i(TAG, "Error reading directory for domain: " + domainToken); |
| 361 |
return null; |
| 362 |
} |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
private String sharedDomainToPath(String domain) throws IOException { |
| 367 |
|
| 368 |
final String volume = domain.substring(FullBackup.SHARED_PREFIX.length()); |
| 369 |
final StorageVolume[] volumes = getVolumeList(); |
| 370 |
final int volNum = Integer.parseInt(volume); |
| 371 |
if (volNum < mVolumes.length) { |
| 372 |
return volumes[volNum].getPathFile().getCanonicalPath(); |
| 373 |
} |
| 374 |
return null; |
| 375 |
} |
| 376 |
|
| 377 |
private StorageVolume[] getVolumeList() { |
| 378 |
if (mStorageManager != null) { |
| 379 |
if (mVolumes == null) { |
| 380 |
mVolumes = mStorageManager.getVolumeList(); |
| 381 |
} |
| 382 |
} else { |
| 383 |
Log.e(TAG, "Unable to access Storage Manager"); |
| 384 |
} |
| 385 |
return mVolumes; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public static class PathWithRequiredFlags { |
| 398 |
private final String mPath; |
| 399 |
private final int mRequiredFlags; |
| 400 |
|
| 401 |
public PathWithRequiredFlags(String path, int requiredFlags) { |
| 402 |
mPath = path; |
| 403 |
mRequiredFlags = requiredFlags; |
| 404 |
} |
| 405 |
|
| 406 |
public String getPath() { |
| 407 |
return mPath; |
| 408 |
} |
| 409 |
|
| 410 |
public int getRequiredFlags() { |
| 411 |
return mRequiredFlags; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
Map<String, Set<PathWithRequiredFlags>> mIncludes; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
ArraySet<PathWithRequiredFlags> mExcludes; |
| 428 |
|
| 429 |
BackupScheme(Context context, @OperationType int operationType) { |
| 430 |
ApplicationInfo applicationInfo = context.getApplicationInfo(); |
| 431 |
|
| 432 |
mDataExtractionRules = applicationInfo.dataExtractionRulesRes; |
| 433 |
mFullBackupContent = applicationInfo.fullBackupContent; |
| 434 |
mOperationType = operationType; |
| 435 |
mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); |
| 436 |
mPackageManager = context.getPackageManager(); |
| 437 |
mPackageName = context.getPackageName(); |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
final Context ceContext = context.createCredentialProtectedStorageContext(); |
| 442 |
FILES_DIR = ceContext.getFilesDir(); |
| 443 |
DATABASE_DIR = ceContext.getDatabasePath("foo").getParentFile(); |
| 444 |
ROOT_DIR = ceContext.getDataDir(); |
| 445 |
SHAREDPREF_DIR = ceContext.getSharedPreferencesPath("foo").getParentFile(); |
| 446 |
CACHE_DIR = ceContext.getCacheDir(); |
| 447 |
NOBACKUP_DIR = ceContext.getNoBackupFilesDir(); |
| 448 |
|
| 449 |
final Context deContext = context.createDeviceProtectedStorageContext(); |
| 450 |
DEVICE_FILES_DIR = deContext.getFilesDir(); |
| 451 |
DEVICE_DATABASE_DIR = deContext.getDatabasePath("foo").getParentFile(); |
| 452 |
DEVICE_ROOT_DIR = deContext.getDataDir(); |
| 453 |
DEVICE_SHAREDPREF_DIR = deContext.getSharedPreferencesPath("foo").getParentFile(); |
| 454 |
DEVICE_CACHE_DIR = deContext.getCacheDir(); |
| 455 |
DEVICE_NOBACKUP_DIR = deContext.getNoBackupFilesDir(); |
| 456 |
|
| 457 |
if (android.os.Process.myUid() != Process.SYSTEM_UID) { |
| 458 |
EXTERNAL_DIR = context.getExternalFilesDir(null); |
| 459 |
} else { |
| 460 |
EXTERNAL_DIR = null; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
boolean isFullBackupEnabled(int transportFlags) { |
| 465 |
try { |
| 466 |
if (isUsingNewScheme()) { |
| 467 |
int requiredTransportFlags = getRequiredTransportFlags(); |
| 468 |
|
| 469 |
|
| 470 |
return (transportFlags & requiredTransportFlags) == requiredTransportFlags; |
| 471 |
} |
| 472 |
} catch (IOException | XmlPullParserException e) { |
| 473 |
Slog.w(TAG, "Failed to interpret the backup scheme: " + e); |
| 474 |
return false; |
| 475 |
} |
| 476 |
|
| 477 |
return isFullBackupContentEnabled(); |
| 478 |
} |
| 479 |
|
| 480 |
boolean isFullRestoreEnabled() { |
| 481 |
try { |
| 482 |
if (isUsingNewScheme()) { |
| 483 |
return true; |
| 484 |
} |
| 485 |
} catch (IOException | XmlPullParserException e) { |
| 486 |
Slog.w(TAG, "Failed to interpret the backup scheme: " + e); |
| 487 |
return false; |
| 488 |
} |
| 489 |
|
| 490 |
return isFullBackupContentEnabled(); |
| 491 |
} |
| 492 |
|
| 493 |
boolean isFullBackupContentEnabled() { |
| 494 |
if (mFullBackupContent < 0) { |
| 495 |
|
| 496 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 497 |
Log.v(FullBackup.TAG_XML_PARSER, "android:fullBackupContent - \"false\""); |
| 498 |
} |
| 499 |
return false; |
| 500 |
} |
| 501 |
return true; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public synchronized Map<String, Set<PathWithRequiredFlags>> |
| 512 |
maybeParseAndGetCanonicalIncludePaths() throws IOException, XmlPullParserException { |
| 513 |
if (mIncludes == null) { |
| 514 |
maybeParseBackupSchemeLocked(); |
| 515 |
} |
| 516 |
return mIncludes; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public synchronized ArraySet<PathWithRequiredFlags> maybeParseAndGetCanonicalExcludePaths() |
| 524 |
throws IOException, XmlPullParserException { |
| 525 |
if (mExcludes == null) { |
| 526 |
maybeParseBackupSchemeLocked(); |
| 527 |
} |
| 528 |
return mExcludes; |
| 529 |
} |
| 530 |
|
| 531 |
@VisibleForTesting |
| 532 |
public synchronized int getRequiredTransportFlags() |
| 533 |
throws IOException, XmlPullParserException { |
| 534 |
if (mRequiredTransportFlags == null) { |
| 535 |
maybeParseBackupSchemeLocked(); |
| 536 |
} |
| 537 |
|
| 538 |
return mRequiredTransportFlags; |
| 539 |
} |
| 540 |
|
| 541 |
private synchronized boolean isUsingNewScheme() |
| 542 |
throws IOException, XmlPullParserException { |
| 543 |
if (mIsUsingNewScheme == null) { |
| 544 |
maybeParseBackupSchemeLocked(); |
| 545 |
} |
| 546 |
|
| 547 |
return mIsUsingNewScheme; |
| 548 |
} |
| 549 |
|
| 550 |
private void maybeParseBackupSchemeLocked() throws IOException, XmlPullParserException { |
| 551 |
|
| 552 |
mIncludes = new ArrayMap<String, Set<PathWithRequiredFlags>>(); |
| 553 |
mExcludes = new ArraySet<PathWithRequiredFlags>(); |
| 554 |
mRequiredTransportFlags = 0; |
| 555 |
mIsUsingNewScheme = false; |
| 556 |
|
| 557 |
if (mFullBackupContent == 0 && mDataExtractionRules == 0) { |
| 558 |
|
| 559 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 560 |
Log.v(FullBackup.TAG_XML_PARSER, "android:fullBackupContent - \"true\""); |
| 561 |
} |
| 562 |
} else { |
| 563 |
|
| 564 |
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) { |
| 565 |
Log.v(FullBackup.TAG_XML_PARSER, "Found xml scheme: " |
| 566 |
+ "android:fullBackupContent=" + mFullBackupContent |
| 567 |
+ "; android:dataExtractionRules=" + mDataExtractionRules); |
| 568 |
} |
| 569 |
|
| 570 |
try { |
| 571 |
parseSchemeForOperationType(mOperationType); |
| 572 |
} catch (PackageManager.NameNotFoundException e) { |
| 573 |
|
| 574 |
throw new IOException(e); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
private void parseSchemeForOperationType(@OperationType int operationType) |
| 580 |
throws PackageManager.NameNotFoundException, IOException, XmlPullParserException { |
| 581 |
String configSection = getConfigSectionForOperationType(operationType); |
| 582 |
if (configSection == null) { |
| 583 |
Slog.w(TAG, "Given operation type isn't supported by backup scheme: " |
| 584 |
+ operationType); |
| 585 |
return; |
| 586 |
} |
| 587 |
|
| 588 |
if (mDataExtractionRules != 0) { |
| 589 |
|
| 590 |
|
| 591 |
boolean isSectionPresent; |
| 592 |
try (XmlResourceParser parser = getParserForResource(mDataExtractionRules)) { |
| 593 |
isSectionPresent = parseNewBackupSchemeFromXmlLocked(parser, configSection, |
| 594 |
mExcludes, mIncludes); |
| 595 |
} |
| 596 |
if (isSectionPresent) { |
| 597 |
|
| 598 |
mIsUsingNewScheme = true; |
| 599 |
return; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
if (operationType == OperationType.MIGRATION |
| 604 |
&& CompatChanges.isChangeEnabled(IGNORE_FULL_BACKUP_CONTENT_IN_D2D)) { |
| 605 |
mIsUsingNewScheme = true; |
| 606 |
return; |
| 607 |
} |
| 608 |
|
| 609 |
if (mFullBackupContent != 0) { |
| 610 |
|
| 611 |
try (XmlResourceParser parser = getParserForResource(mFullBackupContent)) { |
| 612 |
parseBackupSchemeFromXmlLocked(parser, mExcludes, mIncludes); |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
@Nullable |
| 618 |
private String getConfigSectionForOperationType(@OperationType int operationType) { |
| 619 |
switch (operationType) { |
| 620 |
case OperationType.BACKUP: |
| 621 |
return ConfigSection.CLOUD_BACKUP; |
| 622 |
case OperationType.MIGRATION: |
| 623 |
return ConfigSection.DEVICE_TRANSFER; |
| 624 |
default: |
| 625 |
return null; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
private XmlResourceParser getParserForResource(int resourceId) |
| 630 |
throws PackageManager.NameNotFoundException { |
| 631 |
return mPackageManager |
| 632 |
.getResourcesForApplication(mPackageName) |
| 633 |
.getXml(resourceId); |
| 634 |
} |
| 635 |
|
| 636 |
@VisibleForTesting |
| 637 |
public boolean parseNewBackupSchemeFromXmlLocked(XmlPullParser parser, |
| 638 |
@ConfigSection String configSection, |
| 639 |
Set<PathWithRequiredFlags> excludes, |
| 640 |
Map<String, Set<PathWithRequiredFlags>> includes) |
| 641 |
throws IOException, XmlPullParserException { |
| 642 |
verifyTopLevelTag(parser, "data-extraction-rules"); |
| 643 |
|
| 644 |
boolean isSectionPresent = false; |
| 645 |
|
| 646 |
int event; |
| 647 |
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 648 |
if (event != XmlPullParser.START_TAG || !configSection.equals(parser.getName())) { |
| 649 |
continue; |
| 650 |
} |
| 651 |
|
| 652 |
isSectionPresent = true; |
| 653 |
|
| 654 |
parseRequiredTransportFlags(parser, configSection); |
| 655 |
parseRules(parser, excludes, includes, Optional.of(0), configSection); |
| 656 |
} |
| 657 |
|
| 658 |
logParsingResults(excludes, includes); |
| 659 |
|
| 660 |
return isSectionPresent; |
| 661 |
} |
| 662 |
|
| 663 |
private void parseRequiredTransportFlags(XmlPullParser parser, |
| 664 |
@ConfigSection String configSection) { |
| 665 |
if (ConfigSection.CLOUD_BACKUP.equals(configSection)) { |
| 666 |
String encryptionAttribute = parser.getAttributeValue( null, |
| 667 |
FLAG_DISABLE_IF_NO_ENCRYPTION_CAPABILITIES); |
| 668 |
if ("true".equals(encryptionAttribute)) { |
| 669 |
mRequiredTransportFlags = BackupAgent.FLAG_CLIENT_SIDE_ENCRYPTION_ENABLED; |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
@VisibleForTesting |
| 675 |
public void parseBackupSchemeFromXmlLocked(XmlPullParser parser, |
| 676 |
Set<PathWithRequiredFlags> excludes, |
| 677 |
Map<String, Set<PathWithRequiredFlags>> includes) |
| 678 |
throws IOException, XmlPullParserException { |
| 679 |
verifyTopLevelTag(parser, "full-backup-content"); |
| 680 |
|
| 681 |
parseRules(parser, excludes, includes, Optional.empty(), "full-backup-content"); |
| 682 |
|
| 683 |
logParsingResults(excludes, includes); |
| 684 |
} |
| 685 |
|
| 686 |
private void verifyTopLevelTag(XmlPullParser parser, String tag) |
| 687 |
throws XmlPullParserException, IOException { |
| 688 |
int event = parser.getEventType(); |
| 689 |
while (event != XmlPullParser.START_TAG) { |
| 690 |
event = parser.next(); |
| 691 |
} |
| 692 |
|
| 693 |
if (!tag.equals(parser.getName())) { |
| 694 |
throw new XmlPullParserException("Xml file didn't start with correct tag" + |
| 695 |
" (" + tag + " ). Found \"" + parser.getName() + "\""); |
| 696 |
} |
| 697 |
|
| 698 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 699 |
Log.v(TAG_XML_PARSER, "\n"); |
| 700 |
Log.v(TAG_XML_PARSER, "===================================================="); |
| 701 |
Log.v(TAG_XML_PARSER, "Found valid " + tag + "; parsing xml resource."); |
| 702 |
Log.v(TAG_XML_PARSER, "===================================================="); |
| 703 |
Log.v(TAG_XML_PARSER, ""); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
private void parseRules(XmlPullParser parser, |
| 708 |
Set<PathWithRequiredFlags> excludes, |
| 709 |
Map<String, Set<PathWithRequiredFlags>> includes, |
| 710 |
Optional<Integer> maybeRequiredFlags, |
| 711 |
String endingTag) |
| 712 |
throws IOException, XmlPullParserException { |
| 713 |
int event; |
| 714 |
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT |
| 715 |
&& !parser.getName().equals(endingTag)) { |
| 716 |
switch (event) { |
| 717 |
case XmlPullParser.START_TAG: |
| 718 |
validateInnerTagContents(parser); |
| 719 |
final String domainFromXml = parser.getAttributeValue(null, "domain"); |
| 720 |
final File domainDirectory = getDirectoryForCriteriaDomain(domainFromXml); |
| 721 |
if (domainDirectory == null) { |
| 722 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 723 |
Log.v(TAG_XML_PARSER, "...parsing \"" + parser.getName() + "\": " |
| 724 |
+ "domain=\"" + domainFromXml + "\" invalid; skipping"); |
| 725 |
} |
| 726 |
break; |
| 727 |
} |
| 728 |
final File canonicalFile = |
| 729 |
extractCanonicalFile(domainDirectory, |
| 730 |
parser.getAttributeValue(null, "path")); |
| 731 |
if (canonicalFile == null) { |
| 732 |
break; |
| 733 |
} |
| 734 |
|
| 735 |
int requiredFlags = getRequiredFlagsForRule(parser, maybeRequiredFlags); |
| 736 |
|
| 737 |
|
| 738 |
Set<PathWithRequiredFlags> activeSet = parseCurrentTagForDomain( |
| 739 |
parser, excludes, includes, domainFromXml); |
| 740 |
activeSet.add(new PathWithRequiredFlags(canonicalFile.getCanonicalPath(), |
| 741 |
requiredFlags)); |
| 742 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 743 |
Log.v(TAG_XML_PARSER, "...parsed " + canonicalFile.getCanonicalPath() |
| 744 |
+ " for domain \"" + domainFromXml + "\", requiredFlags + \"" |
| 745 |
+ requiredFlags + "\""); |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
if ("database".equals(domainFromXml) && !canonicalFile.isDirectory()) { |
| 753 |
final String canonicalJournalPath = |
| 754 |
canonicalFile.getCanonicalPath() + "-journal"; |
| 755 |
activeSet.add(new PathWithRequiredFlags(canonicalJournalPath, |
| 756 |
requiredFlags)); |
| 757 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 758 |
Log.v(TAG_XML_PARSER, "...automatically generated " |
| 759 |
+ canonicalJournalPath + ". Ignore if nonexistent."); |
| 760 |
} |
| 761 |
final String canonicalWalPath = |
| 762 |
canonicalFile.getCanonicalPath() + "-wal"; |
| 763 |
activeSet.add(new PathWithRequiredFlags(canonicalWalPath, |
| 764 |
requiredFlags)); |
| 765 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 766 |
Log.v(TAG_XML_PARSER, "...automatically generated " |
| 767 |
+ canonicalWalPath + ". Ignore if nonexistent."); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
if ("sharedpref".equals(domainFromXml) && !canonicalFile.isDirectory() && |
| 773 |
!canonicalFile.getCanonicalPath().endsWith(".xml")) { |
| 774 |
final String canonicalXmlPath = |
| 775 |
canonicalFile.getCanonicalPath() + ".xml"; |
| 776 |
activeSet.add(new PathWithRequiredFlags(canonicalXmlPath, |
| 777 |
requiredFlags)); |
| 778 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 779 |
Log.v(TAG_XML_PARSER, "...automatically generated " |
| 780 |
+ canonicalXmlPath + ". Ignore if nonexistent."); |
| 781 |
} |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
private void logParsingResults(Set<PathWithRequiredFlags> excludes, |
| 788 |
Map<String, Set<PathWithRequiredFlags>> includes) { |
| 789 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 790 |
Log.v(TAG_XML_PARSER, "\n"); |
| 791 |
Log.v(TAG_XML_PARSER, "Xml resource parsing complete."); |
| 792 |
Log.v(TAG_XML_PARSER, "Final tally."); |
| 793 |
Log.v(TAG_XML_PARSER, "Includes:"); |
| 794 |
if (includes.isEmpty()) { |
| 795 |
Log.v(TAG_XML_PARSER, " ...nothing specified (This means the entirety of app" |
| 796 |
+ " data minus excludes)"); |
| 797 |
} else { |
| 798 |
for (Map.Entry<String, Set<PathWithRequiredFlags>> entry |
| 799 |
: includes.entrySet()) { |
| 800 |
Log.v(TAG_XML_PARSER, " domain=" + entry.getKey()); |
| 801 |
for (PathWithRequiredFlags includeData : entry.getValue()) { |
| 802 |
Log.v(TAG_XML_PARSER, " path: " + includeData.getPath() |
| 803 |
+ " requiredFlags: " + includeData.getRequiredFlags()); |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
Log.v(TAG_XML_PARSER, "Excludes:"); |
| 809 |
if (excludes.isEmpty()) { |
| 810 |
Log.v(TAG_XML_PARSER, " ...nothing to exclude."); |
| 811 |
} else { |
| 812 |
for (PathWithRequiredFlags excludeData : excludes) { |
| 813 |
Log.v(TAG_XML_PARSER, " path: " + excludeData.getPath() |
| 814 |
+ " requiredFlags: " + excludeData.getRequiredFlags()); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
Log.v(TAG_XML_PARSER, " "); |
| 819 |
Log.v(TAG_XML_PARSER, "===================================================="); |
| 820 |
Log.v(TAG_XML_PARSER, "\n"); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
private int getRequiredFlagsFromString(String requiredFlags) { |
| 825 |
int flags = 0; |
| 826 |
if (requiredFlags == null || requiredFlags.length() == 0) { |
| 827 |
|
| 828 |
return flags; |
| 829 |
} |
| 830 |
String[] flagsStr = requiredFlags.split("\\|"); |
| 831 |
for (String f : flagsStr) { |
| 832 |
switch (f) { |
| 833 |
case FLAG_REQUIRED_CLIENT_SIDE_ENCRYPTION: |
| 834 |
flags |= BackupAgent.FLAG_CLIENT_SIDE_ENCRYPTION_ENABLED; |
| 835 |
break; |
| 836 |
case FLAG_REQUIRED_DEVICE_TO_DEVICE_TRANSFER: |
| 837 |
flags |= BackupAgent.FLAG_DEVICE_TO_DEVICE_TRANSFER; |
| 838 |
break; |
| 839 |
case FLAG_REQUIRED_FAKE_CLIENT_SIDE_ENCRYPTION: |
| 840 |
flags |= BackupAgent.FLAG_FAKE_CLIENT_SIDE_ENCRYPTION_ENABLED; |
| 841 |
default: |
| 842 |
Log.w(TAG, "Unrecognized requiredFlag provided, value: \"" + f + "\""); |
| 843 |
} |
| 844 |
} |
| 845 |
return flags; |
| 846 |
} |
| 847 |
|
| 848 |
private int getRequiredFlagsForRule(XmlPullParser parser, |
| 849 |
Optional<Integer> maybeRequiredFlags) { |
| 850 |
if (maybeRequiredFlags.isPresent()) { |
| 851 |
|
| 852 |
|
| 853 |
return maybeRequiredFlags.get(); |
| 854 |
} |
| 855 |
|
| 856 |
if (TAG_INCLUDE.equals(parser.getName())) { |
| 857 |
|
| 858 |
|
| 859 |
return getRequiredFlagsFromString( |
| 860 |
parser.getAttributeValue(null, "requireFlags")); |
| 861 |
} |
| 862 |
|
| 863 |
return 0; |
| 864 |
} |
| 865 |
|
| 866 |
private Set<PathWithRequiredFlags> parseCurrentTagForDomain(XmlPullParser parser, |
| 867 |
Set<PathWithRequiredFlags> excludes, |
| 868 |
Map<String, Set<PathWithRequiredFlags>> includes, String domain) |
| 869 |
throws XmlPullParserException { |
| 870 |
if (TAG_INCLUDE.equals(parser.getName())) { |
| 871 |
final String domainToken = getTokenForXmlDomain(domain); |
| 872 |
Set<PathWithRequiredFlags> includeSet = includes.get(domainToken); |
| 873 |
if (includeSet == null) { |
| 874 |
includeSet = new ArraySet<PathWithRequiredFlags>(); |
| 875 |
includes.put(domainToken, includeSet); |
| 876 |
} |
| 877 |
return includeSet; |
| 878 |
} else if (TAG_EXCLUDE.equals(parser.getName())) { |
| 879 |
return excludes; |
| 880 |
} else { |
| 881 |
|
| 882 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 883 |
Log.v(TAG_XML_PARSER, "Invalid tag found in xml \"" |
| 884 |
+ parser.getName() + "\"; aborting operation."); |
| 885 |
} |
| 886 |
throw new XmlPullParserException("Unrecognised tag in backup" + |
| 887 |
" criteria xml (" + parser.getName() + ")"); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
private String getTokenForXmlDomain(String xmlDomain) { |
| 897 |
if ("root".equals(xmlDomain)) { |
| 898 |
return FullBackup.ROOT_TREE_TOKEN; |
| 899 |
} else if ("file".equals(xmlDomain)) { |
| 900 |
return FullBackup.FILES_TREE_TOKEN; |
| 901 |
} else if ("database".equals(xmlDomain)) { |
| 902 |
return FullBackup.DATABASE_TREE_TOKEN; |
| 903 |
} else if ("sharedpref".equals(xmlDomain)) { |
| 904 |
return FullBackup.SHAREDPREFS_TREE_TOKEN; |
| 905 |
} else if ("device_root".equals(xmlDomain)) { |
| 906 |
return FullBackup.DEVICE_ROOT_TREE_TOKEN; |
| 907 |
} else if ("device_file".equals(xmlDomain)) { |
| 908 |
return FullBackup.DEVICE_FILES_TREE_TOKEN; |
| 909 |
} else if ("device_database".equals(xmlDomain)) { |
| 910 |
return FullBackup.DEVICE_DATABASE_TREE_TOKEN; |
| 911 |
} else if ("device_sharedpref".equals(xmlDomain)) { |
| 912 |
return FullBackup.DEVICE_SHAREDPREFS_TREE_TOKEN; |
| 913 |
} else if ("external".equals(xmlDomain)) { |
| 914 |
return FullBackup.MANAGED_EXTERNAL_TREE_TOKEN; |
| 915 |
} else { |
| 916 |
return null; |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
private File extractCanonicalFile(File domain, String filePathFromXml) { |
| 928 |
if (filePathFromXml == null) { |
| 929 |
|
| 930 |
filePathFromXml = ""; |
| 931 |
} |
| 932 |
if (filePathFromXml.contains("..")) { |
| 933 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 934 |
Log.v(TAG_XML_PARSER, "...resolved \"" + domain.getPath() + " " + filePathFromXml |
| 935 |
+ "\", but the \"..\" path is not permitted; skipping."); |
| 936 |
} |
| 937 |
return null; |
| 938 |
} |
| 939 |
if (filePathFromXml.contains("//")) { |
| 940 |
if (Log.isLoggable(TAG_XML_PARSER, Log.VERBOSE)) { |
| 941 |
Log.v(TAG_XML_PARSER, "...resolved \"" + domain.getPath() + " " + filePathFromXml |
| 942 |
+ "\", which contains the invalid \"//\" sequence; skipping."); |
| 943 |
} |
| 944 |
return null; |
| 945 |
} |
| 946 |
return new File(domain, filePathFromXml); |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
private File getDirectoryForCriteriaDomain(String domain) { |
| 954 |
if (TextUtils.isEmpty(domain)) { |
| 955 |
return null; |
| 956 |
} |
| 957 |
if ("file".equals(domain)) { |
| 958 |
return FILES_DIR; |
| 959 |
} else if ("database".equals(domain)) { |
| 960 |
return DATABASE_DIR; |
| 961 |
} else if ("root".equals(domain)) { |
| 962 |
return ROOT_DIR; |
| 963 |
} else if ("sharedpref".equals(domain)) { |
| 964 |
return SHAREDPREF_DIR; |
| 965 |
} else if ("device_file".equals(domain)) { |
| 966 |
return DEVICE_FILES_DIR; |
| 967 |
} else if ("device_database".equals(domain)) { |
| 968 |
return DEVICE_DATABASE_DIR; |
| 969 |
} else if ("device_root".equals(domain)) { |
| 970 |
return DEVICE_ROOT_DIR; |
| 971 |
} else if ("device_sharedpref".equals(domain)) { |
| 972 |
return DEVICE_SHAREDPREF_DIR; |
| 973 |
} else if ("external".equals(domain)) { |
| 974 |
return EXTERNAL_DIR; |
| 975 |
} else { |
| 976 |
return null; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
private void validateInnerTagContents(XmlPullParser parser) throws XmlPullParserException { |
| 985 |
if (parser == null) { |
| 986 |
return; |
| 987 |
} |
| 988 |
switch (parser.getName()) { |
| 989 |
case TAG_INCLUDE: |
| 990 |
if (parser.getAttributeCount() > 3) { |
| 991 |
throw new XmlPullParserException("At most 3 tag attributes allowed for " |
| 992 |
+ "\"include\" tag (\"domain\" & \"path\"" |
| 993 |
+ " & optional \"requiredFlags\")."); |
| 994 |
} |
| 995 |
break; |
| 996 |
case TAG_EXCLUDE: |
| 997 |
if (parser.getAttributeCount() > 2) { |
| 998 |
throw new XmlPullParserException("At most 2 tag attributes allowed for " |
| 999 |
+ "\"exclude\" tag (\"domain\" & \"path\"."); |
| 1000 |
} |
| 1001 |
break; |
| 1002 |
default: |
| 1003 |
throw new XmlPullParserException("A valid tag is one of \"<include/>\" or" + |
| 1004 |
" \"<exclude/>. You provided \"" + parser.getName() + "\""); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |