appstore: wear staging APKs are swept instead of leaking forever
- Commit
62b653f1ba1dac34ba90455c2a0d2ccb862985af- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 21:21 UTC (5 days ago)
- Parents
be3776d
Every app ever sent to a watch left one full APK (up to 200 MB) in cacheDir/wear/ permanently: stageApk downloads there, sendToWatch streams but never deletes, and the #871 startup/worker sweep only covered root *.apk, PlayDownloads/ and fdroid-index/ — the wear staging directory was created outside that cleanup funnel, so growth was unbounded in packages sent over the app's lifetime. DownloadCache.sweepStale (the one layer every cache lifetime already flows through, run at phone/TV startup and from UpdateCheckWorker) now also deletes wear/*.apk older than the existing 24 h cutoff. The age gate preserves stageApk's deliberate re-send reuse: fresh staged APKs still re-send without re-downloading. Regression test: stale wear APK reclaimed, fresh one kept. Update detection, F-Droid index parsing, install verification and legacy-repo pruning were audited and cleared. Verified: :appstore:testDevUnitTest 500 tests pass; lint errors are pre-existing on untouched files.
Diff
appstore/src/main/java/com/vayunmathur/appstore/data/DownloadCache.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/DownloadCache.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/DownloadCache.ktindex 235e6dc410..10a88a285c 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/DownloadCache.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/DownloadCache.kt@@ -72,9 +72,10 @@ object DownloadCache { /** * Delete crash leftovers older than [staleAfterMs] (default [STALE_AFTER_MS]): * root `*.apk` files, `*.tmp` resume files under `PlayDownloads/`, version- * dirs under `PlayDownloads/<pkg>/` with nothing fresh in them, and+ * dirs under `PlayDownloads/<pkg>/` with nothing fresh in them, * `fdroid-index/` work dirs (normally deleted in a `finally`, orphaned by- * a crash mid-sync). Returns the number of files/dirs removed.+ * a crash mid-sync), and staged APKs under `wear/` (see below). Returns+ * the number of files/dirs removed. * * Small verification downloads (`fdroid-verified.json`, * `izzy-verified-*.json`) are deliberately left alone: they are kilobytes,@@ -108,6 +109,22 @@ object DownloadCache { removed++ } }+ // Watch staging: WearSyncManager.stageApk downloads each watch-sent+ // APK once into wear/<package>.apk (up to the 200 MB watch limit) and+ // reuses it on a re-send, but nothing ever deletes it afterwards.+ // Without this, every distinct package ever sent leaves a full APK+ // behind forever — the same unbounded growth as #871, in the one+ // staging dir the original sweep did not cover. Age-gated like the+ // rest, so a re-send shortly after the first send still reuses it.+ val wearRoot = File(cacheDir, "wear")+ wearRoot.listFiles { file ->+ file.isFile && file.name.endsWith(".apk") && file.lastModified() < cutoff+ }?.forEach { if (runCatching { it.delete() }.getOrDefault(false)) removed++ }+ if (wearRoot.isDirectory && wearRoot.listFiles()?.isEmpty() == true &&+ runCatching { wearRoot.delete() }.getOrDefault(false)+ ) {+ removed+++ } // Index-sync work dirs are deleted in a finally on the normal path; a // crash between mkdirs and the finally leaves them behind (tens of MB). val indexRoot = File(cacheDir, "fdroid-index")
appstore/src/test/java/com/vayunmathur/appstore/data/DownloadCacheTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/DownloadCacheTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/DownloadCacheTest.ktindex 6e13d7dc93..d748798b33 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/DownloadCacheTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/DownloadCacheTest.kt@@ -149,6 +149,27 @@ class DownloadCacheTest { assertFalse(root.exists()) } + @Test+ fun `sweepStale deletes old wear staging apks but keeps fresh ones`() {+ // WearSyncManager.stageApk downloads each watch-sent APK once into+ // wear/<package>.apk and reuses it on a re-send. Nothing else ever+ // deletes those files, so the startup sweep is the only thing that+ // can stop one full APK per package ever sent accumulating forever.+ val wearDir = File(cacheDir, "wear").apply { mkdirs() }+ val old = File(wearDir, "com.example.old.apk").apply { writeBytes(ByteArray(8)) }+ old.setLastModified(1000L)+ val now = 1000L + DownloadCache.STALE_AFTER_MS + 1+ // Sent just now: a re-send this process may still make, not debris.+ val fresh = File(wearDir, "com.example.new.apk").apply { writeBytes(ByteArray(8)) }+ fresh.setLastModified(now)++ val removed = DownloadCache.sweepStale(cacheDir, nowMs = now)++ assertEquals(1, removed)+ assertFalse(old.exists())+ assertTrue(fresh.exists())+ }+ @Test fun `sweepStale ignores small verification downloads`() { val verified = File(cacheDir, "fdroid-verified.json").apply { writeBytes(ByteArray(8)) }