Files
termos/src/core/util/path_resolver.ts
T
ChaosMaker 00200b4c75 - Fixed all of path_resolver.ts functions
- `resolveInode` functions now `resolvePointer`
- Fixed all resolve pointer -> path functions
2026-11-10 12:02:21 +01:00

283 lines
10 KiB
TypeScript

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";
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("/");
const pathComponents: string[] = path.split("/").filter(c => c.length > 0);
let currentDeviceId: number;
let currentNode: Node;
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.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;
}
}
while (pathComponents.length > 0) {
const name = pathComponents.shift()!;
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: "Not a directory",
timestamp: Date.now(),
inode: currentNode.inode,
nodeType: currentNode.type,
} satisfies NotADirectoryError;
}
const childInode = currentNode.children.get(name);
if (childInode === undefined) {
throw {
type: "NodeNotFound",
message: `${name} not found`,
timestamp: Date.now(),
pointer: toPtr({ device_id: currentDeviceId, inode: currentNode.inode }),
} satisfies NodeNotFoundError;
}
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 { device_id: currentDeviceId, inode: currentNode.inode };
}
export function resolvePointer(state: ShellState, pointer: NodePointer): string {
const node = state.filesystem.getNode(pointer);
const components: string[] = [];
if (node === undefined) return "";
let ptr = pointer;
// TODO: Change while statements condition to not be hardcoded
while (ptr !== "0:0") {
if (state.filesystem.isMountTarget(ptr)) {
const mount = state.filesystem.getMountPoint(ptr);
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;
}
components.push(entries[0].name);
ptr = toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode });
continue;
}
const entries = state.filesystem.getNodeEntries(ptr);
if (!entries?.[0]) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing, FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
components.push(entries[0].name);
ptr = toPtr({ device_id: entries[0].pdevice_id, inode: entries[0].pinode });
}
if (components.length === 0) {
components.push("/");
return components.join("");
}
components.reverse().unshift("");
let result = components.join("/");
return result;
}
export function resolvePointerToHome(state: ShellState, ptr: NodePointer): string {
const home = resolvePointer(state, getCurrentSession(state).user.home);
let result = resolvePointer(state, ptr);
if (result.startsWith(home)) {
result = `~${result.slice(home.length)}`;
}
return result;
}
export function resolvePointerToCwd(state: ShellState, ptr: NodePointer): string {
const cwd = resolvePointer(state, getCurrentSession(state).cwd);
let result = resolvePointer(state, ptr);
if (result.startsWith(cwd)) {
result = `.${result.slice(cwd.length)}`;
}
return result;
}
export function resolvePointerRelative(state: ShellState, ptr: NodePointer, relative: NodePointer): string {
const rel = resolvePointer(state, relative);
let result = resolvePointer(state, ptr);
if (result.startsWith(rel)) {
result = `.${result.slice(rel.length)}`;
}
return result;
}