- 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:
@@ -47,6 +47,13 @@ export interface DeviceNotFoundError {
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface MountPointNotFoundError {
|
||||
type: "MountPointNotFound";
|
||||
message: string;
|
||||
pointer: NodePointer;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface IllegalMutationError {
|
||||
type: "IllegalMutation";
|
||||
message: string;
|
||||
@@ -68,3 +75,10 @@ export interface InvalidNodeNameError {
|
||||
pointer: NodePointer;
|
||||
attemptedName: string;
|
||||
}
|
||||
|
||||
export interface FileSystemCorruptionError {
|
||||
type: "FileSystemCorruption";
|
||||
message: string;
|
||||
timestamp: number;
|
||||
override?: {};
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
AlreadyExistsError,
|
||||
DeviceNotFoundError,
|
||||
DirectoryLoopError,
|
||||
FileSystemCorruptionError,
|
||||
FileSystemInitializationError,
|
||||
IllegalMutationError,
|
||||
InvalidNodeNameError,
|
||||
@@ -32,25 +33,20 @@ export interface FileSystemConfig {
|
||||
}
|
||||
|
||||
export class FileSystem {
|
||||
private readonly _maxNameLength: number;
|
||||
private readonly _maxPathLength: number;
|
||||
private readonly _maxSymlinkDepth: number;
|
||||
|
||||
private _Mounts: Map<NodePointer, { tinode: number; tdevice_id: number }> = new Map();
|
||||
private _Mounts: Map<NodePointer, NodePointer> = new Map();
|
||||
private _RMounts: Map<NodePointer, NodePointer> = new Map();
|
||||
private _VDiskManager: VDiskManager;
|
||||
|
||||
private constructor(
|
||||
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
|
||||
config: FileSystemConfig,
|
||||
) {
|
||||
this._maxNameLength = config.maxNameLength;
|
||||
this._maxPathLength = config.maxPathLength;
|
||||
this._maxSymlinkDepth = config.maxSymlinkDepth;
|
||||
readonly maxNameLength: number;
|
||||
readonly maxPathLength: number;
|
||||
readonly maxSymlinkDepth: number;
|
||||
|
||||
this._VDiskManager = new VDiskManager();
|
||||
for (const dev of devices) {
|
||||
this._VDiskManager.registerDisk(dev.config, dev.dstore, dev.mstore);
|
||||
}
|
||||
private constructor(manager: VDiskManager, config: FileSystemConfig) {
|
||||
this.maxNameLength = config.maxNameLength;
|
||||
this.maxPathLength = config.maxPathLength;
|
||||
this.maxSymlinkDepth = config.maxSymlinkDepth;
|
||||
|
||||
this._VDiskManager = manager;
|
||||
}
|
||||
|
||||
private resolveNode(ptr: NodePointer): Node | undefined {
|
||||
@@ -61,10 +57,11 @@ export class FileSystem {
|
||||
return dev.getNode(fptr.inode);
|
||||
}
|
||||
|
||||
public static init(
|
||||
// Make async
|
||||
public static async init(
|
||||
devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>,
|
||||
config: FileSystemConfig = { maxNameLength: 255, maxPathLength: Infinity, maxSymlinkDepth: 40 },
|
||||
): FileSystem {
|
||||
): Promise<FileSystem> {
|
||||
if (devices.length === 0) {
|
||||
throw {
|
||||
type: "FileSystemInitializationError",
|
||||
@@ -89,19 +86,39 @@ export class FileSystem {
|
||||
} satisfies FileSystemInitializationError;
|
||||
}
|
||||
|
||||
return new FileSystem(devices, config);
|
||||
const manager = new VDiskManager();
|
||||
for (const dev of devices) {
|
||||
await manager.registerDisk(dev.config, dev.dstore, dev.mstore);
|
||||
}
|
||||
|
||||
return new FileSystem(manager, config);
|
||||
}
|
||||
|
||||
public getRoot(): DirectoryNode {
|
||||
const dev = this._VDiskManager.getDisk(0);
|
||||
if (!dev) throw new Error("Filesystem corrupted, system device not found!");
|
||||
if (!dev) {
|
||||
throw {
|
||||
type: "FileSystemCorruption",
|
||||
message: "System device not registered. File System corrupted!",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemCorruptionError;
|
||||
}
|
||||
|
||||
return dev.getNode(0) as DirectoryNode;
|
||||
}
|
||||
|
||||
public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined {
|
||||
const fptr = fromPtr(ptr);
|
||||
const dev = this._VDiskManager.getDisk(fptr.device_id)!;
|
||||
const dev = this._VDiskManager.getDisk(fptr.device_id);
|
||||
|
||||
if (!dev) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
return dev.getIndex().get(ptr);
|
||||
}
|
||||
@@ -114,10 +131,79 @@ export class FileSystem {
|
||||
return this.resolveNode(ptr) !== undefined;
|
||||
}
|
||||
|
||||
public mount(mount: NodePointer, target: NodePointer) {
|
||||
const mptr = fromPtr(mount);
|
||||
const tptr = fromPtr(target);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(mptr.device_id)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${mptr.device_id} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: mptr.device_id,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (!this._VDiskManager.hasDisk(tptr.device_id)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${tptr.device_id} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: tptr.device_id,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (!this.getNode(mount)) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${mptr.inode} not found under device ${mptr.device_id}`,
|
||||
timestamp: Date.now(),
|
||||
pointer: mount,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
if (!this.getNode(target)) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Node under inode ${tptr.inode} not found under device ${tptr.device_id}`,
|
||||
timestamp: Date.now(),
|
||||
pointer: target,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
this._Mounts.set(mount, target);
|
||||
this._RMounts.set(target, mount);
|
||||
}
|
||||
|
||||
public isMountPoint(ptr: NodePointer): boolean {
|
||||
return this._Mounts.has(ptr);
|
||||
}
|
||||
|
||||
public isMountTarget(target: NodePointer): boolean {
|
||||
return this._RMounts.has(target);
|
||||
}
|
||||
|
||||
public getMountTarget(ptr: NodePointer): NodePointer | undefined {
|
||||
return this._Mounts.get(ptr);
|
||||
}
|
||||
|
||||
public getMountPoint(target: NodePointer): NodePointer | undefined {
|
||||
return this._RMounts.get(target);
|
||||
}
|
||||
|
||||
public isFile(ptr: NodePointer): boolean {
|
||||
const fptr = fromPtr(ptr);
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
@@ -134,6 +220,15 @@ export class FileSystem {
|
||||
const fptr = fromPtr(ptr);
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
@@ -150,6 +245,15 @@ export class FileSystem {
|
||||
const fptr = fromPtr(ptr);
|
||||
const node = this.resolveNode(ptr);
|
||||
|
||||
if (!this._VDiskManager.hasDisk(fptr.device_id)) {
|
||||
throw {
|
||||
type: "DeviceNotFound",
|
||||
message: `Device under id ${fptr.device_id} not found`,
|
||||
timestamp: Date.now(),
|
||||
device_id: fptr.device_id,
|
||||
} satisfies DeviceNotFoundError;
|
||||
}
|
||||
|
||||
if (node === undefined) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
@@ -162,7 +266,7 @@ export class FileSystem {
|
||||
return node.type === NodeType.Symlink ? true : false;
|
||||
}
|
||||
|
||||
public moveNode(source: NodePointer, destination: NodePointer, name?: string) {
|
||||
public async moveNode(source: NodePointer, destination: NodePointer, name?: string) {
|
||||
const sptr = fromPtr(source);
|
||||
const dptr = fromPtr(destination);
|
||||
const sdev = this._VDiskManager.getDisk(sptr.device_id);
|
||||
@@ -226,7 +330,13 @@ export class FileSystem {
|
||||
|
||||
const sdevIndex = sdev.getIndex();
|
||||
const s_entry = sdevIndex.get(source);
|
||||
if (!s_entry || !s_entry[0]) throw new Error("File System corruption, reverse index is missing a field");
|
||||
if (!s_entry || !s_entry[0]) {
|
||||
throw {
|
||||
type: "FileSystemCorruption",
|
||||
message: "Reverse index entry missing, FileSystem corrupted",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemCorruptionError;
|
||||
}
|
||||
|
||||
const pinode = s_entry[0].pinode;
|
||||
|
||||
@@ -242,7 +352,7 @@ export class FileSystem {
|
||||
} satisfies AlreadyExistsError;
|
||||
}
|
||||
|
||||
if (nodeName.length > this._maxNameLength) {
|
||||
if (nodeName.length > this.maxNameLength) {
|
||||
throw {
|
||||
type: "InvalidNodeName",
|
||||
message: `Invalid name for the ${source} source node`,
|
||||
@@ -263,13 +373,13 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
if (sptr.device_id === dptr.device_id) {
|
||||
ddev.moveNodeEntry(pinode, dptr.inode, s_entry[0].name, name);
|
||||
await ddev.moveNodeEntry(pinode, dptr.inode, s_entry[0].name, name);
|
||||
} else {
|
||||
console.error("cross device node movement not yet implemented");
|
||||
}
|
||||
}
|
||||
|
||||
public createNode(
|
||||
public async createNode(
|
||||
parent: NodePointer,
|
||||
name: string,
|
||||
data:
|
||||
@@ -326,7 +436,7 @@ export class FileSystem {
|
||||
} satisfies AlreadyExistsError;
|
||||
}
|
||||
|
||||
if (name.length > this._maxNameLength) {
|
||||
if (name.length > this.maxNameLength) {
|
||||
throw {
|
||||
type: "InvalidNodeName",
|
||||
message: `Invalid name for the new node`,
|
||||
@@ -336,14 +446,14 @@ export class FileSystem {
|
||||
} satisfies InvalidNodeNameError;
|
||||
}
|
||||
|
||||
pdev.createNodeEntry(pptr.inode, name, data);
|
||||
await pdev.createNodeEntry(pptr.inode, name, data);
|
||||
}
|
||||
|
||||
public deleteNode(target: NodePointer) {
|
||||
public async deleteNode(target: NodePointer) {
|
||||
const tptr = fromPtr(target);
|
||||
const tdev = this._VDiskManager.getDisk(tptr.device_id);
|
||||
|
||||
if (tptr.device_id === 0 && tptr.inode === 0) {
|
||||
if (tptr.inode === 0) {
|
||||
throw {
|
||||
type: "IllegalMutation",
|
||||
message: `Attempt at illegal mutation of node under inode 0 (root)`,
|
||||
@@ -362,12 +472,18 @@ export class FileSystem {
|
||||
|
||||
const tdevIndex = tdev.getIndex();
|
||||
const t_entry = tdevIndex.get(target);
|
||||
if (!t_entry || !t_entry[0]) throw new Error("File System corruption, reverse index is missing a field");
|
||||
if (!t_entry || !t_entry[0]) {
|
||||
throw {
|
||||
type: "FileSystemCorruption",
|
||||
message: "Reverse index entry missing, FileSystem corrupted",
|
||||
timestamp: Date.now(),
|
||||
} satisfies FileSystemCorruptionError;
|
||||
}
|
||||
|
||||
tdev.unlinkNodeEntry(tptr.inode, t_entry[0].name);
|
||||
await tdev.unlinkNodeEntry(t_entry[0].pinode, t_entry[0].name);
|
||||
}
|
||||
|
||||
public writeContent(target: NodePointer, content: Blob) {
|
||||
public async writeContent(target: NodePointer, content: Blob) {
|
||||
const ptr = fromPtr(target);
|
||||
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
||||
const node = this.resolveNode(target);
|
||||
@@ -390,7 +506,7 @@ export class FileSystem {
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
if (node.type === NodeType.Directory) {
|
||||
if (node.type === NodeType.Directory || node.type === NodeType.Symlink) {
|
||||
throw {
|
||||
type: "NotAFile",
|
||||
message: `Node is not a file`,
|
||||
@@ -400,7 +516,7 @@ export class FileSystem {
|
||||
} satisfies NotAFileError;
|
||||
}
|
||||
|
||||
dev.writeContent((node as FileNode).content, content);
|
||||
await dev.writeContent((node as FileNode).content, content);
|
||||
}
|
||||
|
||||
public streamContent(target: NodePointer): Readable {
|
||||
@@ -439,7 +555,7 @@ export class FileSystem {
|
||||
return dev.streamContent((node as FileNode).content);
|
||||
}
|
||||
|
||||
public modifyNode(target: NodePointer, data: { mode?: number; owner?: number; group?: number }) {
|
||||
public async modifyNode(target: NodePointer, data: { mode?: number; owner?: number; group?: number }) {
|
||||
const ptr = fromPtr(target);
|
||||
const dev = this._VDiskManager.getDisk(ptr.device_id);
|
||||
const node = this.resolveNode(target);
|
||||
@@ -462,7 +578,7 @@ export class FileSystem {
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
if (node.type === NodeType.Directory) {
|
||||
if (node.type === NodeType.Directory || node.type === NodeType.Symlink) {
|
||||
throw {
|
||||
type: "NotAFile",
|
||||
message: `Node is not a file`,
|
||||
@@ -472,7 +588,7 @@ export class FileSystem {
|
||||
} satisfies NotAFileError;
|
||||
}
|
||||
|
||||
dev.writeNodeEntry(node.inode, data);
|
||||
await dev.writeNodeEntry(node.inode, data);
|
||||
}
|
||||
|
||||
public canRead(nodep: NodePointer, user: UserInfo): boolean {
|
||||
@@ -490,7 +606,7 @@ export class FileSystem {
|
||||
|
||||
if (user.uid === node.owner) {
|
||||
return (node.mode & 0o400) !== 0;
|
||||
} else if (user.gid === node.group) {
|
||||
} else if (user.gid === node.group || user.group.includes(node.group)) {
|
||||
return (node.mode & 0o040) !== 0;
|
||||
} else {
|
||||
return (node.mode & 0o004) !== 0;
|
||||
@@ -512,7 +628,7 @@ export class FileSystem {
|
||||
|
||||
if (user.uid === node.owner) {
|
||||
return (node.mode & 0o200) !== 0;
|
||||
} else if (user.gid === node.group) {
|
||||
} else if (user.gid === node.group || user.group.includes(node.group)) {
|
||||
return (node.mode & 0o020) !== 0;
|
||||
} else {
|
||||
return (node.mode & 0o002) !== 0;
|
||||
@@ -534,7 +650,7 @@ export class FileSystem {
|
||||
|
||||
if (user.uid === node.owner) {
|
||||
return (node.mode & 0o100) !== 0;
|
||||
} else if (user.gid === node.group) {
|
||||
} else if (user.gid === node.group || user.group.includes(node.group)) {
|
||||
return (node.mode & 0o010) !== 0;
|
||||
} else {
|
||||
return (node.mode & 0o001) !== 0;
|
||||
|
||||
@@ -3,7 +3,7 @@ export type Node = FileNode | DirectoryNode | SymlinkNode;
|
||||
export type NodeHash = ReadonlyMap<number, Node>;
|
||||
export type ShadowHash = ReadonlyMap<number, Node | null>;
|
||||
|
||||
export type NodePointer = string;
|
||||
export type NodePointer = `${string}:${string}`;
|
||||
|
||||
export interface NodeEntry {
|
||||
readonly pinode: number;
|
||||
@@ -62,7 +62,8 @@ export function toPtr({ device_id, inode }: { device_id: number; inode: number }
|
||||
}
|
||||
|
||||
export function fromPtr(ptr: NodePointer): { device_id: number; inode: number } {
|
||||
if (!ptr.match(/\d+[:]\d+$/g)) {
|
||||
if (!ptr.match(/^\d+[:]\d+$/)) {
|
||||
//TODO: Make typed
|
||||
throw new Error("Invalid node pointer format");
|
||||
}
|
||||
const arr = ptr.split(":");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { NodePointer } from "./filesystem/node";
|
||||
import { type HistoryState } from "./history_state";
|
||||
import { createShellState, type ShellState } from "./shell_state";
|
||||
import { createShellError, type LoginError, type UserNotFoundError } from "./util/errors";
|
||||
@@ -13,15 +14,15 @@ export interface UserInfo {
|
||||
readonly uid: number;
|
||||
readonly gid: number;
|
||||
readonly group: readonly number[];
|
||||
readonly home: number;
|
||||
readonly home: NodePointer;
|
||||
}
|
||||
|
||||
export interface SessionState {
|
||||
readonly user: UserInfo;
|
||||
readonly envVars: ReadonlyMap<string, string>;
|
||||
readonly history: HistoryState;
|
||||
readonly cwd: number;
|
||||
readonly pwd: number;
|
||||
readonly cwd: NodePointer;
|
||||
readonly pwd: NodePointer;
|
||||
readonly exitCode: number;
|
||||
}
|
||||
|
||||
|
||||
+166
-50
@@ -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 {
|
||||
|
||||
+32
-23
@@ -1,58 +1,67 @@
|
||||
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||
import { TreeNodeType } from "@core/filesystem/node";
|
||||
import { fromPtr, NodeType, toPtr, type NodePointer, type SymlinkNode } from "@core/filesystem/node";
|
||||
|
||||
export function printTree(fs: FileSystem, root: number): string {
|
||||
export function printTree(fs: FileSystem, root: NodePointer, rootLabel = "/"): string {
|
||||
const rootNode = fs.getNode(root);
|
||||
if (rootNode === undefined) return "";
|
||||
|
||||
const lines: string[] = [];
|
||||
|
||||
lines.push(rootNode.name + ` (${rootNode.inode})`);
|
||||
const rootFptr = fromPtr(root);
|
||||
lines.push(`${rootLabel} (${rootFptr.device_id}:${rootFptr.inode})`);
|
||||
|
||||
type Frame = {
|
||||
inode: number;
|
||||
ptr: NodePointer;
|
||||
name: string;
|
||||
prefix: string;
|
||||
isLast: boolean;
|
||||
};
|
||||
|
||||
const stack: Frame[] = [];
|
||||
|
||||
const getSorterChildren = (inode: number): number[] => {
|
||||
const node = fs.getNode(inode);
|
||||
if (!node || node.type !== TreeNodeType.Directory) return [];
|
||||
const getSortedChildren = (ptr: NodePointer): Array<{ name: string; ptr: NodePointer }> => {
|
||||
const node = fs.getNode(ptr);
|
||||
if (!node || node.type !== NodeType.Directory) return [];
|
||||
|
||||
const entries = Object.entries(node.children);
|
||||
const { device_id } = fromPtr(ptr);
|
||||
const entries = Array.from(node.children.entries());
|
||||
entries.sort(([a], [b]) => a.localeCompare(b));
|
||||
return entries.map(([, childInode]) => childInode) as number[];
|
||||
|
||||
return entries.map(([name, inode]) => ({
|
||||
name,
|
||||
ptr: toPtr({ device_id, inode }),
|
||||
}));
|
||||
};
|
||||
|
||||
const rootChildren = getSorterChildren(root);
|
||||
|
||||
const rootChildren = getSortedChildren(root);
|
||||
for (let i = rootChildren.length - 1; i >= 0; i--) {
|
||||
const childInode: number = rootChildren[i] as number;
|
||||
const child = rootChildren[i]!;
|
||||
const isLast = i === rootChildren.length - 1;
|
||||
|
||||
stack.push({ inode: childInode, prefix: "", isLast });
|
||||
stack.push({ ptr: child.ptr, name: child.name, prefix: "", isLast });
|
||||
}
|
||||
|
||||
while (stack.length > 0) {
|
||||
const frame = stack.pop()!;
|
||||
const node = fs.getNode(frame.inode);
|
||||
const node = fs.getNode(frame.ptr);
|
||||
if (!node) continue;
|
||||
|
||||
const name = node.name;
|
||||
const fptr = fromPtr(frame.ptr);
|
||||
const connector = frame.isLast ? "└── " : "├── ";
|
||||
lines.push(frame.prefix + connector + name + ` (${node.inode})`);
|
||||
const symlinkSuffix = node.type === NodeType.Symlink ? ` -> ${(node as SymlinkNode).target}` : "";
|
||||
lines.push(`${frame.prefix}${connector}${frame.name} (${fptr.device_id}:${fptr.inode})${symlinkSuffix}`);
|
||||
|
||||
if (node.type === TreeNodeType.Directory) {
|
||||
if (node.type === NodeType.Directory) {
|
||||
const childPrefix = frame.prefix + (frame.isLast ? " " : "│ ");
|
||||
const children = getSorterChildren(frame.inode);
|
||||
const children = getSortedChildren(frame.ptr);
|
||||
|
||||
for (let i = children.length - 1; i >= 0; i--) {
|
||||
const childInode = children[i] as number;
|
||||
const child = children[i]!;
|
||||
const childIsLast = i === children.length - 1;
|
||||
|
||||
stack.push({ inode: childInode, prefix: childPrefix, isLast: childIsLast });
|
||||
stack.push({
|
||||
ptr: child.ptr,
|
||||
name: child.name,
|
||||
prefix: childPrefix,
|
||||
isLast: childIsLast,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user