Skip to content

Native Android client for Spotify, built with Jetpack Compose

Extensions and lyrics: a package, registry, setting, storage or cache file that cannot be replaced keeps its old copy instead of the old one being deleted first and possibly lost, such as an extension's only package during an update; an install or revert that cannot write the new package now fails and says so [skip ci]

kota-zo4a8b857fec17
diff --git a/base/app/src/main/java/com/music/spotui/data/SafeWrite.kt b/base/app/src/main/java/com/music/spotui/data/SafeWrite.kt
index 3b30f64e..a54b7d89 100644
--- a/base/app/src/main/java/com/music/spotui/data/SafeWrite.kt
+++ b/base/app/src/main/java/com/music/spotui/data/SafeWrite.kt
@@ -10,12 +10,18 @@ import java.io.IOException
* is tried once more with the old file moved aside first, and put back if that fails too: the old
* file is never lost, and an [IOException] says the new text was not saved.
*/
-fun writeTextSafely(file: File, text: String) {
+fun writeTextSafely(file: File, text: String, sync: Boolean = true) = writeBytesSafely(file, text.toByteArray(), sync)
+
+/**
+ * [writeTextSafely] for [bytes]: a [NotReplacedException] when only the move failed (the old file kept).
+ * [sync] false skips the flush to the disk, for frequent writes of what can be lost in a power cut.
+ */
+fun writeBytesSafely(file: File, bytes: ByteArray, sync: Boolean = true) {
file.parentFile?.mkdirs()
val tmp = File(file.parentFile, file.name + ".tmp")
FileOutputStream(tmp).use { out ->
- out.write(text.toByteArray())
- out.fd.sync()
+ out.write(bytes)
+ if (sync) out.fd.sync()
}
if (tmp.renameTo(file)) return
val old = File(file.parentFile, file.name + ".old")
@@ -25,5 +31,8 @@ fun writeTextSafely(file: File, text: String) {
if (old.exists()) old.renameTo(file)
}
tmp.delete()
- throw IOException("could not replace ${file.name}")
+ throw NotReplacedException(file.name)
}
+
+/** The new content was written but could not be moved over the old file, which is kept as it was. */
+class NotReplacedException(name: String) : IOException("could not replace $name")
diff --git a/base/app/src/main/java/com/music/spotui/data/api/LyricsApi.kt b/base/app/src/main/java/com/music/spotui/data/api/LyricsApi.kt
index c372560f..8a756607 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/LyricsApi.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/LyricsApi.kt
@@ -268,9 +268,7 @@ object LyricsApi {
runCatching {
val f = file(key) ?: return
val o = encode(lyrics).put("key", key).put("at", System.currentTimeMillis())
- val tmp = java.io.File(f.parentFile, f.name + ".tmp")
- tmp.writeText(o.toString())
- if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) }
+ com.music.spotui.data.writeTextSafely(f, o.toString(), sync = false)
if (++writes % 20 == 0) prune()
}.onFailure { Log.w("LyricsApi", "could not keep lyrics: ${it.javaClass.simpleName}") }
}
diff --git a/base/app/src/main/java/com/music/spotui/extensions/ExtensionRegistry.kt b/base/app/src/main/java/com/music/spotui/extensions/ExtensionRegistry.kt
index 3ad9c721..7e1cc63a 100644
--- a/base/app/src/main/java/com/music/spotui/extensions/ExtensionRegistry.kt
+++ b/base/app/src/main/java/com/music/spotui/extensions/ExtensionRegistry.kt
@@ -321,9 +321,14 @@ class FailureCounter(private val limit: Int = 3) {
@Synchronized fun reset(id: String) { inARow.remove(id) }
}
+/**
+ * Replaces [file] whole (see [com.music.spotui.data.writeTextSafely]); the old file is never deleted to
+ * make room. A write that fails throws, as before; a move that fails keeps the old file and is only logged.
+ */
internal fun writeAtomically(file: File, text: String) {
- file.parentFile?.mkdirs()
- val tmp = File(file.parentFile, file.name + ".tmp")
- tmp.writeText(text)
- if (!tmp.renameTo(file)) { file.delete(); tmp.renameTo(file) }
+ try {
+ com.music.spotui.data.writeTextSafely(file, text, sync = false)
+ } catch (e: com.music.spotui.data.NotReplacedException) {
+ runCatching { app.myspoti.host.android.Logbook.log("ext: ${e.message}, the old copy kept") }
+ }
}
diff --git a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
index 56e4d4ee..d4daecde 100644
--- a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
+++ b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
@@ -1310,11 +1310,8 @@ object Extensions {
private fun settingsOf(id: String) = settings.getOrPut(id) { FileStore(File(dir, "$id/settings.json")) }
private fun storeOf(id: String) = stores.getOrPut(id) { FileStore(File(dir, "$id/storage.json")) }
- internal fun writeBytesAtomically(file: File, bytes: ByteArray) {
- val tmp = File(file.parentFile, file.name + ".tmp")
- tmp.writeBytes(bytes)
- if (!tmp.renameTo(file)) { file.delete(); tmp.renameTo(file) }
- }
+ /** Replaces [file] whole or throws, never losing the old one (see [com.music.spotui.data.writeBytesSafely]). */
+ internal fun writeBytesAtomically(file: File, bytes: ByteArray) = com.music.spotui.data.writeBytesSafely(file, bytes)
// ── What the sandbox reaches of the app ──
diff --git a/base/app/src/test/java/com/music/spotui/data/SafeWriteTest.kt b/base/app/src/test/java/com/music/spotui/data/SafeWriteTest.kt
index 99930a5c..ef947eb9 100644
--- a/base/app/src/test/java/com/music/spotui/data/SafeWriteTest.kt
+++ b/base/app/src/test/java/com/music/spotui/data/SafeWriteTest.kt
@@ -20,6 +20,17 @@ class SafeWriteTest {
dir.deleteRecursively()
}
+ @Test
+ fun replacesBytesWholeAndKeepsNoOldCopy() {
+ val dir = Files.createTempDirectory("safe").toFile()
+ val f = File(dir, "package.zip")
+ f.writeBytes(byteArrayOf(1, 2))
+ writeBytesSafely(f, byteArrayOf(3, 4, 5), sync = false)
+ assertEquals(listOf<Byte>(3, 4, 5), f.readBytes().toList())
+ assertEquals(listOf("package.zip"), dir.list()!!.toList())
+ dir.deleteRecursively()
+ }
+
@Test
fun createsMissingFolders() {
val dir = Files.createTempDirectory("safe").toFile()