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(); }