From 4372ba410524c02aac96ac87d423971ba81c44b0 Mon Sep 17 00:00:00 2001 From: jimmy Date: Wed, 4 Nov 2026 22:25:22 +1300 Subject: [PATCH] fix: recover a collection cut off between remove and rename store_save removes the old file before renaming the finished .tmp, so a restart between the two started that collection empty. Co-Authored-By: Claude Opus 5.5 --- main/store.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main/store.c b/main/store.c index 54294d1..0229523 100644 --- a/main/store.c +++ b/main/store.c @@ -47,6 +47,15 @@ static cJSON *load_file(const char *name) char path[48]; snprintf(path, sizeof(path), "/db/%s.json", name); FILE *f = fopen(path, "rb"); + if (!f) { + // store_save removes the old file before renaming the finished .tmp, so a restart between the two leaves only .tmp + char tmp[48]; + snprintf(tmp, sizeof(tmp), "/db/%s.tmp", name); + if (rename(tmp, path) == 0) { + ESP_LOGW(TAG, "%s: recovered from %s", path, tmp); + f = fopen(path, "rb"); + } + } if (!f) { return cJSON_CreateObject(); }