diff options
| author | Lena <lena@omega> | 2026-07-01 00:00:00 +0000 |
|---|---|---|
| committer | Lena <lena@omega> | 2026-07-01 00:00:00 +0000 |
| commit | d804eb4399850fe828462b3a1832a4c9df1541f8 (patch) | |
| tree | 7af81040081cc7cded2c5a2a836dee9be445e0fc | |
| parent | d2745c0cca84ea006fa44edb4d9bbebffd35d917 (diff) | |
| download | rsend-d804eb4399850fe828462b3a1832a4c9df1541f8.tar.gz | |
app: write config atomically
A crash mid-write corrupted config.json, and every later load then
threw on parse, crash-looping the app until its data was cleared,
losing the key and pinned host. Write to a temp file and rename.
| -rw-r--r-- | app/src/main/java/invalid/lena/rsend/Config.kt | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/app/src/main/java/invalid/lena/rsend/Config.kt b/app/src/main/java/invalid/lena/rsend/Config.kt index 8c9c22c..d7f17eb 100644 --- a/app/src/main/java/invalid/lena/rsend/Config.kt +++ b/app/src/main/java/invalid/lena/rsend/Config.kt @@ -4,6 +4,7 @@ import android.content.Context import org.json.JSONArray import org.json.JSONObject import java.io.File +import java.io.IOException // Config is rsend's whole state: the remote target, the schedule, and the // folders to push. It is stored as plain JSON in app-private storage. @@ -64,8 +65,13 @@ data class Config( return fromJson(JSONObject(f.readText())) } + // save writes tmp-then-rename so a crash mid-write cannot corrupt the + // config and brick every later load. fun save(ctx: Context, c: Config) { - file(ctx).writeText(c.toJson().toString(2)) + val f = file(ctx) + val tmp = File(f.path + ".tmp") + tmp.writeText(c.toJson().toString(2)) + if (!tmp.renameTo(f)) throw IOException("rename ${tmp.path} failed") } fun fromJson(o: JSONObject): Config { |