- Fixed resolvePath

- Added symlink resolution
- Added mount resolution
- Fixed filesystem not awaiting for disks to be mounted
(now `FileSystem.init()` needs to be called with `await`)
- Removed old test fixture files
- Added a feature-full `resolvePath`test suite

*all tests passing green*
This commit is contained in:
2026-11-10 08:04:57 +01:00
parent dd804f6f14
commit 4fa284de13
14 changed files with 4608 additions and 657 deletions
+166 -50
View File
@@ -1,7 +1,7 @@
import type { FileSystemCorruptionError, NodeNotFoundError, NotADirectoryError } from "@core/filesystem/errors";
import { fromPtr, NodeType, toPtr, type Node, type NodePointer } from "@core/filesystem/node";
import { getCurrentSession } from "@core/session_manager";
import type { ShellState } from "@core/shell_state";
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
import { type DirectoryNode, type NodeEntry, type NodePointer } from "../filesystem/node";
/*
/home/grzegorz/Documents/homework
@@ -9,77 +9,193 @@ import { type DirectoryNode, type NodeEntry, type NodePointer } from "../filesys
""
*/
export function resolvePath(state: ShellState, path: string): NodeEntry {
export function resolvePath(state: ShellState, path: string): { device_id: number; inode: number } {
let linkDepth = 0;
const maxDepth = state.filesystem.maxSymlinkDepth;
const session = getCurrentSession(state);
const isAbsolute = path.startsWith("/"); // true
const isAbsolute = path.startsWith("/");
const normalizedPath: string = path.replace(/^\/+|\/+$/g, ""); // home/grzegorz/Documents/homework
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0); // ["home", "grzegorz", "Documents", "homework"]
let currentVD: number = state.filesystem.getSysPartitionID();
const pathComponents: string[] = path.split("/").filter(c => c.length > 0);
// Fix to dynamically get the root inode VV
if (pathComponents.length === 0 && isAbsolute) return [0, currentVD];
// Move behaviour into cd built-in
if (pathComponents.length === 0) return [session.cwd, currentVD];
let currentDeviceId: number;
let currentNode: Node;
let currentNode = isAbsolute === true ? state.filesystem.getRoot() : state.filesystem.getNode(session.cwd)!;
if (isAbsolute) {
currentDeviceId = 0;
currentNode = state.filesystem.getRoot();
} else if (!isAbsolute && pathComponents[0] === "~") {
const home = fromPtr(session.user.home);
currentDeviceId = home.device_id;
currentNode = state.filesystem.getNode(session.user.home)!;
if (!currentNode) {
throw {
type: "NodeNotFound",
message: `[Panic] ${session.user.username}'s home directory does not exist!`,
timestamp: Date.now(),
pointer: session.user.home,
} satisfies NodeNotFoundError;
}
pathComponents.shift();
if (!currentNode) {
throw Error("Current working directory does not exist in the filesystem! Is the userdata corrupted?");
if (currentNode.type === NodeType.Symlink) {
if (++linkDepth > maxDepth) throw new Error("Symlink max depth - aborting!");
const target = currentNode.target;
if (target.startsWith("/")) {
currentDeviceId = 0;
currentNode = state.filesystem.getRoot();
} else {
const entries = state.filesystem.getNodeEntries(session.user.home);
if (!entries?.[0]) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
currentDeviceId = entries[0].pdevice_id;
currentNode = state.filesystem.getNode(
toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode }),
)!;
}
pathComponents.unshift(...target.split("/").filter(c => c.length > 0));
}
} else {
const cwd = fromPtr(session.cwd);
currentDeviceId = cwd.device_id;
currentNode = state.filesystem.getNode(session.cwd)!;
if (!currentNode) {
throw {
type: "NodeNotFound",
message: "[Panic] Current working directory does not exist!",
timestamp: Date.now(),
pointer: session.cwd,
} satisfies NodeNotFoundError;
}
}
for (let i = 0; i < pathComponents.length; i++) {
// Null assertion because by this point the array cannot be empty
const name: string = pathComponents[i]!;
while (pathComponents.length > 0) {
const name = pathComponents.shift()!;
if (state.filesystem.isFile(currentNode.inode) && !(i === pathComponents.length - 1))
if (name === ".") continue;
if (name === "..") {
if (state.filesystem.isMountTarget(toPtr({ device_id: currentDeviceId, inode: currentNode.inode }))) {
const mount = state.filesystem.getMountPoint(
toPtr({ device_id: currentDeviceId, inode: currentNode.inode }),
);
if (!mount) {
throw {
type: "FileSystemCorruption",
message: "Mount registry entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
const entries = state.filesystem.getNodeEntries(mount);
if (!entries?.[0]) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
currentDeviceId = entries[0].pdevice_id;
currentNode = state.filesystem.getNode(
toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode }),
)!;
continue;
}
if (currentNode.inode === 0) continue;
const entries = state.filesystem.getNodeEntries(
toPtr({ device_id: currentDeviceId, inode: currentNode.inode }),
);
if (!entries?.[0]) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
currentDeviceId = entries[0].pdevice_id;
currentNode = state.filesystem.getNode(
toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode }),
)!;
continue;
}
if (currentNode.type !== NodeType.Directory) {
throw {
type: "NotADirectory",
message: `${name} is not a directory`,
message: "Not a directory",
timestamp: Date.now(),
inode: currentNode.inode,
nodeType: TreeNodeType.File,
nodeType: currentNode.type,
} satisfies NotADirectoryError;
if (currentNode.type === TreeNodeType.Device) {
currentVD = currentNode.target_id;
continue;
}
if (currentNode.type === TreeNodeType.Symlink) {
const ptr: NodePointer = resolvePath(state, currentNode.target);
currentNode = state.filesystem.getNode(ptr)!;
if (!currentNode) {
throw {
type: "NodeNotFound",
message: `${name} not found`,
timestamp: Date.now(),
inode: ptr[0],
} satisfies NodeNotFoundError;
}
continue;
}
if (name === "~") {
currentNode = state.filesystem.getNode(session.user.home) as DirectoryNode;
continue;
}
const inode = currentNode.content.get(name);
if (!inode)
const childInode = currentNode.children.get(name);
if (childInode === undefined) {
throw {
type: "NodeNotFound",
message: `${name} not found`,
timestamp: Date.now(),
inode: currentNode.inode,
pointer: toPtr({ device_id: currentDeviceId, inode: currentNode.inode }),
} satisfies NodeNotFoundError;
}
currentNode = state.filesystem.getNode(inode) as DirectoryNode;
let childPtr = toPtr({ device_id: currentDeviceId, inode: childInode });
if (state.filesystem.isMountPoint(childPtr)) {
const mount = state.filesystem.getMountTarget(childPtr)!.split("-")[0] as NodePointer;
childPtr = mount;
currentDeviceId = fromPtr(mount).device_id;
}
let child = state.filesystem.getNode(childPtr);
if (!child) {
throw {
type: "NodeNotFound",
message: `Node under ${childPtr} not found (probable dangling orphan)`,
timestamp: Date.now(),
pointer: childPtr,
} satisfies NodeNotFoundError;
}
if (child.type === NodeType.Symlink) {
// Change to typed error later on
if (++linkDepth > maxDepth) throw new Error("Symlink max depth - aborting!");
const target = child.target;
if (target.startsWith("/")) {
currentDeviceId = 0;
currentNode = state.filesystem.getRoot();
} else {
const entries = state.filesystem.getNodeEntries(childPtr);
if (!entries?.[0]) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
currentDeviceId = entries[0].pdevice_id;
currentNode = state.filesystem.getNode(
toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode }),
)!;
}
pathComponents.unshift(...target.split("/").filter(c => c.length > 0));
continue;
}
currentNode = child;
}
return [currentNode.inode, currentVD];
return { device_id: currentDeviceId, inode: currentNode.inode };
}
export function resolveInode(state: ShellState, inode: number): string {