128 lines
3.7 KiB
Nim
128 lines
3.7 KiB
Nim
# SPDX-License-Identifier: LSL-1.0
|
|
# Copyright (c) 2026 Markus Maiwald
|
|
# Stewardship: Self Sovereign Society Foundation
|
|
#
|
|
# This file is part of the Nexus Sovereign Core.
|
|
# See legal/LICENSE_SOVEREIGN.md for license terms.
|
|
|
|
## Rumpk Layer 1: Sovereign VFS (The Loom)
|
|
|
|
# MARKUS MAIWALD (ARCHITECT) | VOXIS FORGE (AI)
|
|
# VFS dispatcher for SPEC-130 alignment.
|
|
|
|
import strutils, tables
|
|
import tar, sfs
|
|
|
|
type
|
|
VFSMode = enum
|
|
MODE_TAR, MODE_SFS, MODE_RAM
|
|
|
|
MountPoint = object
|
|
prefix: string
|
|
mode: VFSMode
|
|
|
|
var mounts: seq[MountPoint] = @[]
|
|
|
|
type
|
|
FileHandle = object
|
|
path: string
|
|
offset: uint64
|
|
mode: VFSMode
|
|
is_ram: bool
|
|
|
|
var fds = initTable[int, FileHandle]()
|
|
var next_fd = 3
|
|
|
|
proc vfs_mount_init*() =
|
|
# SPEC-130: The Three-Domain Root
|
|
# SPEC-021: The Sovereign Overlay Strategy
|
|
mounts.add(MountPoint(prefix: "/nexus", mode: MODE_SFS)) # The Sovereign State (Persistent)
|
|
mounts.add(MountPoint(prefix: "/sysro", mode: MODE_TAR)) # The Projected Reality (Immutable InitRD)
|
|
mounts.add(MountPoint(prefix: "/state", mode: MODE_RAM)) # The Mutable Dust (Transient)
|
|
|
|
proc resolve_path(path: string): (VFSMode, string) =
|
|
for m in mounts:
|
|
if path.startsWith(m.prefix):
|
|
let sub = if path.len > m.prefix.len: path[m.prefix.len..^1] else: "/"
|
|
return (m.mode, sub)
|
|
return (MODE_TAR, path)
|
|
|
|
# Syscall implementation procs
|
|
# Kernel Imports
|
|
# (Currently unused, relying on kprintln from kernel)
|
|
proc ion_vfs_open*(path: cstring, flags: int32): int32 {.exportc, cdecl.} =
|
|
let p = $path
|
|
let (mode, sub) = resolve_path(p)
|
|
|
|
var fd = -1
|
|
case mode:
|
|
of MODE_TAR: fd = tar.vfs_open(sub, flags)
|
|
of MODE_SFS: fd = 0 # Placeholder for SFS open
|
|
of MODE_RAM: fd = tar.vfs_open(sub, flags) # Using TAR's RamFS for now
|
|
|
|
if fd > 0:
|
|
let kernel_fd = next_fd
|
|
fds[kernel_fd] = FileHandle(path: sub, offset: 0, mode: mode, is_ram: (mode == MODE_RAM))
|
|
next_fd += 1
|
|
return int32(kernel_fd)
|
|
return -1
|
|
|
|
proc ion_vfs_read*(fd: int32, buf: pointer, count: uint64): int64 {.exportc, cdecl.} =
|
|
let ifd = int(fd)
|
|
if not fds.hasKey(ifd): return -1
|
|
let fh = addr fds[ifd]
|
|
|
|
case fh.mode:
|
|
of MODE_TAR, MODE_RAM:
|
|
let n = tar.vfs_read_at(fh.path, buf, count, fh.offset)
|
|
if n > 0: fh.offset += uint64(n)
|
|
return n
|
|
of MODE_SFS:
|
|
# SFS current read-whole-file shim
|
|
var temp_buf: array[4096, byte] # FIXME: Small stack buffer
|
|
let total = sfs.sfs_read_file(cstring(fh.path), addr temp_buf[0], 4096)
|
|
if total < 0: return -1
|
|
if fh.offset >= uint64(total): return 0
|
|
let avail = uint64(total) - fh.offset
|
|
let actual = min(count, avail)
|
|
if actual > 0:
|
|
copyMem(buf, addr temp_buf[int(fh.offset)], int(actual))
|
|
fh.offset += actual
|
|
return int64(actual)
|
|
|
|
proc ion_vfs_write*(fd: int32, buf: pointer, count: uint64): int64 {.exportc, cdecl.} =
|
|
let ifd = int(fd)
|
|
if not fds.hasKey(ifd): return -1
|
|
let fh = addr fds[ifd]
|
|
|
|
case fh.mode:
|
|
of MODE_TAR, MODE_RAM:
|
|
let n = tar.vfs_write_at(fh.path, buf, count, fh.offset)
|
|
if n > 0: fh.offset += uint64(n)
|
|
return n
|
|
of MODE_SFS:
|
|
sfs.sfs_write_file(cstring(fh.path), cast[cstring](buf), int(count))
|
|
return int64(count)
|
|
|
|
proc ion_vfs_close*(fd: int32): int32 {.exportc, cdecl.} =
|
|
let ifd = int(fd)
|
|
if fds.hasKey(ifd):
|
|
fds.del(ifd)
|
|
return 0
|
|
return -1
|
|
|
|
proc ion_vfs_list*(buf: pointer, max_len: uint64): int64 {.exportc, cdecl.} =
|
|
var s = "/nexus\n/sysro\n/state\n"
|
|
for name in tar.vfs_get_names():
|
|
s.add("/sysro/" & name & "\n")
|
|
|
|
# Add SFS files under /nexus
|
|
let sfs_names = sfs.sfs_get_files()
|
|
for line in sfs_names.splitLines():
|
|
if line.len > 0:
|
|
s.add("/nexus/" & line & "\n")
|
|
|
|
let n = min(s.len, int(max_len))
|
|
if n > 0: copyMem(buf, addr s[0], n)
|
|
return int64(n)
|