From 4372fa889423158cd130c94215985f792cc51a0c Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 2 Nov 2019 01:04:24 +0100 Subject: [PATCH 1/5] define two weak functions defaulting to no-op redefine them to do something useful when either spiffs or littlefs are used --- cores/esp8266/FS.h | 6 ++++++ cores/esp8266/spiffs_api.cpp | 6 ++++++ .../src/ESP8266HTTPUpdateServer-impl.h | 5 ++--- libraries/LittleFS/src/LittleFS.cpp | 6 ++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 39fd7c0f06..1f2b402bdc 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -245,6 +245,12 @@ class FS } // namespace fs +extern "C" +{ +void littlefs_weak_end(void); +void spiffs_weak_end(void); +} + #ifndef FS_NO_GLOBALS using fs::FS; using fs::File; diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 04fc461d1e..4e62d7607e 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -127,6 +127,12 @@ bool isSpiffsFilenameValid(const char* name) }; // namespace +extern "C" void spiffs_weak_end(void) +{ + //ets_printf("debug: not weak spiffs end\n"); + SPIFFS.end(); +} + // these symbols should be defined in the linker script for each flash layout #ifndef CORE_MOCK #ifdef ARDUINO diff --git a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h index 6761177a0a..fa390e033f 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,8 @@ 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(); + spiffs_weak_end(); + littlefs_weak_end(); 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 b4aba1970a..94ff8c6d35 100644 --- a/libraries/LittleFS/src/LittleFS.cpp +++ b/libraries/LittleFS/src/LittleFS.cpp @@ -181,6 +181,12 @@ int LittleFSImpl::lfs_flash_sync(const struct lfs_config *c) { }; // namespace +extern "C" void littlefs_weak_end(void) +{ + //ets_printf("debug: not weak littlefs end\n"); + LittleFS.end(); +} + // these symbols should be defined in the linker script for each flash layout #ifndef CORE_MOCK #ifdef ARDUINO From f83de1af4a04a90d0757961d91ac43b5c3592f23 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 2 Nov 2019 01:10:13 +0100 Subject: [PATCH 2/5] noop --- cores/esp8266/FSnoop.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 cores/esp8266/FSnoop.cpp diff --git a/cores/esp8266/FSnoop.cpp b/cores/esp8266/FSnoop.cpp new file mode 100644 index 0000000000..2a897887f7 --- /dev/null +++ b/cores/esp8266/FSnoop.cpp @@ -0,0 +1,23 @@ +/* + * no-op implementations + * used/linked when no not-weak implementation already exists elsewhere + */ + +#include + +extern "C" +{ + +void littlefs_weak_end(void) __attribute__((weak)); +void littlefs_weak_end(void) +{ + //ets_printf("debug: noop: littlefs_weak_end\n"); +} + +void spiffs_weak_end(void) __attribute__((weak)); +void spiffs_weak_end(void) +{ + //ets_printf("debug: noop: spiffs_weak_end\n"); +} + +} From c7242ad8a796d710b01a3256772a1592c82c4c07 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 2 Nov 2019 01:31:47 +0100 Subject: [PATCH 3/5] single entry point for closing FSes --- cores/esp8266/FS.h | 1 + cores/esp8266/FSnoop.cpp | 6 ++++++ .../src/ESP8266HTTPUpdateServer-impl.h | 3 +-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 1f2b402bdc..6926285dd9 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -247,6 +247,7 @@ class FS extern "C" { +void close_all_fs(void); void littlefs_weak_end(void); void spiffs_weak_end(void); } diff --git a/cores/esp8266/FSnoop.cpp b/cores/esp8266/FSnoop.cpp index 2a897887f7..7f2db1d787 100644 --- a/cores/esp8266/FSnoop.cpp +++ b/cores/esp8266/FSnoop.cpp @@ -8,6 +8,12 @@ extern "C" { +void close_all_fs(void) +{ + littlefs_weak_end(); + spiffs_weak_end(); +} + void littlefs_weak_end(void) __attribute__((weak)); void littlefs_weak_end(void) { diff --git a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h index fa390e033f..190a597066 100644 --- a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h +++ b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h @@ -93,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_weak_end(); - littlefs_weak_end(); + close_all_fs(); if (!Update.begin(fsSize, U_FS)){//start with max available size if (_serial_output) Update.printError(Serial); } From 664b05f46b419e4771344999632f74b69533b80d Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 4 Nov 2019 22:47:28 +0100 Subject: [PATCH 4/5] rename functions, override when instanciated, add link to explanation --- cores/esp8266/FS.h | 4 ++-- cores/esp8266/FSnoop.cpp | 22 +++++++++++++--------- cores/esp8266/spiffs_api.cpp | 14 ++++++++------ libraries/LittleFS/src/LittleFS.cpp | 14 ++++++++------ 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 6926285dd9..60a0e46d2d 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -248,8 +248,8 @@ class FS extern "C" { void close_all_fs(void); -void littlefs_weak_end(void); -void spiffs_weak_end(void); +void littlefs_request_end(void); +void spiffs_request_end(void); } #ifndef FS_NO_GLOBALS diff --git a/cores/esp8266/FSnoop.cpp b/cores/esp8266/FSnoop.cpp index 7f2db1d787..6cead006f2 100644 --- a/cores/esp8266/FSnoop.cpp +++ b/cores/esp8266/FSnoop.cpp @@ -1,6 +1,6 @@ /* * no-op implementations - * used/linked when no not-weak implementation already exists elsewhere + * used/linked when no strong implementation already exists elsewhere */ #include @@ -10,20 +10,24 @@ extern "C" void close_all_fs(void) { - littlefs_weak_end(); - spiffs_weak_end(); + littlefs_request_end(); + spiffs_request_end(); } -void littlefs_weak_end(void) __attribute__((weak)); -void littlefs_weak_end(void) +// 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_weak_end\n"); + //ets_printf("debug: noop: littlefs_request_end\n"); } -void spiffs_weak_end(void) __attribute__((weak)); -void spiffs_weak_end(void) +void spiffs_request_end(void) __attribute__((weak)); +void spiffs_request_end(void) { - //ets_printf("debug: noop: spiffs_weak_end\n"); + //ets_printf("debug: noop: spiffs_request_end\n"); } } diff --git a/cores/esp8266/spiffs_api.cpp b/cores/esp8266/spiffs_api.cpp index 4e62d7607e..1f0278bfc5 100644 --- a/cores/esp8266/spiffs_api.cpp +++ b/cores/esp8266/spiffs_api.cpp @@ -127,12 +127,6 @@ bool isSpiffsFilenameValid(const char* name) }; // namespace -extern "C" void spiffs_weak_end(void) -{ - //ets_printf("debug: not weak spiffs end\n"); - SPIFFS.end(); -} - // these symbols should be defined in the linker script for each flash layout #ifndef CORE_MOCK #ifdef ARDUINO @@ -147,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/libraries/LittleFS/src/LittleFS.cpp b/libraries/LittleFS/src/LittleFS.cpp index 94ff8c6d35..c30bb9f73d 100644 --- a/libraries/LittleFS/src/LittleFS.cpp +++ b/libraries/LittleFS/src/LittleFS.cpp @@ -181,12 +181,6 @@ int LittleFSImpl::lfs_flash_sync(const struct lfs_config *c) { }; // namespace -extern "C" void littlefs_weak_end(void) -{ - //ets_printf("debug: not weak littlefs end\n"); - LittleFS.end(); -} - // these symbols should be defined in the linker script for each flash layout #ifndef CORE_MOCK #ifdef ARDUINO @@ -196,6 +190,14 @@ extern "C" void littlefs_weak_end(void) #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 From 88b5303e05b7b2e400d15c9b55459debee4588ba Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 4 Nov 2019 22:48:24 +0100 Subject: [PATCH 5/5] spiffs: call end on destructor --- cores/esp8266/spiffs_api.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cores/esp8266/spiffs_api.h b/cores/esp8266/spiffs_api.h index 44f34cd1d4..a40d59b0a8 100644 --- a/cores/esp8266/spiffs_api.h +++ b/cores/esp8266/spiffs_api.h @@ -80,6 +80,11 @@ class SPIFFSImpl : public FSImpl 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;