Skip to content

Commit

Permalink
Add FS::format (esp8266#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Aug 31, 2015
1 parent b8a6b71 commit 041f971
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
7 changes: 7 additions & 0 deletions cores/esp8266/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ bool FS::begin() {
return _impl->begin();
}

bool FS::format() {
if (!_impl) {
return false;
}
return _impl->format();
}

File FS::open(const String& path, const char* mode) {
return open(path.c_str(), mode);
}
Expand Down
2 changes: 2 additions & 0 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class FS

bool begin();

bool format();

File open(const char* path, const char* mode);
File open(const String& path, const char* mode);

Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class DirImpl {
class FSImpl {
public:
virtual bool begin() = 0;
virtual bool format() = 0;
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
virtual DirImplPtr openDir(const char* path) = 0;
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
Expand Down
19 changes: 19 additions & 0 deletions cores/esp8266/spiffs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ class SPIFFSImpl : public FSImpl {
return _tryMount();
}

bool format() override {
bool wasMounted = (SPIFFS_mounted(&_fs) != 0);

if (_tryMount()) {
SPIFFS_unmount(&_fs);
}
auto rc = SPIFFS_format(&_fs);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
return false;
}

if (wasMounted) {
return _tryMount();
}

return true;
}

protected:
friend class SPIFFSFileImpl;
friend class SPIFFSDirImpl;
Expand Down
39 changes: 34 additions & 5 deletions tests/FSWrapper/FSWrapper.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

void fail(const char* msg) {
Serial.println(msg);
while(true) {
while (true) {
yield();
}
}
Expand All @@ -15,6 +15,21 @@ void setup() {
WiFi.mode(WIFI_OFF);
Serial.println("\n\nFS test\n");

{
if (!SPIFFS.format()) {
fail("format failed");
}
Dir root = SPIFFS.openDir("/");
int count = 0;
while (root.next()) {
++count;
}
if (count > 0) {
fail("some files left after format");
}
}


if (!SPIFFS.begin()) {
fail("SPIFFS init failed");
}
Expand Down Expand Up @@ -63,15 +78,15 @@ void setup() {
{
Dir root = SPIFFS.openDir("/");
while (root.next()) {
String fileName = root.fileName();
File f = root.openFile("r");
Serial.printf("%s: %d\r\n", fileName.c_str(), f.size());
String fileName = root.fileName();
File f = root.openFile("r");
Serial.printf("%s: %d\r\n", fileName.c_str(), f.size());
}
}

{
Dir root = SPIFFS.openDir("/");
while(root.next()) {
while (root.next()) {
String fileName = root.fileName();
Serial.print("deleting ");
Serial.println(fileName);
Expand All @@ -96,6 +111,20 @@ void setup() {
}
}

{
if (!SPIFFS.format()) {
fail("format failed");
}
Dir root = SPIFFS.openDir("/");
int count = 0;
while (root.next()) {
++count;
}
if (count > 0) {
fail("some files left after format");
}
}

Serial.println("success");
}

Expand Down

0 comments on commit 041f971

Please sign in to comment.