# rickyzheng/uffs architecture

> Architecture diagram of the GitHub repository rickyzheng/uffs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rickyzheng/uffs
- Repository: https://github.com/rickyzheng/uffs

## Overview

1. **What this repo is**
   UFFS is a C-based NAND flash file system for embedded systems, with a strong split between a portable core and pluggable platform/flash backends. The same codebase supports real devices, a PC simulator, and cross-platform testing.

2. **Core file-system engine**
   `src/uffs` contains the actual filesystem logic: mounting, directory/tree management, file descriptors, block bookkeeping, lookup, versioning, and public API entry points. This is the subsystem that translates filesystem operations into flash-page and block operations.

3. **Flash and device abstraction boundary**
   The architecture is built around a hardware abstraction layer in `src/inc/uffs/*.h` and `src/uffs/uffs_device.c`, `uffs_flash.c`, `uffs_fs.c`. The core does not talk directly to a specific NAND controller; instead it uses abstract flash/device interfaces so the same filesystem can run on bare metal, POSIX, Win32, or the emulator.

4. **Reliability and flash-management layers**
   Several subsystems exist specifically for NAND constraints:
   - `uffs_badblock.*` for bad-block detection and management
   - `uffs_ecc.*` for ECC handling
   - `uffs_crc.*` for page/data integrity checks
   - `uffs_blockinfo.*`, `uffs_buf.*`, `uffs_pool.*` for cached metadata and page buffering  
   These layers mediate between file operations and unreliable flash media.

5. **Tree, metadata, and recovery model**
   `uffs_tree.*`, `uffs_mtb.*`, and related code handle the logical tree of files/directories plus the “mount tree”/metadata reconstruction from flash. The readme emphasizes quick mount by scanning only spare data, so boot-time recovery is a key architectural path, not an afterthought.

6. **Memory model as a first-class design choice**
   UFFS is designed to work with static allocation as well as native/system allocation. `uffs_mem.*`, `uffs_pool.*`, and the example `static-mem-allocate.c` show that memory is intentionally bounded and configurable, which is central to the embedded use case.

7. **OS and portability layer**
   `src/platform/posix` and `src/platform/win32` provide OS wrappers (`uffs_os.c`, `uffs_config.h`) for threading, synchronization, timing, and platform-specific services. This is the main boundary that lets the filesystem run either with an RTOS/host OS or in a more bare-metal style environment.

8. **PC simulator / flash emulator**
   `src/emu` is the simulator-side flash model and command shell. Files like `uffs_fileem.c`, ECC variants, and `cmdline.c` emulate NAND behavior, bad blocks, and ECC so the filesystem can be exercised on a desktop without real hardware.

9. **Public API surface**
   `src/inc/uffs/uffs_public.h`, `uffs_fs.h`, `uffs_fd.h`, `uffs_flash.h`, and `uffs.h` define the API that application code uses. The repo includes example clients in `src/example` showing the intended integration path for embedded applications.

10. **Integration and test harness**
   `src/test/api_test` provides a server/client test setup that bridges the simulator and external test processes. This is used to compare UFFS behavior against a reference filesystem and validate POSIX-like semantics.

11. **SQLite regression suite reuse**
   A large vendored SQLite source tree lives under `src/test/sqlite3/sqlite-src-3070900`. UFFS adapts SQLite’s test infrastructure through the `test-uffs` harness and scripts, using it as a high-coverage filesystem regression workload. This is a major testing subsystem, not just a dependency copy.

12. **Command-line utilities**
   `src/utils/mkuffs.c` is the main tool for building and interacting with UFFS disk images/simulator data. It is part of the operational workflow: format the simulated flash, mount it, and run tests against it.

13. **Build and packaging**
   The project uses CMake at the top level and per-subdirectory CMake files to build the core, simulator, tests, and examples. Build output is intended to span multiple targets: library/core, simulator executable, test fixtures, and example programs.

14. **Overall data flow**
   Application code calls the public UFFS API, which routes into the core filesystem engine. The core consults cached metadata, buffer pools, tree/block info, and integrity layers, then issues abstract flash operations through the device/OS boundary. In simulation, those flash operations are handled by the emulator; on real hardware, they are implemented by platform-specific flash drivers.

