From 9cf55f129812c6c3d47e34fb58845ac8618c20b9 Mon Sep 17 00:00:00 2001 From: TD-er Date: Wed, 3 Apr 2019 14:01:58 +0200 Subject: [PATCH] Make SPIFFS garbage collection publicly available Original issue: https://github.com/esp8266/Arduino/issues/2870 --- cores/esp8266/FS.cpp | 7 +++++++ cores/esp8266/FS.h | 6 ++++-- cores/esp8266/FSImpl.h | 1 + cores/esp8266/spiffs_api.h | 7 ++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index 4951b798b7..9461d9fddc 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -262,6 +262,13 @@ void FS::end() { } } +bool FS::gc() { + if (!_impl) { + return false; + } + return _impl->gc(); +} + bool FS::format() { if (!_impl) { return false; diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index d9c43ef3b8..2cc6acd124 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -107,7 +107,7 @@ class File : public Stream File openNextFile(); String readString() override; - + protected: FileImplPtr _p; @@ -181,7 +181,7 @@ class FS bool begin(); void end(); - + bool format(); bool info(FSInfo& info); @@ -206,6 +206,8 @@ class FS bool rmdir(const char* path); bool rmdir(const String& path); + bool gc(); + protected: FSImplPtr _impl; }; diff --git a/cores/esp8266/FSImpl.h b/cores/esp8266/FSImpl.h index 4c0ab2ccff..a668f2887d 100644 --- a/cores/esp8266/FSImpl.h +++ b/cores/esp8266/FSImpl.h @@ -82,6 +82,7 @@ class FSImpl { virtual bool remove(const char* path) = 0; virtual bool mkdir(const char* path) = 0; virtual bool rmdir(const char* path) = 0; + virtual bool gc() { return true; } // May not be implemented in all file systems. }; } // namespace fs diff --git a/cores/esp8266/spiffs_api.h b/cores/esp8266/spiffs_api.h index a0136849e8..95600a22ad 100644 --- a/cores/esp8266/spiffs_api.h +++ b/cores/esp8266/spiffs_api.h @@ -208,6 +208,11 @@ class SPIFFSImpl : public FSImpl return true; } + bool gc() override + { + return SPIFFS_gc_quick( &_fs, 0 ) == SPIFFS_OK; + } + protected: friend class SPIFFSFileImpl; friend class SPIFFSDirImpl; @@ -290,7 +295,7 @@ class SPIFFSImpl : public FSImpl (void) report; (void) arg1; (void) arg2; - + // TODO: spiffs doesn't pass any context pointer along with _check_cb, // so we can't do anything useful here other than perhaps // feeding the watchdog