From 6f7eb2828a84ca8981ec487614f18c98ac9ccb14 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Thu, 7 Nov 2019 01:37:52 +0100 Subject: [PATCH] Make SPIFFS and LittleFS stay out of link when not needed (#6699) * define two weak functions defaulting to no-op redefine them to do something useful when either spiffs or littlefs are used * noop * single entry point for closing FSes * rename functions, override when instanciated, add link to explanation * spiffs: call end on destructor --- cores/esp8266/FS.h | 7 ++++ cores/esp8266/FSnoop.cpp | 33 +++++++++++++++++++ cores/esp8266/spiffs_api.cpp | 8 +++++ cores/esp8266/spiffs_api.h | 5 +++ .../src/ESP8266HTTPUpdateServer-impl.h | 4 +-- libraries/LittleFS/src/LittleFS.cpp | 8 +++++ 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 cores/esp8266/FSnoop.cpp diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 39fd7c0f0..60a0e46d2 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -245,6 +245,13 @@ protected: } // namespace fs +extern "C" +{ +void close_all_fs(void); +void littlefs_request_end(void); +void spiffs_request_end(void); +} + #ifndef FS_NO_GLOBALS using fs::FS; using fs::File; diff --git a/cores/esp8266/FSnoop.cpp b/cores/esp8266/FSnoop.cpp new file mode 100644 index 000000000..6cead006f --- /dev/null +++ b/cores/esp8266/FSnoop.cpp @@ -0,0 +1,33 @@ +/* + * no-op implementations + * used/linked when no strong implementation already exists elsewhere + */ + +#include + +extern "C" +{ + +void close_all_fs(void) +{ + littlefs_request_end(); + spiffs_request_end(); +} + +// default weak definitions +// they are overriden in their respective real implementation +// hint: https://github.com/esp8266/Arduino/pull/6699#issuecomment-549085382 + +void littlefs_request_end(void) __attribute__((weak)); +void littlefs_request_end(void) +{ + //ets_printf("debug: noop: littlefs_request_end\n"); +} + +void spiffs_request_end(void) __attribute__((weak)); +void spiffs_request_end(void) +{ + //ets_printf("debug: noop: spiffs_request_end\n"); +} + +} diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 04fc461d1..1f0278bfc 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -141,6 +141,14 @@ FS SPIFFS = FS(FSImplPtr(new spiffs_impl::SPIFFSImpl( FS_PHYS_PAGE, FS_PHYS_BLOCK, SPIFFS_MAX_OPEN_FILES))); + +extern "C" void spiffs_request_end(void) +{ + // override default weak function + //ets_printf("debug: not weak spiffs end\n"); + SPIFFS.end(); +} + #endif // ARDUINO #endif // !CORE_MOCK diff --git a/cores/esp8266/spiffs_api.h b/cores/esp8266/spiffs_api.h index 44f34cd1d..a40d59b0a 100644 --- a/cores/esp8266/spiffs_api.h +++ b/cores/esp8266/spiffs_api.h @@ -80,6 +80,11 @@ public: memset(&_fs, 0, sizeof(_fs)); } + ~SPIFFSImpl() + { + end(); + } + FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override; bool exists(const char* path) override; DirImplPtr openDir(const char* path) override; diff --git a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h index 6761177a0..190a59706 100644 --- a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h +++ b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h @@ -5,7 +5,6 @@ #include #include #include -#include #include "StreamString.h" #include "ESP8266HTTPUpdateServer.h" @@ -94,8 +93,7 @@ void ESP8266HTTPUpdateServerTemplate::setup(ESP8266WebServerTemplate Serial.printf("Update: %s\n", upload.filename.c_str()); if (upload.name == "filesystem") { size_t fsSize = ((size_t) &_FS_end - (size_t) &_FS_start); - SPIFFS.end(); - LittleFS.end(); + close_all_fs(); if (!Update.begin(fsSize, U_FS)){//start with max available size if (_serial_output) Update.printError(Serial); } diff --git a/libraries/LittleFS/src/LittleFS.cpp b/libraries/LittleFS/src/LittleFS.cpp index b4aba1970..c30bb9f73 100644 --- a/libraries/LittleFS/src/LittleFS.cpp +++ b/libraries/LittleFS/src/LittleFS.cpp @@ -190,6 +190,14 @@ int LittleFSImpl::lfs_flash_sync(const struct lfs_config *c) { #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS) FS LittleFS = FS(FSImplPtr(new littlefs_impl::LittleFSImpl(FS_PHYS_ADDR, FS_PHYS_SIZE, FS_PHYS_PAGE, FS_PHYS_BLOCK, FS_MAX_OPEN_FILES))); + +extern "C" void littlefs_request_end(void) +{ + // override default weak function + //ets_printf("debug: not weak littlefs end\n"); + LittleFS.end(); +} + #endif #endif // !CORE_MOCK