Skip to content

littlefs: fixes for mock/emulation, use in FSBrowser example #6211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#include <LittleFS.h>

//FS* filesystem = &SPIFFS;
FS* filesystem = &LittleFS;

#define DBG_OUTPUT_PORT Serial

Expand Down Expand Up @@ -94,11 +98,11 @@ bool handleFileRead(String path) {
}
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if (SPIFFS.exists(pathWithGz)) {
if (filesystem->exists(pathWithGz) || filesystem->exists(path)) {
if (filesystem->exists(pathWithGz)) {
path += ".gz";
}
File file = SPIFFS.open(path, "r");
File file = filesystem->open(path, "r");
server.streamFile(file, contentType);
file.close();
return true;
Expand All @@ -117,7 +121,7 @@ void handleFileUpload() {
filename = "/" + filename;
}
DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
fsUploadFile = SPIFFS.open(filename, "w");
fsUploadFile = filesystem->open(filename, "w");
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
//DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
Expand All @@ -141,10 +145,10 @@ void handleFileDelete() {
if (path == "/") {
return server.send(500, "text/plain", "BAD PATH");
}
if (!SPIFFS.exists(path)) {
if (!filesystem->exists(path)) {
return server.send(404, "text/plain", "FileNotFound");
}
SPIFFS.remove(path);
filesystem->remove(path);
server.send(200, "text/plain", "");
path = String();
}
Expand All @@ -158,10 +162,10 @@ void handleFileCreate() {
if (path == "/") {
return server.send(500, "text/plain", "BAD PATH");
}
if (SPIFFS.exists(path)) {
if (filesystem->exists(path)) {
return server.send(500, "text/plain", "FILE EXISTS");
}
File file = SPIFFS.open(path, "w");
File file = filesystem->open(path, "w");
if (file) {
file.close();
} else {
Expand All @@ -179,7 +183,7 @@ void handleFileList() {

String path = server.arg("dir");
DBG_OUTPUT_PORT.println("handleFileList: " + path);
Dir dir = SPIFFS.openDir(path);
Dir dir = filesystem->openDir(path);
path = String();

String output = "[";
Expand All @@ -192,7 +196,11 @@ void handleFileList() {
output += "{\"type\":\"";
output += (isDir) ? "dir" : "file";
output += "\",\"name\":\"";
output += String(entry.name()).substring(1);
if (entry.name()[0] == '/') {
output += &(entry.name()[1]);
} else {
output += entry.name();
}
output += "\"}";
entry.close();
}
Expand All @@ -205,9 +213,9 @@ void setup(void) {
DBG_OUTPUT_PORT.begin(115200);
DBG_OUTPUT_PORT.print("\n");
DBG_OUTPUT_PORT.setDebugOutput(true);
SPIFFS.begin();
filesystem->begin();
{
Dir dir = SPIFFS.openDir("/");
Dir dir = filesystem->openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Expand Down
24 changes: 14 additions & 10 deletions tests/host/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $(warning Cannot compile in 32 bit mode, switching to native mode)
else
N32 = 32
M32 = -m32
E32 = .32
endif
endif

Expand Down Expand Up @@ -106,9 +107,9 @@ MOCK_CPP_FILES_EMU := $(MOCK_CPP_FILES_COMMON) $(addprefix common/,\
ArduinoMain.cpp \
ArduinoMainUdp.cpp \
ArduinoMainSpiffs.cpp \
ArduinoMainLittlefs.cpp \
user_interface.cpp \
)
#(not in tree) ArduinoMainLittlefs.cpp

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

C_SOURCE_FILES = $(MOCK_C_FILES) $(CORE_C_FILES)
CPP_SOURCE_FILES = $(MOCK_CPP_FILES) $(CORE_CPP_FILES) $(TEST_CPP_FILES)
C_OBJECTS = $(C_SOURCE_FILES:.c=.c.o)
C_OBJECTS = $(C_SOURCE_FILES:.c=.c$(E32).o)

CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp.o) $(CORE_CPP_FILES:.cpp=.cpp.o)
CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp.o)
CPP_OBJECTS_CORE = $(MOCK_CPP_FILES:.cpp=.cpp$(E32).o) $(CORE_CPP_FILES:.cpp=.cpp$(E32).o)
CPP_OBJECTS_TESTS = $(TEST_CPP_FILES:.cpp=.cpp$(E32).o)

CPP_OBJECTS = $(CPP_OBJECTS_CORE) $(CPP_OBJECTS_TESTS)

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

clean: clean-objects clean-coverage # clean everything
clean:
make FORCE32=0 cleanarch; make FORCE32=1 cleanarch

cleanarch: clean-objects clean-coverage # clean everything
rm -rf $(BINDIR)

clean-objects:
Expand Down Expand Up @@ -215,11 +219,11 @@ build-info: # show toolchain version
-include $(BINDIR)/.*.d
.SUFFIXES:

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

.PRECIOUS: %.cpp.o
%.cpp.o: %.cpp
.PRECIOUS: %.cpp$(E32).o
%.cpp$(E32).o: %.cpp
$(VERBCXX) $(CXX) $(PREINCLUDES) $(CXXFLAGS) $(INC_PATHS) -MD -MF $(BINDIR)/.$(notdir $<).d -c -o $@ $<

$(BINDIR)/core.a: $(C_OBJECTS) $(CPP_OBJECTS_CORE)
Expand Down Expand Up @@ -307,13 +311,13 @@ USERLIBDIRS = $(shell test -z "$(ULIBPATHS)" || for d in $(ULIBPATHS); do for dd
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)
INC_PATHS += $(USERLIBDIRS)
INC_PATHS += -I$(INODIR)/..
CPP_OBJECTS_CORE_EMU = $(CPP_SOURCES_CORE_EMU:.cpp=.cpp.o) $(USERLIBSRCS:.cpp=.cpp.o)
CPP_OBJECTS_CORE_EMU = $(CPP_SOURCES_CORE_EMU:.cpp=.cpp$(E32).o) $(USERLIBSRCS:.cpp=.cpp$(E32).o)

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

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

Expand Down
17 changes: 17 additions & 0 deletions tests/host/common/ArduinoMainLittlefs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include "littlefs_mock.h"

LittleFSMock* littlefs_mock = nullptr;

void mock_start_littlefs (const String& fname, size_t size_kb, size_t block_kb, size_t page_b)
{
littlefs_mock = new LittleFSMock(size_kb * 1024, block_kb * 1024, page_b, fname);
}

void mock_stop_littlefs ()
{
if (littlefs_mock)
delete littlefs_mock;
littlefs_mock = nullptr;
}