# elvisar-git/c_file_system_simulator architecture

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

- Interactive diagram: https://gitdiagram.com/elvisar-git/c_file_system_simulator
- Repository: https://github.com/elvisar-git/c_file_system_simulator

## Overview

##### Project Type and Purpose
- This is a single‐executable C “file system simulator” that models basic file system operations entirely in memory.
- It is a command‐line tool: you build it with `make` and run `./sim` to enter the simulator REPL or execute a batch of commands.

##### File Structure Analysis
- Top level:
- sim.c – the entire simulator logic (lexer/parser, command dispatch, data structures, REPL loop)
- makefile – build rules for compiling sim.c into the `sim` binary
- README.md, .gitignore

##### Key Components to Highlight
a. User Interface Layer (CLI)
– REPL loop or command‐line invocation
– Reads user commands from stdin, writes responses to stdout/stderr
b. Parser / Command Dispatcher
– Parses input lines into tokens/commands
– Maps parsed commands to simulator actions (e.g., create file, remove directory, list contents)
c. File System Core Model
– In‐memory data structures:
- Superblock (global FS metadata)
- Inode table (file/directory metadata)
- Block bitmap or free‐list (tracks free/used blocks)
- Directory tree (hierarchical mapping of names to inode numbers)
– Core operations: allocate/free inodes and blocks, update directory entries
d. Memory Management Subsystem
– Allocator for inode and block structures
– Garbage collection or simple free‐list updates
e. Persistence Stub (optional)
– If sim.c supports dumping/loading state to a file, show this component and its interface
f. Error Handling and Logging
– Centralized routines to report invalid commands or FS state errors

##### Relationships and Data Flows
- Within the Core Model, show bi‐directional relationships between the Superblock, Inode Table, Block Bitmap, and Directory Tree
- Error Handling can be an overlay component that intercepts failures in any core module and sends messages back to the CLI

##### Architectural Patterns and Principles
- Single binary, modular layering (UI → Parser → Core → Data)
- Separation of concerns: parsing logic is distinct from data‐structure manipulation
- In‐memory simulation: no external DB or network calls, but you might abstract a “Storage Engine” interface if future extensibility is desired
- Command‐pattern style dispatch: each FS command is implemented as a handler function

##### Capturing Essential Details
- The goal is to give a clear picture of how input flows through parsing into state changes in the FS model.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface Layer"
        CLI["CLI / REPL"]:::ui
    end

    %% Parser and Dispatcher
    subgraph "Parser / Command Dispatcher"
        Parser["Parser & Command Dispatcher"]:::ui
    end

    %% Core Model
    subgraph "File System Core Model"
        SB["Superblock"]:::core
        IT["Inode Table"]:::core
        BB["Block Bitmap"]:::core
        DT["Directory Tree"]:::core
    end

    %% Memory Management
    subgraph "Memory Management Subsystem"
        MM["Inode/Block Allocator & Free-List"]:::mem
    end

    %% Persistence Stub
    subgraph "Persistence Stub (optional)"
        PS["State Dump/Load"]:::pers
    end

    %% Error Handling and Logging
    EH["Error Handling & Logging"]:::err

    %% External Components
    Make["Build System (makefile)"]:::ext
    Readme["Documentation (README.md)"]:::ext
    Gitignore["Git Exclusions (.gitignore)"]:::ext

    %% Connections
    CLI -->|"reads commands"| Parser
    Parser -->|"dispatches actions"| SB
    Parser -->|"dispatches actions"| IT
    Parser -->|"dispatches actions"| BB
    Parser -->|"dispatches actions"| DT
    Parser -->|"requests allocation"| MM
    SB <--> IT
    SB <--> BB
    SB <--> DT
    IT <--> BB
    IT <--> DT
    BB <--> DT
    SB -->|"persist state"| PS
    PS -->|"load state"| SB
    SB -->|"errors"| EH
    IT -->|"errors"| EH
    BB -->|"errors"| EH
    DT -->|"errors"| EH
    MM -->|"errors"| EH
    EH -->|"reports to"| CLI

    %% External links
    Make -->|builds| CLI
    CLI -->|usage docs| Readme
    Make -->|ignored files| Gitignore

    %% Click Events
    click CLI "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click Parser "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click SB "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click IT "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click BB "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click DT "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click MM "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click PS "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click EH "https://github.com/elvisar-git/c_file_system_simulator/blob/master/sim.c"
    click Make "https://github.com/elvisar-git/c_file_system_simulator/tree/master/makefile"
    click Readme "https://github.com/elvisar-git/c_file_system_simulator/blob/master/README.md"
    click Gitignore "https://github.com/elvisar-git/c_file_system_simulator/blob/master/.gitignore"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,color:#004085
    classDef core fill:#d4edda,stroke:#155724,color:#155724
    classDef mem fill:#fff3cd,stroke:#856404,color:#856404
    classDef pers fill:#e2e3e5,stroke:#6c757d,color:#6c757d
    classDef err fill:#f8d7da,stroke:#721c24,color:#721c24
    classDef ext fill:#f0f0f0,stroke:#666666,color:#333333,stroke-dasharray: 5 5
```
