resolvePath function implemented and tested, all green.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import type { Result, ShellError } from "./result";
|
||||
import type { ShellState } from "./shell_state";
|
||||
|
||||
type CommandFn = (state: ShellState, flags: string[], args: string[]) => CommandOutput<ShellError>;
|
||||
|
||||
export interface CommandDescriptor {
|
||||
fn: CommandFn;
|
||||
name: string;
|
||||
flags?: readonly FlagDescriptor[];
|
||||
requresRoot?: boolean;
|
||||
description?: string;
|
||||
keybinds?: readonly string[];
|
||||
}
|
||||
|
||||
export interface FlagDescriptor {
|
||||
name: string;
|
||||
takesValue?: boolean;
|
||||
}
|
||||
|
||||
export interface CommandOutput<E extends ShellError> {
|
||||
readonly state: ShellState;
|
||||
readonly result: Result<E>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { CommandDescriptor } from "./command";
|
||||
import { descriptor as echo } from "./commands/echo";
|
||||
|
||||
const reg = new Map<string, CommandDescriptor>();
|
||||
//reg.set(descriptor.name, descriptor <- imported const from command implementation file);
|
||||
reg.set(echo.name, echo);
|
||||
Object.freeze(reg);
|
||||
|
||||
export const COMMAND_REGISTRY: ReadonlyMap<string, CommandDescriptor> = reg;
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||
import type { ResultSuccess, ShellError } from "@core/result";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
|
||||
export const COMMAND_NAME = "echo";
|
||||
|
||||
function main(state: ShellState, flags: string[], args: string[]): CommandOutput<ShellError> {
|
||||
const joined = args.join(" ");
|
||||
const encoded = new TextEncoder().encode(joined);
|
||||
|
||||
const stdout = createImmutableBuffer(encoded);
|
||||
|
||||
const result: ResultSuccess = {
|
||||
type: "success",
|
||||
exitCode: 0,
|
||||
stdout,
|
||||
stderr: createImmutableBuffer(new Uint8Array()),
|
||||
commandName: COMMAND_NAME,
|
||||
};
|
||||
|
||||
return {
|
||||
state: state,
|
||||
result,
|
||||
};
|
||||
}
|
||||
|
||||
export const descriptor: CommandDescriptor = {
|
||||
fn: main,
|
||||
name: COMMAND_NAME,
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { UserInfo } from "@core/user_info";
|
||||
import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal";
|
||||
import type {
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import type { NodeNotFoundError, NotADirectoryError } from "./filesystem/errors";
|
||||
import type { FileSystem } from "./filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode } from "./filesystem/treenode";
|
||||
|
||||
export function resolvePath(fs: FileSystem, cwd: number, path: string): number {
|
||||
const isAbsolute = path.startsWith("/");
|
||||
|
||||
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
||||
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
||||
|
||||
if (pathComponents.length === 0 && isAbsolute) return 0;
|
||||
if (pathComponents.length === 0) return cwd;
|
||||
|
||||
let currentNode = isAbsolute ? fs.getRoot() : (fs.getNode(cwd) as DirectoryNode);
|
||||
if (!currentNode) {
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `CWD node under inode ${cwd} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: cwd,
|
||||
} satisfies NodeNotFoundError;
|
||||
}
|
||||
|
||||
for (const name of pathComponents) {
|
||||
if (name === ".") continue;
|
||||
|
||||
if (name === "..") {
|
||||
currentNode = fs.getNode(currentNode.parent) as DirectoryNode;
|
||||
continue;
|
||||
}
|
||||
|
||||
const inode = currentNode.children[name];
|
||||
|
||||
if (!inode)
|
||||
throw {
|
||||
type: "NodeNotFound",
|
||||
message: `Child node in ${currentNode.name} under name ${name} not found`,
|
||||
timestamp: Date.now(),
|
||||
inode: currentNode.inode,
|
||||
} satisfies NodeNotFoundError;
|
||||
if (fs.isFile(inode))
|
||||
throw {
|
||||
type: "NotADirectory",
|
||||
message: `Child node ${name} is not a directory`,
|
||||
timestamp: Date.now(),
|
||||
inode: inode,
|
||||
nodeType: TreeNodeType.File,
|
||||
} satisfies NotADirectoryError;
|
||||
|
||||
currentNode = fs.getNode(inode) as DirectoryNode;
|
||||
}
|
||||
|
||||
return currentNode.inode;
|
||||
}
|
||||
Reference in New Issue
Block a user