From 79777a7b6faa359a0b9b62b537811a1e44cc40e0 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 23 Sep 2021 16:37:49 +0200 Subject: [PATCH] Close K/V DB after restoring records also don't log stack trace when not finding backup type --- .../seedvault/transport/backup/KVDbManager.kt | 3 +- .../seedvault/transport/restore/KVRestore.kt | 30 +++++++++++-------- .../transport/restore/RestoreCoordinator.kt | 11 +++++-- .../transport/restore/KVRestoreTest.kt | 4 ++- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVDbManager.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVDbManager.kt index f6d03e72..95a026fb 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVDbManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/KVDbManager.kt @@ -64,13 +64,12 @@ class KvDbManagerImpl(private val context: Context) : KvDbManager { } } -interface KVDb { +interface KVDb : AutoCloseable { fun put(key: String, value: ByteArray) fun get(key: String): ByteArray? fun getAll(): List> fun delete(key: String) fun vacuum() - fun close() } class KVDbImpl(context: Context, fileName: String) : diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/KVRestore.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/KVRestore.kt index ed55267d..65a6c4d7 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/KVRestore.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/KVRestore.kt @@ -98,20 +98,26 @@ internal class KVRestore( val isAutoRestore = state.packageInfo.packageName == MAGIC_PACKAGE_MANAGER && pmPackageName != null return try { - val db = if (isAutoRestore) getCachedRestoreDb(state) else downloadRestoreDb(state) - val out = outputFactory.getBackupDataOutput(data) - val records = if (isAutoRestore) { - val keys = listOf(ANCESTRAL_RECORD_KEY, GLOBAL_METADATA_KEY, pmPackageName) - Log.d(TAG, "Single package restore, restrict restore keys to $pmPackageName") - db.getAll().filter { it.first in keys } + val database = if (isAutoRestore) { + getCachedRestoreDb(state) } else { - db.getAll() + downloadRestoreDb(state) } - records.sortedBy { it.first }.forEach { (key, value) -> - val size = value.size - Log.v(TAG, " ... key=$key size=$size") - out.writeEntityHeader(key, size) - out.writeEntityData(value, size) + database.use { db -> + val out = outputFactory.getBackupDataOutput(data) + val records = if (isAutoRestore) { + val keys = listOf(ANCESTRAL_RECORD_KEY, GLOBAL_METADATA_KEY, pmPackageName) + Log.d(TAG, "Single package restore, restrict restore keys to $pmPackageName") + db.getAll().filter { it.first in keys } + } else { + db.getAll() + } + records.sortedBy { it.first }.forEach { (key, value) -> + val size = value.size + Log.v(TAG, " ... key=$key size=$size") + out.writeEntityHeader(key, size) + out.writeEntityData(value, size) + } } TRANSPORT_OK } catch (e: UnsupportedVersionException) { diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt index 9d700d5e..ea5df7db 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestoreCoordinator.kt @@ -225,9 +225,16 @@ internal class RestoreCoordinator( full.initializeState(version, state.token, name, packageInfo) state.currentPackage = packageName TYPE_FULL_STREAM - } else throw IOException("No data found for $packageName. Skipping.") + } else throw IOException("No data found for $packageName. Skipping...") + } + null -> { + Log.i(TAG, "No backup type found for $packageName. Skipping...") + state.backupMetadata.packageMetadataMap[packageName]?.backupType?.let { s -> + Log.w(TAG, "State was ${s.name}") + } + failedPackages.add(packageName) + return nextRestorePackage() } - null -> throw IOException("No backup type found for $packageName. Skipping.") } } catch (e: IOException) { Log.e(TAG, "Error finding restore data for $packageName.", e) diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/restore/KVRestoreTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/restore/KVRestoreTest.kt index 7ab12f90..678d6b63 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/restore/KVRestoreTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/restore/KVRestoreTest.kt @@ -11,9 +11,9 @@ import com.stevesoltys.seedvault.header.VERSION import com.stevesoltys.seedvault.header.VersionHeader import com.stevesoltys.seedvault.header.getADForKV import com.stevesoltys.seedvault.plugins.LegacyStoragePlugin +import com.stevesoltys.seedvault.plugins.StoragePlugin import com.stevesoltys.seedvault.transport.backup.KVDb import com.stevesoltys.seedvault.transport.backup.KvDbManager -import com.stevesoltys.seedvault.plugins.StoragePlugin import io.mockk.Runs import io.mockk.coEvery import io.mockk.every @@ -148,6 +148,7 @@ internal class KVRestoreTest : RestoreTest() { every { output.writeEntityHeader(key2, data2.size) } returns 42 every { output.writeEntityData(data2, data2.size) } returns data2.size + every { db.close() } just Runs every { dbManager.deleteDb(packageInfo.packageName, true) } returns true streamsGetClosed() @@ -159,6 +160,7 @@ internal class KVRestoreTest : RestoreTest() { output.writeEntityData(data, data.size) output.writeEntityHeader(key2, data2.size) output.writeEntityData(data2, data2.size) + db.close() dbManager.deleteDb(packageInfo.packageName, true) } }