From ae77ebed8f37316317941f48dd9542d055dcac69 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 8 Nov 2024 10:41:18 -0300 Subject: [PATCH] Remove known bad blobs from do-not-use list when deleting them --- .../java/com/stevesoltys/seedvault/repo/Pruner.kt | 15 +++++++++++---- .../com/stevesoltys/seedvault/repo/PrunerTest.kt | 6 +++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/repo/Pruner.kt b/app/src/main/java/com/stevesoltys/seedvault/repo/Pruner.kt index 4284e292..424e173c 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/repo/Pruner.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/repo/Pruner.kt @@ -23,6 +23,7 @@ internal class Pruner( private val crypto: Crypto, private val backendManager: BackendManager, private val snapshotManager: SnapshotManager, + private val blobCache: BlobCache, ) { private val log = KotlinLogging.logger {} @@ -77,11 +78,17 @@ internal class Pruner( blob.id.hexFromProto() } }.toSet() - blobHandles.forEach { blobHandle -> - if (blobHandle.name !in usedBlobIds) { - log.info { "Removing blob ${blobHandle.name}" } - backendManager.backend.remove(blobHandle) + val removedBlobs = mutableSetOf() + try { + blobHandles.forEach { blobHandle -> + if (blobHandle.name !in usedBlobIds) { + log.info { "Removing blob ${blobHandle.name}" } + backendManager.backend.remove(blobHandle) + removedBlobs.add(blobHandle.name) + } } + } finally { + if (removedBlobs.isNotEmpty()) blobCache.onBlobsRemoved(removedBlobs) } } diff --git a/app/src/test/java/com/stevesoltys/seedvault/repo/PrunerTest.kt b/app/src/test/java/com/stevesoltys/seedvault/repo/PrunerTest.kt index 0b40708b..f200bf08 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/repo/PrunerTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/repo/PrunerTest.kt @@ -33,9 +33,10 @@ internal class PrunerTest : TransportTest() { private val backendManager: BackendManager = mockk() private val snapshotManager: SnapshotManager = mockk() + private val blobCache: BlobCache = mockk() private val backend: Backend = mockk() - private val pruner = Pruner(crypto, backendManager, snapshotManager) + private val pruner = Pruner(crypto, backendManager, snapshotManager, blobCache) private val folder = TopLevelFolder(repoId) private val snapshotHandle1 = @@ -84,6 +85,7 @@ internal class PrunerTest : TransportTest() { blobIds.forEach { coEvery { backend.remove(AppBackupFileType.Blob(repoId, it)) } just Runs } + every { blobCache.onBlobsRemoved(blobIds.toSet()) } just Runs pruner.removeOldSnapshotsAndPruneUnusedBlobs() } @@ -143,12 +145,14 @@ internal class PrunerTest : TransportTest() { // now extra blobs will get removed coEvery { backend.remove(AppBackupFileType.Blob(repoId, blob1)) } just Runs coEvery { backend.remove(AppBackupFileType.Blob(repoId, blob2)) } just Runs + every { blobCache.onBlobsRemoved(setOf(blob1, blob2)) } just Runs pruner.removeOldSnapshotsAndPruneUnusedBlobs() coVerify { backend.remove(AppBackupFileType.Blob(repoId, blob1)) backend.remove(AppBackupFileType.Blob(repoId, blob2)) + blobCache.onBlobsRemoved(setOf(blob1, blob2)) } }