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 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-04 22:25:22 +13:00
1 parent f47237696e
commit 4372ba4105
1 file changed
+9
+9
View File
@@ -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();
}