diff --git a/src/SafeFile.cpp b/src/SafeFile.cpp index c942aa0ee..74e72ac53 100644 --- a/src/SafeFile.cpp +++ b/src/SafeFile.cpp @@ -7,16 +7,23 @@ static File openFile(const char *filename, bool fullAtomic) { concurrency::LockGuard g(spiLock); LOG_DEBUG("Opening %s, fullAtomic=%d", filename, fullAtomic); + #ifdef ARCH_NRF52 FSCom.remove(filename); return FSCom.open(filename, FILE_O_WRITE); #endif - if (!fullAtomic) + if (!fullAtomic) { FSCom.remove(filename); // Nuke the old file to make space (ignore if it !exists) + } String filenameTmp = filename; filenameTmp += ".tmp"; + // If we are doing a full atomic write, remove the old tmp file now + if (fullAtomic) { + FSCom.remove(filename); + } + // clear any previous LFS errors return FSCom.open(filenameTmp.c_str(), FILE_O_WRITE); } diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 5e4ce08b9..e66259cd8 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -1243,7 +1243,12 @@ bool NodeDB::saveToDiskNoRetry(int saveWhat) config.has_bluetooth = true; config.has_security = true; - success &= saveProto(configFileName, meshtastic_LocalConfig_size, &meshtastic_LocalConfig_msg, &config); + size_t configSize; + pb_get_encoded_size(&configSize, meshtastic_LocalConfig_fields, &config); + LOG_DEBUG("Saving config version %d", config.version); + spiLock->lock(); + success &= saveProto(configFileName, configSize, &meshtastic_LocalConfig_msg, &config); + spiLock->unlock(); } if (saveWhat & SEGMENT_MODULECONFIG) { @@ -1259,9 +1264,13 @@ bool NodeDB::saveToDiskNoRetry(int saveWhat) moduleConfig.has_ambient_lighting = true; moduleConfig.has_audio = true; moduleConfig.has_paxcounter = true; + LOG_DEBUG("Saving moduleConfig version %d", moduleConfig.version); - success &= - saveProto(moduleConfigFileName, meshtastic_LocalModuleConfig_size, &meshtastic_LocalModuleConfig_msg, &moduleConfig); + size_t moduleConfigSize; + pb_get_encoded_size(&moduleConfigSize, meshtastic_LocalModuleConfig_fields, &moduleConfig); + spiLock->lock(); + success &= saveProto(moduleConfigFileName, moduleConfigSize, &meshtastic_LocalModuleConfig_msg, &moduleConfig); + spiLock->unlock(); } if (saveWhat & SEGMENT_CHANNELS) {