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:
parent
b8a6b71a1f
commit
041f971a8b
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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");
|
||||
}
|
||||
@ -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");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user