- Fixed cat command
- Moved device config initialization to `serializer.ts` - Minor bugfixes - Added `read()` to `filesystem.ts`
This commit is contained in:
+20
-11
@@ -1,6 +1,7 @@
|
|||||||
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
import type { CommandIO } from "@core/command_io";
|
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 { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result";
|
||||||
import { getCurrentSession } from "@core/session_manager";
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
import type { ShellState } from "@core/shell_state";
|
import type { ShellState } from "@core/shell_state";
|
||||||
@@ -12,8 +13,10 @@ export const COMMAND_NAME = "cat";
|
|||||||
//Need to implement escape sequences
|
//Need to implement escape sequences
|
||||||
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
for (const arg of args) {
|
for (const arg of args) {
|
||||||
const inode = resolvePath(state, arg);
|
let ptr: NodePointer;
|
||||||
if (inode === -1) {
|
try {
|
||||||
|
ptr = resolvePath(state, arg);
|
||||||
|
} catch (e) {
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
result: createErrorResult(
|
result: createErrorResult(
|
||||||
@@ -25,15 +28,20 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
} satisfies CommandOutput<PathResolutionError>;
|
} satisfies CommandOutput<PathResolutionError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Casting to TreeNode because the resolvePath already ensures the inode is correct;
|
if (!state.filesystem.isFile(ptr)) {
|
||||||
const node = state.filesystem.getNode(inode) as TreeNode;
|
const entries = state.filesystem.getNodeEntries(ptr);
|
||||||
|
if (!entries) {
|
||||||
if (node.type != TreeNodeType.File) {
|
throw {
|
||||||
|
type: "FileSystemCorruption",
|
||||||
|
message: "Reverse index entry missing. FileSystem corrupted",
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} satisfies FileSystemCorruptionError;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
result: createErrorResult(
|
result: createErrorResult(
|
||||||
"PathResolutionError",
|
"PathResolutionError",
|
||||||
`${node.name} is a directory`,
|
`${entries[0]!.name} is a directory`,
|
||||||
{ path: arg },
|
{ path: arg },
|
||||||
COMMAND_NAME,
|
COMMAND_NAME,
|
||||||
),
|
),
|
||||||
@@ -42,15 +50,16 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
|
|||||||
|
|
||||||
const session = getCurrentSession(state);
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
if (!state.filesystem.canRead(inode, session.user)) {
|
if (!state.filesystem.canRead(ptr, session.user)) {
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
result: createErrorResult("PermissionDenied", "Permission denied", { resource: arg }, COMMAND_NAME),
|
result: createErrorResult("PermissionDenied", "Permission denied", { resource: arg }, COMMAND_NAME),
|
||||||
} satisfies CommandOutput<PermissionDeniedError>;
|
} satisfies CommandOutput<PermissionDeniedError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
node as FileNode;
|
const content = await state.filesystem.read(ptr);
|
||||||
io.writeStdout(node.content.toArray());
|
|
||||||
|
io.writeStdout(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|||||||
@@ -121,6 +121,15 @@ export class FileSystem {
|
|||||||
return this.resolveNode(ptr) !== undefined;
|
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) {
|
public mount(mount: NodePointer, target: NodePointer) {
|
||||||
if (!this._VDiskManager.hasDisk(mount.device)) {
|
if (!this._VDiskManager.hasDisk(mount.device)) {
|
||||||
throw {
|
throw {
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ export function resolvePath(state: ShellState, path: string): NodePointer {
|
|||||||
} satisfies NodeNotFoundError;
|
} satisfies NodeNotFoundError;
|
||||||
}
|
}
|
||||||
|
|
||||||
let childPtr = NodePointer.of(currentDeviceId, currentNode.inode);
|
let childPtr = NodePointer.of(currentDeviceId, childInode);
|
||||||
|
|
||||||
if (state.filesystem.isMountPoint(childPtr)) {
|
if (state.filesystem.isMountPoint(childPtr)) {
|
||||||
const mount = state.filesystem.getMountTarget(childPtr)!;
|
const mount = state.filesystem.getMountTarget(childPtr)!;
|
||||||
|
|||||||
+2
-24
File diff suppressed because one or more lines are too long
+29
-7
@@ -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";
|
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 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);
|
const raw = JSON.parse(json);
|
||||||
|
|
||||||
// prettier-ignore
|
// 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) {
|
if(tree === null || tree.type === undefined) {
|
||||||
const time = Date.now();
|
const time = Date.now();
|
||||||
const defaultNode: DirectoryNode = {
|
const defaultNode: DirectoryNode = {
|
||||||
@@ -32,7 +36,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
|
|||||||
const children: Map<string, number> = new Map();
|
const children: Map<string, number> = new Map();
|
||||||
|
|
||||||
for (const child of tree.children) {
|
for (const child of tree.children) {
|
||||||
parseNode(child, tree.inode, map);
|
parseNode(child, tree.inode, store, map);
|
||||||
children.set(child.name, child.inode);
|
children.set(child.name, child.inode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +58,7 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "regular": {
|
case "regular": {
|
||||||
|
const uuid = randomUUID();
|
||||||
const fileNode: FileNode = {
|
const fileNode: FileNode = {
|
||||||
type: NodeType.File,
|
type: NodeType.File,
|
||||||
inode: tree.inode,
|
inode: tree.inode,
|
||||||
@@ -64,9 +69,11 @@ export function deserializeFileSystem(json: string): { id: number; map: Map<numb
|
|||||||
mtime: tree.mtime,
|
mtime: tree.mtime,
|
||||||
ctime: tree.ctime,
|
ctime: tree.ctime,
|
||||||
atime: tree.atime,
|
atime: tree.atime,
|
||||||
content: tree.content,
|
content: uuid,
|
||||||
size: tree.size,
|
size: tree.size,
|
||||||
};
|
};
|
||||||
|
const buff: Uint8Array<ArrayBuffer> = Uint8Array.from(tree.content);
|
||||||
|
store.put(uuid, new Blob([buff]));
|
||||||
map.set(tree.inode, Object.freeze(fileNode));
|
map.set(tree.inode, Object.freeze(fileNode));
|
||||||
break;
|
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) {
|
for (const dev of raw.dev) {
|
||||||
const map = new Map();
|
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;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user