1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Add FS::format (#702)

This commit is contained in:
Ivan Grokhotkov 2015-08-31 10:24:30 +03:00
parent b8a6b71a1f
commit 041f971a8b
5 changed files with 63 additions and 5 deletions

View File

@ -167,6 +167,13 @@ bool FS::begin() {
return _impl->begin(); return _impl->begin();
} }
bool FS::format() {
if (!_impl) {
return false;
}
return _impl->format();
}
File FS::open(const String& path, const char* mode) { File FS::open(const String& path, const char* mode) {
return open(path.c_str(), mode); return open(path.c_str(), mode);
} }

View File

@ -92,6 +92,8 @@ public:
bool begin(); bool begin();
bool format();
File open(const char* path, const char* mode); File open(const char* path, const char* mode);
File open(const String& path, const char* mode); File open(const String& path, const char* mode);

View File

@ -63,6 +63,7 @@ public:
class FSImpl { class FSImpl {
public: public:
virtual bool begin() = 0; virtual bool begin() = 0;
virtual bool format() = 0;
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0; virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
virtual DirImplPtr openDir(const char* path) = 0; virtual DirImplPtr openDir(const char* path) = 0;
virtual bool rename(const char* pathFrom, const char* pathTo) = 0; virtual bool rename(const char* pathFrom, const char* pathTo) = 0;

View File

@ -102,6 +102,25 @@ public:
return _tryMount(); 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: protected:
friend class SPIFFSFileImpl; friend class SPIFFSFileImpl;
friend class SPIFFSDirImpl; friend class SPIFFSDirImpl;

View File

@ -4,7 +4,7 @@
void fail(const char* msg) { void fail(const char* msg) {
Serial.println(msg); Serial.println(msg);
while(true) { while (true) {
yield(); yield();
} }
} }
@ -15,6 +15,21 @@ void setup() {
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
Serial.println("\n\nFS test\n"); 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()) { if (!SPIFFS.begin()) {
fail("SPIFFS init failed"); fail("SPIFFS init failed");
} }
@ -71,7 +86,7 @@ void setup() {
{ {
Dir root = SPIFFS.openDir("/"); Dir root = SPIFFS.openDir("/");
while(root.next()) { while (root.next()) {
String fileName = root.fileName(); String fileName = root.fileName();
Serial.print("deleting "); Serial.print("deleting ");
Serial.println(fileName); 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"); Serial.println("success");
} }