diff --git a/src/core/filesystem.cpp b/src/core/filesystem.cpp index 611520c..5339aaf 100644 --- a/src/core/filesystem.cpp +++ b/src/core/filesystem.cpp @@ -4,10 +4,52 @@ #include #include "log.h" -void filesystemMount() { +void fsMount() { if (LittleFS.begin()) { - info("filesystem mounted: %3d%% used", (int) round(100.0 * (LittleFS.usedBytes()) / LittleFS.totalBytes())); + info("filesystem mounted: %3d%% used (%d bytes)", static_cast(round(100.0 * LittleFS.usedBytes() / LittleFS.totalBytes())), LittleFS.usedBytes()); + fsList(LittleFS, "/"); } else { error("failed to mount filesystem"); } } + +void fsList(fs::FS& fs, const char *path, const String& indent) { + auto dir = fs.open(path); + if (!dir) { + error("not found: %s", path); + return; + } + if (!dir.isDirectory()) { + error("not a directory: %s", path); + return; + } + + info("LS: %s %4s %s", indent, "", dir.name()); + + const auto indent2 = indent + " "; + + while (true) { + auto file = dir.openNextFile(); + if (!file) { + break; + } + if (file.isDirectory()) { + fsList(fs, file.path()); + } + file.close(); + } + + dir.rewindDirectory(); + while (true) { + auto file = dir.openNextFile(); + if (!file) { + break; + } + if (!file.isDirectory()) { + info("LS: %s %4d %s", indent2, file.size(), file.name()); + } + file.close(); + } + + dir.close(); +} diff --git a/src/core/filesystem.h b/src/core/filesystem.h index dc08a30..a4dec85 100644 --- a/src/core/filesystem.h +++ b/src/core/filesystem.h @@ -1,6 +1,11 @@ #ifndef FILESYSTEM_H #define FILESYSTEM_H -void filesystemMount() ; +#include +#include + +void fsMount(); + +void fsList(fs::FS& fs, const char *path, const String& indent = ""); #endif diff --git a/src/core/log.cpp b/src/core/log.cpp index edd04e0..92236e3 100644 --- a/src/core/log.cpp +++ b/src/core/log.cpp @@ -6,6 +6,7 @@ #include #include "clock.h" +#include "filesystem.h" void doLog(LogLevel level, const char *format, va_list args); @@ -50,7 +51,7 @@ void execute(const char *cmd) { info(" %-10s %s", "startup:", getStartupStr()); info(" %-10s %s", "uptime:", getUptimeStr()); } else if (strcmp(cmd, "ls") == 0) { - info("TODO"); + fsList(LittleFS, "/", ""); } else { info("UNKNOWN COMMAND: %s", cmd); } diff --git a/src/main.cpp b/src/main.cpp index 24e6838..65332bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,7 @@ void setup() { logSetup(); bootDelay(); - filesystemMount(); + fsMount(); httpSetup(); appStart(APP_MATCH_NAME); }