Skip to content

Commit a42bc48

Browse files
Earle F. Philhower, IIIEarle F. Philhower, III
Earle F. Philhower, III
authored and
Earle F. Philhower, III
committed
Merge in the FSConfig changs from SDFS PR
Enable setConfig for LittleFS as well plys merge the SPIFFS changes done in the SDFS PR.
1 parent c22b9d4 commit a42bc48

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

cores/esp8266/FS.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ bool Dir::rewind() {
220220
return _impl->rewind();
221221
}
222222

223+
bool FS::setConfig(const FSConfig *cfg) {
224+
if (!_impl || !cfg) {
225+
return false;
226+
}
227+
228+
return _impl->setConfig(cfg);
229+
}
230+
223231
bool FS::begin() {
224232
if (!_impl) {
225233
return false;

cores/esp8266/FS.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,41 @@ struct FSInfo {
119119
size_t maxPathLength;
120120
};
121121

122+
class FSConfig
123+
{
124+
public:
125+
FSConfig(bool autoFormat = true) {
126+
_type = FSConfig::fsid::FSId;
127+
_autoFormat = autoFormat;
128+
}
129+
130+
enum fsid { FSId = 0x00000000 };
131+
FSConfig setAutoFormat(bool val = true) {
132+
_autoFormat = val;
133+
return *this;
134+
}
135+
136+
uint32_t _type;
137+
bool _autoFormat;
138+
};
139+
140+
class SPIFFSConfig : public FSConfig
141+
{
142+
public:
143+
SPIFFSConfig(bool autoFormat = true) {
144+
_type = SPIFFSConfig::fsid::FSId;
145+
_autoFormat = autoFormat;
146+
}
147+
enum fsid { FSId = 0x53504946 };
148+
};
149+
122150
class FS
123151
{
124152
public:
125153
FS(FSImplPtr impl) : _impl(impl) { }
126154

155+
bool setConfig(const FSConfig *cfg);
156+
127157
bool begin();
128158
void end();
129159

@@ -166,6 +196,7 @@ using fs::SeekSet;
166196
using fs::SeekCur;
167197
using fs::SeekEnd;
168198
using fs::FSInfo;
199+
using fs::FSConfig;
169200
#endif //FS_NO_GLOBALS
170201

171202
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)

cores/esp8266/FSImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class DirImpl {
6969
class FSImpl {
7070
public:
7171
virtual ~FSImpl () { }
72+
virtual bool setConfig(const FSConfig *cfg) = 0;
7273
virtual bool begin() = 0;
7374
virtual void end() = 0;
7475
virtual bool format() = 0;

cores/esp8266/spiffs_api.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ class SPIFFSImpl : public FSImpl
123123
return false;
124124
}
125125

126+
bool setConfig(const FSConfig *cfg) override
127+
{
128+
if ((cfg->_type != SPIFFSConfig::fsid::FSId) || (SPIFFS_mounted(&_fs) != 0)) {
129+
return false;
130+
}
131+
_cfg = *static_cast<const SPIFFSConfig *>(cfg);
132+
return true;
133+
}
134+
126135
bool begin() override
127136
{
128137
#if defined(ARDUINO) && !defined(CORE_MOCK)
@@ -139,12 +148,15 @@ class SPIFFSImpl : public FSImpl
139148
if (_tryMount()) {
140149
return true;
141150
}
142-
auto rc = SPIFFS_format(&_fs);
143-
if (rc != SPIFFS_OK) {
144-
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
145-
return false;
151+
if (_cfg._autoFormat) {
152+
auto rc = SPIFFS_format(&_fs);
153+
if (rc != SPIFFS_OK) {
154+
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
155+
return false;
156+
}
157+
return _tryMount();
146158
}
147-
return _tryMount();
159+
return false;
148160
}
149161

150162
void end() override
@@ -290,6 +302,8 @@ class SPIFFSImpl : public FSImpl
290302
std::unique_ptr<uint8_t[]> _workBuf;
291303
std::unique_ptr<uint8_t[]> _fdsBuf;
292304
std::unique_ptr<uint8_t[]> _cacheBuf;
305+
306+
SPIFFSConfig _cfg;
293307
};
294308

295309
#define CHECKFD() while (_fd == 0) { panic(); }

libraries/LittleFS/src/LittleFS.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ namespace littlefs_impl {
4444
class LittleFSFileImpl;
4545
class LittleFSDirImpl;
4646

47+
class LittleFSConfig : public FSConfig
48+
{
49+
public:
50+
LittleFSConfig(bool autoFormat = true) {
51+
_type = LittleFSConfig::fsid::FSId;
52+
_autoFormat = autoFormat;
53+
}
54+
enum fsid { FSId = 0x4c495454 };
55+
};
56+
4757
class LittleFSImpl : public FSImpl
4858
{
4959
public:
@@ -147,6 +157,14 @@ class LittleFSImpl : public FSImpl
147157
return remove(path); // Same call on LittleFS
148158
}
149159

160+
bool setConfig(const FSConfig *cfg) override {
161+
if ((cfg->_type != LittleFSConfig::fsid::FSId) || _mounted) {
162+
return false;
163+
}
164+
_cfg = *static_cast<const LittleFSConfig *>(cfg);
165+
return true;
166+
}
167+
150168
bool begin() override {
151169
if (_size <= 0) {
152170
DEBUGV("LittleFS size is <= zero");
@@ -155,7 +173,7 @@ class LittleFSImpl : public FSImpl
155173
if (_tryMount()) {
156174
return true;
157175
}
158-
if (!format()) {
176+
if (!_cfg._autoFormat || !format()) {
159177
return false;
160178
}
161179
return _tryMount();
@@ -282,6 +300,8 @@ class LittleFSImpl : public FSImpl
282300
lfs_t _lfs;
283301
lfs_config _lfs_cfg;
284302

303+
LittleFSConfig _cfg;
304+
285305
uint32_t _start;
286306
uint32_t _size;
287307
uint32_t _pageSize;
@@ -521,6 +541,7 @@ class LittleFSDirImpl : public DirImpl
521541

522542
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS)
523543
extern FS LittleFS;
544+
using littlefs_impl::LittleFSConfig;
524545
#endif // ARDUINO
525546

526547

0 commit comments

Comments
 (0)