From de1ce64226e5252b08b81bbfb5d0a172b8540d0b Mon Sep 17 00:00:00 2001 From: orion Date: Fri, 13 Nov 2026 04:15:28 +0100 Subject: [PATCH] - Fixed cat command - Moved device config initialization to `serializer.ts` - Minor bugfixes - Added `read()` to `filesystem.ts` --- src/core/commands/cat.ts | 31 ++++++++++++++++---------- src/core/filesystem/filesystem.ts | 9 ++++++++ src/core/util/path_resolver.ts | 2 +- src/dev.ts | 26 ++-------------------- src/shell/serializer.ts | 36 +++++++++++++++++++++++++------ 5 files changed, 61 insertions(+), 43 deletions(-) diff --git a/src/core/commands/cat.ts b/src/core/commands/cat.ts index 185e179..37013da 100644 --- a/src/core/commands/cat.ts +++ b/src/core/commands/cat.ts @@ -1,6 +1,7 @@ import type { CommandDescriptor, CommandOutput } from "@core/command"; import type { CommandIO } from "@core/command_io"; -import { type FileNode } from "@core/filesystem/node"; +import type { FileSystemCorruptionError } from "@core/filesystem/errors"; +import { type NodePointer } from "@core/filesystem/node"; import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result"; import { getCurrentSession } from "@core/session_manager"; import type { ShellState } from "@core/shell_state"; @@ -12,8 +13,10 @@ export const COMMAND_NAME = "cat"; //Need to implement escape sequences async function main(io: CommandIO, state: ShellState, args: string[]): Promise> { for (const arg of args) { - const inode = resolvePath(state, arg); - if (inode === -1) { + let ptr: NodePointer; + try { + ptr = resolvePath(state, arg); + } catch (e) { return { state, result: createErrorResult( @@ -25,15 +28,20 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise; } - // Casting to TreeNode because the resolvePath already ensures the inode is correct; - const node = state.filesystem.getNode(inode) as TreeNode; - - if (node.type != TreeNodeType.File) { + if (!state.filesystem.isFile(ptr)) { + const entries = state.filesystem.getNodeEntries(ptr); + if (!entries) { + throw { + type: "FileSystemCorruption", + message: "Reverse index entry missing. FileSystem corrupted", + timestamp: Date.now(), + } satisfies FileSystemCorruptionError; + } return { state, result: createErrorResult( "PathResolutionError", - `${node.name} is a directory`, + `${entries[0]!.name} is a directory`, { path: arg }, COMMAND_NAME, ), @@ -42,15 +50,16 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise; } - node as FileNode; - io.writeStdout(node.content.toArray()); + const content = await state.filesystem.read(ptr); + + io.writeStdout(content); } const result: ResultSuccess = createSuccessResult(COMMAND_NAME); diff --git a/src/core/filesystem/filesystem.ts b/src/core/filesystem/filesystem.ts index f0c680c..a45dfef 100644 --- a/src/core/filesystem/filesystem.ts +++ b/src/core/filesystem/filesystem.ts @@ -121,6 +121,15 @@ export class FileSystem { return this.resolveNode(ptr) !== undefined; } + public async read(ptr: NodePointer): Promise { + const dev = this._VDiskManager.getDisk(ptr.device); + if (!dev) return new Uint8Array(); + const node = dev.getNode(ptr.inode); + if (!node) return new Uint8Array(); + + return dev.readContent((node as FileNode).content); + } + public mount(mount: NodePointer, target: NodePointer) { if (!this._VDiskManager.hasDisk(mount.device)) { throw { diff --git a/src/core/util/path_resolver.ts b/src/core/util/path_resolver.ts index 213ee36..5b68737 100644 --- a/src/core/util/path_resolver.ts +++ b/src/core/util/path_resolver.ts @@ -131,7 +131,7 @@ export function resolvePath(state: ShellState, path: string): NodePointer { } satisfies NodeNotFoundError; } - let childPtr = NodePointer.of(currentDeviceId, currentNode.inode); + let childPtr = NodePointer.of(currentDeviceId, childInode); if (state.filesystem.isMountPoint(childPtr)) { const mount = state.filesystem.getMountTarget(childPtr)!; diff --git a/src/dev.ts b/src/dev.ts index e9c06dd..7e3cbf7 100644 --- a/src/dev.ts +++ b/src/dev.ts @@ -13,38 +13,16 @@ namespace Termos { //import { FileSystem } from "@core/filesystem/filesystem"; import { FileSystem } from "@core/filesystem/filesystem"; import { NodePointer } from "@core/filesystem/node"; -import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk"; import { getCurrentSession, type GroupInfo, type UserInfo } from "@core/session_manager"; import { createInitialShellState, type ShellState } from "@core/shell_state"; import { resolvePointerToHome } from "@core/util/path_resolver"; -import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores"; import { deserializeFileSystem } from "@shell/serializer"; import { Shell } from "@shell/shell"; //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,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 5,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 5,"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,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 7,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 7,"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,"mode": 493,"mtime": 1759940170,"name": "about.md","owner": 1000,"parent": 9,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 9,"mode": 493,"mtime": 1759940182,"name": "This Website","owner": 1000,"parent": 4,"size": 22,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 4,"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,"mode": 493,"mtime": 1759939881,"name": "C#","owner": 1000,"parent": 3,"size": 6,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 3,"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,"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,"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,"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,"mode": 493,"mtime": 1757812927,"name": "nggyu.lrc","owner": 1000,"parent": 15,"size": 171,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 15,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"mode": 493,"mtime": 1757780889,"name": ".xinitrc","owner": 1000,"parent": 2,"size": 1,"type": "regular"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 2,"mode": 493,"mtime": 1772024996,"name": "kamil","owner": 1000,"parent": 1,"size": 4096,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 1,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"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,"mode": 493,"mtime": 1757686874,"name": "var","owner": 1000,"parent": 0,"size": 6,"type": "directory"}],"content": null,"ctime": 1772323891,"group": 1000,"inode": 0,"mode": 493,"mtime": 1757686876,"name": "root","owner": 1000,"parent": 0,"size": 4096,"type": "directory"}},{"id":1,"tree":{}}]}`; -const arr = deserializeFileSystem(rawJson); -const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = []; - -for (const i of arr) { - const data = new MemDataStore(); - const meta = new MemMetaStore(); - const config: VDiskConfig = { - id: i.id, - label: `disk-${i.id}`, - index: 0, - readonly: false, - size: 1024 * 1024 * 1024 * 8, //8 GiB - base: i.map, - dataC: { - chunkSize: 1024 * 8, //8 KiB - cacheSize: 1024 * 1024 * 64, //64 MiB - cacheTTL: 1000 * 60, // 1 min - }, - }; - devices.push({ dstore: data, mstore: meta, config }); -} +const devices = deserializeFileSystem(rawJson); const fs = await FileSystem.init(devices); @@ -55,7 +33,7 @@ const user: UserInfo = { uid: 1000, gid: 1000, // <- PRIMARY GID group: [1000], // <- GROUPS ARRAY - home: NodePointer.of(0, 14), + home: NodePointer.of(0, 2), }; const group: GroupInfo = { groupname: "kamil", diff --git a/src/shell/serializer.ts b/src/shell/serializer.ts index f62d360..3c8cc98 100644 --- a/src/shell/serializer.ts +++ b/src/shell/serializer.ts @@ -1,14 +1,18 @@ +import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk"; +import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores"; +import { Blob } from "node:buffer"; +import { randomUUID } from "node:crypto"; import { NodeType, type DirectoryNode, type FileNode, type Node, type SymlinkNode } from "../core/filesystem/node"; /* export function serializeFileSystem(map: ReadonlyMap): string { } */ -export function deserializeFileSystem(json: string): { id: number; map: Map }[] { +export function deserializeFileSystem(json: string): { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] { const raw = JSON.parse(json); // prettier-ignore - function parseNode(tree: any, parent: number, map: Map): void { // eslint-disable-line @typescript-eslint/no-explicit-any + function parseNode(tree: any, parent: number, store: MemDataStore, map: Map): void { // eslint-disable-line @typescript-eslint/no-explicit-any if(tree === null || tree.type === undefined) { const time = Date.now(); const defaultNode: DirectoryNode = { @@ -32,7 +36,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map = new Map(); for (const child of tree.children) { - parseNode(child, tree.inode, map); + parseNode(child, tree.inode, store, map); children.set(child.name, child.inode); } @@ -54,6 +58,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map = Uint8Array.from(tree.content); + store.put(uuid, new Blob([buff])); map.set(tree.inode, Object.freeze(fileNode)); break; } @@ -91,12 +98,27 @@ export function deserializeFileSystem(json: string): { id: number; map: Map }[] = []; + const result: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = []; for (const dev of raw.dev) { const map = new Map(); - parseNode(dev.tree, dev.tree.inode, map); + const data = new MemDataStore(); + const meta = new MemMetaStore(); + parseNode(dev.tree, dev.tree.inode, data, map); - result.push({ id: dev.id, map }); + const config: VDiskConfig = { + id: dev.id, + label: `disk-${dev.id}`, + index: 0, + readonly: false, + size: 1024 * 1024 * 1024 * 8, //8 GiB + base: map, + dataC: { + chunkSize: 1024 * 8, //8 KiB + cacheSize: 1024 * 1024 * 64, //64 MiB + cacheTTL: 1000 * 60, // 1 min + }, + }; + result.push({ dstore: data, mstore: meta, config }); } return result;