SPIFFSGuard

This commit is contained in:
Scott Bezek 2023-06-18 13:38:38 -07:00
parent fde5a17f38
commit d72621ae00
2 changed files with 31 additions and 2 deletions

View File

@ -21,8 +21,8 @@ Configuration::~Configuration() {
bool Configuration::loadFromDisk() {
SemaphoreGuard lock(mutex_);
if (!SPIFFS.begin(true)) {
log("Failed to mount SPIFFS");
SPIFFSGuard spiffs(logger_);
if (!spiffs.ok_) {
return false;
}
@ -77,6 +77,10 @@ bool Configuration::saveToDisk() {
return false;
}
SPIFFSGuard spiffs(logger_);
if (!spiffs.ok_) {
return false;
}
File f = SPIFFS.open(CONFIG_PATH, FILE_WRITE);
if (!f) {
log("Failed to read config file");

View File

@ -31,3 +31,28 @@ class Configuration {
void log(const char* msg);
};
class SPIFFSGuard {
public:
SPIFFSGuard(Logger* logger) : logger_(logger) {
if (!SPIFFS.begin(true)) {
if (logger != nullptr) {
logger->log("Failed to mount SPIFFS");
}
return;
}
ok_ = true;
}
~SPIFFSGuard() {
if (ok_) {
SPIFFS.end();
logger_->log("Unmounted SPIFFS");
}
}
SPIFFSGuard(SPIFFSGuard const&)=delete;
SPIFFSGuard& operator=(SPIFFSGuard const&)=delete;
bool ok_ = false;
private:
Logger* logger_;
};