- Downward tree traversal fixed (untested - might have bugs)

- Moved filesystem mockups to a separate file
- Tree visualizer now has support for symlinks and mountpoints
This commit is contained in:
2026-11-10 09:08:15 +01:00
parent 4fa284de13
commit 584e594726
4 changed files with 252 additions and 243 deletions
+64 -28
View File
@@ -1,38 +1,50 @@
import type { FileSystem } from "@core/filesystem/filesystem";
import { fromPtr, NodeType, toPtr, type NodePointer, type SymlinkNode } from "@core/filesystem/node";
type Frame = {
ptr: NodePointer;
name: string;
prefix: string;
isLast: boolean;
};
function getSortedChildren(fs: FileSystem, ptr: NodePointer): Array<{ name: string; ptr: NodePointer }> {
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 }),
}));
}
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})`;
}
function symlinkSuffix(node: { type: NodeType; target?: string }): string {
return node.type === NodeType.Symlink ? ` -> ${(node as SymlinkNode).target}` : "";
}
export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): string {
const rootNode = fs.getNode(root);
if (rootNode === undefined) return "";
const lines: string[] = [];
const rootFptr = fromPtr(root);
lines.push(`${rootLabel} (${rootFptr.device_id}:${rootFptr.inode})`);
type Frame = {
ptr: NodePointer;
name: string;
prefix: string;
isLast: boolean;
};
const rootSuffix = mountSuffix(fs, root) + symlinkSuffix(rootNode);
lines.push(`${rootLabel} (${rootFptr.device_id}:${rootFptr.inode})${rootSuffix}`);
const stack: Frame[] = [];
const getSortedChildren = (ptr: NodePointer): Array<{ name: string; ptr: NodePointer }> => {
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 }),
}));
};
const rootChildren = getSortedChildren(root);
const rootChildren = getSortedChildren(fs, root);
for (let i = rootChildren.length - 1; i >= 0; i--) {
const child = rootChildren[i]!;
const isLast = i === rootChildren.length - 1;
@@ -46,12 +58,12 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s
const fptr = fromPtr(frame.ptr);
const connector = frame.isLast ? "└── " : "├── ";
const symlinkSuffix = node.type === NodeType.Symlink ? ` -> ${(node as SymlinkNode).target}` : "";
lines.push(`${frame.prefix}${connector}${frame.name} (${fptr.device_id}:${fptr.inode})${symlinkSuffix}`);
const suffix = mountSuffix(fs, frame.ptr) + symlinkSuffix(node);
lines.push(`${frame.prefix}${connector}${frame.name} (${fptr.device_id}:${fptr.inode})${suffix}`);
if (node.type === NodeType.Directory) {
if (node.type === NodeType.Directory && !fs.isMountPoint(frame.ptr)) {
const childPrefix = frame.prefix + (frame.isLast ? " " : "│ ");
const children = getSortedChildren(frame.ptr);
const children = getSortedChildren(fs, frame.ptr);
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i]!;
@@ -68,3 +80,27 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s
return lines.join("\n");
}
export function printDeviceTree(fs: FileSystem, deviceId: number, rootLabel?: string): string {
return printTree(fs, toPtr({ device_id: deviceId, inode: 0 }), rootLabel ?? `disk-${deviceId}:/`);
}
export function printAllDeviceTrees(fs: FileSystem, deviceIds?: number[]): string {
const ids =
deviceIds ??
(() => {
const mgr = (fs as unknown as { _VDiskManager?: { listID(): number[] } })._VDiskManager;
if (!mgr) {
throw new Error(
"printAllDeviceTrees: FileSystem has no public device list — pass deviceIds explicitly",
);
}
return mgr.listID();
})();
return ids
.slice()
.sort((a, b) => a - b)
.map(id => printDeviceTree(fs, id))
.join("\n\n");
}