- 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 type { FileSystem } from "@core/filesystem/filesystem";
|
||||||
import { fromPtr, NodeType, toPtr, type NodeEntry, type NodePointer } from "@core/filesystem/node";
|
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 [];
|
if (fs.getNode(root) === undefined) return [];
|
||||||
|
|
||||||
const result: number[] = [];
|
const result: NodePointer[] = [];
|
||||||
const stack: number[] = [];
|
const stack: NodePointer[] = [];
|
||||||
stack.push(root);
|
stack.push(root);
|
||||||
while (stack.length > 0) {
|
while (stack.length > 0) {
|
||||||
const node = fs.getNode(stack.pop() as number);
|
const ptr = stack.pop()!;
|
||||||
if (node === undefined) continue;
|
const node = fs.getNode(ptr);
|
||||||
|
if (!node) continue;
|
||||||
|
|
||||||
result.push(node.inode);
|
result.push(ptr);
|
||||||
|
|
||||||
if (node.type === NodeType.Directory) {
|
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--) {
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+64
-28
@@ -1,38 +1,50 @@
|
|||||||
import type { FileSystem } from "@core/filesystem/filesystem";
|
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||||
import { fromPtr, NodeType, toPtr, type NodePointer, type SymlinkNode } from "@core/filesystem/node";
|
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 {
|
export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): string {
|
||||||
const rootNode = fs.getNode(root);
|
const rootNode = fs.getNode(root);
|
||||||
if (rootNode === undefined) return "";
|
if (rootNode === undefined) return "";
|
||||||
|
|
||||||
const lines: string[] = [];
|
const lines: string[] = [];
|
||||||
const rootFptr = fromPtr(root);
|
const rootFptr = fromPtr(root);
|
||||||
lines.push(`${rootLabel} (${rootFptr.device_id}:${rootFptr.inode})`);
|
const rootSuffix = mountSuffix(fs, root) + symlinkSuffix(rootNode);
|
||||||
|
lines.push(`${rootLabel} (${rootFptr.device_id}:${rootFptr.inode})${rootSuffix}`);
|
||||||
type Frame = {
|
|
||||||
ptr: NodePointer;
|
|
||||||
name: string;
|
|
||||||
prefix: string;
|
|
||||||
isLast: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
const stack: Frame[] = [];
|
const stack: Frame[] = [];
|
||||||
|
const rootChildren = getSortedChildren(fs, root);
|
||||||
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);
|
|
||||||
for (let i = rootChildren.length - 1; i >= 0; i--) {
|
for (let i = rootChildren.length - 1; i >= 0; i--) {
|
||||||
const child = rootChildren[i]!;
|
const child = rootChildren[i]!;
|
||||||
const isLast = i === rootChildren.length - 1;
|
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 fptr = fromPtr(frame.ptr);
|
||||||
const connector = frame.isLast ? "└── " : "├── ";
|
const connector = frame.isLast ? "└── " : "├── ";
|
||||||
const symlinkSuffix = node.type === NodeType.Symlink ? ` -> ${(node as SymlinkNode).target}` : "";
|
const suffix = mountSuffix(fs, frame.ptr) + symlinkSuffix(node);
|
||||||
lines.push(`${frame.prefix}${connector}${frame.name} (${fptr.device_id}:${fptr.inode})${symlinkSuffix}`);
|
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 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--) {
|
for (let i = children.length - 1; i >= 0; i--) {
|
||||||
const child = children[i]!;
|
const child = children[i]!;
|
||||||
@@ -68,3 +80,27 @@ export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): s
|
|||||||
|
|
||||||
return lines.join("\n");
|
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");
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,191 +1,11 @@
|
|||||||
// @tests/core/util/path_resolver.test.ts (adjust)
|
// @tests/core/util/path_resolver.test.ts (adjust)
|
||||||
import { FileSystem } from "@core/filesystem/filesystem";
|
import { toPtr, type NodePointer } from "@core/filesystem/node";
|
||||||
import {
|
|
||||||
NodeType,
|
|
||||||
toPtr,
|
|
||||||
type DirectoryNode,
|
|
||||||
type FileNode,
|
|
||||||
type Node,
|
|
||||||
type NodePointer,
|
|
||||||
type SymlinkNode,
|
|
||||||
} from "@core/filesystem/node";
|
|
||||||
import { DefaultVDiskConfig } from "@core/filesystem/vdisk";
|
|
||||||
import type { SessionState, UserInfo } from "@core/session_manager";
|
|
||||||
import type { ShellState } from "@core/shell_state";
|
|
||||||
import { resolvePath } from "@core/util/path_resolver";
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores";
|
|
||||||
import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers";
|
import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers";
|
||||||
|
import { makeFS, makeShellState, makeTree, makeUser } from "@tests/helpers/filesystems_fixture";
|
||||||
import fc from "fast-check";
|
import fc from "fast-check";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// Node fixtures
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
function makeDir(inode: number, children: Record<string, number> = {}): DirectoryNode {
|
|
||||||
return {
|
|
||||||
type: NodeType.Directory,
|
|
||||||
inode,
|
|
||||||
link_count: 2,
|
|
||||||
mode: 0o755,
|
|
||||||
owner: 0,
|
|
||||||
group: 0,
|
|
||||||
mtime: 0,
|
|
||||||
ctime: 0,
|
|
||||||
atime: 0,
|
|
||||||
children: new Map(Object.entries(children)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeFile(inode: number): FileNode {
|
|
||||||
return {
|
|
||||||
type: NodeType.File,
|
|
||||||
inode,
|
|
||||||
link_count: 1,
|
|
||||||
mode: 0o644,
|
|
||||||
owner: 0,
|
|
||||||
group: 0,
|
|
||||||
mtime: 0,
|
|
||||||
ctime: 0,
|
|
||||||
atime: 0,
|
|
||||||
content: `uuid-${inode}`,
|
|
||||||
size: 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeSymlink(inode: number, target: string): SymlinkNode {
|
|
||||||
return {
|
|
||||||
type: NodeType.Symlink,
|
|
||||||
inode,
|
|
||||||
link_count: 1,
|
|
||||||
mode: 0o777,
|
|
||||||
owner: 0,
|
|
||||||
group: 0,
|
|
||||||
mtime: 0,
|
|
||||||
ctime: 0,
|
|
||||||
atime: 0,
|
|
||||||
target,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeUser(home: NodePointer = toPtr({ device_id: 0, inode: 0 })): UserInfo {
|
|
||||||
return {
|
|
||||||
username: "test",
|
|
||||||
uid: 1000,
|
|
||||||
gid: 1000,
|
|
||||||
group: [],
|
|
||||||
home,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// Tree builder — auto-assigns inodes, validates before use
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
interface TreeBuilder {
|
|
||||||
addDir(parentInode: number, name: string): number;
|
|
||||||
addFile(parentInode: number, name: string): number;
|
|
||||||
addSymlink(parentInode: number, name: string, target: string): number;
|
|
||||||
build(): Map<number, Node>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeTree(): TreeBuilder {
|
|
||||||
const nodes = new Map<number, Node>([[0, makeDir(0)]]);
|
|
||||||
let nextInode = 1;
|
|
||||||
|
|
||||||
function attach(parentInode: number, name: string, childInode: number): void {
|
|
||||||
const parent = nodes.get(parentInode);
|
|
||||||
if (!parent || parent.type !== NodeType.Directory) {
|
|
||||||
throw new Error(`Parent inode ${parentInode} is not a directory`);
|
|
||||||
}
|
|
||||||
const children = new Map(parent.children);
|
|
||||||
children.set(name, childInode);
|
|
||||||
nodes.set(parentInode, { ...parent, children });
|
|
||||||
}
|
|
||||||
|
|
||||||
function add(node: Node, parentInode: number, name: string): number {
|
|
||||||
attach(parentInode, name, node.inode);
|
|
||||||
nodes.set(node.inode, node);
|
|
||||||
return node.inode;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
addDir(parentInode, name) {
|
|
||||||
return add(makeDir(nextInode++), parentInode, name);
|
|
||||||
},
|
|
||||||
addFile(parentInode, name) {
|
|
||||||
return add(makeFile(nextInode++), parentInode, name);
|
|
||||||
},
|
|
||||||
addSymlink(parentInode, name, target) {
|
|
||||||
return add(makeSymlink(nextInode++, target), parentInode, name);
|
|
||||||
},
|
|
||||||
build() {
|
|
||||||
for (const [key, node] of nodes) {
|
|
||||||
if (node.inode !== key) {
|
|
||||||
throw new Error(`inode mismatch: key=${key}, node.inode=${node.inode}`);
|
|
||||||
}
|
|
||||||
if (node.type === NodeType.Directory) {
|
|
||||||
for (const [cname, cinode] of node.children) {
|
|
||||||
if (!nodes.has(cinode)) {
|
|
||||||
throw new Error(`dangling child '${cname}' → inode ${cinode}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nodes;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// FileSystem builder — real VDisk stack, one device per base map
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
async function makeFS(bases: Map<number, Node>[]): Promise<FileSystem> {
|
|
||||||
const devices = bases.map((base, id) => ({
|
|
||||||
dstore: new MemDataStore(),
|
|
||||||
mstore: new MemMetaStore(),
|
|
||||||
config: {
|
|
||||||
...DefaultVDiskConfig(id),
|
|
||||||
base,
|
|
||||||
index: Math.max(0, ...base.keys()),
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
const fs = FileSystem.init(devices);
|
|
||||||
// `FileSystem.init` does not await `registerDisk` (see the init suite BUG);
|
|
||||||
// flush a macrotask so every disk is registered before the first resolve.
|
|
||||||
await new Promise(r => setImmediate(r));
|
|
||||||
return fs;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// Shell state fixture
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
function makeShellState(
|
|
||||||
fs: FileSystem,
|
|
||||||
opts: { cwd?: NodePointer; home?: NodePointer; user?: UserInfo } = {},
|
|
||||||
): ShellState {
|
|
||||||
const cwd = opts.cwd ?? toPtr({ device_id: 0, inode: 0 });
|
|
||||||
const user = opts.user ?? makeUser(opts.home);
|
|
||||||
const session = {
|
|
||||||
user,
|
|
||||||
envVars: new Map<string, string>(),
|
|
||||||
history: [],
|
|
||||||
cwd,
|
|
||||||
pwd: cwd,
|
|
||||||
exitCode: 0,
|
|
||||||
} as unknown as SessionState;
|
|
||||||
|
|
||||||
return {
|
|
||||||
filesystem: fs,
|
|
||||||
users: new Map(),
|
|
||||||
groups: new Map(),
|
|
||||||
sessionStack: [session],
|
|
||||||
} as unknown as ShellState;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Path normalization
|
// Path normalization
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user