- Fixed `NodePointer` class to work with by reference comparison (maps use that) so the reverse index can look up entries properly`. `NodePointer` now caches every pointer created so logic can use reference comparison
270 lines
10 KiB
TypeScript
270 lines
10 KiB
TypeScript
import type { FileSystemCorruptionError, NodeNotFoundError, NotADirectoryError } from "@core/filesystem/errors";
|
|
import { NodePointer, NodeType, type Node } from "@core/filesystem/node";
|
|
import { getCurrentSession } from "@core/session_manager";
|
|
import type { ShellState } from "@core/shell_state";
|
|
|
|
export function resolvePath(state: ShellState, path: string): NodePointer {
|
|
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 = session.user.home;
|
|
currentDeviceId = home.device;
|
|
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(NodePointer.of(entries[0].pdevice_id, entries[0].pinode))!;
|
|
}
|
|
|
|
pathComponents.unshift(...target.split("/").filter(c => c.length > 0));
|
|
}
|
|
} else {
|
|
currentDeviceId = session.cwd.device;
|
|
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(NodePointer.of(currentDeviceId, currentNode.inode))) {
|
|
const mount = state.filesystem.getMountPoint(NodePointer.of(currentDeviceId, 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(NodePointer.of(entries[0].pdevice_id, entries[0].pinode))!;
|
|
continue;
|
|
}
|
|
if (currentNode.inode === 0) continue;
|
|
|
|
const entries = state.filesystem.getNodeEntries(NodePointer.of(currentDeviceId, 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(NodePointer.of(entries[0].pdevice_id, 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: NodePointer.of(currentDeviceId, currentNode.inode),
|
|
} satisfies NodeNotFoundError;
|
|
}
|
|
|
|
let childPtr = NodePointer.of(currentDeviceId, currentNode.inode);
|
|
|
|
if (state.filesystem.isMountPoint(childPtr)) {
|
|
const mount = state.filesystem.getMountTarget(childPtr)!;
|
|
childPtr = mount;
|
|
currentDeviceId = mount.device;
|
|
}
|
|
|
|
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(NodePointer.of(entries[0].pdevice_id, entries[0].pinode))!;
|
|
}
|
|
|
|
pathComponents.unshift(...target.split("/").filter(c => c.length > 0));
|
|
continue;
|
|
}
|
|
|
|
currentNode = child;
|
|
}
|
|
|
|
return NodePointer.of(currentDeviceId, 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.equals(NodePointer.of(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 = NodePointer.of(entries[0].pdevice_id, 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 = NodePointer.of(entries[0].pdevice_id, 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;
|
|
}
|