Skip to content

Commit fc77f2e

Browse files
authored
littlefs: fixes for mock/emulation, use in FSBrowser example (#6211)
* littlefs: fixes for mock/emulation, use in FSBrowser example * emulation: makefile: integrate arch size into object file names
1 parent a78fb72 commit fc77f2e

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino

+20-12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include <ESP8266WebServer.h>
2828
#include <ESP8266mDNS.h>
2929
#include <FS.h>
30+
#include <LittleFS.h>
31+
32+
//FS* filesystem = &SPIFFS;
33+
FS* filesystem = &LittleFS;
3034

3135
#define DBG_OUTPUT_PORT Serial
3236

@@ -94,11 +98,11 @@ bool handleFileRead(String path) {
9498
}
9599
String contentType = getContentType(path);
96100
String pathWithGz = path + ".gz";
97-
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
98-
if (SPIFFS.exists(pathWithGz)) {
101+
if (filesystem->exists(pathWithGz) || filesystem->exists(path)) {
102+
if (filesystem->exists(pathWithGz)) {
99103
path += ".gz";
100104
}
101-
File file = SPIFFS.open(path, "r");
105+
File file = filesystem->open(path, "r");
102106
server.streamFile(file, contentType);
103107
file.close();
104108
return true;
@@ -117,7 +121,7 @@ void handleFileUpload() {
117121
filename = "/" + filename;
118122
}
119123
DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
120-
fsUploadFile = SPIFFS.open(filename, "w");
124+
fsUploadFile = filesystem->open(filename, "w");
121125
filename = String();
122126
} else if (upload.status == UPLOAD_FILE_WRITE) {
123127
//DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
@@ -141,10 +145,10 @@ void handleFileDelete() {
141145
if (path == "/") {
142146
return server.send(500, "text/plain", "BAD PATH");
143147
}
144-
if (!SPIFFS.exists(path)) {
148+
if (!filesystem->exists(path)) {
145149
return server.send(404, "text/plain", "FileNotFound");
146150
}
147-
SPIFFS.remove(path);
151+
filesystem->remove(path);
148152
server.send(200, "text/plain", "");
149153
path = String();
150154
}
@@ -158,10 +162,10 @@ void handleFileCreate() {
158162
if (path == "/") {
159163
return server.send(500, "text/plain", "BAD PATH");
160164
}
161-
if (SPIFFS.exists(path)) {
165+
if (filesystem->exists(path)) {
162166
return server.send(500, "text/plain", "FILE EXISTS");
163167
}
164-
File file = SPIFFS.open(path, "w");
168+
File file = filesystem->open(path, "w");
165169
if (file) {
166170
file.close();
167171
} else {
@@ -179,7 +183,7 @@ void handleFileList() {
179183

180184
String path = server.arg("dir");
181185
DBG_OUTPUT_PORT.println("handleFileList: " + path);
182-
Dir dir = SPIFFS.openDir(path);
186+
Dir dir = filesystem->openDir(path);
183187
path = String();
184188

185189
String output = "[";
@@ -192,7 +196,11 @@ void handleFileList() {
192196
output += "{\"type\":\"";
193197
output += (isDir) ? "dir" : "file";
194198
output += "\",\"name\":\"";
195-
output += String(entry.name()).substring(1);
199+
if (entry.name()[0] == '/') {
200+
output += &(entry.name()[1]);
201+
} else {
202+
output += entry.name();
203+
}
196204
output += "\"}";
197205
entry.close();
198206
}
@@ -205,9 +213,9 @@ void setup(void) {
205213
DBG_OUTPUT_PORT.begin(115200);
206214
DBG_OUTPUT_PORT.print("\n");
207215
DBG_OUTPUT_PORT.setDebugOutput(true);
208-
SPIFFS.begin();
216+
filesystem->begin();
209217
{
210-
Dir dir = SPIFFS.openDir("/");
218+
Dir dir = filesystem->openDir("/");
211219
while (dir.next()) {
212220
String fileName = dir.fileName();
213221
size_t fileSize = dir.fileSize();

tests/host/Makefile

+14-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ $(warning Cannot compile in 32 bit mode, switching to native mode)
2626
else
2727
N32 = 32
2828
M32 = -m32
29+
E32 = .32
2930
endif
3031
endif
3132

@@ -106,9 +107,9 @@ MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\
106107
ArduinoMain.cpp \
107108
ArduinoMainUdp.cpp \
108109
ArduinoMainSpiffs.cpp \
110+
ArduinoMainLittlefs.cpp \
109111
user_interface.cpp \
110112
)
111-
#(not in tree) ArduinoMainLittlefs.cpp
112113

113114
MOCK_C_FILES := $(addprefix common/,\
114115
md5.c \
@@ -163,10 +164,10 @@ remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-ou
163164

164165
C_SOURCE_FILES = $(MOCK_C_FILES) $(CORE_C_FILES)
165166
CPP_SOURCE_FILES = $(MOCK_CPP_FILES) $(CORE_CPP_FILES) $(TEST_CPP_FILES)
166-
C_OBJECTS = $(C_SOURCE_FILES:.c=.c.o)
167+
C_OBJECTS = $(C_SOURCE_FILES:.c=.c$(E32).o)
167168

168-
CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp.o) $(CORE_CPP_FILES:.cpp=.cpp.o)
169-
CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp.o)
169+
CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp$(E32).o) $(CORE_CPP_FILES:.cpp=.cpp$(E32).o)
170+
CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp$(E32).o)
170171

171172
CPP_OBJECTS = $(CPP_OBJECTS_CORE) $(CPP_OBJECTS_TESTS)
172173

@@ -183,7 +184,10 @@ doCI: build-info $(OUTPUT_BINARY) valgrind test gcov
183184
test: $(OUTPUT_BINARY) # run host test for CI
184185
$(OUTPUT_BINARY)
185186

186-
clean: clean-objects clean-coverage # clean everything
187+
clean:
188+
make FORCE32=0 cleanarch; make FORCE32=1 cleanarch
189+
190+
cleanarch: clean-objects clean-coverage # clean everything
187191
rm -rf $(BINDIR)
188192

189193
clean-objects:
@@ -215,11 +219,11 @@ build-info: # show toolchain version
215219
-include $(BINDIR)/.*.d
216220
.SUFFIXES:
217221

218-
%.c.o: %.c
222+
%.c$(E32).o: %.c
219223
$(VERBC) $(CC) $(PREINCLUDES) $(CFLAGS) $(INC_PATHS) -MD -MF $(BINDIR)/.$(notdir $<).d -c -o $@ $<
220224

221-
.PRECIOUS: %.cpp.o
222-
%.cpp.o: %.cpp
225+
.PRECIOUS: %.cpp$(E32).o
226+
%.cpp$(E32).o: %.cpp
223227
$(VERBCXX) $(CXX) $(PREINCLUDES) $(CXXFLAGS) $(INC_PATHS) -MD -MF $(BINDIR)/.$(notdir $<).d -c -o $@ $<
224228

225229
$(BINDIR)/core.a: $(C_OBJECTS) $(CPP_OBJECTS_CORE)
@@ -307,13 +311,13 @@ USERLIBDIRS = $(shell test -z "$(ULIBPATHS)" || for d in $(ULIBPATHS); do for dd
307311
USERLIBSRCS = $(shell test -z "$(ULIBPATHS)" || for d in $(ULIBPATHS); do for ss in $$d/*.cpp $$d/src/*.cpp; do test -r $$ss && echo $$ss; done; done)
308312
INC_PATHS += $(USERLIBDIRS)
309313
INC_PATHS += -I$(INODIR)/..
310-
CPP_OBJECTS_CORE_EMU = $(CPP_SOURCES_CORE_EMU:.cpp=.cpp.o) $(USERLIBSRCS:.cpp=.cpp.o)
314+
CPP_OBJECTS_CORE_EMU = $(CPP_SOURCES_CORE_EMU:.cpp=.cpp$(E32).o) $(USERLIBSRCS:.cpp=.cpp$(E32).o)
311315

312316
bin/fullcore.a: $(C_OBJECTS) $(CPP_OBJECTS_CORE_EMU)
313317
$(VERBAR) ar -rcu $@ $^
314318
$(VERBAR) ranlib -c $@
315319

316-
%: %.ino.cpp.o bin/fullcore.a
320+
%: %.ino.cpp$(E32).o bin/fullcore.a
317321
$(VERBLD) $(CXX) $(LDFLAGS) $< bin/fullcore.a $(LIBSSL) -o $@
318322
@echo "----> $@ <----"
319323

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#include "littlefs_mock.h"
3+
4+
LittleFSMock* littlefs_mock = nullptr;
5+
6+
void mock_start_littlefs (const String& fname, size_t size_kb, size_t block_kb, size_t page_b)
7+
{
8+
littlefs_mock = new LittleFSMock(size_kb * 1024, block_kb * 1024, page_b, fname);
9+
}
10+
11+
void mock_stop_littlefs ()
12+
{
13+
if (littlefs_mock)
14+
delete littlefs_mock;
15+
littlefs_mock = nullptr;
16+
}
17+

0 commit comments

Comments
 (0)