- Fixed cat command

- Moved device config initialization to `serializer.ts`
- Minor bugfixes
- Added `read()` to `filesystem.ts`
This commit is contained in:
2026-11-13 04:15:28 +01:00
parent d5566aa494
commit de1ce64226
5 changed files with 61 additions and 43 deletions
+20 -11
View File
@@ -1,6 +1,7 @@
import type { CommandDescriptor, CommandOutput } from "@core/command";
import type { CommandIO } from "@core/command_io";
import { type FileNode } from "@core/filesystem/node";
import type { FileSystemCorruptionError } from "@core/filesystem/errors";
import { type NodePointer } from "@core/filesystem/node";
import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result";
import { getCurrentSession } from "@core/session_manager";
import type { ShellState } from "@core/shell_state";
@@ -12,8 +13,10 @@ export const COMMAND_NAME = "cat";
//Need to implement escape sequences
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
for (const arg of args) {
const inode = resolvePath(state, arg);
if (inode === -1) {
let ptr: NodePointer;
try {
ptr = resolvePath(state, arg);
} catch (e) {
return {
state,
result: createErrorResult(
@@ -25,15 +28,20 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
} 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) {
if (!state.filesystem.isFile(ptr)) {
const entries = state.filesystem.getNodeEntries(ptr);
if (!entries) {
throw {
type: "FileSystemCorruption",
message: "Reverse index entry missing. FileSystem corrupted",
timestamp: Date.now(),
} satisfies FileSystemCorruptionError;
}
return {
state,
result: createErrorResult(
"PathResolutionError",
`${node.name} is a directory`,
`${entries[0]!.name} is a directory`,
{ path: arg },
COMMAND_NAME,
),
@@ -42,15 +50,16 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
const session = getCurrentSession(state);
if (!state.filesystem.canRead(inode, session.user)) {
if (!state.filesystem.canRead(ptr, 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 content = await state.filesystem.read(ptr);
io.writeStdout(content);
}
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);