File tree Expand file tree Collapse file tree 5 files changed +81
-6
lines changed Expand file tree Collapse file tree 5 files changed +81
-6
lines changed Original file line number Diff line number Diff line change @@ -220,6 +220,14 @@ bool Dir::rewind() {
220
220
return _impl->rewind ();
221
221
}
222
222
223
+ bool FS::setConfig (const FSConfig *cfg) {
224
+ if (!_impl || !cfg) {
225
+ return false ;
226
+ }
227
+
228
+ return _impl->setConfig (cfg);
229
+ }
230
+
223
231
bool FS::begin () {
224
232
if (!_impl) {
225
233
return false ;
Original file line number Diff line number Diff line change @@ -119,11 +119,41 @@ struct FSInfo {
119
119
size_t maxPathLength;
120
120
};
121
121
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
+
122
150
class FS
123
151
{
124
152
public:
125
153
FS (FSImplPtr impl) : _impl(impl) { }
126
154
155
+ bool setConfig (const FSConfig *cfg);
156
+
127
157
bool begin ();
128
158
void end ();
129
159
@@ -166,6 +196,7 @@ using fs::SeekSet;
166
196
using fs::SeekCur;
167
197
using fs::SeekEnd;
168
198
using fs::FSInfo;
199
+ using fs::FSConfig;
169
200
#endif // FS_NO_GLOBALS
170
201
171
202
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)
Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ class DirImpl {
69
69
class FSImpl {
70
70
public:
71
71
virtual ~FSImpl () { }
72
+ virtual bool setConfig (const FSConfig *cfg) = 0;
72
73
virtual bool begin () = 0;
73
74
virtual void end () = 0;
74
75
virtual bool format () = 0;
Original file line number Diff line number Diff line change @@ -123,6 +123,15 @@ class SPIFFSImpl : public FSImpl
123
123
return false ;
124
124
}
125
125
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
+
126
135
bool begin () override
127
136
{
128
137
#if defined(ARDUINO) && !defined(CORE_MOCK)
@@ -139,12 +148,15 @@ class SPIFFSImpl : public FSImpl
139
148
if (_tryMount ()) {
140
149
return true ;
141
150
}
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 ();
146
158
}
147
- return _tryMount () ;
159
+ return false ;
148
160
}
149
161
150
162
void end () override
@@ -290,6 +302,8 @@ class SPIFFSImpl : public FSImpl
290
302
std::unique_ptr<uint8_t []> _workBuf;
291
303
std::unique_ptr<uint8_t []> _fdsBuf;
292
304
std::unique_ptr<uint8_t []> _cacheBuf;
305
+
306
+ SPIFFSConfig _cfg;
293
307
};
294
308
295
309
#define CHECKFD () while (_fd == 0 ) { panic (); }
Original file line number Diff line number Diff line change @@ -44,6 +44,16 @@ namespace littlefs_impl {
44
44
class LittleFSFileImpl ;
45
45
class LittleFSDirImpl ;
46
46
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
+
47
57
class LittleFSImpl : public FSImpl
48
58
{
49
59
public:
@@ -147,6 +157,14 @@ class LittleFSImpl : public FSImpl
147
157
return remove (path); // Same call on LittleFS
148
158
}
149
159
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
+
150
168
bool begin () override {
151
169
if (_size <= 0 ) {
152
170
DEBUGV (" LittleFS size is <= zero" );
@@ -155,7 +173,7 @@ class LittleFSImpl : public FSImpl
155
173
if (_tryMount ()) {
156
174
return true ;
157
175
}
158
- if (!format ()) {
176
+ if (!_cfg. _autoFormat || ! format ()) {
159
177
return false ;
160
178
}
161
179
return _tryMount ();
@@ -282,6 +300,8 @@ class LittleFSImpl : public FSImpl
282
300
lfs_t _lfs;
283
301
lfs_config _lfs_cfg;
284
302
303
+ LittleFSConfig _cfg;
304
+
285
305
uint32_t _start;
286
306
uint32_t _size;
287
307
uint32_t _pageSize;
@@ -521,6 +541,7 @@ class LittleFSDirImpl : public DirImpl
521
541
522
542
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS)
523
543
extern FS LittleFS;
544
+ using littlefs_impl::LittleFSConfig;
524
545
#endif // ARDUINO
525
546
526
547
You can’t perform that action at this time.
0 commit comments