- 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:
@@ -1,26 +1,28 @@
|
||||
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||
import { fromPtr, NodeType, toPtr, type NodeEntry, type NodePointer } from "@core/filesystem/node";
|
||||
|
||||
export function dnTraverseTree(fs: FileSystem, root: number): number[] {
|
||||
export function dnTraverseTree(fs: FileSystem, root: NodePointer): NodePointer[] {
|
||||
if (fs.getNode(root) === undefined) return [];
|
||||
|
||||
const result: number[] = [];
|
||||
const stack: number[] = [];
|
||||
const result: NodePointer[] = [];
|
||||
const stack: NodePointer[] = [];
|
||||
stack.push(root);
|
||||
while (stack.length > 0) {
|
||||
const node = fs.getNode(stack.pop() as number);
|
||||
if (node === undefined) continue;
|
||||
const ptr = stack.pop()!;
|
||||
const node = fs.getNode(ptr);
|
||||
if (!node) continue;
|
||||
|
||||
result.push(node.inode);
|
||||
result.push(ptr);
|
||||
|
||||
if (node.type === NodeType.Directory) {
|
||||
const children = Object.values(node.children);
|
||||
const children = Array.from(node.children.values());
|
||||
for (let i = children.length - 1; i >= 0; i--) {
|
||||
stack.push(children[i] as number);
|
||||
const child = children[i]!;
|
||||
const pointer = toPtr({ device_id: Number(ptr.split(":")[0]), inode: child });
|
||||
stack.push(pointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
+64
-28
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user