Skip to content

Commit 3dd2cef

Browse files
committed
Only apply to devices with sufficient storage
1 parent 2688f4e commit 3dd2cef

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

examples/companion_radio/DataStore.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
#define MAX_BLOBRECS 20
88
#endif
99

10+
// Atomic writes require ~2x storage for contacts file
11+
// Only enable on platforms with sufficient flash
12+
#if !defined(NRF52_PLATFORM) || defined(EXTRAFS) || defined(QSPIFLASH)
13+
#define HAS_ATOMIC_WRITE_SUPPORT
14+
#endif
15+
1016
DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(nullptr), _clock(&clock),
1117
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
1218
identity_store(fs, "")
@@ -270,6 +276,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
270276
FILESYSTEM* fs = _getContactsChannelsFS();
271277
File file = openRead(fs, "/contacts3");
272278

279+
#ifdef HAS_ATOMIC_WRITE_SUPPORT
273280
// If main file doesn't exist or is empty, try backup
274281
if (!file || file.size() == 0) {
275282
if (file) file.close();
@@ -278,6 +285,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
278285
file = openRead(fs, "/contacts3.bak");
279286
}
280287
}
288+
#endif
281289

282290
if (file) {
283291
bool full = false;
@@ -311,8 +319,12 @@ void DataStore::loadContacts(DataStoreHost* host) {
311319
void DataStore::saveContacts(DataStoreHost* host) {
312320
FILESYSTEM* fs = _getContactsChannelsFS();
313321

314-
// Write to temp file first (atomic write pattern)
322+
#ifdef HAS_ATOMIC_WRITE_SUPPORT
315323
File file = openWrite(fs, "/contacts3.tmp");
324+
#else
325+
File file = openWrite(fs, "/contacts3");
326+
#endif
327+
316328
if (file) {
317329
uint32_t idx = 0;
318330
ContactInfo c;
@@ -343,16 +355,16 @@ void DataStore::saveContacts(DataStoreHost* host) {
343355
file.flush();
344356
file.close();
345357

358+
#ifdef HAS_ATOMIC_WRITE_SUPPORT
346359
if (write_success) {
347-
// Atomic swap: remove old backup, rename current to backup, rename temp to current
348360
fs->remove("/contacts3.bak");
349361
fs->rename("/contacts3", "/contacts3.bak");
350362
fs->rename("/contacts3.tmp", "/contacts3");
351363
} else {
352-
// Write failed, remove incomplete temp file
353364
fs->remove("/contacts3.tmp");
354-
MESH_DEBUG_PRINTLN("ERROR: saveContacts write failed, temp file removed");
365+
MESH_DEBUG_PRINTLN("ERROR: saveContacts write failed");
355366
}
367+
#endif
356368
}
357369
}
358370

0 commit comments

Comments
 (0)