- 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);
+9
View File
@@ -121,6 +121,15 @@ export class FileSystem {
return this.resolveNode(ptr) !== undefined;
}
public async read(ptr: NodePointer): Promise<Uint8Array> {
const dev = this._VDiskManager.getDisk(ptr.device);
if (!dev) return new Uint8Array();
const node = dev.getNode(ptr.inode);
if (!node) return new Uint8Array();
return dev.readContent((node as FileNode).content);
}
public mount(mount: NodePointer, target: NodePointer) {
if (!this._VDiskManager.hasDisk(mount.device)) {
throw {
+1 -1
View File
@@ -131,7 +131,7 @@ export function resolvePath(state: ShellState, path: string): NodePointer {
} satisfies NodeNotFoundError;
}
let childPtr = NodePointer.of(currentDeviceId, currentNode.inode);
let childPtr = NodePointer.of(currentDeviceId, childInode);
if (state.filesystem.isMountPoint(childPtr)) {
const mount = state.filesystem.getMountTarget(childPtr)!;
+2 -24
View File
File diff suppressed because one or more lines are too long
+29 -7
View File
@@ -1,14 +1,18 @@
import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk";
import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores";
import { Blob } from "node:buffer";
import { randomUUID } from "node:crypto";
import { NodeType, type DirectoryNode, type FileNode, type Node, type SymlinkNode } from "../core/filesystem/node";
/* export function serializeFileSystem(map: ReadonlyMap<number, TreeNode | null>): string {
} */
export function deserializeFileSystem(json: string): { id: number; map: Map<number, Node> }[] {
export function deserializeFileSystem(json: string): { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] {
const raw = JSON.parse(json);
// prettier-ignore
function parseNode(tree: any, parent: number, map: Map<number, Node>): void { // eslint-disable-line @typescript-eslint/no-explicit-any
function parseNode(tree: any, parent: number, store: MemDataStore, map: Map<number, Node>): void { // eslint-disable-line @typescript-eslint/no-explicit-any
if(tree === null || tree.type === undefined) {
const time = Date.now();
const defaultNode: DirectoryNode = {
@@ -32,7 +36,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
const children: Map<string, number> = new Map();
for (const child of tree.children) {
parseNode(child, tree.inode, map);
parseNode(child, tree.inode, store, map);
children.set(child.name, child.inode);
}
@@ -54,6 +58,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
break;
}
case "regular": {
const uuid = randomUUID();
const fileNode: FileNode = {
type: NodeType.File,
inode: tree.inode,
@@ -64,9 +69,11 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
mtime: tree.mtime,
ctime: tree.ctime,
atime: tree.atime,
content: tree.content,
content: uuid,
size: tree.size,
};
const buff: Uint8Array<ArrayBuffer> = Uint8Array.from(tree.content);
store.put(uuid, new Blob([buff]));
map.set(tree.inode, Object.freeze(fileNode));
break;
}
@@ -91,12 +98,27 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
}
}
const result: { id: number; map: Map<number, Node> }[] = [];
const result: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = [];
for (const dev of raw.dev) {
const map = new Map();
parseNode(dev.tree, dev.tree.inode, map);
const data = new MemDataStore();
const meta = new MemMetaStore();
parseNode(dev.tree, dev.tree.inode, data, map);
result.push({ id: dev.id, map });
const config: VDiskConfig = {
id: dev.id,
label: `disk-${dev.id}`,
index: 0,
readonly: false,
size: 1024 * 1024 * 1024 * 8, //8 GiB
base: map,
dataC: {
chunkSize: 1024 * 8, //8 KiB
cacheSize: 1024 * 1024 * 64, //64 MiB
cacheTTL: 1000 * 60, // 1 min
},
};
result.push({ dstore: data, mstore: meta, config });
}
return result;