mirror of
https://github.com/scottbez1/smartknob.git
synced 2025-09-26 23:09:27 +08:00
SPIFFSGuard
This commit is contained in:
parent
fde5a17f38
commit
d72621ae00
@ -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");
|
||||
|
@ -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_;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user