Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Downloads: a damaged download index read from its older copy (or not read at all, or only in part) no longer deletes the songs that copy does not list, such as those added since: their files are kept aside in a recovered folder that nothing deletes, only the index itself read in full sweeps leftovers, and a downloads folder with such files is never taken for an empty one at sign-in [skip ci]
kota-zoe1b743450572
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineAccounts.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineAccounts.ktindex 11eb65f3..3e0bde9a 100644--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineAccounts.kt+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineAccounts.kt@@ -98,7 +98,9 @@ object OfflineAccounts {private fun hasDownloads(dir: File): Boolean =File(dir, "index.json").length() > 2 || File(dir, "collections.json").length() > 2 ||- File(dir, "pending.json").length() > 2 || File(dir, "podcasts-pending.json").length() > 2+ File(dir, "pending.json").length() > 2 || File(dir, "podcasts-pending.json").length() > 2 ||+ // Songs whose index was lost (only the older copy, or none, could be read) are downloads too.+ File(dir, "audio").list()?.isNotEmpty() == true || File(dir, "recovered").exists()/** Running downloads stop; the queues on disk are kept as they were, so they resume with their account. */private suspend fun stopDownloads(dir: File) {diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.ktindex 4a1b880c..cb18099c 100644--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.kt+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineStore.kt@@ -115,7 +115,10 @@ object OfflineStore {audioDir.mkdirs(); coverDir.mkdirs()// The index lists only finished files; anything else in the folders is a leftover.// An index that cannot be read fully is never trusted to delete anything: the last- // good copy is used instead, and if that fails too the files are all kept.+ // good copy is used instead, and if that fails too the files are all kept. Only the+ // index itself, read in full, sweeps leftovers: the last good copy is older and lacks+ // the songs added since, so with it (or with no index) the files it does not list are+ // moved to the recovered folder, which nothing deletes, rather than swept by a later load.val read = readIndex(indexFile) ?: run {if (indexFile.exists()) {indexFile.copyTo(File(root, "index.corrupt-${System.currentTimeMillis()}.json"), overwrite = true)@@ -126,21 +129,38 @@ object OfflineStore {val list = read?.tracks.orEmpty()val kept = list.filter { File(audioDir, it.file).isFile }kept.forEach { tracks[it.trackId] = it }- if (read != null && read.complete) {- if (kept.size != list.size || read.file == backupFile) writeIndex()- val names = kept.map { it.file }.toSet()+ val names = kept.map { it.file }.toSet()+ val covers = kept.map { it.cover }.toSet()+ if (read != null && read.complete && read.file == indexFile) {+ if (kept.size != list.size) writeIndex()audioDir.listFiles()?.forEach { if (it.name !in names) it.delete() }- val covers = kept.map { it.cover }.toSet()coverDir.listFiles()?.forEach { if (it.name !in covers) it.delete() }+ } else {+ val aside = keepAside(audioDir, names, File(recoveredDir, "audio")) + keepAside(coverDir, covers, File(recoveredDir, "covers"))+ if (aside > 0) runCatching { app.myspoti.host.android.Logbook.log("offline: download index not read in full; $aside files the index does not list kept in ${recoveredDir.name}/") }+ if (read != null && read.complete) writeIndex()}return kept.map { File(audioDir, it.file) }}+ /**+ * Moves the files of [from] not in [listed] to [to], under another name when one is in the way;+ * returns how many. A file that cannot be moved stays where it is.+ */+ private fun keepAside(from: File, listed: Set<String>, to: File): Int =+ from.listFiles()?.filter { it.name !in listed }?.count { f ->+ to.mkdirs()+ val target = File(to, f.name).takeUnless { it.exists() } ?: File(to, "${System.currentTimeMillis()}-${f.name}")+ f.renameTo(target)+ } ?: 0+val audioDir: File get() = File(root, "audio")val coverDir: File get() = File(root, "covers")private val indexFile: File get() = File(root, "index.json")/** The index before the last write, the fallback when [indexFile] is damaged. */private val backupFile: File get() = File(root, "index.json.bak")+ /** Files an index read only in part (or its older copy) did not list: kept, never swept. */+ internal val recoveredDir: File get() = File(root, "recovered")internal val pendingFile: File get() = File(root, "pending.json")/** The podcast queue: kept apart so a restart resumes episodes without touching music. */internal val podcastPendingFile: File get() = File(root, "podcasts-pending.json")diff --git a/base/app/src/test/java/com/music/spotui/data/offline/OfflineIndexTest.kt b/base/app/src/test/java/com/music/spotui/data/offline/OfflineIndexTest.ktindex 817a7a61..a1f36c94 100644--- a/base/app/src/test/java/com/music/spotui/data/offline/OfflineIndexTest.kt+++ b/base/app/src/test/java/com/music/spotui/data/offline/OfflineIndexTest.kt@@ -31,8 +31,8 @@ class OfflineIndexTest {@Test fun truncatedIndexKeepsEveryFile() {val root = folder("[{\"id\":\"a\",\"file\":\"a.m4a\"", "a", "b")OfflineStore.load(root)- assertEquals(2, File(root, "audio").list()!!.size)- assertEquals(2, File(root, "covers").list()!!.size)+ assertEquals(2, kept(root, "audio"))+ assertEquals(2, kept(root, "covers"))assertTrue(root.list()!!.any { it.startsWith("index.corrupt-") })}@@ -40,7 +40,31 @@ class OfflineIndexTest {val root = folder(JSONArray().put(entry("a")).put(JSONObject().put("title", "no id")).toString(), "a", "b")OfflineStore.load(root)assertEquals(setOf("a"), OfflineStore.tracks.keys.toSet())- assertEquals(2, File(root, "audio").list()!!.size)+ assertEquals(2, kept(root, "audio"))+ }++ /** Files still on the phone: in place, or kept aside in the recovered folder. */+ private fun kept(root: File, sub: String) =+ File(root, sub).list()!!.size + (File(root, "recovered/$sub").list()?.size ?: 0)++ @Test fun damagedIndexWithAnOlderBackupKeepsTheSongsAddedSince() {+ val root = folder(null)+ OfflineStore.load(root)+ listOf("a", "b").forEach { id ->+ File(root, "audio/$id.m4a").writeText("x"); File(root, "covers/$id.jpg").writeText("x")+ OfflineStore.add(OfflineTrack.fromJson(entry(id)))+ }+ // The index lists a and b, its backup only a; then the index is damaged.+ File(root, "index.json").writeText("{oops")+ OfflineStore.load(root)+ assertEquals(setOf("a"), OfflineStore.tracks.keys.toSet())+ assertTrue(File(root, "audio/a.m4a").exists())+ assertTrue(File(root, "recovered/audio/b.m4a").exists())+ assertTrue(File(root, "recovered/covers/b.jpg").exists())+ // The index rewritten from the backup, read in full, sweeps nothing that was kept aside.+ OfflineStore.load(root)+ assertTrue(File(root, "recovered/audio/b.m4a").exists())+ root.deleteRecursively()}@Test fun backupIsUsedWhenTheIndexIsDamaged() {