Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Keep an update from leaving the app unable to log in

Lelonio91a446ebc9ad
diff --git a/app/src/main/java/dev/lelonio/square/auth/EngineCredentials.kt b/app/src/main/java/dev/lelonio/square/auth/EngineCredentials.kt
index 84cbcd4..c8d61a9 100644
--- a/app/src/main/java/dev/lelonio/square/auth/EngineCredentials.kt
+++ b/app/src/main/java/dev/lelonio/square/auth/EngineCredentials.kt
@@ -22,9 +22,19 @@ object EngineCredentials {
/** Where librespot keeps its own copy; the native side is given this path. */
fun dir(context: Context): File = context.filesDir.resolve("librespot")
- /** True when the engine has a credential of its own to log in with. */
- fun exist(context: Context): Boolean =
- dir(context).resolve(KEPT).resolve(FILE).isFile
+ /**
+ * True when the engine has a credential of its own to log in with.
+ *
+ * Read, not only found. A file left empty by a process killed while
+ * writing it is there and holds nothing, and taking it for a credential
+ * kept the app out of the login screen with no way in: offline, for good,
+ * with nothing saying why.
+ */
+ fun exist(context: Context): Boolean {
+ val file = dir(context).resolve(KEPT).resolve(FILE)
+ if (!file.isFile || file.length() == 0L) return false
+ return runCatching { org.json.JSONObject(file.readText()) }.isSuccess
+ }
/** Dropped along with the OAuth session, so signing out really signs out. */
fun clear(context: Context) {
diff --git a/native/vendor/README.md b/native/vendor/README.md
index 0ecc623..e29cecf 100644
--- a/native/vendor/README.md
+++ b/native/vendor/README.md
@@ -123,6 +123,15 @@ And a reply of the wrong size is refused rather than trusted. Upstream copies
the key with `copy_from_slice` and reads the error code by index, both of which
panic on a short packet, on the task that reads every packet of the session.
+#### 6. `src/cache.rs`: credentials are replaced, never emptied
+
+Upstream writes the credentials file with `File::create`, which empties it
+before the new contents are written. A process killed between the two, which
+installing an update does, leaves an empty file, and with it a device that
+cannot log in and says only that it is offline. The new contents are written to
+a file beside it and renamed over it, so the old one stays whole until the new
+one is.
+
### Maintenance
Re-apply this patch when bumping `librespot-core`. If the whole file is replaced,
diff --git a/native/vendor/librespot-core/src/cache.rs b/native/vendor/librespot-core/src/cache.rs
index 15e35d2..85cc71a 100644
--- a/native/vendor/librespot-core/src/cache.rs
+++ b/native/vendor/librespot-core/src/cache.rs
@@ -336,10 +336,19 @@ impl Cache {
pub fn save_credentials(&self, cred: &Credentials) {
if let Some(location) = &self.credentials_location {
- let result = File::create(location).and_then(|mut file| {
- let data = serde_json::to_string(cred)?;
- write!(file, "{data}")
- });
+ // LOCAL PATCH: written beside the file and renamed over it.
+ // `File::create` empties the file before a byte of the new one is
+ // written, so a process killed in between, which an app update
+ // does, left an empty file and a device that could no longer log
+ // in. A rename replaces the old file whole or not at all.
+ let staging = location.with_extension("json.tmp");
+ let result = File::create(&staging)
+ .and_then(|mut file| {
+ let data = serde_json::to_string(cred)?;
+ write!(file, "{data}")?;
+ file.sync_all()
+ })
+ .and_then(|_| fs::rename(&staging, location));
if let Err(e) = result {
warn!("Cannot save credentials to cache: {e}")