Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/helpers/ClientACL.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include "ClientACL.h"

static char* getTmpPath(const char* filename) {
static char tmp_filename[32];
snprintf(tmp_filename, sizeof(tmp_filename), "%s.tmp", filename);
return tmp_filename;
}

static File openWrite(FILESYSTEM* _fs, const char* filename) {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
_fs->remove(filename);
return _fs->open(filename, FILE_O_WRITE);
char* tmp_filename = getTmpPath(filename);
_fs->remove(tmp_filename);
return _fs->open(tmp_filename, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
return _fs->open(filename, "w");
#else
Expand Down Expand Up @@ -54,7 +61,8 @@ void ClientACL::load(FILESYSTEM* fs, const mesh::LocalIdentity& self_id) {

void ClientACL::save(FILESYSTEM* fs, bool (*filter)(ClientInfo*)) {
_fs = fs;
File file = openWrite(_fs, "/s_contacts");
const char* real_path = "/s_contacts";
File file = openWrite(_fs, real_path);
if (file) {
uint8_t unused[2];
memset(unused, 0, sizeof(unused));
Expand All @@ -74,6 +82,14 @@ void ClientACL::save(FILESYSTEM* fs, bool (*filter)(ClientInfo*)) {
if (!success) break; // write failed
}
file.close();

#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oltaco Can you verify that _fs->rename() definitely will work in this case? ie. does the real_path file get overwritten/replaced?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, the underlying lfs_rename() call will point the old directory entry for file at the blocks referenced by file.tmp and then remove the directory entry for file.tmp

char* tmp_path = getTmpPath(real_path);
if (!_fs->rename(tmp_path, real_path)) {
_fs->remove(tmp_path);
MESH_DEBUG_PRINTLN("ERROR: ClientACL::save rename failed!");
}
#endif
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/helpers/CommonCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {

void CommonCLI::savePrefs(FILESYSTEM* fs) {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
fs->remove("/com_prefs");
File file = fs->open("/com_prefs", FILE_O_WRITE);
// Atomic write: write to temp file, then rename over the real file.
// LittleFS rename() is a single metadata commit -- atomic even on power loss.
const char* tmp_path = "/com_prefs.tmp";
const char* real_path = "/com_prefs";
fs->remove(tmp_path); // clean up any stale temp from previous failed write
File file = fs->open(tmp_path, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
File file = fs->open("/com_prefs", "w");
#else
Expand Down Expand Up @@ -183,6 +187,13 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
// next: 291

file.close();

#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
if (!fs->rename(tmp_path, real_path)) {
fs->remove(tmp_path);
MESH_DEBUG_PRINTLN("ERROR: savePrefs rename failed!");
}
#endif
}
}

Expand Down
25 changes: 21 additions & 4 deletions src/helpers/RegionMap.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "RegionMap.h"
#include <helpers/TxtDataHelpers.h>
#include <SHA256.h>
#include <SHA256.h>

// helper class for region map exporter, we emulate Stream with a safe buffer writer.

Expand Down Expand Up @@ -58,10 +58,17 @@ static const char* skip_hash(const char* name) {
return *name == '#' ? name + 1 : name;
}

static char* getTmpPath(const char* filename) {
static char tmp_filename[32];
snprintf(tmp_filename, sizeof(tmp_filename), "%s.tmp", filename);
return tmp_filename;
}

static File openWrite(FILESYSTEM* _fs, const char* filename) {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
_fs->remove(filename);
return _fs->open(filename, FILE_O_WRITE);
char* tmp_filename = getTmpPath(filename);
_fs->remove(tmp_filename);
return _fs->open(tmp_filename, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
return _fs->open(filename, "w");
#else
Expand Down Expand Up @@ -115,7 +122,8 @@ bool RegionMap::load(FILESYSTEM* _fs, const char* path) {
}

bool RegionMap::save(FILESYSTEM* _fs, const char* path) {
File file = openWrite(_fs, path ? path : "/regions2");
const char* real_path = path ? path : "/regions2";
File file = openWrite(_fs, real_path);
if (file) {
uint8_t pad[128];
memset(pad, 0, sizeof(pad));
Expand All @@ -139,6 +147,15 @@ bool RegionMap::save(FILESYSTEM* _fs, const char* path) {
}
}
file.close();

#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
char* tmp_path = getTmpPath(real_path);
if (!_fs->rename(tmp_path, real_path)) {
_fs->remove(tmp_path);
MESH_DEBUG_PRINTLN("ERROR: RegionMap::save rename failed!");
return false;
}
#endif
return true;
}
return false; // failed
Expand Down