diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index c7d5c71..94f7ff5 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -13,12 +13,12 @@ "state": { "type": "markdown", "state": { - "file": "todo.md", + "file": "README.md", "mode": "source", "source": false }, "icon": "lucide-file", - "title": "todo" + "title": "README" } } ] @@ -188,7 +188,7 @@ "bases:Create new base": false } }, - "active": "af4cfba92021d1c8", + "active": "1db1d6b317bc05ae", "lastOpenFiles": [ "src/util/posix.ts", "src/core/util/sort.ts" diff --git a/src/core/commands/cd.ts b/src/core/commands/cd.ts index d6546a3..abe8c05 100644 --- a/src/core/commands/cd.ts +++ b/src/core/commands/cd.ts @@ -10,7 +10,7 @@ import { type PermissionDeniedError, type ShellError, } from "@core/util/errors"; -import { resolveInode, resolvePath } from "@core/util/path_resolver"; +import { resolvePath } from "@core/util/path_resolver"; export const COMMAND_NAME = "cd"; @@ -58,12 +58,7 @@ async function main( if (state.filesystem.isFile(inode)) { return { state, - result: createErrorResult( - "PathResolutionError", - "No such file or directory", - { path: resolveInode(state.filesystem, inode) }, - COMMAND_NAME, - ), + result: createErrorResult("PathResolutionError", "Not a directory", { path: args[0]! }, COMMAND_NAME), } satisfies CommandOutput; } diff --git a/src/shell/process.ts b/src/shell/process.ts index e4c4ebd..6692918 100644 --- a/src/shell/process.ts +++ b/src/shell/process.ts @@ -5,6 +5,7 @@ import type { ShellState } from "@core/shell_state"; import { SIGNAL_MAP, type Signal } from "@core/signal"; import type { ShellError, SignalTerminationError } from "@core/util/errors"; import { createImmutableBuffer } from "@core/util/immutable_buffer"; +import { concatUint8Arrays, splitUint8Array } from "@termos/util/concat"; import { EventEmitter } from "./event_emitter"; import type { ShellEventMap } from "./shell"; @@ -36,34 +37,6 @@ export class Process { this._EventEmitter = new EventEmitter(); } - private concatUint8Arrays(arrays: Uint8Array[]): Uint8Array { - const size = arrays.reduce((sum, arr) => sum + arr.length, 0); - const result = new Uint8Array(size); - - let offset = 0; - for (const arr of arrays) { - result.set(arr, offset); - offset += arr.length; - } - - return result; - } - - // Tested - private divideToChunks(data: Uint8Array): Uint8Array[] { - const nChunks: number = Math.ceil(data.length / this._MaxChunkByteSize); - const chunks: Uint8Array[] = []; - - for (let i = 0; i < nChunks; i++) { - const start = this._MaxChunkByteSize * i; - const end = this._MaxChunkByteSize * (i + 1); - - chunks.push(data.subarray(start, end)); - } - - return chunks; - } - private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void { if (this._Settled) return; @@ -72,7 +45,7 @@ export class Process { this._EventEmitter.emit(eventType, { pid: this.PID, data }); return; } else { - const arr = this.divideToChunks(data); + const arr = splitUint8Array(data, this._MaxChunkByteSize); for (const ent of arr) { chunks.push(ent); this._EventEmitter.emit(eventType, { pid: this.PID, data: ent }); @@ -102,8 +75,8 @@ export class Process { if (this._Settled) return; let result: ResultSuccess | ResultError; - const stdout: Uint8Array = this.concatUint8Arrays(this._StdoutChunks); - const stderr: Uint8Array = this.concatUint8Arrays(this._StderrChunks); + const stdout: Uint8Array = concatUint8Arrays(this._StdoutChunks); + const stderr: Uint8Array = concatUint8Arrays(this._StderrChunks); if (output.result.type === "success") { result = createSuccessResult( diff --git a/src/shell/serializer.ts b/src/shell/serializer.ts index 82fddc1..180552a 100644 --- a/src/shell/serializer.ts +++ b/src/shell/serializer.ts @@ -33,7 +33,9 @@ const DirectoryNodeSchema = z.object({ const TreeNodeSchema = z.union([FileNodeSchema, DirectoryNodeSchema]); -export function serializeFileSystem() {} +/* export function serializeFileSystem(map: ReadonlyMap): string { + +} */ export function deserializeFileSystem(json: string): ReadonlyMap { const raw = JSON.parse(json); diff --git a/src/util/concat.ts b/src/util/concat.ts new file mode 100644 index 0000000..99fbea2 --- /dev/null +++ b/src/util/concat.ts @@ -0,0 +1,26 @@ +export function concatUint8Arrays(arrays: Uint8Array[]): Uint8Array { + const size = arrays.reduce((sum, arr) => sum + arr.length, 0); + const array = new Uint8Array(size); + + let offset = 0; + for (const arr of arrays) { + array.set(arr, offset); + offset += arr.length; + } + + return array; +} + +export function splitUint8Array(data: Uint8Array, size: number): Uint8Array[] { + const nChunks: number = Math.ceil(data.length / size); + const chunks: Uint8Array[] = []; + + for (let i = 0; i < nChunks; i++) { + const start = size * i; + const end = size * (i + 1); + + chunks.push(data.subarray(start, end)); + } + + return chunks; +} diff --git a/test/shell/process.test.ts b/test/shell/process.test.ts deleted file mode 100644 index f83b6b7..0000000 --- a/test/shell/process.test.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { expect, test } from "vitest"; - -function divideToChunks(data: Uint8Array): Uint8Array[] { - const nChunks: number = Math.ceil(data.length / 1024); - - const chunks: Uint8Array[] = []; - for (let i = 0; i < nChunks; i++) { - const start = 1024 * i; - const end = 1024 * (i + 1); - - chunks.push(data.slice(start, end)); - } - - return chunks; -} - -test("divide to chunks", () => { - const data: Uint8Array = new Uint8Array(2048); - const result: Uint8Array[] = divideToChunks(data); - expect(result.length).toEqual(2); -}); - -test("divide to chunks", () => { - const data: Uint8Array = new Uint8Array(2049); - const result = divideToChunks(data); - expect(result.length).toEqual(3); -}); diff --git a/test/util/concat.test.ts b/test/util/concat.test.ts new file mode 100644 index 0000000..dce4eaf --- /dev/null +++ b/test/util/concat.test.ts @@ -0,0 +1,20 @@ +import { splitUint8Array } from "@termos/util/concat"; +import { expect, test } from "vitest"; + +test("divide to chunks", () => { + const data: Uint8Array = new Uint8Array(2048); + const result = splitUint8Array(data, 1024); + expect(result.length).toEqual(2); +}); + +test("divide to chunks", () => { + const data: Uint8Array = new Uint8Array(2049); + const result = splitUint8Array(data, 1024); + expect(result.length).toEqual(3); +}); + +test("divide to chunks", () => { + const data: Uint8Array = new Uint8Array(2048); + const result = splitUint8Array(data, 53); + expect(result.length).toEqual(Math.ceil(2048 / 53)); +});