File system persistence fixes

This commit is contained in:
Ben Meadors 2025-01-12 19:03:21 -06:00
parent 0e3c419652
commit 87601f4760
2 changed files with 23 additions and 12 deletions

View File

@ -291,6 +291,13 @@ NodeDB::NodeDB()
info->user = TypeConversions::ConvertToUserLite(owner);
info->has_user = true;
// If node database has not been saved for the first time, save it now
#ifdef FSCom
if (!FSCom.exists(nodeDatabaseFileName)) {
saveNodeDatabaseToDisk();
}
#endif
#ifdef ARCH_ESP32
Preferences preferences;
preferences.begin("meshtastic", false);
@ -920,14 +927,6 @@ void NodeDB::pickNewNodeNum()
myNodeInfo.my_node_num = nodeNum;
}
static const char *deviceStateFileName = "/prefs/device.proto";
static const char *legacyPrefFileName = "/prefs/db.proto";
static const char *nodeDatabaseFileName = "/prefs/nodes.proto";
static const char *configFileName = "/prefs/config.proto";
static const char *uiconfigFileName = "/prefs/uiconfig.proto";
static const char *moduleConfigFileName = "/prefs/module.proto";
static const char *channelFileName = "/prefs/channels.proto";
/** Load a protobuf from a file, return LoadFileResult */
LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields,
void *dest_struct)
@ -970,14 +969,17 @@ void NodeDB::loadFromDisk()
#ifdef ARCH_ESP32
spiLock->lock();
// If the legacy deviceState exists, start over with a factory reset
if (FSCom.exists(legacyPrefFileName)) {
rmDir("/prefs");
}
if (FSCom.exists("/static/static"))
rmDir("/static/static"); // Remove bad static web files bundle from initial 2.5.13 release
spiLock->unlock();
#endif
#ifdef FSCom
spiLock->lock();
if (FSCom.exists(legacyPrefFileName)) {
rmDir("/prefs");
}
spiLock->unlock();
#endif
auto state = loadProto(nodeDatabaseFileName, getMaxNodesAllocatedSize(), sizeof(meshtastic_NodeDatabase),
&meshtastic_NodeDatabase_msg, &nodeDatabase);
if (nodeDatabase.version < DEVICESTATE_MIN_VER) {
@ -1430,6 +1432,7 @@ bool NodeDB::updateUser(uint32_t nodeId, meshtastic_User &p, uint8_t channelInde
// We just changed something about a User,
// store our DB unless we just did so less than a minute ago
if (!Throttle::isWithinTimespanMs(lastNodeDbSave, ONE_MINUTE_MS)) {
saveToDisk(SEGMENT_NODEDATABASE);
lastNodeDbSave = millis();

View File

@ -37,6 +37,14 @@ extern meshtastic_LocalModuleConfig moduleConfig;
extern meshtastic_User &owner;
extern meshtastic_Position localPosition;
static const char *deviceStateFileName = "/prefs/device.proto";
static const char *legacyPrefFileName = "/prefs/db.proto";
static const char *nodeDatabaseFileName = "/prefs/nodes.proto";
static const char *configFileName = "/prefs/config.proto";
static const char *uiconfigFileName = "/prefs/uiconfig.proto";
static const char *moduleConfigFileName = "/prefs/module.proto";
static const char *channelFileName = "/prefs/channels.proto";
/// Given a node, return how many seconds in the past (vs now) that we last heard from it
uint32_t sinceLastSeen(const meshtastic_NodeInfoLite *n);