repeater-observer hybrid fixup

This commit is contained in:
liquidraver committed 2026-07-10 22:43:26 +02:00
1 parent 983e7a198c
commit 56e41284e1
7 files changed
+148 -73

No files matched your search

+22 -5
View File
@@ -45,24 +45,41 @@ extern "C" bool observer_creds_load(struct ObserverCreds *creds,
extern "C" bool observer_creds_save(const struct ObserverCreds *creds,
const char *base_path)
{
/* Atomic replace (.tmp + fs_sync + fs_rename), same pattern as
* identity/prefs — power loss mid-write must not corrupt the creds. */
char path[96];
char tmp_path[104];
snprintf(path, sizeof(path), "%s/obs_creds", base_path);
if (snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path) >=
(int)sizeof(tmp_path)) {
return false;
}
fs_unlink(tmp_path);
struct fs_file_t f;
fs_file_t_init(&f);
int rc = fs_open(&f, path, FS_O_WRITE | FS_O_CREATE | FS_O_TRUNC);
int rc = fs_open(&f, tmp_path, FS_O_WRITE | FS_O_CREATE | FS_O_TRUNC);
if (rc < 0) {
LOG_ERR("Cannot open %s for write: %d", path, rc);
LOG_ERR("Cannot open %s for write: %d", tmp_path, rc);
return false;
}
ssize_t n = fs_write(&f, creds, sizeof(*creds));
rc = fs_sync(&f);
fs_close(&f);
if (n != (ssize_t)sizeof(*creds)) {
LOG_ERR("obs_creds write failed (%d/%d)", (int)n,
(int)sizeof(*creds));
if (n != (ssize_t)sizeof(*creds) || rc < 0) {
LOG_ERR("obs_creds write failed (%d/%d sync=%d)", (int)n,
(int)sizeof(*creds), rc);
fs_unlink(tmp_path);
return false;
}
if (fs_rename(tmp_path, path) < 0) {
LOG_ERR("obs_creds rename failed");
fs_unlink(tmp_path);
return false;
}