- 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
This commit is contained in:
2026-11-16 12:16:49 +01:00
parent d4ec6b0c49
commit 2f25e44681
8 changed files with 236 additions and 102 deletions
+18 -9
View File
@@ -1,11 +1,20 @@
export interface TermosConfig { export interface TermosConfig {
serverAddress: string; groups: string[];
dbName: string; users: {
idbName: string; 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",
};
+1 -1
View File
@@ -336,7 +336,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
} }
//Link count //Link count
const linkCount = node.link_count.toString().padStart(maxLinksWidth, " "); const linkCount = node.link_count.toString().padStart(maxLinksWidth, " ") + " ";
buff.push(e.encode(linkCount)); buff.push(e.encode(linkCount));
// owner group // owner group
+2 -2
View File
@@ -384,7 +384,7 @@ export class FileSystem {
} }
| { type: NodeType.Directory; mode: number; owner: number; group: number } | { type: NodeType.Directory; mode: number; owner: number; group: number }
| { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string }, | { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string },
) { ): Promise<number> {
const pdev = this._VDiskManager.getDisk(parent.device); const pdev = this._VDiskManager.getDisk(parent.device);
const parentNode = this.resolveNode(parent); const parentNode = this.resolveNode(parent);
@@ -436,7 +436,7 @@ export class FileSystem {
} satisfies InvalidNodeNameError; } satisfies InvalidNodeNameError;
} }
await pdev.createNodeEntry(parent.inode, name, data); return pdev.createNodeEntry(parent.inode, name, data);
} }
public async deleteNode(target: NodePointer) { public async deleteNode(target: NodePointer) {
View File
+24 -50
View File
File diff suppressed because one or more lines are too long
+188
View File
@@ -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<Shell> {
// TODO: Validate json schema
const devices = deserializeFileSystem(config.filesystem.json);
const fs = await FileSystem.init(devices);
const users: Map<number, UserInfo> = new Map();
const groups: Map<number, GroupInfo> = new Map();
const reverseGroups: Map<string, number> = new Map();
const reverseUsers: Map<string, number> = 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,
};
+1 -12
View File
@@ -1,12 +1 @@
import { defaultConfig, TermosConfig } from "./config"; export { Termos } from "./entry";
let config: TermosConfig = { ...defaultConfig };
function init(config: Partial<TermosConfig>) {
config = { ...defaultConfig, ...config };
console.log("termos initialized");
}
export const Termos = {
init,
};
+1 -27
View File
@@ -10,7 +10,6 @@ import { EventEmitter } from "./event_emitter";
import { ProcessManager } from "./process_manager"; import { ProcessManager } from "./process_manager";
export class Shell { export class Shell {
static #Instance: Shell | null;
private _State: Readonly<ShellState>; private _State: Readonly<ShellState>;
private _ProcessManager: ProcessManager = new ProcessManager(); private _ProcessManager: ProcessManager = new ProcessManager();
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter(); private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
@@ -18,7 +17,7 @@ export class Shell {
public BoundPID: number | null = null; public BoundPID: number | null = null;
private constructor(state: ShellState) { constructor(state: ShellState) {
this._State = Object.freeze(state); this._State = Object.freeze(state);
//Temporary solution for sigint keybind //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<void> { public async execute(input: string): Promise<void> {
const parsed = tokenize(input); const parsed = tokenize(input);
@@ -119,16 +103,6 @@ export class Shell {
return this._State; 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<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void { public on<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
return this._EventEmitter.on(type, handler); return this._EventEmitter.on(type, handler);
} }