From b161e42b3f48ee3db5aa4e4839423c948ed94648 Mon Sep 17 00:00:00 2001 From: hyperion Date: Tue, 10 Nov 2026 16:19:10 +0100 Subject: [PATCH] - Fixed ls (mostly) - Fixed sorting - Many minor changes (need to fix the json serializer to include size in its results *the C++ one*) --- src/core/commands/ls.ts | 102 +++++++++++-------- src/core/filesystem/filesystem.ts | 163 +++++++++++++----------------- src/core/filesystem/vdisk.ts | 14 +-- src/core/util/sort.ts | 92 ++++++----------- src/core/util/tree_traversal.ts | 8 +- src/shell/serializer.ts | 8 +- src/util/tree_visualizer.ts | 18 ++-- 7 files changed, 179 insertions(+), 226 deletions(-) diff --git a/src/core/commands/ls.ts b/src/core/commands/ls.ts index e254178..bb57b8d 100644 --- a/src/core/commands/ls.ts +++ b/src/core/commands/ls.ts @@ -1,17 +1,22 @@ import type { CommandDescriptor, CommandOutput } from "@core/command"; import type { CommandIO } from "@core/command_io"; -import { type DirectoryNode, type NodePointer } from "@core/filesystem/node"; +import { NodePointer, NodeType, type DirectoryNode, type FileNode, type Node } from "@core/filesystem/node"; import { createErrorResult, createSuccessResult } from "@core/result"; import { getCurrentSession } from "@core/session_manager"; import type { ShellState } from "@core/shell_state"; import type { ShellError } from "@core/util/errors"; import { resolvePath } from "@core/util/path_resolver"; -import { SortTreeNodeBy, sortTreeNodes } from "@core/util/sort"; +import { SortNodeBy, sortNodes } from "@core/util/sort"; import { concatUint8Arrays } from "@termos/util/concat"; import { convertToUnit } from "@termos/util/format_unit"; export const COMMAND_NAME = "ls"; +interface DirEntry { + name: string; + ptr: NodePointer; +} + async function main(io: CommandIO, state: ShellState, args: string[]): Promise> { const entries: { ptr: NodePointer; path: string }[] = []; const paths: string[] = []; @@ -24,7 +29,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise state.filesystem.getNode(c[1])!); - if (!options.noSort) children = sortTreeNodes(state, children, options.reverse, options.sortBy); + let children: DirEntry[] = Array.from(node.children.entries()).map(([name, inode]) => ({ + name, + ptr: NodePointer.of(entry.ptr.device, inode), + })); + if (!options.noSort) children = sortNodes(state, children, options.reverse, options.sortBy); - if (!options.all && !options.almostAll) children = children.filter(c => !c.name.startsWith(".")); + if (!options.all && !options.almostAll) { + children = children.filter(c => !c.name.startsWith(".")); + } if (options.long) { - longMode(io, state, children, node); + longMode(io, state, children); if (options.recursive) io.writeStdout(e.encode("\n")); } if (options.recursive) { - for (const child of children) { - if (child.type === TreeNodeType.Directory) { + for (const c of children) { + const child = state.filesystem.getNode(c.ptr)!; + if (child.type === NodeType.Directory) { let path; if (entry.path === "/") { - path = "/" + child.name; + path = "/" + c.name; } else if (entry.path.endsWith("/")) { - path = entry.path + child.name; - } else path = entry.path + "/" + child.name; - listDirEntries({ inode: child.inode, path }); + path = entry.path + c.name; + } else path = entry.path + "/" + c.name; + listDirEntries({ ptr: c.ptr, path }); } } } } - function longMode(io: CommandIO, state: ShellState, children: TreeNode[], current: TreeNode): void { + function longMode(io: CommandIO, state: ShellState, children: DirEntry[]): void { const e = new TextEncoder(); const buff: Uint8Array[] = []; - const nodes: TreeNode[] = [...children]; + const nodes: DirEntry[] = [...children]; - if (options.all) { + /* if (options.all) { const parentNode = state.filesystem.getNode(current.parent)!; - const parent: TreeNode = { ...parentNode, name: ".." }; + const parent: NodePointer = { ...parentNode, name: ".." }; nodes.unshift(parent); const cwd: TreeNode = { ...current, name: "." }; nodes.unshift(cwd); - } + } */ // Calculate the string size of the biggerst node in the array; // Sets 4 length for directories because the arbitrary size is 4096 later in the code - const maxInodeWidth = Math.max(...nodes.map(c => c.inode.toString().length)); + const maxInodeWidth = Math.max(...nodes.map(c => c.ptr.inode.toString().length)); const maxSizeWidth = Math.max( - ...nodes.map(c => (c.type === TreeNodeType.File ? c.content.byteSize().toString().length : 4)), + ...nodes.map(c => + state.filesystem.getNode(c.ptr)!.type === NodeType.File + ? (state.filesystem.getNode(c.ptr) as FileNode).size.toString().length + : 4, + ), ); const maxChildrenWidth = Math.max( ...nodes.map(c => - c.type === TreeNodeType.Directory ? Object.entries(c.children).length.toString().length : 2, + state.filesystem.getNode(c.ptr)!.type === NodeType.Directory + ? Object.entries((state.filesystem.getNode(c.ptr) as DirectoryNode)!.children).length + : 2, ), ); const namePadding = nodes.every(c => !c.name.includes(" ")); - for (const node of nodes) { + for (const n of nodes) { //Inode + const node = state.filesystem.getNode(n.ptr)!; if (options.inode) { const inode = node.inode.toString().padStart(maxInodeWidth, " "); buff.push(e.encode(inode + " ")); @@ -320,17 +338,17 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise( state: ShellState, - arr: NodePointer[], + arr: T[], r: boolean = false, by: SortNodeBy = SortNodeBy.NAME, -): NodePointer[] { +): T[] { if (arr.length === 0) return []; - /* const nodes: NodePointer[] = []; - - if (arr[0] instanceof NodePointer) { - nodes.push(...(arr as NodePointer[])); - for (const e of arr as NodePointer[]) { - const node = state.filesystem.getNode(e); - if (!node) continue; - - nodes.push(node); - } - } else { - for (const e of arr as Node[]) { - const ptr = NodePointer.of(device, e.inode); - nodes.push(ptr); - } - } - */ nodeQS(arr, r, by, 0, arr.length - 1, state); - return arr; - - /* if (arr[0] instanceof NodePointer) { - return nodes.map(n => state.filesystem.getNode(n as NodePointer)) as T[]; - } else { - return nodes as T[]; - } */ } -function nodeQS(arr: NodePointer[], r: boolean, by: SortNodeBy, start: number, end: number, state: ShellState): void { +function nodeQS( + arr: { name: string; ptr: NodePointer }[], + r: boolean, + by: SortNodeBy, + start: number, + end: number, + state: ShellState, +): void { if (end <= start) return; const pivot = partitionNodeArray(arr, r, by, start, end, state); @@ -59,7 +41,7 @@ function nodeQS(arr: NodePointer[], r: boolean, by: SortNodeBy, start: number, e } function partitionNodeArray( - part: NodePointer[], + part: { name: string; ptr: NodePointer }[], r: boolean, by: SortNodeBy, start: number, @@ -86,21 +68,17 @@ function partitionNodeArray( return i; } -function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean, state: ShellState): number { +function compareNode( + a: { name: string; ptr: NodePointer }, + b: { name: string; ptr: NodePointer }, + by: SortNodeBy, + r: boolean, + state: ShellState, +): number { switch (by) { case SortNodeBy.NAME: { - const entriesA = state.filesystem.getNodeEntries(a); - const entriesB = state.filesystem.getNodeEntries(b); - if (!entriesA || !entriesB) { - throw { - type: "FileSystemCorruption", - message: "Reverse index entry missing, FileSystem corrupted", - timestamp: Date.now(), - } satisfies FileSystemCorruptionError; - } - - const A: string = entriesA[0]!.name; - const B: string = entriesB[0]!.name; + const A: string = a.name; + const B: string = b.name; const min = Math.min(A.length, B.length); for (let i = 0; i < min; i++) { @@ -118,8 +96,8 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean, case SortNodeBy.MTIME: case SortNodeBy.ATIME: case SortNodeBy.CTIME: { - const nodeA = state.filesystem.getNode(a)!; - const nodeB = state.filesystem.getNode(a)!; + const nodeA = state.filesystem.getNode(a.ptr)!; + const nodeB = state.filesystem.getNode(b.ptr)!; const A: number = nodeA[by]; const B: number = nodeB[by]; @@ -127,8 +105,8 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean, } case SortNodeBy.SIZE: { - const nodeA = state.filesystem.getNode(a)!; - const nodeB = state.filesystem.getNode(a)!; + const nodeA = state.filesystem.getNode(a.ptr)!; + const nodeB = state.filesystem.getNode(b.ptr)!; const A: number = (nodeA as FileNode).size; const B: number = (nodeB as FileNode).size; @@ -136,18 +114,8 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean, } case SortNodeBy.EXTENSION: { - const entriesA = state.filesystem.getNodeEntries(a); - const entriesB = state.filesystem.getNodeEntries(b); - if (!entriesA || !entriesB) { - throw { - type: "FileSystemCorruption", - message: "Reverse index entry missing, FileSystem corrupted", - timestamp: Date.now(), - } satisfies FileSystemCorruptionError; - } - - const A: string = entriesA[0]!.name.split(".").pop() ?? ""; - const B: string = entriesB[0]!.name.split(".").pop() ?? ""; + const A: string = a.name.split(".").pop() ?? ""; + const B: string = b.name.split(".").pop() ?? ""; const min = Math.min(A.length, B.length); for (let i = 0; i < min; i++) { @@ -163,12 +131,12 @@ function compareNode(a: NodePointer, b: NodePointer, by: SortNodeBy, r: boolean, } case SortNodeBy.INODE: { - return r ? a.inode - b.inode : b.inode - a.inode; + return r ? a.ptr.inode - b.ptr.inode : b.ptr.inode - a.ptr.inode; } case SortNodeBy.TYPE: { - const nodeA = state.filesystem.getNode(a)!; - const nodeB = state.filesystem.getNode(a)!; + const nodeA = state.filesystem.getNode(a.ptr)!; + const nodeB = state.filesystem.getNode(a.ptr)!; return r ? nodeB.type - nodeA.type : nodeA.type - nodeB.type; } } diff --git a/src/core/util/tree_traversal.ts b/src/core/util/tree_traversal.ts index 938cefb..0259410 100644 --- a/src/core/util/tree_traversal.ts +++ b/src/core/util/tree_traversal.ts @@ -1,5 +1,5 @@ import type { FileSystem } from "@core/filesystem/filesystem"; -import { fromPtr, NodeType, toPtr, type NodeEntry, type NodePointer } from "@core/filesystem/node"; +import { NodePointer, NodeType, type NodeEntry } from "@core/filesystem/node"; export function dnTraverseTree(fs: FileSystem, root: NodePointer): NodePointer[] { if (fs.getNode(root) === undefined) return []; @@ -18,7 +18,7 @@ export function dnTraverseTree(fs: FileSystem, root: NodePointer): NodePointer[] const children = Array.from(node.children.values()); for (let i = children.length - 1; i >= 0; i--) { const child = children[i]!; - const pointer = toPtr({ device_id: Number(ptr.split(":")[0]), inode: child }); + const pointer = NodePointer.of(ptr.device, child); stack.push(pointer); } } @@ -32,12 +32,12 @@ export function upTraverseTree(fs: FileSystem, ptr: NodePointer): NodePointer[] const result: NodePointer[] = [ptr]; let currentPtr = ptr; - while (fromPtr(currentPtr).inode !== 0) { + while (currentPtr.inode !== 0) { const entries = fs.getNodeEntries(currentPtr); if (!entries || entries.length === 0) throw new Error(`No reverse-index entry found for ${currentPtr}`); const parent = entries[0] as NodeEntry; - currentPtr = toPtr({ device_id: parent.pdevice_id, inode: parent.pinode }); + currentPtr = NodePointer.of(parent.pdevice_id, parent.pinode); result.push(currentPtr); } diff --git a/src/shell/serializer.ts b/src/shell/serializer.ts index bee6786..f62d360 100644 --- a/src/shell/serializer.ts +++ b/src/shell/serializer.ts @@ -8,7 +8,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map): void { // eslint-disable-line @typescript-eslint/no-explicit-any + function parseNode(tree: any, parent: number, 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 +32,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map = new Map(); for (const child of tree.children) { - parseNode(child, map); + parseNode(child, tree.inode, map); children.set(child.name, child.inode); } @@ -48,6 +48,8 @@ export function deserializeFileSystem(json: string): { id: number; map: Map }[] = []; for (const dev of raw.dev) { const map = new Map(); - parseNode(dev.tree, map); + parseNode(dev.tree, dev.tree.inode, map); result.push({ id: dev.id, map }); } diff --git a/src/util/tree_visualizer.ts b/src/util/tree_visualizer.ts index ae83b27..3567848 100644 --- a/src/util/tree_visualizer.ts +++ b/src/util/tree_visualizer.ts @@ -1,5 +1,5 @@ import type { FileSystem } from "@core/filesystem/filesystem"; -import { fromPtr, NodeType, toPtr, type NodePointer, type SymlinkNode } from "@core/filesystem/node"; +import { NodePointer, NodeType, type SymlinkNode } from "@core/filesystem/node"; type Frame = { ptr: NodePointer; @@ -12,13 +12,12 @@ function getSortedChildren(fs: FileSystem, ptr: NodePointer): Array<{ name: stri const node = fs.getNode(ptr); if (!node || node.type !== NodeType.Directory) return []; - const { device_id } = fromPtr(ptr); const entries = Array.from(node.children.entries()); entries.sort(([a], [b]) => a.localeCompare(b)); return entries.map(([name, inode]) => ({ name, - ptr: toPtr({ device_id, inode }), + ptr: NodePointer.of(ptr.device, inode), })); } @@ -26,8 +25,7 @@ function mountSuffix(fs: FileSystem, ptr: NodePointer): string { if (!fs.isMountPoint(ptr)) return ""; const target = fs.getMountPoint(ptr); if (!target) return " -> [mount: target missing]"; - const t = fromPtr(target); - return ` -> (${t.device_id}:${t.inode})`; + return ` -> ${target.toString()}`; } function symlinkSuffix(node: { type: NodeType; target?: string }): string { @@ -39,14 +37,14 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s if (rootNode === undefined) return ""; const lines: string[] = []; - const rootFptr = fromPtr(root); const rootSuffix = mountSuffix(fs, root) + symlinkSuffix(rootNode); - lines.push(`${rootLabel} (${rootFptr.device_id}:${rootFptr.inode})${rootSuffix}`); + lines.push(`${rootLabel} ${root.toString()}${rootSuffix}`); const stack: Frame[] = []; const rootChildren = getSortedChildren(fs, root); for (let i = rootChildren.length - 1; i >= 0; i--) { const child = rootChildren[i]!; + if (child.name.startsWith(".")) continue; const isLast = i === rootChildren.length - 1; stack.push({ ptr: child.ptr, name: child.name, prefix: "", isLast }); } @@ -56,10 +54,9 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s const node = fs.getNode(frame.ptr); if (!node) continue; - const fptr = fromPtr(frame.ptr); const connector = frame.isLast ? "└── " : "├── "; const suffix = mountSuffix(fs, frame.ptr) + symlinkSuffix(node); - lines.push(`${frame.prefix}${connector}${frame.name} (${fptr.device_id}:${fptr.inode})${suffix}`); + lines.push(`${frame.prefix}${connector}${frame.name} ${frame.ptr.toString()}${suffix}`); if (node.type === NodeType.Directory && !fs.isMountPoint(frame.ptr)) { const childPrefix = frame.prefix + (frame.isLast ? " " : "│ "); @@ -67,6 +64,7 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s for (let i = children.length - 1; i >= 0; i--) { const child = children[i]!; + if (child.name.startsWith(".")) continue; const childIsLast = i === children.length - 1; stack.push({ ptr: child.ptr, @@ -82,7 +80,7 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s } export function printDeviceTree(fs: FileSystem, deviceId: number, rootLabel?: string): string { - return printTree(fs, toPtr({ device_id: deviceId, inode: 0 }), rootLabel ?? `disk-${deviceId}:/`); + return printTree(fs, NodePointer.of(deviceId, 0), rootLabel ?? `disk-${deviceId}:/`); } export function printAllDeviceTrees(fs: FileSystem, deviceIds?: number[]): string {