Skip to content

audit: fl/system/file_system.h has drifted from the API Object Pattern #4003

Closed

Description

@zackees

Summary

src/fl/system/file_system.h (146 lines) has drifted from the conventions documented in agents/docs/cpp-standards.md. It is the header every filesystem-using sketch includes, and it currently mixes the public API, the platform extension point, per-format convenience methods, test-only hooks, and an inline implementation. Requesting an audit and a decision on whether to rectify.

Surfaced while adding a LittleFS backend (#3984) — the natural next step was to add beginLittleFs() / littleFs() next to beginSd() / sd(), which would have widened the class again. That was backed out in favor of the existing generic seam, but it made the shape hard to ignore.

Observations

1. The API object contains implementation

cpp-standards.md → API Object Pattern, rule 2: "The API object wraps, it doesn't implement... contains no core logic itself."

file_system.h ends with a fully inline loadJpegFromSD() that constructs a FileSystem, calls beginSd(), builds an error string, and loads a JPEG. That is core logic living in the public header, and it hard-couples SD to JPEG inside the filesystem API.

2. No matching implementation directory

Rule 1: "One header, one directory, same name. foo.h is the public API; foo/ holds everything behind it."

There is no fl/system/file_system/. The implementation is spread across fl/system/file_system.cpp.hpp, fl/system/sd/, and (proposed) fl/system/littlefs/. The directory naming does not follow the documented pairing, so there is no obvious home for a new backend.

3. FileSystem accumulates a method per format

The class currently exposes openVideo, openMpeg1Video, readText, readJson, readScreenMaps, readScreenMap, loadJpeg, openMp3, and loadFled. A filesystem class knows about JPEG, MP3, JSON, ScreenMap, Video, and Fled.

Consequences worth weighing:

  • every new asset format widens the class
  • the header drags JpegConfig, Mp3DecoderPtr, json, ScreenMap, Video, Fled, and flat_map declarations into the include graph of anyone who touches a file
  • the natural place for readFoo() is here, so the pressure is always toward more

An alternative shape is free functions taking a FileSystem& (or a filebuf_ptr), which keeps the class to open/read/close and lets each format live with its own decoder. Not proposing that as the answer — just naming the axis the audit should decide on.

4. Public API and platform extension point share a header

FsImpl — the interface a platform subclasses — is declared in the same header as the user-facing FileSystem. Sketch authors and platform implementers read the same file for different reasons. make_sdcard_filesystem() / make_littlefs_filesystem() sit in an earlier namespace fl {} block, separated from FsImpl by the entire FileSystem class.

5. Test-only symbols in the public header

setTestFileSystemRoot() / getTestFileSystemRoot() are declared under #ifdef FASTLED_TESTING in the public header, with the comment noting they are implemented in platforms/stub/fs_stub.hpp.

6. Two namespace fl { } blocks

The file opens and closes namespace fl twice with unrelated declarations in each. Probably incremental growth rather than intent.

What this issue is asking for

  1. Audit file_system.h against agents/docs/cpp-standards.md, especially the API Object Pattern.
  2. Decide which divergences are deliberate (there may be good linker or include-graph reasons — the SD split in fl/system/sd/ is clearly deliberate and well documented) and which are drift.
  3. If rectifying: agree the target shape before more backends land, so LittleFS (feat: add a LittleFS FsImpl backend — on-chip flash storage is unreachable today #3984) and any future filesystem are added to the intended structure rather than bolted onto the current one.

Note on the SD split

fl/system/sd/ and src/fl/build/fl.system.sd+.cpp are a deliberate, well-commented design — the SD chain gets its own translation unit so the linker can dead-strip ~16 KB on ESP32-S3 when beginSd() is never called (FastLED #2773 item 1.2). Any restructuring must preserve that property. It is a constraint on the fix, not part of the problem.

Related

Activity

  1. zackees commented on Aug 23, 2026

    @zackees
    MemberAuthor

    Additional evidence: the header re-exports codec headers

    Observation 3 above is stronger than I first described. The top of file_system.h does not merely forward-declare the format types — it re-exports their headers:

    #include "fl/stl/fstream.h"            // IWYU pragma: export
    #include "fl/stl/flat_map.h"           // IWYU pragma: export
    #include "fl/stl/string.h"             // IWYU pragma: export
    #include "fl/fx/video.h"               // IWYU pragma: export
    #include "fl/codec/jpeg.h"             // IWYU pragma: export
    #include "fl/stl/compiler_control.h"   // IWYU pragma: export

    IWYU pragma: export means a consumer that includes file_system.h is intended to get video.h and jpeg.h transitively. So every sketch that opens a file also pulls the video and JPEG headers into its translation unit, whether or not it decodes anything.

    That is the include-graph cost of the god-object shape, and it is the part most likely to show up as compile time and flash on constrained targets.

    Note the mixed strategy: Mp3Decoder is forward-declared (lines 17-20) rather than included, and ScreenMap / Fled / Video are forward-declared again lower down — so the file already demonstrates the lighter approach for some formats while including others outright. Whatever the audit concludes, being consistent here looks like a cheap win.

  2. zackees commented on Sep 1, 2026

    @zackees
    MemberAuthor

    Resolved by #4010 (merged as a5d65bb). The audit produced and implemented the target shape under src/fl/fs/: FsImpl is isolated in backend.h, format-oriented free functions live in read.h with out-of-line bodies, test hooks live in testing.h, the SD backend retains its separate translation unit for dead-stripping, and fl/system/file_system.h is now only a compatibility forwarder. The former inline loadJpegFromSD() implementation moved out of the public header.\n\nThe remaining legacy FileSystem format members and their codec includes were explicitly reviewed and deliberately retained for source compatibility; new formats use the free-function seam instead, with removal reserved for a future breaking/deprecation cycle. #3984 subsequently landed through the generic backend interface and is closed.

  3. moved this from Triage to Done in FastLED Trackeron Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions