diff --git a/src/core/command_registry.ts b/src/core/command_registry.ts index 2756cac..68663d8 100644 --- a/src/core/command_registry.ts +++ b/src/core/command_registry.ts @@ -1,4 +1,5 @@ import type { CommandDescriptor } from "./command"; +import { descriptor as cat } from "./commands/cat"; import { descriptor as cd } from "./commands/cd"; import { descriptor as echo } from "./commands/echo"; import { descriptor as ls } from "./commands/ls"; @@ -10,6 +11,7 @@ reg.set(echo.name, echo); reg.set(cd.name, cd); reg.set(testasync.name, testasync); reg.set(ls.name, ls); +reg.set(cat.name, cat); Object.freeze(reg); export const COMMAND_REGISTRY: ReadonlyMap = reg; diff --git a/src/core/commands/__template__.ts b/src/core/commands/__template__.ts new file mode 100644 index 0000000..a64086e --- /dev/null +++ b/src/core/commands/__template__.ts @@ -0,0 +1,27 @@ +import type { CommandDescriptor, CommandOutput } from "@core/command"; +import type { CommandIO } from "@core/command_io"; +import { createSuccessResult, type ResultSuccess } from "@core/result"; +import type { ShellState } from "@core/shell_state"; +import type { ShellError } from "@core/util/errors"; + +export const COMMAND_NAME = ""; + +//Need to implement escape sequences +async function main( + io: CommandIO, + state: ShellState, + flags: string[], + args: string[], +): Promise> { + const result: ResultSuccess = createSuccessResult(COMMAND_NAME); + + return { + state: state, + result, + }; +} + +export const descriptor: CommandDescriptor = { + fn: main, + name: COMMAND_NAME, +}; diff --git a/src/core/commands/cat.ts b/src/core/commands/cat.ts new file mode 100644 index 0000000..dbae363 --- /dev/null +++ b/src/core/commands/cat.ts @@ -0,0 +1,72 @@ +import type { CommandDescriptor, CommandOutput } from "@core/command"; +import type { CommandIO } from "@core/command_io"; +import { TreeNodeType, type FileNode, type TreeNode } from "@core/filesystem/treenode"; +import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result"; +import { getCurrentSession } from "@core/session_manager"; +import type { ShellState } from "@core/shell_state"; +import type { PathResolutionError, PermissionDeniedError, ShellError } from "@core/util/errors"; +import { resolvePath } from "@core/util/path_resolver"; + +export const COMMAND_NAME = "cat"; + +//Need to implement escape sequences +async function main( + io: CommandIO, + state: ShellState, + flags: string[], + args: string[], +): Promise> { + for (const arg of args) { + const inode = resolvePath(state, arg); + if (inode === -1) { + return { + state, + result: createErrorResult( + "PathResolutionError", + "No such file or directory", + { path: arg }, + COMMAND_NAME, + ), + } satisfies CommandOutput; + } + + // Casting to TreeNode because the resolvePath already ensures the inode is correct; + const node = state.filesystem.getNode(inode) as TreeNode; + + if (node.type != TreeNodeType.File) { + return { + state, + result: createErrorResult( + "PathResolutionError", + `${node.name} is a directory`, + { path: arg }, + COMMAND_NAME, + ), + } satisfies CommandOutput; + } + + const session = getCurrentSession(state); + + if (!state.filesystem.canRead(inode, session.user)) { + return { + state, + result: createErrorResult("PermissionDenied", "Permission denied", { resource: arg }, COMMAND_NAME), + } satisfies CommandOutput; + } + + node as FileNode; + io.writeStdout(node.content.toArray()); + } + + const result: ResultSuccess = createSuccessResult(COMMAND_NAME); + + return { + state: state, + result, + }; +} + +export const descriptor: CommandDescriptor = { + fn: main, + name: COMMAND_NAME, +}; diff --git a/src/core/commands/cd.ts b/src/core/commands/cd.ts index ee976d7..a65fec1 100644 --- a/src/core/commands/cd.ts +++ b/src/core/commands/cd.ts @@ -39,8 +39,8 @@ async function main( } else if (args[0] === "-") { inode = session.pwd; } else { - const result = safeResolvePath(state, args[0]); - if (result.type === "error") { + inode = resolvePath(state, args[0]); + if (inode === -1) { return { state, result: createErrorResult( @@ -51,7 +51,6 @@ async function main( ), } satisfies CommandOutput; } - inode = result.inode; } if (state.filesystem.isFile(inode)) { @@ -74,14 +73,6 @@ async function main( state: returnState, result: createSuccessResult(COMMAND_NAME), }; - - function safeResolvePath(state: ShellState, path: string): { type: "success"; inode: number } | { type: "error" } { - try { - return { type: "success", inode: resolvePath(state, path) }; - } catch { - return { type: "error" }; - } - } } export const descriptor: CommandDescriptor = { diff --git a/src/core/commands/ls.ts b/src/core/commands/ls.ts index 3e42aa3..47b376e 100644 --- a/src/core/commands/ls.ts +++ b/src/core/commands/ls.ts @@ -48,10 +48,8 @@ async function main( entries.push({ inode: session.cwd, path: "." }); } else { for (const arg of args) { - let inode; - try { - inode = resolvePath(state, arg); - } catch { + const inode = resolvePath(state, arg); + if (inode === -1) { return { state, result: createErrorResult( diff --git a/src/core/commands/touch.ts b/src/core/commands/touch.ts new file mode 100644 index 0000000..600440a --- /dev/null +++ b/src/core/commands/touch.ts @@ -0,0 +1,59 @@ +import type { CommandDescriptor, CommandOutput } from "@core/command"; +import type { CommandIO } from "@core/command_io"; +import { TreeNodeType } from "@core/filesystem/treenode"; +import { createSuccessResult, type ResultSuccess } from "@core/result"; +import { getCurrentSession } from "@core/session_manager"; +import type { ShellState } from "@core/shell_state"; +import type { ShellError } from "@core/util/errors"; +import { resolvePath } from "@core/util/path_resolver"; + +export const COMMAND_NAME = "touch"; + +//Need to implement escape sequences +async function main( + io: CommandIO, + state: ShellState, + flags: string[], + args: string[], +): Promise> { + for (const arg of args) { + const inode = resolvePath(state, arg); + const session = getCurrentSession(state); + + let newState; + + if (inode === -1) { + const index = arg.lastIndexOf("/"); + let parentPath; + if (index === -1) { + parentPath = "./"; + } else { + parentPath = arg.slice(0, index); + } + const fileName = arg.slice(index + 1); + const parent = resolvePath(state, parentPath); + + newState = state.filesystem.createNode( + parent, + fileName, + TreeNodeType.File, + 0o644, + session.user.uid, + session.user.gid, + ); + } else { + } + } + + const result: ResultSuccess = createSuccessResult(COMMAND_NAME); + + return { + state: state, + result, + }; +} + +export const descriptor: CommandDescriptor = { + fn: main, + name: COMMAND_NAME, +}; diff --git a/src/core/util/path_resolver.ts b/src/core/util/path_resolver.ts index 2f875c6..d18e6c0 100644 --- a/src/core/util/path_resolver.ts +++ b/src/core/util/path_resolver.ts @@ -16,16 +16,11 @@ export function resolvePath(state: ShellState, path: string): number { let currentNode = isAbsolute ? state.filesystem.getRoot() : (state.filesystem.getNode(session.cwd) as DirectoryNode); - if (!currentNode) { - throw { - type: "NodeNotFound", - message: `CWD node under inode ${session.cwd} not found`, - timestamp: Date.now(), - inode: session.cwd, - } satisfies NodeNotFoundError; - } - for (const name of pathComponents) { + if (!currentNode) return -1; + + for (let i = 0; i < pathComponents.length; i++) { + const name = pathComponents[i]!; if (name === ".") continue; if (name === "..") { @@ -42,7 +37,7 @@ export function resolvePath(state: ShellState, path: string): number { timestamp: Date.now(), inode: currentNode.inode, } satisfies NodeNotFoundError; - if (state.filesystem.isFile(inode)) + if (state.filesystem.isFile(inode) && !(i === pathComponents.length - 1)) throw { type: "NotADirectory", message: `Child node ${name} is not a directory`,