Add locks to SafeFile, remove from readcb, introduce some LockGuards

This commit is contained in:
GUVWAF 2024-12-19 20:09:33 +01:00
parent 12f0df49a1
commit 058980178c
7 changed files with 18 additions and 25 deletions

View File

@ -95,20 +95,18 @@ bool copyFile(const char *from, const char *to)
#elif defined(FSCom) #elif defined(FSCom)
// take SPI Lock // take SPI Lock
spiLock->lock(); concurrency::LockGuard g(spiLock);
unsigned char cbuffer[16]; unsigned char cbuffer[16];
File f1 = FSCom.open(from, FILE_O_READ); File f1 = FSCom.open(from, FILE_O_READ);
if (!f1) { if (!f1) {
LOG_ERROR("Failed to open source file %s", from); LOG_ERROR("Failed to open source file %s", from);
spiLock->unlock();
return false; return false;
} }
File f2 = FSCom.open(to, FILE_O_WRITE); File f2 = FSCom.open(to, FILE_O_WRITE);
if (!f2) { if (!f2) {
LOG_ERROR("Failed to open destination file %s", to); LOG_ERROR("Failed to open destination file %s", to);
spiLock->unlock();
return false; return false;
} }
@ -120,7 +118,6 @@ bool copyFile(const char *from, const char *to)
f2.flush(); f2.flush();
f2.close(); f2.close();
f1.close(); f1.close();
spiLock->unlock();
return true; return true;
#endif #endif
} }

View File

