Skip to content

Native Android client for Spotify, built with Jetpack Compose

Downloads: songs downloaded before encryption are encrypted one at a time while no account switch moves the downloads folder, and the encryption stops once it has moved (the rest is done at the next launch), so a song of another account with the same file name is never encrypted a second time and made unplayable [skip ci]

kota-zof722a3c8c6c0
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.kt
index c4f4d67f..2d0c42f8 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
@@ -37,7 +37,7 @@ object OfflineAccounts {
// Whose they are is unknown (never signed in online since): kept aside, never given to anyone.
val key = owner ?: "unclaimed-${System.currentTimeMillis()}"
stopDownloads(dir)
- val moved = withContext(Dispatchers.IO) { putAside(dir, parked(context, key)) }
+ val moved = withContext(Dispatchers.IO) { OfflineStore.moving { putAside(dir, parked(context, key)) } }
if (!moved) {
// Still in place and still stamped with their account: the next sign-in puts them aside.
Logbook.log("offline: downloads not put aside for sign-out")
@@ -59,18 +59,7 @@ object OfflineAccounts {
}
// Another account's downloads are in place (a sign-out that could not put them aside), or none.
stopDownloads(dir)
- val ready = withContext(Dispatchers.IO) {
- // This account's stamp only goes on an emptied folder or its own: downloads that could not
- // be moved keep their own account's stamp, never this one.
- val cleared = if (owner != null) putAside(dir, parked(context, owner)) else dir.deleteRecursively()
- if (!cleared) return@withContext Claimed.NOT_CLEARED
- val back = parked(context, key)
- // Not stamped when its own folder did not come back: an empty folder with its stamp would
- // read as its downloads in place, and the folder put aside would never be tried again.
- if (back.isDirectory && !(back.renameTo(dir) && dir.isDirectory)) return@withContext Claimed.NOT_BROUGHT_BACK
- stamp(dir, key)
- Claimed.READY
- }
+ val ready = withContext(Dispatchers.IO) { OfflineStore.moving { swap(context, dir, owner, key) } }
if (ready != Claimed.READY) {
Logbook.log(
if (ready == Claimed.NOT_CLEARED) "offline: the other account's downloads could not be put aside"
@@ -88,6 +77,20 @@ object OfflineAccounts {
private enum class Claimed { READY, NOT_CLEARED, NOT_BROUGHT_BACK }
+ /** The downloads of [owner] (or none) in [dir] go aside, and those of [key] come back in their place. */
+ private fun swap(context: Context, dir: File, owner: String?, key: String): Claimed {
+ // This account's stamp only goes on an emptied folder or its own: downloads that could not
+ // be moved keep their own account's stamp, never this one.
+ val cleared = if (owner != null) putAside(dir, parked(context, owner)) else dir.deleteRecursively()
+ if (!cleared) return Claimed.NOT_CLEARED
+ val back = parked(context, key)
+ // Not stamped when its own folder did not come back: an empty folder with its stamp would
+ // read as its downloads in place, and the folder put aside would never be tried again.
+ if (back.isDirectory && !(back.renameTo(dir) && dir.isDirectory)) return Claimed.NOT_BROUGHT_BACK
+ stamp(dir, key)
+ return Claimed.READY
+ }
+
/**
* Moves [dir] to [to]. A folder already there (left by an earlier sign-out that went wrong) is
* kept under another name rather than deleted. False when the move failed: [dir] is untouched.
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.kt
index cb18099c..3a50dc69 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
@@ -95,13 +95,31 @@ object OfflineStore {
fun init(context: Context) {
if (::root.isInitialized) return
val plain = load(File(context.filesDir, "offline"))
- // Songs downloaded before encryption existed: encrypted once, in the background.
+ // Songs downloaded before encryption existed: encrypted once, in the background. One file at a
+ // time under [folderLock], and not after the folder was moved (another account's in its place,
+ // where a file of the same name would be read as this one's): what is left goes at the next launch.
+ val start = moves
if (plain.isNotEmpty()) Thread {
- plain.forEach { f -> runCatching { OfflineCrypto.encryptInPlace(context.applicationContext, f) }
- .onFailure { android.util.Log.w("Offline", "could not encrypt ${f.name}: ${it.message}") } }
+ for (f in plain) {
+ val done = synchronized(folderLock) {
+ if (moves != start) return@synchronized false
+ runCatching { OfflineCrypto.encryptInPlace(context.applicationContext, f) }
+ .onFailure { android.util.Log.w("Offline", "could not encrypt ${f.name}: ${it.message}") }
+ true
+ }
+ if (!done) break
+ }
}.apply { name = "offline-encrypt"; priority = Thread.MIN_PRIORITY }.start()
}
+ /** Held while a downloaded file is encrypted in place, and while the downloads folder is moved. */
+ private val folderLock = Any()
+ /** How many times the downloads folder was moved (see [moving]). */
+ @Volatile private var moves = 0
+
+ /** Runs [block], which moves the downloads folder (OfflineAccounts), never during a file's encryption. */
+ internal fun <T> moving(block: () -> T): T = synchronized(folderLock) { moves++; block() }
+
/** Reads the downloads folder again (main thread): another account's downloads were put in its place. */
fun reopen(context: Context) {
status.clear()