diff options
Diffstat (limited to 'app/src/main')
| -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 { |