@ -5,6 +5,7 @@
// Only way to work on both esp32 and nrf52 // Only way to work on both esp32 and nrf52
static File openFile(const char *filename, bool fullAtomic) static File openFile(const char *filename, bool fullAtomic)
{ {
concurrency::LockGuard g(spiLock);
if (!fullAtomic) if (!fullAtomic)
FSCom.remove(filename); // Nuke the old file to make space (ignore if it !exists) FSCom.remove(filename); // Nuke the old file to make space (ignore if it !exists)
@ -53,15 +54,20 @@ bool SafeFile::close()
if (!f) if (!f)
return false; return false;
spiLock->lock();
f.close(); f.close();
spiLock->unlock();
if (!testReadback()) if (!testReadback())
return false; return false;
{ // Scope for lock
concurrency::LockGuard g(spiLock);
// brief window of risk here ;-) // brief window of risk here ;-)
if (fullAtomic && FSCom.exists(filename.c_str()) && !FSCom.remove(filename.c_str())) { if (fullAtomic && FSCom.exists(filename.c_str()) && !FSCom.remove(filename.c_str())) {
LOG_ERROR("Can't remove old pref file"); LOG_ERROR("Can't remove old pref file");
return false; return false;
} }
}
String filenameTmp = filename; String filenameTmp = filename;
filenameTmp += ".tmp"; filenameTmp += ".tmp";
@ -76,6 +82,7 @@ bool SafeFile::close()
/// Read our (closed) tempfile back in and compare the hash /// Read our (closed) tempfile back in and compare the hash
bool SafeFile::testReadback() bool SafeFile::testReadback()
{ {
concurrency::LockGuard g(spiLock);
bool lfs_failed = lfs_assert_failed; bool lfs_failed = lfs_assert_failed;
lfs_assert_failed = false; lfs_assert_failed = false;

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "FSCommon.h" #include "FSCommon.h"
#include "SPILock.h"
#include "configuration.h" #include "configuration.h"
#ifdef FSCom #ifdef FSCom

View File

@ -839,7 +839,7 @@ LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t
{ {
LoadFileResult state = LoadFileResult::OTHER_FAILURE; LoadFileResult state = LoadFileResult::OTHER_FAILURE;
#ifdef FSCom #ifdef FSCom
spiLock->lock(); concurrency::LockGuard g(spiLock);
auto f = FSCom.open(filename, FILE_O_READ); auto f = FSCom.open(filename, FILE_O_READ);
@ -859,7 +859,6 @@ LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t
} else { } else {
LOG_ERROR("Could not open / read %s", filename); LOG_ERROR("Could not open / read %s", filename);
} }
spiLock->unlock();
#else #else
LOG_ERROR("ERROR: Filesystem not implemented"); LOG_ERROR("ERROR: Filesystem not implemented");
state = LoadFileResult::NO_FILESYSTEM; state = LoadFileResult::NO_FILESYSTEM;
@ -1026,7 +1025,6 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_
{ {
bool okay = false; bool okay = false;
#ifdef FSCom #ifdef FSCom
spiLock->lock();
auto f = SafeFile(filename, fullAtomic); auto f = SafeFile(filename, fullAtomic);
LOG_INFO("Save %s", filename); LOG_INFO("Save %s", filename);
@ -1039,7 +1037,6 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_
} }
bool writeSucceeded = f.close(); bool writeSucceeded = f.close();
spiLock->unlock();
if (!okay || !writeSucceeded) { if (!okay || !writeSucceeded) {
LOG_ERROR("Can't write prefs!"); LOG_ERROR("Can't write prefs!");

View File

@ -37,13 +37,11 @@ bool pb_decode_from_bytes(const uint8_t *srcbuf, size_t srcbufsize, const pb_msg
bool readcb(pb_istream_t *stream, uint8_t *buf, size_t count) bool readcb(pb_istream_t *stream, uint8_t *buf, size_t count)
{ {
bool status = false; bool status = false;
spiLock->lock();
File *file = (File *)stream->state; File *file = (File *)stream->state;
if (buf == NULL) { if (buf == NULL) {
while (count-- && file->read() != EOF) while (count-- && file->read() != EOF)
; ;
spiLock->unlock();
return count == 0; return count == 0;
} }
@ -52,8 +50,6 @@ bool readcb(pb_istream_t *stream, uint8_t *buf, size_t count)
if (file->available() == 0) if (file->available() == 0)
stream->bytes_left = 0; stream->bytes_left = 0;
spiLock->unlock();
return status; return status;
} }

View File

@ -210,16 +210,14 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp)
LOG_DEBUG("gpsStatus->getDOP() %d", gpsStatus->getDOP()); LOG_DEBUG("gpsStatus->getDOP() %d", gpsStatus->getDOP());
LOG_DEBUG("-----------------------------------------"); LOG_DEBUG("-----------------------------------------");
*/ */
spiLock->lock(); concurrency::LockGuard g(spiLock);
if (!FSBegin()) { if (!FSBegin()) {
LOG_DEBUG("An Error has occurred while mounting the filesystem"); LOG_DEBUG("An Error has occurred while mounting the filesystem");
spiLock->unlock();
return 0; return 0;
} }
if (FSCom.totalBytes() - FSCom.usedBytes() < 51200) { if (FSCom.totalBytes() - FSCom.usedBytes() < 51200) {
LOG_DEBUG("Filesystem doesn't have enough free space. Aborting write"); LOG_DEBUG("Filesystem doesn't have enough free space. Aborting write");
spiLock->unlock();
return 0; return 0;
} }
@ -232,7 +230,6 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp)
if (!fileToWrite) { if (!fileToWrite) {
LOG_ERROR("There was an error opening the file for writing"); LOG_ERROR("There was an error opening the file for writing");
spiLock->unlock();
return 0; return 0;
} }
@ -252,7 +249,6 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp)
if (!fileToAppend) { if (!fileToAppend) {
LOG_ERROR("There was an error opening the file for appending"); LOG_ERROR("There was an error opening the file for appending");
spiLock->unlock();
return 0; return 0;
} }
@ -295,7 +291,6 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp)
fileToAppend.printf("\"%s\"\n", p.payload.bytes); fileToAppend.printf("\"%s\"\n", p.payload.bytes);
fileToAppend.flush(); fileToAppend.flush();
fileToAppend.close(); fileToAppend.close();
spiLock->unlock();
#endif #endif
return 1; return 1;

View File

@ -99,7 +99,6 @@ void NAU7802Sensor::tare()
bool NAU7802Sensor::saveCalibrationData() bool NAU7802Sensor::saveCalibrationData()
{ {
spiLock->lock();
auto file = SafeFile(nau7802ConfigFileName); auto file = SafeFile(nau7802ConfigFileName);
nau7802config.zeroOffset = nau7802.getZeroOffset(); nau7802config.zeroOffset = nau7802.getZeroOffset();
nau7802config.calibrationFactor = nau7802.getCalibrationFactor(); nau7802config.calibrationFactor = nau7802.getCalibrationFactor();
@ -113,6 +112,7 @@ bool NAU7802Sensor::saveCalibrationData()
} else { } else {
okay = true; okay = true;
} }
spiLock->lock();
okay &= file.close(); okay &= file.close();
spiLock->unlock(); spiLock->unlock();