Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e9227f6d8 |
@@ -1,4 +1,5 @@
|
|||||||
import type { CommandDescriptor } from "./command";
|
import type { CommandDescriptor } from "./command";
|
||||||
|
import { descriptor as cat } from "./commands/cat";
|
||||||
import { descriptor as cd } from "./commands/cd";
|
import { descriptor as cd } from "./commands/cd";
|
||||||
import { descriptor as echo } from "./commands/echo";
|
import { descriptor as echo } from "./commands/echo";
|
||||||
import { descriptor as ls } from "./commands/ls";
|
import { descriptor as ls } from "./commands/ls";
|
||||||
@@ -10,6 +11,7 @@ reg.set(echo.name, echo);
|
|||||||
reg.set(cd.name, cd);
|
reg.set(cd.name, cd);
|
||||||
reg.set(testasync.name, testasync);
|
reg.set(testasync.name, testasync);
|
||||||
reg.set(ls.name, ls);
|
reg.set(ls.name, ls);
|
||||||
|
reg.set(cat.name, cat);
|
||||||
Object.freeze(reg);
|
Object.freeze(reg);
|
||||||
|
|
||||||
export const COMMAND_REGISTRY: ReadonlyMap<string, CommandDescriptor> = reg;
|
export const COMMAND_REGISTRY: ReadonlyMap<string, CommandDescriptor> = reg;
|
||||||
|
|||||||
@@ -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<CommandOutput<ShellError>> {
|
||||||
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -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<CommandOutput<ShellError>> {
|
||||||
|
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<PathResolutionError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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<PathResolutionError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
if (!state.filesystem.canRead(inode, session.user)) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult("PermissionDenied", "Permission denied", { resource: arg }, COMMAND_NAME),
|
||||||
|
} satisfies CommandOutput<PermissionDeniedError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
+2
-11
@@ -39,8 +39,8 @@ async function main(
|
|||||||
} else if (args[0] === "-") {
|
} else if (args[0] === "-") {
|
||||||
inode = session.pwd;
|
inode = session.pwd;
|
||||||
} else {
|
} else {
|
||||||
const result = safeResolvePath(state, args[0]);
|
inode = resolvePath(state, args[0]);
|
||||||
if (result.type === "error") {
|
if (inode === -1) {
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
result: createErrorResult(
|
result: createErrorResult(
|
||||||
@@ -51,7 +51,6 @@ async function main(
|
|||||||
),
|
),
|
||||||
} satisfies CommandOutput<PathResolutionError>;
|
} satisfies CommandOutput<PathResolutionError>;
|
||||||
}
|
}
|
||||||
inode = result.inode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.filesystem.isFile(inode)) {
|
if (state.filesystem.isFile(inode)) {
|
||||||
@@ -74,14 +73,6 @@ async function main(
|
|||||||
state: returnState,
|
state: returnState,
|
||||||
result: createSuccessResult(COMMAND_NAME),
|
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 = {
|
export const descriptor: CommandDescriptor = {
|
||||||
|
|||||||
@@ -48,10 +48,8 @@ async function main(
|
|||||||
entries.push({ inode: session.cwd, path: "." });
|
entries.push({ inode: session.cwd, path: "." });
|
||||||
} else {
|
} else {
|
||||||
for (const arg of args) {
|
for (const arg of args) {
|
||||||
let inode;
|
const inode = resolvePath(state, arg);
|
||||||
try {
|
if (inode === -1) {
|
||||||
inode = resolvePath(state, arg);
|
|
||||||
} catch {
|
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
result: createErrorResult(
|
result: createErrorResult(
|
||||||
|
|||||||
@@ -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<CommandOutput<ShellError>> {
|
||||||
|
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,
|
||||||
|
};
|
||||||
@@ -16,16 +16,11 @@ export function resolvePath(state: ShellState, path: string): number {
|
|||||||
let currentNode = isAbsolute
|
let currentNode = isAbsolute
|
||||||
? state.filesystem.getRoot()
|
? state.filesystem.getRoot()
|
||||||
: (state.filesystem.getNode(session.cwd) as DirectoryNode);
|
: (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 === ".") continue;
|
||||||
|
|
||||||
if (name === "..") {
|
if (name === "..") {
|
||||||
@@ -42,7 +37,7 @@ export function resolvePath(state: ShellState, path: string): number {
|
|||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
inode: currentNode.inode,
|
inode: currentNode.inode,
|
||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
if (state.filesystem.isFile(inode))
|
if (state.filesystem.isFile(inode) && !(i === pathComponents.length - 1))
|
||||||
throw {
|
throw {
|
||||||
type: "NotADirectory",
|
type: "NotADirectory",
|
||||||
message: `Child node ${name} is not a directory`,
|
message: `Child node ${name} is not a directory`,
|
||||||
|
|||||||
Reference in New Issue
Block a user