From 041f971a8b59bfe23281514cf625ffc3f714ecc8 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 31 Aug 2015 10:24:30 +0300 Subject: [PATCH] Add FS::format (#702) --- cores/esp8266/FS.cpp | 7 +++++++ cores/esp8266/FS.h | 2 ++ cores/esp8266/FSImpl.h | 1 + cores/esp8266/spiffs_api.cpp | 19 +++++++++++++++++ tests/FSWrapper/FSWrapper.ino | 39 ++++++++++++++++++++++++++++++----- 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index 8bf0d8d18..d7217df01 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -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); } diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index d8d8dedd7..1301a631d 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -92,6 +92,8 @@ public: bool begin(); + bool format(); + File open(const char* path, const char* mode); File open(const String& path, const char* mode); diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index 20d886e76..b6bed005e 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -63,6 +63,7 @@ public: 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; diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 6f63c59cd..4af8f6ffc 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -102,6 +102,25 @@ public: 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; diff --git a/tests/FSWrapper/FSWrapper.ino b/tests/FSWrapper/FSWrapper.ino index 1da05a59a..2fefc4ccb 100644 --- a/tests/FSWrapper/FSWrapper.ino +++ b/tests/FSWrapper/FSWrapper.ino @@ -4,7 +4,7 @@ void fail(const char* msg) { Serial.println(msg); - while(true) { + while (true) { yield(); } } @@ -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"); } @@ -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); @@ -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"); }