Skip to content

Commit 6d1d131

Browse files
committed
Add FS::format (#702)
1 parent b65162f commit 6d1d131

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

hardware/esp8266com/esp8266/cores/esp8266/FS.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ bool FS::begin() {
167167
return _impl->begin();
168168
}
169169

170+
bool FS::format() {
171+
if (!_impl) {
172+
return false;
173+
}
174+
return _impl->format();
175+
}
176+
170177
File FS::open(const String& path, const char* mode) {
171178
return open(path.c_str(), mode);
172179
}

hardware/esp8266com/esp8266/cores/esp8266/FS.h

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class FS
9292

9393
bool begin();
9494

95+
bool format();
96+
9597
File open(const char* path, const char* mode);
9698
File open(const String& path, const char* mode);
9799

hardware/esp8266com/esp8266/cores/esp8266/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class DirImpl {
6363
class FSImpl {
6464
public:
6565
virtual bool begin() = 0;
66+
virtual bool format() = 0;
6667
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
6768
virtual DirImplPtr openDir(const char* path) = 0;
6869
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;

hardware/esp8266com/esp8266/cores/esp8266/spiffs_api.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ class SPIFFSImpl : public FSImpl {
102102
return _tryMount();
103103
}
104104

105+
bool format() override {
106+
bool wasMounted = (SPIFFS_mounted(&_fs) != 0);
107+
108+
if (_tryMount()) {
109+
SPIFFS_unmount(&_fs);
110+
}
111+
auto rc = SPIFFS_format(&_fs);
112+
if (rc != SPIFFS_OK) {
113+
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
114+
return false;
115+
}
116+
117+
if (wasMounted) {
118+
return _tryMount();
119+
}
120+
121+
return true;
122+
}
123+
105124
protected:
106125
friend class SPIFFSFileImpl;
107126
friend class SPIFFSDirImpl;

hardware/esp8266com/esp8266/tests/FSWrapper/FSWrapper.ino

+34-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
void fail(const char* msg) {
66
Serial.println(msg);
7-
while(true) {
7+
while (true) {
88
yield();
99
}
1010
}
@@ -15,6 +15,21 @@ void setup() {
1515
WiFi.mode(WIFI_OFF);
1616
Serial.println("\n\nFS test\n");
1717

18+
{
19+
if (!SPIFFS.format()) {
20+
fail("format failed");
21+
}
22+
Dir root = SPIFFS.openDir("/");
23+
int count = 0;
24+
while (root.next()) {
25+
++count;
26+
}
27+
if (count > 0) {
28+
fail("some files left after format");
29+
}
30+
}
31+
32+
1833
if (!SPIFFS.begin()) {
1934
fail("SPIFFS init failed");
2035
}
@@ -63,15 +78,15 @@ void setup() {
6378
{
6479
Dir root = SPIFFS.openDir("/");
6580
while (root.next()) {
66-
String fileName = root.fileName();
67-
File f = root.openFile("r");
68-
Serial.printf("%s: %d\r\n", fileName.c_str(), f.size());
81+
String fileName = root.fileName();
82+
File f = root.openFile("r");
83+
Serial.printf("%s: %d\r\n", fileName.c_str(), f.size());
6984
}
7085
}
7186

7287
{
7388
Dir root = SPIFFS.openDir("/");
74-
while(root.next()) {
89+
while (root.next()) {
7590
String fileName = root.fileName();
7691
Serial.print("deleting ");
7792
Serial.println(fileName);
@@ -96,6 +111,20 @@ void setup() {
96111
}
97112
}
98113

114+
{
115+
if (!SPIFFS.format()) {
116+
fail("format failed");
117+
}
118+
Dir root = SPIFFS.openDir("/");
119+
int count = 0;
120+
while (root.next()) {
121+
++count;
122+
}
123+
if (count > 0) {
124+
fail("some files left after format");
125+
}
126+
}
127+
99128
Serial.println("success");
100129
}
101130

0 commit comments

Comments
 (0)