Skip to content

Commit 6f7eb28

Browse files
d-a-vdevyte
authored andcommitted
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
1 parent c28838d commit 6f7eb28

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

cores/esp8266/FS.h

+7
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ class FS
245245

246246
} // namespace fs
247247

248+
extern "C"
249+
{
250+
void close_all_fs(void);
251+
void littlefs_request_end(void);
252+
void spiffs_request_end(void);
253+
}
254+
248255
#ifndef FS_NO_GLOBALS
249256
using fs::FS;
250257
using fs::File;

cores/esp8266/FSnoop.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* no-op implementations
3+
* used/linked when no strong implementation already exists elsewhere
4+
*/
5+
6+
#include <FS.h>
7+
8+
extern "C"
9+
{
10+
11+
void close_all_fs(void)
12+
{
13+
littlefs_request_end();
14+
spiffs_request_end();
15+
}
16+
17+
// default weak definitions
18+
// they are overriden in their respective real implementation
19+
// hint: https://github.com/esp8266/Arduino/pull/6699#issuecomment-549085382
20+
21+
void littlefs_request_end(void) __attribute__((weak));
22+
void littlefs_request_end(void)
23+
{
24+
//ets_printf("debug: noop: littlefs_request_end\n");
25+
}
26+
27+
void spiffs_request_end(void) __attribute__((weak));
28+
void spiffs_request_end(void)
29+
{
30+
//ets_printf("debug: noop: spiffs_request_end\n");
31+
}
32+
33+
}

cores/esp8266/spiffs_api.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ FS SPIFFS = FS(FSImplPtr(new spiffs_impl::SPIFFSImpl(
141141
FS_PHYS_PAGE,
142142
FS_PHYS_BLOCK,
143143
SPIFFS_MAX_OPEN_FILES)));
144+
145+
extern "C" void spiffs_request_end(void)
146+
{
147+
// override default weak function
148+
//ets_printf("debug: not weak spiffs end\n");
149+
SPIFFS.end();
150+
}
151+
144152
#endif // ARDUINO
145153
#endif // !CORE_MOCK
146154

cores/esp8266/spiffs_api.h

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class SPIFFSImpl : public FSImpl
8080
memset(&_fs, 0, sizeof(_fs));
8181
}
8282

83+
~SPIFFSImpl()
84+
{
85+
end();
86+
}
87+
8388
FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override;
8489
bool exists(const char* path) override;
8590
DirImplPtr openDir(const char* path) override;

libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <WiFiUdp.h>
66
#include <flash_hal.h>
77
#include <FS.h>
8-
#include <LittleFS.h>
98
#include "StreamString.h"
109
#include "ESP8266HTTPUpdateServer.h"
1110

@@ -94,8 +93,7 @@ void ESP8266HTTPUpdateServerTemplate<ServerType>::setup(ESP8266WebServerTemplate
9493
Serial.printf("Update: %s\n", upload.filename.c_str());
9594
if (upload.name == "filesystem") {
9695
size_t fsSize = ((size_t) &_FS_end - (size_t) &_FS_start);
97-
SPIFFS.end();
98-
LittleFS.end();
96+
close_all_fs();
9997
if (!Update.begin(fsSize, U_FS)){//start with max available size
10098
if (_serial_output) Update.printError(Serial);
10199
}

libraries/LittleFS/src/LittleFS.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ int LittleFSImpl::lfs_flash_sync(const struct lfs_config *c) {
190190

191191
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS)
192192
FS LittleFS = FS(FSImplPtr(new littlefs_impl::LittleFSImpl(FS_PHYS_ADDR, FS_PHYS_SIZE, FS_PHYS_PAGE, FS_PHYS_BLOCK, FS_MAX_OPEN_FILES)));
193+
194+
extern "C" void littlefs_request_end(void)
195+
{
196+
// override default weak function
197+
//ets_printf("debug: not weak littlefs end\n");
198+
LittleFS.end();
199+
}
200+
193201
#endif
194202

195203
#endif // !CORE_MOCK

0 commit comments

Comments
 (0)