Force backup initialization when we have no cached metadata
as it can happen after clearing app data
diff --git a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt
index a1a5306..8964fae 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt
@@ -222,8 +222,8 @@
internal val salt: String
@Synchronized get() = metadata.salt
- internal val isLegacyFormat: Boolean
- @Synchronized get() = metadata.version < VERSION
+ internal val requiresInit: Boolean
+ @Synchronized get() = metadata == uninitializedMetadata || metadata.version < VERSION
@Synchronized
fun getPackageMetadata(packageName: String): PackageMetadata? {
diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt
index 70f283a..a7253f2 100644
--- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt
+++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinator.kt
@@ -136,8 +136,8 @@
TRANSPORT_OK
} catch (e: IOException) {
Log.e(TAG, "Error initializing device", e)
- // Show error notification if we were ready for backups
- if (settingsManager.canDoBackupNow()) nm.onBackupError()
+ // Show error notification if we needed init or were ready for backups
+ if (metadataManager.requiresInit || settingsManager.canDoBackupNow()) nm.onBackupError()
TRANSPORT_ERROR
}
@@ -243,17 +243,7 @@
// What else we tried can be found in: https://github.com/seedvault-app/seedvault/issues/102
if (packageName == MAGIC_PACKAGE_MANAGER) {
val isIncremental = flags and FLAG_INCREMENTAL != 0
- if (!settingsManager.canDoBackupNow()) {
- // Returning anything else here (except non-incremental-required which re-tries)
- // will make the system consider the backup state compromised
- // and force re-initialization on next run.
- // Errors for other packages are OK, but this one is not allowed to fail.
- Log.w(TAG, "Skipping @pm@ backup as we can't do backup right now.")
- state.skippedPmBackup = true
- settingsManager.pmBackupNextTimeNonIncremental = true
- data.close()
- return TRANSPORT_OK
- } else if (metadataManager.isLegacyFormat) {
+ if (metadataManager.requiresInit) {
// start a new restore set to upgrade from legacy format
// by starting a clean backup with all files using the new version
try {
@@ -263,6 +253,16 @@
}
// this causes a backup error, but things should go back to normal afterwards
return TRANSPORT_NOT_INITIALIZED
+ } else if (!settingsManager.canDoBackupNow()) {
+ // Returning anything else here (except non-incremental-required which re-tries)
+ // will make the system consider the backup state compromised
+ // and force re-initialization on next run.
+ // Errors for other packages are OK, but this one is not allowed to fail.
+ Log.w(TAG, "Skipping @pm@ backup as we can't do backup right now.")
+ state.skippedPmBackup = true
+ settingsManager.pmBackupNextTimeNonIncremental = true
+ data.close()
+ return TRANSPORT_OK
} else if (isIncremental && settingsManager.pmBackupNextTimeNonIncremental) {
settingsManager.pmBackupNextTimeNonIncremental = false
data.close()
diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt
index 15a5ee0..6d4aa38 100644
--- a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt
+++ b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/BackupCoordinatorTest.kt
@@ -156,7 +156,7 @@
assertEquals(TRANSPORT_OK, backup.performIncrementalBackup(packageInfo, data, 0))
every { settingsManager.canDoBackupNow() } returns true
- every { metadataManager.isLegacyFormat } returns false
+ every { metadataManager.requiresInit } returns false
every { settingsManager.pmBackupNextTimeNonIncremental } returns true
every { settingsManager.pmBackupNextTimeNonIncremental = false } just Runs
@@ -172,7 +172,7 @@
val packageInfo = PackageInfo().apply { packageName = MAGIC_PACKAGE_MANAGER }
every { settingsManager.canDoBackupNow() } returns true
- every { metadataManager.isLegacyFormat } returns true
+ every { metadataManager.requiresInit } returns true
// start new restore set
every { clock.time() } returns token + 1
@@ -416,7 +416,7 @@
val packageMetadata: PackageMetadata = mockk()
every { settingsManager.canDoBackupNow() } returns true
- every { metadataManager.isLegacyFormat } returns false
+ every { metadataManager.requiresInit } returns false
every { settingsManager.getToken() } returns token
every { metadataManager.salt } returns salt
// do actual @pm@ backup