Move backup of APKs of opt-out apps to after uploading @pm@ DB

This commit is contained in:
Torsten Grote 2021-09-23 09:26:48 +02:00 committed by Chirayu Desai
parent a0f3c6b45f
commit 1885021c1c
3 changed files with 31 additions and 19 deletions

View file

@ -270,13 +270,7 @@ internal class BackupCoordinator(
} }
val token = settingsManager.getToken() ?: error("no token in performFullBackup") val token = settingsManager.getToken() ?: error("no token in performFullBackup")
val salt = metadataManager.salt val salt = metadataManager.salt
val result = kv.performBackup(packageInfo, data, flags, token, salt) return kv.performBackup(packageInfo, data, flags, token, salt)
if (result == TRANSPORT_OK && packageName == MAGIC_PACKAGE_MANAGER) {
// TODO move to finish backup of @pm@ so we can upload the DB before
// hook in here to back up APKs of apps that are otherwise not allowed for backup
backUpApksOfNotBackedUpPackages()
}
return result
} }
// ------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------
@ -392,10 +386,15 @@ internal class BackupCoordinator(
"K/V backup has state, but full backup has dangling state as well" "K/V backup has state, but full backup has dangling state as well"
} }
// getCurrentPackage() not-null because we have state // getCurrentPackage() not-null because we have state
onPackageBackedUp(kv.getCurrentPackage()!!, BackupType.KV) val packageInfo = kv.getCurrentPackage()!!
val isPmBackup = kv.getCurrentPackage()!!.packageName == MAGIC_PACKAGE_MANAGER onPackageBackedUp(packageInfo, BackupType.KV)
kv.finishBackup() val isPmBackup = packageInfo.packageName == MAGIC_PACKAGE_MANAGER
// TODO move @pm@ backup hook here val result = kv.finishBackup()
// hook in here to back up APKs of apps that are otherwise not allowed for backup
if (result == TRANSPORT_OK && isPmBackup) {
backUpApksOfNotBackedUpPackages()
}
result
} }
full.hasState() -> { full.hasState() -> {
check(!kv.hasState()) { check(!kv.hasState()) {

View file

@ -186,20 +186,19 @@ internal class KVBackup(
if (!dbManager.deleteDb(packageInfo.packageName)) throw IOException() if (!dbManager.deleteDb(packageInfo.packageName)) throw IOException()
} }
@Throws(IOException::class)
suspend fun finishBackup(): Int { suspend fun finishBackup(): Int {
val state = this.state ?: error("No state in finishBackup") val state = this.state ?: error("No state in finishBackup")
val packageName = state.packageInfo.packageName val packageName = state.packageInfo.packageName
Log.i(TAG, "Finish K/V Backup of $packageName") Log.i(TAG, "Finish K/V Backup of $packageName")
try { return try {
if (state.needsUpload) uploadDb(state.token, state.name, packageName, state.db) if (state.needsUpload) uploadDb(state.token, state.name, packageName, state.db)
TRANSPORT_OK
} catch (e: IOException) { } catch (e: IOException) {
return TRANSPORT_ERROR TRANSPORT_ERROR
} finally { } finally {
this.state = null this.state = null
} }
return TRANSPORT_OK
} }
/** /**
@ -235,11 +234,10 @@ internal class KVBackup(
dbManager.getDbInputStream(packageName).use { inputStream -> dbManager.getDbInputStream(packageName).use { inputStream ->
inputStream.copyTo(gZipStream) inputStream.copyTo(gZipStream)
} }
// TODO remove log
Log.d(TAG, "=> Uploaded db file for $packageName")
} }
} }
} }
Log.d(TAG, "Uploaded db file for $packageName")
} }
private class KVOperation( private class KVOperation(

View file

@ -402,7 +402,7 @@ internal class BackupCoordinatorTest : BackupTest() {
} }
@Test @Test
fun `not allowed apps get their APKs backed up during @pm@ backup`() = runBlocking { fun `not allowed apps get their APKs backed up after @pm@ backup`() = runBlocking {
val packageInfo = PackageInfo().apply { packageName = MAGIC_PACKAGE_MANAGER } val packageInfo = PackageInfo().apply { packageName = MAGIC_PACKAGE_MANAGER }
val notAllowedPackages = listOf( val notAllowedPackages = listOf(
PackageInfo().apply { packageName = "org.example.1" }, PackageInfo().apply { packageName = "org.example.1" },
@ -422,6 +422,21 @@ internal class BackupCoordinatorTest : BackupTest() {
coEvery { coEvery {
kv.performBackup(packageInfo, fileDescriptor, 0, token, salt) kv.performBackup(packageInfo, fileDescriptor, 0, token, salt)
} returns TRANSPORT_OK } returns TRANSPORT_OK
assertEquals(
TRANSPORT_OK,
backup.performIncrementalBackup(packageInfo, fileDescriptor, 0)
)
// finish @pm@ backup
every { kv.hasState() } returns true
every { full.hasState() } returns false
every { kv.getCurrentPackage() } returns pmPackageInfo
every {
metadataManager.onPackageBackedUp(pmPackageInfo, BackupType.KV, metadataOutputStream)
} just Runs
coEvery { kv.finishBackup() } returns TRANSPORT_OK
// now check if we have opt-out apps that we need to back up APKs for // now check if we have opt-out apps that we need to back up APKs for
every { packageService.notBackedUpPackages } returns notAllowedPackages every { packageService.notBackedUpPackages } returns notAllowedPackages
// update notification // update notification
@ -465,7 +480,7 @@ internal class BackupCoordinatorTest : BackupTest() {
} just Runs } just Runs
every { metadataOutputStream.close() } just Runs every { metadataOutputStream.close() } just Runs
assertEquals(TRANSPORT_OK, backup.performIncrementalBackup(packageInfo, fileDescriptor, 0)) assertEquals(TRANSPORT_OK, backup.finishBackup())
coVerify { coVerify {
apkBackup.backupApkIfNecessary(notAllowedPackages[0], NOT_ALLOWED, any()) apkBackup.backupApkIfNecessary(notAllowedPackages[0], NOT_ALLOWED, any())