15. **Key architectural boundaries to show in a diagram**
   The most important lines to draw are:
   - application/API boundary
   - filesystem core vs. flash/hardware abstraction
   - core vs. platform OS services
   - core vs. simulator/emulator
   - core vs. test harness/regression tooling  
   Those boundaries explain how UFFS stays portable while still modeling NAND-specific behavior closely.

Main entry points: [`src/CMakeLists.txt`](https://github.com/rickyzheng/uffs/blob/master/src/CMakeLists.txt) (Src build), [`src/uffs/uffs_fs.c`](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_fs.c) (FS core), [`src/uffs/uffs_device.c`](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_device.c) (Device layer), [`src/emu/uffs_fileem.c`](https://github.com/rickyzheng/uffs/blob/master/src/emu/uffs_fileem.c) (Emulator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_build["Build"]
  node_root_build["CMake<br/>build system<br/>[CMakeLists.txt]"]
  node_src_build["Src build<br/>build orchestration<br/>[CMakeLists.txt]"]
end

subgraph group_public["API"]
  node_api_headers["Public API<br/>headers<br/>[uffs_public.h]"]
  node_fs_api["FS API<br/>filesystem API<br/>[uffs_fs.h]"]
  node_flash_api["Flash API<br/>[uffs_flash.h]"]
  node_examples["Examples<br/>sample clients"]
end

subgraph group_core["Core"]
  node_core_engine["FS core<br/>filesystem engine<br/>[uffs_fs.c]"]
  node_device_abstraction["Device layer<br/>device abstraction<br/>[uffs_device.c]"]
  node_flash_ops["Flash ops<br/>flash driver glue<br/>[uffs_flash.c]"]
  node_tree_mgmt["Tree model<br/>metadata tree<br/>[uffs_tree.c]"]
  node_mount_recovery["Mount tree<br/>recovery scan<br/>[uffs_mtb.c]"]
  node_reliability{{"Reliability<br/>nand integrity<br/>[uffs_badblock.c]"}}
  node_memory_model{{"Memory pool<br/>bounded memory<br/>[uffs_pool.c]"}}
end

subgraph group_port["Ports"]
  node_posix_port["POSIX port<br/>os wrapper<br/>[uffs_os.c]"]
  node_win32_port["Win32 port<br/>os wrapper<br/>[uffs_os.c]"]
end

subgraph group_test["Tests"]
  node_emu_flash["Emulator<br/>nand simulator<br/>[uffs_fileem.c]"]
  node_api_test["API test<br/>integration harness<br/>[api_test.c]"]
  node_sqlite_suite["SQLite suite<br/>regression workload<br/>[test-uffs.sh]"]
end

subgraph group_tools["Tools"]
  node_mkuffs_tool["mkuffs<br/>image utility<br/>[mkuffs.c]"]
end

node_root_build -->|"builds"| node_src_build
node_src_build -->|"exports"| node_api_headers
node_src_build -->|"compiles"| node_core_engine
node_src_build -->|"compiles"| node_posix_port
node_src_build -->|"compiles"| node_win32_port
node_src_build -->|"compiles"| node_emu_flash
node_src_build -->|"compiles"| node_api_test
node_src_build -->|"compiles"| node_examples
node_api_headers -->|"defines"| node_fs_api
node_api_headers -->|"defines"| node_flash_api
node_fs_api -->|"calls"| node_core_engine
node_flash_api -->|"contracts"| node_device_abstraction
node_core_engine -->|"uses"| node_tree_mgmt
node_core_engine -->|"uses"| node_mount_recovery
node_core_engine -->|"uses"| node_reliability
node_core_engine -->|"uses"| node_memory_model
node_core_engine -->|"issues IO"| node_device_abstraction
node_device_abstraction -->|"adapts to"| node_flash_ops
node_device_abstraction -->|"depends on"| node_posix_port
node_device_abstraction -->|"depends on"| node_win32_port
node_emu_flash -->|"implements"| node_flash_ops
node_emu_flash -->|"simulates"| node_reliability
node_api_test -->|"drives"| node_emu_flash
node_api_test -->|"validates"| node_core_engine
node_sqlite_suite -->|"runs via"| node_api_test
node_mkuffs_tool -->|"prepares"| node_emu_flash
node_examples -->|"uses"| node_api_headers

click node_root_build "https://github.com/rickyzheng/uffs/blob/master/CMakeLists.txt"
click node_src_build "https://github.com/rickyzheng/uffs/blob/master/src/CMakeLists.txt"
click node_api_headers "https://github.com/rickyzheng/uffs/blob/master/src/inc/uffs/uffs_public.h"
click node_fs_api "https://github.com/rickyzheng/uffs/blob/master/src/inc/uffs/uffs_fs.h"
click node_flash_api "https://github.com/rickyzheng/uffs/blob/master/src/inc/uffs/uffs_flash.h"
click node_core_engine "https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_fs.c"
click node_device_abstraction "https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_device.c"
click node_flash_ops "https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_flash.c"
click node_tree_mgmt "https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_tree.c"
click node_mount_recovery "https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_mtb.c"
click node_reliability "https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_badblock.c"
click node_memory_model "https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_pool.c"
click node_posix_port "https://github.com/rickyzheng/uffs/blob/master/src/platform/posix/uffs_os.c"
click node_win32_port "https://github.com/rickyzheng/uffs/blob/master/src/platform/win32/uffs_os.c"
click node_emu_flash "https://github.com/rickyzheng/uffs/blob/master/src/emu/uffs_fileem.c"
click node_api_test "https://github.com/rickyzheng/uffs/blob/master/src/test/api_test/api_test.c"
click node_sqlite_suite "https://github.com/rickyzheng/uffs/blob/master/src/test/sqlite3/sqlite-src-3070900/test-uffs.sh"
click node_mkuffs_tool "https://github.com/rickyzheng/uffs/blob/master/src/utils/mkuffs.c"
click node_examples "https://github.com/rickyzheng/uffs/blob/master/src/example/flash-interface-example.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_root_build,node_src_build toneBlue
class node_api_headers,node_fs_api,node_flash_api,node_examples toneAmber
class node_core_engine,node_device_abstraction,node_flash_ops,node_tree_mgmt,node_mount_recovery,node_reliability,node_memory_model toneMint
class node_posix_port,node_win32_port toneRose
class node_emu_flash,node_api_test,node_sqlite_suite toneIndigo
class node_mkuffs_tool toneTeal
```

## Components

### Build

Top-level and per-subdirectory CMake build orchestration.

- [CMake](https://github.com/rickyzheng/uffs/blob/master/CMakeLists.txt): build system · Top-level build and packaging entry point. · `CMakeLists.txt`
- [Src build](https://github.com/rickyzheng/uffs/blob/master/src/CMakeLists.txt): build orchestration · Builds the core, ports, simulator, tests, and examples. · `src/CMakeLists.txt`

### API

Public headers and application-facing entry points.

- [Public API](https://github.com/rickyzheng/uffs/blob/master/src/inc/uffs/uffs_public.h): headers · Application-facing filesystem and flash interfaces. · `src/inc/uffs/uffs_public.h`
- [FS API](https://github.com/rickyzheng/uffs/blob/master/src/inc/uffs/uffs_fs.h): filesystem API · Main filesystem entry points used by applications. · `src/inc/uffs/uffs_fs.h`
- [Flash API](https://github.com/rickyzheng/uffs/blob/master/src/inc/uffs/uffs_flash.h): flash API · Abstract flash device operations and geometry. · `src/inc/uffs/uffs_flash.h`
- [Examples](https://github.com/rickyzheng/uffs/blob/master/src/example/flash-interface-example.c): sample clients · Reference application integrations and allocation patterns. · `src/example/flash-interface-example.c`

### Core

Filesystem engine and flash-management internals.

- [FS core](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_fs.c): filesystem engine · Mounting, lookup, file operations, and orchestration. · `src/uffs/uffs_fs.c`
- [Device layer](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_device.c): device abstraction · Bridges filesystem logic to abstract devices and backends. · `src/uffs/uffs_device.c`
- [Flash ops](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_flash.c): flash driver glue · Abstract flash page/block operations used by the core. · `src/uffs/uffs_flash.c`
- [Tree model](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_tree.c): metadata tree · Logical file and directory structure in flash. · `src/uffs/uffs_tree.c`
- [Mount tree](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_mtb.c): recovery scan · Reconstructs metadata during mount and boot-time recovery. · `src/uffs/uffs_mtb.c`
- [Reliability](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_badblock.c): nand integrity · Bad blocks, ECC, CRC, buffering, and block metadata. · `src/uffs/uffs_badblock.c`
- [Memory pool](https://github.com/rickyzheng/uffs/blob/master/src/uffs/uffs_pool.c): bounded memory · Static and system allocation support for embedded use. · `src/uffs/uffs_pool.c`

### Ports

Operating-system and hardware abstraction backends.

- [POSIX port](https://github.com/rickyzheng/uffs/blob/master/src/platform/posix/uffs_os.c): os wrapper · Host/RTOS-style services for POSIX environments. · `src/platform/posix/uffs_os.c`
- [Win32 port](https://github.com/rickyzheng/uffs/blob/master/src/platform/win32/uffs_os.c): os wrapper · Host services for Windows environments. · `src/platform/win32/uffs_os.c`

### Tests

Simulator-backed integration and regression tooling.

- [Emulator](https://github.com/rickyzheng/uffs/blob/master/src/emu/uffs_fileem.c): nand simulator · Desktop flash model with ECC, bad blocks, and command shell. · `src/emu/uffs_fileem.c`
- [API test](https://github.com/rickyzheng/uffs/blob/master/src/test/api_test/api_test.c): integration harness · Client/server test bridge between simulator and external processes. · `src/test/api_test/api_test.c`
- [SQLite suite](https://github.com/rickyzheng/uffs/blob/master/src/test/sqlite3/sqlite-src-3070900/test-uffs.sh): regression workload · Vendored SQLite tests adapted to exercise UFFS behavior. · `src/test/sqlite3/sqlite-src-3070900/test-uffs.sh`

### Tools

Image and utility commands used to operate UFFS.

- [mkuffs](https://github.com/rickyzheng/uffs/blob/master/src/utils/mkuffs.c): image utility · Builds and manipulates UFFS images and simulator data. · `src/utils/mkuffs.c`

## Connections

- CMake → Src build: builds · Top-level build delegates into src subprojects.
- Src build → Public API: exports · Build exposes application-facing headers.
- Src build → FS core: compiles · Core filesystem engine is built from src/uffs.
- Src build → POSIX port: compiles · POSIX service layer is one build target.
- Src build → Win32 port: compiles · Windows service layer is one build target.
- Src build → Emulator: compiles · Simulator/emulator is built as a separate target.
- Src build → API test: compiles · Integration test harness is built as a separate target.
- Src build → Examples: compiles · Example clients are included in the build.
- Public API → FS API: defines · Public API includes filesystem entry points.
- Public API → Flash API: defines · Public API includes abstract flash interfaces.
- FS API → FS core: calls · Filesystem API routes into the core engine.
- Flash API → Device layer: contracts · Flash interface is implemented through device abstraction.
- FS core → Tree model: uses · Core relies on the logical tree for namespace operations.
- FS core → Mount tree: uses · Mount path reconstructs metadata from flash.
- FS core → Reliability: uses · Core consults integrity and NAND-management layers.
- FS core → Memory pool: uses · Core uses bounded allocation and pooled buffers.
- FS core → Device layer: issues IO · Core sends abstract operations through the device boundary.
- Device layer → Flash ops: adapts to · Device boundary maps into flash operations.
- Device layer → POSIX port: depends on · POSIX backend provides host services where needed.
- Device layer → Win32 port: depends on · Win32 backend provides host services where needed.
- Emulator → Flash ops: implements · Emulator provides NAND behavior for flash operations.
- Emulator → Reliability: simulates · Emulator models ECC and bad blocks for testing.
- API test → Emulator: drives · Integration harness exercises the emulator-backed filesystem.
- API test → FS core: validates · Harness validates filesystem behavior through the core API.
- SQLite suite → API test: runs via · SQLite regression workload is adapted through the test harness.
- mkuffs → Emulator: prepares · Utility prepares disk images and simulator data.
- Examples → Public API: uses · Examples show application integration through the public API.
