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+
1016DataStore::DataStore (FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(nullptr ), _clock(&clock),
1117#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
1218 identity_store (fs, " " )
@@ -272,6 +278,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
272278 FILESYSTEM* fs = _getContactsChannelsFS ();
273279 File file = openRead (fs, " /contacts3" );
274280
281+ #ifdef HAS_ATOMIC_WRITE_SUPPORT
275282 // If main file doesn't exist or is empty, try backup
276283 if (!file || file.size () == 0 ) {
277284 if (file) file.close ();
@@ -280,6 +287,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
280287 file = openRead (fs, " /contacts3.bak" );
281288 }
282289 }
290+ #endif
283291
284292 if (file) {
285293 bool full = false ;
@@ -313,8 +321,12 @@ void DataStore::loadContacts(DataStoreHost* host) {
313321void DataStore::saveContacts (DataStoreHost* host) {
314322 FILESYSTEM* fs = _getContactsChannelsFS ();
315323
316- // Write to temp file first (atomic write pattern)
324+ # ifdef HAS_ATOMIC_WRITE_SUPPORT
317325 File file = openWrite (fs, " /contacts3.tmp" );
326+ #else
327+ File file = openWrite (fs, " /contacts3" );
328+ #endif
329+
318330 if (file) {
319331 uint32_t idx = 0 ;
320332 ContactInfo c;
@@ -345,16 +357,16 @@ void DataStore::saveContacts(DataStoreHost* host) {
345357 file.flush ();
346358 file.close ();
347359
360+ #ifdef HAS_ATOMIC_WRITE_SUPPORT
348361 if (write_success) {
349- // Atomic swap: remove old backup, rename current to backup, rename temp to current
350362 fs->remove (" /contacts3.bak" );
351363 fs->rename (" /contacts3" , " /contacts3.bak" );
352364 fs->rename (" /contacts3.tmp" , " /contacts3" );
353365 } else {
354- // Write failed, remove incomplete temp file
355366 fs->remove (" /contacts3.tmp" );
356- MESH_DEBUG_PRINTLN (" ERROR: saveContacts write failed, temp file removed " );
367+ MESH_DEBUG_PRINTLN (" ERROR: saveContacts write failed" );
357368 }
369+ #endif
358370 }
359371}
360372
0 commit comments