From 2f25e446813fa5ec5ce7f3f4aed642139809b9b9 Mon Sep 17 00:00:00 2001 From: hyperion Date: Mon, 16 Nov 2026 12:16:49 +0100 Subject: [PATCH] - Fixed output formatting error in `ls.ts` - Added an exported `Termos` object entry point - Defined a basic config for the entry point - Wrote a self contained initialization function for parsin the config into a returned `Shell` instance - Adjusted `dev.ts` to use the said entry point --- src/config.ts | 27 +++-- src/core/commands/ls.ts | 2 +- src/core/filesystem/filesystem.ts | 4 +- src/core/index.ts | 0 src/dev.ts | 76 ++++-------- src/entry.ts | 188 ++++++++++++++++++++++++++++++ src/index.ts | 13 +-- src/shell/shell.ts | 28 +---- 8 files changed, 236 insertions(+), 102 deletions(-) create mode 100644 src/core/index.ts create mode 100644 src/entry.ts diff --git a/src/config.ts b/src/config.ts index cacbc59..f4abdb4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,11 +1,20 @@ export interface TermosConfig { - serverAddress: string; - dbName: string; - idbName: string; + groups: string[]; + users: { + username: string; + groups: string[]; + home: string; + }[]; + general: { + user: string; + serverAddress?: string; + dbName?: string; + idbName?: string; + }; + filesystem: { + json: string; + maxNameLength?: number; + maxPathLength?: number; + maxSymlinkDepth?: number; + }; } - -export const defaultConfig: TermosConfig = { - serverAddress: "", - dbName: "termos", - idbName: "cache", -}; diff --git a/src/core/commands/ls.ts b/src/core/commands/ls.ts index a31bbea..bcd0cd7 100644 --- a/src/core/commands/ls.ts +++ b/src/core/commands/ls.ts @@ -336,7 +336,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise { const pdev = this._VDiskManager.getDisk(parent.device); const parentNode = this.resolveNode(parent); @@ -436,7 +436,7 @@ export class FileSystem { } satisfies InvalidNodeNameError; } - await pdev.createNodeEntry(parent.inode, name, data); + return pdev.createNodeEntry(parent.inode, name, data); } public async deleteNode(target: NodePointer) { diff --git a/src/core/index.ts b/src/core/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/dev.ts b/src/dev.ts index c8070d5..858c1fb 100644 --- a/src/dev.ts +++ b/src/dev.ts @@ -1,57 +1,31 @@ -/* import { defaultConfig, TermosConfig } from "./config"; - -let config: TermosConfig = { ...defaultConfig }; - -namespace Termos { - export function init(config: Partial) { - config = { ...defaultConfig, ...config }; - console.log("termos initialized"); - } -} -*/ - -//import { FileSystem } from "@core/filesystem/filesystem"; -import { FileSystem } from "@core/filesystem/filesystem"; -import { NodePointer } from "@core/filesystem/node"; -import { getCurrentSession, type GroupInfo, type UserInfo } from "@core/session_manager"; -import { createInitialShellState, type ShellState } from "@core/shell_state"; +import { getCurrentSession } from "@core/session_manager"; import { resolvePointerToHome } from "@core/util/path_resolver"; -import { deserializeFileSystem } from "@shell/serializer"; -import { Shell } from "@shell/shell"; +import { Termos } from "./entry"; + //import { deserializeFileSystem } from "@shell/serializer"; //import { Shell } from "@shell/shell"; const rawJson = `{"dev":[{"id":0,"tree": {"atime": 1772323866,"children": [{"atime": 1772323866,"children": [{"atime": 1772323866,"children": [{"atime": 1772323866,"children": [{"atime": 1772323866,"children": [{"atime": 1772323867,"children": [{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323878,"group": 1000,"inode": 6,"link_count": 1,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 5,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 5,"link_count": 2,"mode": 493,"mtime": 1759942574,"name": "Sheepo","owner": 1000,"parent": 4,"size": 22,"type": "directory"},{"atime": 1772323867,"children": [{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323878,"group": 1000,"inode": 8,"link_count": 1,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 7,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 7,"link_count": 2,"mode": 493,"mtime": 1759942576,"name": "SchoolProjectManager","owner": 1000,"parent": 4,"size": 22,"type": "directory"},{"atime": 1772323867,"children": [{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323878,"group": 1000,"inode": 10,"link_count": 1,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 9,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 9,"link_count": 2,"mode": 493,"mtime": 1759940182,"name": "This Website","owner": 1000,"parent": 4,"size": 22,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 4,"link_count": 5,"mode": 493,"mtime": 1759940133,"name": "Websites","owner": 1000,"parent": 3,"size": 68,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 11,"link_count": 2,"mode": 493,"mtime": 1759939881,"name": "C#","owner": 1000,"parent": 3,"size": 6,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 3,"link_count": 4,"mode": 493,"mtime": 1759939982,"name": "Projects","owner": 1000,"parent": 2,"size": 32,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 12,"link_count": 2,"mode": 493,"mtime": 1757766551,"name": "Documents","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 13,"link_count": 2,"mode": 493,"mtime": 1757766577,"name": "Desktop","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 14,"link_count": 2,"mode": 493,"mtime": 1757766580,"name": "Downloads Test","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [{"atime": 1772232964,"children": [],"content": [78,101,118,101,114,32,103,111,110,110,97,32,103,105,118,101,32,121,111,117,32,117,112,10,78,101,118,101,114,32,103,111,110,110,97,32,108,101,116,32,121,111,117,32,100,111,119,110,10,78,101,118,101,114,32,103,111,110,110,97,32,114,117,110,32,97,114,111,117,110,100,32,97,110,100,32,100,101,115,101,114,116,32,121,111,117,10,78,101,118,101,114,32,103,111,110,110,97,32,109,97,107,101,32,121,111,117,32,99,114,121,10,78,101,118,101,114,32,103,111,110,110,97,32,115,97,121,32,103,111,111,100,98,121,101,10,78,101,118,101,114,32,103,111,110,110,97,32,116,101,108,108,32,97,32,108,105,101,32,97,110,100,32,104,117,114,116,32,121,111,117],"ctime": 1772323869,"group": 1000,"inode": 16,"link_count": 1,"mode": 493,"mtime": 1757812927,"name": "nggyu.lrc","owner": 1000,"parent": 15,"size": 171,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 15,"link_count": 2,"mode": 493,"mtime": 1759939803,"name": "Music","owner": 1000,"parent": 2,"size": 23,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 17,"link_count": 2,"mode": 493,"mtime": 1757766591,"name": "Pictures","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 18,"link_count": 2,"mode": 493,"mtime": 1757766597,"name": "Videos","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 19,"link_count": 2,"mode": 493,"mtime": 1757766614,"name": ".cache","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 20,"link_count": 2,"mode": 493,"mtime": 1757766620,"name": ".cargo","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 21,"link_count": 2,"mode": 493,"mtime": 1757766627,"name": ".config","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 22,"link_count": 2,"mode": 493,"mtime": 1757766631,"name": ".java","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 23,"link_count": 2,"mode": 493,"mtime": 1757766636,"name": ".dotnet","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 24,"link_count": 2,"mode": 493,"mtime": 1757766640,"name": ".local","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 25,"link_count": 2,"mode": 493,"mtime": 1757766646,"name": ".npm","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 26,"link_count": 2,"mode": 493,"mtime": 1757766652,"name": ".ssh","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 27,"link_count": 2,"mode": 493,"mtime": 1757766660,"name": ".var","owner": 1000,"parent": 2,"size": 6,"type": "directory"},{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323867,"group": 1000,"inode": 28,"link_count": 1,"mode": 493,"mtime": 1757780715,"name": ".bash_profile","owner": 1000,"parent": 2,"size": 1,"type": "regular"},{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323867,"group": 1000,"inode": 29,"link_count": 1,"mode": 493,"mtime": 1757780715,"name": ".bashrc","owner": 1000,"parent": 2,"size": 1,"type": "regular"},{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323867,"group": 1000,"inode": 30,"link_count": 1,"mode": 493,"mtime": 1757780849,"name": ".bash_logout","owner": 1000,"parent": 2,"size": 1,"type": "regular"},{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323867,"group": 1000,"inode": 31,"link_count": 1,"mode": 493,"mtime": 1757780811,"name": ".bash_history","owner": 1000,"parent": 2,"size": 1,"type": "regular"},{"atime": 1772232964,"children": [],"content": [10],"ctime": 1772323867,"group": 1000,"inode": 32,"link_count": 1,"mode": 493,"mtime": 1757780889,"name": ".xinitrc","owner": 1000,"parent": 2,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 2,"link_count": 18,"mode": 493,"mtime": 1772024996,"name": "kamil","owner": 1000,"parent": 1,"size": 4096,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 1,"link_count": 3,"mode": 493,"mtime": 1757766534,"name": "home","owner": 1000,"parent": 0,"size": 19,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 33,"link_count": 2,"mode": 493,"mtime": 1757686689,"name": "root","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 34,"link_count": 2,"mode": 493,"mtime": 1757686765,"name": "bin","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 35,"link_count": 2,"mode": 493,"mtime": 1757686768,"name": "boot","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 36,"link_count": 2,"mode": 493,"mtime": 1757686772,"name": "dev","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 37,"link_count": 2,"mode": 493,"mtime": 1757686775,"name": "etc","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 38,"link_count": 2,"mode": 493,"mtime": 1757686777,"name": "lib","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 39,"link_count": 2,"mode": 493,"mtime": 1757686781,"name": "lib64","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 40,"link_count": 2,"mode": 493,"mtime": 1757686785,"name": "lost+found","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 41,"link_count": 2,"mode": 493,"mtime": 1757686791,"name": "opt","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 42,"link_count": 2,"mode": 493,"mtime": 1757686804,"name": "proc","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 43,"link_count": 2,"mode": 493,"mtime": 1757686809,"name": "run","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 44,"link_count": 2,"mode": 493,"mtime": 1757686823,"name": "sbin","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 45,"link_count": 2,"mode": 493,"mtime": 1757686830,"name": "srv","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 46,"link_count": 2,"mode": 493,"mtime": 1757686863,"name": "sys","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 47,"link_count": 2,"mode": 493,"mtime": 1757686867,"name": "tmp","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 48,"link_count": 2,"mode": 493,"mtime": 1757686871,"name": "usr","owner": 1000,"parent": 0,"size": 6,"type": "directory"},{"atime": 1772323866,"children": [],"content": null,"ctime": 1772323891,"group": 1000,"inode": 49,"link_count": 2,"mode": 493,"mtime": 1757686874,"name": "var","owner": 1000,"parent": 0,"size": 6,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 0,"link_count": 20,"mode": 493,"mtime": 1757686876,"name": "root","owner": 1000,"parent": 0,"size": 4096,"type": "directory"}}]}`; -const devices = deserializeFileSystem(rawJson); - -const fs = await FileSystem.init(devices); - -const users: Map = new Map(); -const groups: Map = new Map(); -const user: UserInfo = { - username: "kamil", - uid: 1000, - gid: 1000, // <- PRIMARY GID - group: [1000], // <- GROUPS ARRAY - home: NodePointer.of(0, 2), +const config = { + groups: ["users"], + users: [ + { + username: "kamil", + groups: ["users", "sudoers"], + home: "/home/kamil", + }, + ], + general: { + user: "kamil", + }, + filesystem: { + json: rawJson, + }, }; -const group: GroupInfo = { - groupname: "kamil", - gid: 1000, - members: [1000], // <- UID -}; -users.set(1000, user); -groups.set(1000, group); -const state: ShellState = createInitialShellState(fs, users, groups, user); -Shell.init(state); +const shell = await Termos.getShell(config); -//console.log(resolveInode(Shell.Instance?.State!, 6)); -//console.log(resolveInode(Shell.Instance?.State!, 6, true)); - -//console.log(sortTreeNodes(Shell.Instance?.State!, dnTraverseTree(Shell.Instance?.State.filesystem!, 0))); - -Shell.Instance().on("result", payload => { +shell.on("result", payload => { //const decoded = new TextDecoder().decode(payload.result.stdout.toArray()); if (payload.result.type === "error") { console.log(payload.result.error.message + "\n"); @@ -66,21 +40,21 @@ Shell.Instance().on("result", payload => { Need to use this only if a continuous process is running */ -Shell.Instance().on("stdout", payload => { +shell.on("stdout", payload => { const decoded = new TextDecoder().decode(payload.data); process.stdout.write(decoded); }); console.clear(); -process.stdout.write(resolvePointerToHome(state, getCurrentSession(state).cwd) + "\n> "); +process.stdout.write(resolvePointerToHome(shell.State, getCurrentSession(shell.State).cwd) + "\n> "); process.stdin.on("data", async data => { - if (Shell.Instance().BoundPID !== null) { - Shell.Instance().sendInput(data.toString().trim()); + if (shell.BoundPID !== null) { + shell.sendInput(data.toString().trim()); return; } const input = data.toString().trim(); - await Shell.Instance().execute(input); + await shell.execute(input); }); //export { Termos }; diff --git a/src/entry.ts b/src/entry.ts new file mode 100644 index 0000000..2d11332 --- /dev/null +++ b/src/entry.ts @@ -0,0 +1,188 @@ +import type { FileSystemCorruptionError, NodeNotFoundError, NotADirectoryError } from "@core/filesystem/errors"; +import { FileSystem } from "@core/filesystem/filesystem"; +import { NodePointer, NodeType, type Node } from "@core/filesystem/node"; +import type { GroupInfo, UserInfo } from "@core/session_manager"; +import { createInitialShellState, type ShellState } from "@core/shell_state"; +import { deserializeFileSystem } from "@shell/serializer"; +import { Shell } from "@shell/shell"; +import type { TermosConfig } from "./config"; + +async function getShell(config: TermosConfig): Promise { + // TODO: Validate json schema + const devices = deserializeFileSystem(config.filesystem.json); + + const fs = await FileSystem.init(devices); + + const users: Map = new Map(); + const groups: Map = new Map(); + const reverseGroups: Map = new Map(); + const reverseUsers: Map = new Map(); + + if (config.users.length === 0) throw new Error("System needs to have at least one user"); + if (config.groups.length === 0) throw new Error("System needs to have at least one user group"); + for (let i = 1000; i < config.groups.length + 1000; i++) { + const group = config.groups[i - 1000]!; + reverseGroups.set(group, i); + + const members: number[] = []; + for (let i = 1000; i < config.users.length + 1000; i++) { + const user = config.users[i - 1000]!; + if (user.groups.some(g => g === group)) { + members.push(i); + } + } + groups.set(i, { groupname: group, gid: i, members }); + } + + for (let i = 1000; i < config.users.length + 1000; i++) { + const user = config.users[i - 1000]!; + if (user.groups.length === 0) throw new Error("User must belong to at least one user group"); + + const gid = reverseGroups.get(user.groups[0]!); + if (!gid) throw new Error("User is a member of an undefined group"); + + const groups: number[] = []; + + for (const group of user.groups) { + if (group === "sudoers") continue; + const gid = reverseGroups.get(group); + if (!gid) throw new Error("User is a member of an undefined group"); + groups.push(gid); + } + + reverseUsers.set(user.username, i); + users.set(i, { username: user.username, uid: i, gid, group: groups, home: resPath(user.home) }); + } + const uid = reverseUsers.get(config.general.user); + if (!uid) throw new Error("Tried to initialize termos with an undefined user"); + + const user = users.get(uid)!; + + const state: ShellState = createInitialShellState(fs, users, groups, user); + return new Shell(state); + + function resPath(path: string) { + let linkDepth = 0; + const maxDepth = fs.maxSymlinkDepth; + + const pathComponents: string[] = path.split("/").filter(c => c.length > 0); + + let currentDeviceId: number = 0; + let currentNode: Node = fs.getRoot(); + + while (pathComponents.length > 0) { + const name = pathComponents.shift()!; + + if (name === ".") continue; + if (name === "..") { + if (fs.isMountTarget(NodePointer.of(currentDeviceId, currentNode.inode))) { + const mount = fs.getMountPoint(NodePointer.of(currentDeviceId, currentNode.inode)); + if (!mount) { + throw { + type: "FileSystemCorruption", + message: "Mount registry entry missing, FileSystem corrupted", + timestamp: Date.now(), + } satisfies FileSystemCorruptionError; + } + + const entries = fs.getNodeEntries(mount); + if (!entries?.[0]) { + throw { + type: "FileSystemCorruption", + message: "Reverse index entry missing, FileSystem corrupted", + timestamp: Date.now(), + } satisfies FileSystemCorruptionError; + } + + currentDeviceId = entries[0].pdevice_id; + currentNode = fs.getNode(NodePointer.of(entries[0].pdevice_id, entries[0].pinode))!; + continue; + } + if (currentNode.inode === 0) continue; + + const entries = fs.getNodeEntries(NodePointer.of(currentDeviceId, currentNode.inode)); + + if (!entries?.[0]) { + throw { + type: "FileSystemCorruption", + message: "Reverse index entry missing, FileSystem corrupted", + timestamp: Date.now(), + } satisfies FileSystemCorruptionError; + } + + currentDeviceId = entries[0].pdevice_id; + currentNode = fs.getNode(NodePointer.of(entries[0].pdevice_id, entries[0].pinode))!; + continue; + } + + if (currentNode.type !== NodeType.Directory) { + throw { + type: "NotADirectory", + message: "Not a directory", + timestamp: Date.now(), + inode: currentNode.inode, + nodeType: currentNode.type, + } satisfies NotADirectoryError; + } + + const childInode = currentNode.children.get(name); + if (childInode === undefined) { + throw { + type: "NodeNotFound", + message: `${name} not found`, + timestamp: Date.now(), + pointer: NodePointer.of(currentDeviceId, currentNode.inode), + } satisfies NodeNotFoundError; + } + + let childPtr = NodePointer.of(currentDeviceId, childInode); + + if (fs.isMountPoint(childPtr)) { + const mount = fs.getMountTarget(childPtr)!; + childPtr = mount; + currentDeviceId = mount.device; + } + + let child = fs.getNode(childPtr); + if (!child) { + throw { + type: "NodeNotFound", + message: `Node under ${childPtr} not found (probable dangling orphan)`, + timestamp: Date.now(), + pointer: childPtr, + } satisfies NodeNotFoundError; + } + + if (child.type === NodeType.Symlink) { + if (++linkDepth > maxDepth) throw new Error("Symlink max depth - aborting!"); + const target = child.target; + if (target.startsWith("/")) { + currentDeviceId = 0; + currentNode = fs.getRoot(); + } else { + const entries = fs.getNodeEntries(childPtr); + if (!entries?.[0]) { + throw { + type: "FileSystemCorruption", + message: "Reverse index entry missing, FileSystem corrupted", + timestamp: Date.now(), + } satisfies FileSystemCorruptionError; + } + currentDeviceId = entries[0].pdevice_id; + currentNode = fs.getNode(NodePointer.of(entries[0].pdevice_id, entries[0].pinode))!; + } + + pathComponents.unshift(...target.split("/").filter(c => c.length > 0)); + continue; + } + + currentNode = child; + } + + return NodePointer.of(currentDeviceId, currentNode.inode); + } +} + +export const Termos = { + getShell, +}; diff --git a/src/index.ts b/src/index.ts index 2881e17..e37a86b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1 @@ -import { defaultConfig, TermosConfig } from "./config"; - -let config: TermosConfig = { ...defaultConfig }; - -function init(config: Partial) { - config = { ...defaultConfig, ...config }; - console.log("termos initialized"); -} - -export const Termos = { - init, -}; +export { Termos } from "./entry"; diff --git a/src/shell/shell.ts b/src/shell/shell.ts index 9d5c532..6195295 100644 --- a/src/shell/shell.ts +++ b/src/shell/shell.ts @@ -10,7 +10,6 @@ import { EventEmitter } from "./event_emitter"; import { ProcessManager } from "./process_manager"; export class Shell { - static #Instance: Shell | null; private _State: Readonly; private _ProcessManager: ProcessManager = new ProcessManager(); private _EventEmitter: EventEmitter = new EventEmitter(); @@ -18,7 +17,7 @@ export class Shell { public BoundPID: number | null = null; - private constructor(state: ShellState) { + constructor(state: ShellState) { this._State = Object.freeze(state); //Temporary solution for sigint keybind @@ -42,21 +41,6 @@ export class Shell { }); } - static destroy(): void { - Shell.#Instance = null; - return; - } - - static init(initialState: ShellState): Shell { - if (Shell.#Instance) { - console.warn("Shell already initialized"); - return Shell.#Instance; - } - - Shell.#Instance = new Shell(initialState); - return Shell.#Instance; - } - public async execute(input: string): Promise { const parsed = tokenize(input); @@ -119,16 +103,6 @@ export class Shell { return this._State; } - public static Instance(initialState?: ShellState): Shell { - if (!Shell.#Instance) { - if (!initialState) { - throw new Error("Shell needs to be initialized first"); - } - Shell.#Instance = new Shell(initialState); - } - return Shell.#Instance; - } - public on(type: K, handler: (payload: ShellEventMap[K]) => void): () => void { return this._EventEmitter.on(type, handler); }