From dd804f6f1418e310368b620fff2e5d98653e3157 Mon Sep 17 00:00:00 2001 From: orion Date: Mon, 9 Nov 2026 05:10:44 +0100 Subject: [PATCH] Virtual FileSystem fixed - (Needs unit testing)! --- src/core/filesystem/errors.ts | 22 +- src/core/filesystem/filesystem.ts | 412 ++++++++++++------------ src/core/filesystem/vdisk.ts | 24 +- src/core/util/temp_mem_stores.ts | 6 +- test/core/filesystem/filesystem.test.ts | 99 +++--- test/helpers/filesystems_fixture.ts | 90 ++---- 6 files changed, 307 insertions(+), 346 deletions(-) diff --git a/src/core/filesystem/errors.ts b/src/core/filesystem/errors.ts index a063f79..736b8d8 100644 --- a/src/core/filesystem/errors.ts +++ b/src/core/filesystem/errors.ts @@ -1,4 +1,4 @@ -import type { TreeNodeType } from "./node"; +import type { NodePointer, NodeType } from "./node"; export type FileSystemError = NotADirectoryError | AlreadyExistsError | NodeNotFoundError | IllegalMutationError | FileSystemInitializationError; @@ -14,7 +14,15 @@ export interface NotADirectoryError { message: string; timestamp: number; type: "NotADirectory"; - nodeType: TreeNodeType; + nodeType: NodeType; +} + +export interface NotAFileError { + inode: number; + message: string; + timestamp: number; + type: "NotAFile"; + nodeType: NodeType; } export interface AlreadyExistsError { @@ -28,7 +36,7 @@ export interface AlreadyExistsError { export interface NodeNotFoundError { type: "NodeNotFound"; message: string; - inode: number; + pointer: NodePointer; timestamp: number; } @@ -52,3 +60,11 @@ export interface DirectoryLoopError { descendantInode: number; ancestorInode: number; } + +export interface InvalidNodeNameError { + type: "InvalidNodeName"; + message: string; + timestamp: number; + pointer: NodePointer; + attemptedName: string; +} diff --git a/src/core/filesystem/filesystem.ts b/src/core/filesystem/filesystem.ts index 78a8325..579ff29 100644 --- a/src/core/filesystem/filesystem.ts +++ b/src/core/filesystem/filesystem.ts @@ -1,15 +1,18 @@ import type { UserInfo } from "@core/session_manager"; -import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer"; -import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal"; +import { upTraverseTree } from "@core/util/tree_traversal"; import { VDiskManager } from "@core/util/vdisk_manager"; +import type { Blob } from "node:buffer"; +import type { Readable } from "node:stream"; import type { AlreadyExistsError, DeviceNotFoundError, DirectoryLoopError, FileSystemInitializationError, IllegalMutationError, + InvalidNodeNameError, NodeNotFoundError, NotADirectoryError, + NotAFileError, } from "./errors"; import { fromPtr, @@ -91,7 +94,7 @@ export class FileSystem { public getRoot(): DirectoryNode { const dev = this._VDiskManager.getDisk(0); - if (!dev) throw new Error("Kernel Panic - Filesystem corrupted, system device not found!"); + if (!dev) throw new Error("Filesystem corrupted, system device not found!"); return dev.getNode(0) as DirectoryNode; } @@ -120,7 +123,7 @@ export class FileSystem { type: "NodeNotFound", message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`, timestamp: Date.now(), - inode: fptr.inode, + pointer: ptr, } satisfies NodeNotFoundError; } @@ -136,7 +139,7 @@ export class FileSystem { type: "NodeNotFound", message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`, timestamp: Date.now(), - inode: fptr.inode, + pointer: ptr, } satisfies NodeNotFoundError; } @@ -152,7 +155,7 @@ export class FileSystem { type: "NodeNotFound", message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`, timestamp: Date.now(), - inode: fptr.inode, + pointer: ptr, } satisfies NodeNotFoundError; } @@ -188,9 +191,9 @@ export class FileSystem { if (sourceNode === undefined) { throw { type: "NodeNotFound", - message: `Source node under inode ${sptr.inode} not found`, + message: `Source node under ${source} not found`, timestamp: Date.now(), - inode: sptr.inode, + pointer: source, } satisfies NodeNotFoundError; } @@ -205,9 +208,9 @@ export class FileSystem { if (destinationNode === undefined) { throw { type: "NodeNotFound", - message: `Destination node under inode ${dptr.inode} not found`, + message: `Destination node under ${destination} not found`, timestamp: Date.now(), - inode: dptr.inode, + pointer: destination, } satisfies NodeNotFoundError; } @@ -229,7 +232,7 @@ export class FileSystem { const nodeName = name ?? s_entry[0].name; - if (destinationNode.children.get(nodeName) !== undefined) { + if (destinationNode.children.has(nodeName)) { throw { type: "AlreadyExists", message: `A node inside the destination inode ${destinationNode.inode} under the name ${nodeName} already exists`, @@ -239,6 +242,16 @@ export class FileSystem { } satisfies AlreadyExistsError; } + if (nodeName.length > this._maxNameLength) { + throw { + type: "InvalidNodeName", + message: `Invalid name for the ${source} source node`, + timestamp: Date.now(), + pointer: source, + attemptedName: nodeName, + } satisfies InvalidNodeNameError; + } + if (upTraverseTree(this, destination).includes(source)) { throw { type: "DirectoryLoop", @@ -256,165 +269,81 @@ export class FileSystem { } } - public modifyNode(target: NodePointer, changes: Partial) { - const node = this.resolveNode(target); - if (node === undefined) { - throw { - type: "NodeNotFound", - message: `Node under inode ${inode} not found`, - timestamp: Date.now(), - inode: inode, - } satisfies NodeNotFoundError; - } - - const tptr = fromPtr(target); - let newNode: TreeNode; - - if (node.type === TreeNodeType.File) { - const fileNode: FileNode = { - type: node.type, - inode: node.inode, - name: node.name, - parent: node.parent, - mode: changes.mode ?? node.mode, - owner: changes.owner ?? node.owner, - group: changes.group ?? node.group, - mtime: changes.content ? Date.now() : node.mtime, - atime: node.atime, - ctime: Date.now(), - content: changes.content ?? node.content, - }; - - newNode = fileNode; - } else { - const directoryNode: DirectoryNode = { - type: node.type, - inode: node.inode, - name: node.name, - parent: node.parent, - mode: changes.mode ?? node.mode, - owner: changes.owner ?? node.owner, - group: changes.group ?? node.group, - mtime: changes.content ? Date.now() : node.mtime, - atime: node.atime, - ctime: Date.now(), - children: node.children, - }; - - newNode = directoryNode; - } - - const map = new Map(this.Overlay); - map.set(node.inode, Object.freeze(newNode)); - - const newOverlay: ReadonlyMap = map; - - return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex); - } - public createNode( - parent: number, + parent: NodePointer, name: string, - type: TreeNodeType, - mode: number, - owner: number, - group: number, - content?: ImmutableBuffer, - ): FileSystem { - let node: TreeNode; - + data: + | { + type: NodeType.File; + mode: number; + owner: number; + group: number; + content: Blob | Readable; + size: number; + } + | { type: NodeType.Directory; mode: number; owner: number; group: number } + | { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string }, + ) { + const pptr = fromPtr(parent); + const pdev = this._VDiskManager.getDisk(pptr.device_id); const parentNode = this.resolveNode(parent); - const time: number = Date.now(); - const newInode: number = this._INodeIndex + 1; - if (parentNode === undefined) { + if (!pdev) { + throw { + type: "DeviceNotFound", + message: `Device under id ${pptr.device_id} not found`, + timestamp: Date.now(), + device_id: pptr.device_id, + } satisfies DeviceNotFoundError; + } + + if (!parentNode) { throw { type: "NodeNotFound", - message: `Parent inode ${parent} not found`, + message: `Destination node under ${parent} not found`, timestamp: Date.now(), - inode: parent, + pointer: parent, } satisfies NodeNotFoundError; } - if (parentNode.type === TreeNodeType.File) { + if (parentNode.type !== NodeType.Directory) { throw { type: "NotADirectory", - message: `Parent inode ${parent} is not a directory`, + message: `Destination node under inode ${pptr.inode} is not a directory`, + inode: pptr.inode, timestamp: Date.now(), - inode: parent, nodeType: parentNode.type, } satisfies NotADirectoryError; } - if (parentNode.type === TreeNodeType.Directory && parentNode.children[name] !== undefined) { + if (parentNode.children.has(name)) { throw { type: "AlreadyExists", - message: `Child under the name of ${name} inside the parent inode ${parent} already exists`, + message: `A node inside the destination inode ${parentNode.inode} under the name ${name} already exists`, timestamp: Date.now(), - parentInode: parent, - name, + parentInode: parentNode.inode, + name: name, } satisfies AlreadyExistsError; } - if (type === TreeNodeType.File) { - const fileNode: FileNode = { - type, - inode: newInode, - name, - parent, - mode, - owner, - group, - mtime: time, - atime: time, - ctime: time, - content: content ?? createImmutableBuffer(new Uint8Array()), - }; - - node = fileNode; - } else { - const directoryNode: DirectoryNode = { - type, - inode: newInode, - name, - parent, - mode, - owner, - group, - mtime: time, - atime: time, - ctime: time, - children: {}, - }; - - node = directoryNode; + if (name.length > this._maxNameLength) { + throw { + type: "InvalidNodeName", + message: `Invalid name for the new node`, + timestamp: Date.now(), + pointer: parent, + attemptedName: name, + } satisfies InvalidNodeNameError; } - const newParentNode: DirectoryNode = { - type: parentNode.type, - inode: parent, - name: parentNode.name, - parent: parentNode.parent, - mode: parentNode.mode, - owner: parentNode.owner, - group: parentNode.group, - mtime: time, - atime: parentNode.atime, - ctime: time, - children: { ...parentNode.children, [name]: newInode }, - }; - - const map = new Map(this.Overlay); - map.set(newInode, Object.freeze(node)); - map.set(parent, Object.freeze(newParentNode)); - - const newOverlay: ReadonlyMap = map; - - return FileSystem.init(this._BaseFS, newOverlay, newInode); + pdev.createNodeEntry(pptr.inode, name, data); } - public deleteNode(inode: number): FileSystem { - if (inode === 0) { + public deleteNode(target: NodePointer) { + const tptr = fromPtr(target); + const tdev = this._VDiskManager.getDisk(tptr.device_id); + + if (tptr.device_id === 0 && tptr.inode === 0) { throw { type: "IllegalMutation", message: `Attempt at illegal mutation of node under inode 0 (root)`, @@ -422,64 +351,138 @@ export class FileSystem { } satisfies IllegalMutationError; } - const node = this.resolveNode(inode); - if (node === undefined) { + if (!tdev) { throw { - type: "NodeNotFound", - message: `Node under inode ${inode} not found`, + type: "DeviceNotFound", + message: `Device under id ${tptr.device_id} not found`, timestamp: Date.now(), - inode: inode, - } satisfies NodeNotFoundError; + device_id: tptr.device_id, + } satisfies DeviceNotFoundError; } - const time: number = Date.now(); - const descendants: number[] = dnTraverseTree(this, inode); + const tdevIndex = tdev.getIndex(); + const t_entry = tdevIndex.get(target); + if (!t_entry || !t_entry[0]) throw new Error("File System corruption, reverse index is missing a field"); - const map: Map = new Map(this._OverlayFS); - for (const entry of descendants) { - map.set(entry, null); - } - - const parentNode = this.resolveNode(node.parent) as DirectoryNode; - if (parentNode === undefined) { - throw { - type: "NodeNotFound", - message: `Parent inode ${node.parent} not found (should be impossible unless the filesystem corrupted!)`, - timestamp: Date.now(), - inode: node.parent, - } satisfies NodeNotFoundError; - } - - const { [node.name]: __, ...newChildren } = parentNode.children; - - const newParentNode: DirectoryNode = { - type: parentNode.type, - inode: parentNode.inode, - name: parentNode.name, - parent: parentNode.parent, - mode: parentNode.mode, - owner: parentNode.owner, - group: parentNode.group, - mtime: time, - atime: parentNode.atime, - ctime: time, - children: newChildren, - }; - - map.set(node.parent, Object.freeze(newParentNode)); - - const newOverlay: ReadonlyMap = map; - return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex); + tdev.unlinkNodeEntry(tptr.inode, t_entry[0].name); } - public canRead(inode: number, user: UserInfo): boolean { - const node = this.resolveNode(inode); - if (node === undefined) { + public writeContent(target: NodePointer, content: Blob) { + const ptr = fromPtr(target); + const dev = this._VDiskManager.getDisk(ptr.device_id); + const node = this.resolveNode(target); + + if (!dev) { + throw { + type: "DeviceNotFound", + message: `Device under id ${ptr.device_id} not found`, + timestamp: Date.now(), + device_id: ptr.device_id, + } satisfies DeviceNotFoundError; + } + + if (!node) { throw { type: "NodeNotFound", - message: `Node under inode ${inode} not found`, + message: `Destination node under ${target} not found`, timestamp: Date.now(), - inode: inode, + pointer: target, + } satisfies NodeNotFoundError; + } + + if (node.type === NodeType.Directory) { + throw { + type: "NotAFile", + message: `Node is not a file`, + inode: ptr.inode, + timestamp: Date.now(), + nodeType: node.type, + } satisfies NotAFileError; + } + + dev.writeContent((node as FileNode).content, content); + } + + public streamContent(target: NodePointer): Readable { + const ptr = fromPtr(target); + const dev = this._VDiskManager.getDisk(ptr.device_id); + const node = this.resolveNode(target); + + if (!dev) { + throw { + type: "DeviceNotFound", + message: `Device under id ${ptr.device_id} not found`, + timestamp: Date.now(), + device_id: ptr.device_id, + } satisfies DeviceNotFoundError; + } + + if (!node) { + throw { + type: "NodeNotFound", + message: `Destination node under ${target} not found`, + timestamp: Date.now(), + pointer: target, + } satisfies NodeNotFoundError; + } + + if (node.type === NodeType.Directory) { + throw { + type: "NotAFile", + message: `Node is not a file`, + inode: ptr.inode, + timestamp: Date.now(), + nodeType: node.type, + } satisfies NotAFileError; + } + + return dev.streamContent((node as FileNode).content); + } + + public modifyNode(target: NodePointer, data: { mode?: number; owner?: number; group?: number }) { + const ptr = fromPtr(target); + const dev = this._VDiskManager.getDisk(ptr.device_id); + const node = this.resolveNode(target); + + if (!dev) { + throw { + type: "DeviceNotFound", + message: `Device under id ${ptr.device_id} not found`, + timestamp: Date.now(), + device_id: ptr.device_id, + } satisfies DeviceNotFoundError; + } + + if (!node) { + throw { + type: "NodeNotFound", + message: `Destination node under ${target} not found`, + timestamp: Date.now(), + pointer: target, + } satisfies NodeNotFoundError; + } + + if (node.type === NodeType.Directory) { + throw { + type: "NotAFile", + message: `Node is not a file`, + inode: ptr.inode, + timestamp: Date.now(), + nodeType: node.type, + } satisfies NotAFileError; + } + + dev.writeNodeEntry(node.inode, data); + } + + public canRead(nodep: NodePointer, user: UserInfo): boolean { + const node = this.resolveNode(nodep); + if (!node) { + throw { + type: "NodeNotFound", + message: `Node under ${nodep} not found`, + timestamp: Date.now(), + pointer: nodep, } satisfies NodeNotFoundError; } @@ -494,14 +497,14 @@ export class FileSystem { } } - public canWrite(inode: number, user: UserInfo): boolean { - const node = this.resolveNode(inode); - if (node === undefined) { + public canWrite(nodep: NodePointer, user: UserInfo): boolean { + const node = this.resolveNode(nodep); + if (!node) { throw { type: "NodeNotFound", - message: `Node under inode ${inode} not found`, + message: `Node under ${nodep} not found`, timestamp: Date.now(), - inode: inode, + pointer: nodep, } satisfies NodeNotFoundError; } @@ -516,14 +519,14 @@ export class FileSystem { } } - public canExecute(inode: number, user: UserInfo): boolean { - const node = this.resolveNode(inode); - if (node === undefined) { + public canExecute(nodep: NodePointer, user: UserInfo): boolean { + const node = this.resolveNode(nodep); + if (!node) { throw { type: "NodeNotFound", - message: `Node under inode ${inode} not found`, + message: `Node under ${nodep} not found`, timestamp: Date.now(), - inode: inode, + pointer: nodep, } satisfies NodeNotFoundError; } @@ -537,11 +540,4 @@ export class FileSystem { return (node.mode & 0o001) !== 0; } } - - /* public get Overlay(): ReadonlyMap { - return this._OverlayFS; - } - public get INodeIndex(): number { - return this._INodeIndex; - } */ } diff --git a/src/core/filesystem/vdisk.ts b/src/core/filesystem/vdisk.ts index 87e19f1..d59bf96 100644 --- a/src/core/filesystem/vdisk.ts +++ b/src/core/filesystem/vdisk.ts @@ -1,3 +1,6 @@ +import type { Blob } from "node:buffer"; +import { Readable } from "node:stream"; +import { blob } from "node:stream/consumers"; import { NodeType, toPtr, @@ -58,7 +61,7 @@ export interface DataConfig { export interface IDataStore { read(uuid: string): Promise; - stream(uuid: string): ReadableStream; + stream(uuid: string): Readable; put(uuid: string, blob: Blob): Promise; delete(uuid: string): Promise; has(uuid: string): Promise; @@ -204,15 +207,22 @@ export class VDisk { return this._base.get(inode); } - public createNodeEntry( + public async createNodeEntry( pinode: number, name: string, data: - | { type: NodeType.File; mode: number; owner: number; group: number; content: string; size: number } + | { + type: NodeType.File; + mode: number; + owner: number; + group: number; + content: Blob | Readable; + size: number; + } | { type: NodeType.Directory; mode: number; owner: number; group: number } | { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string }, ): Promise { - return this._batch(() => { + return this._batch(async () => { this._isWritable(); const index = ++this._iIndex; const time = Date.now(); @@ -231,11 +241,13 @@ export class VDisk { }; const pnode = this.getNode(pinode) as DirectoryNode; + const uuid = crypto.randomUUID(); let node: Node; switch (data.type) { case NodeType.File: - node = { ...base, type: NodeType.File, content: data.content, size: data.size }; + node = { ...base, type: NodeType.File, content: uuid, size: data.size }; + this.writeContent(uuid, data.content instanceof Readable ? await blob(data.content) : data.content); break; case NodeType.Directory: node = { ...base, type: NodeType.Directory, children: new Map() }; @@ -385,7 +397,7 @@ export class VDisk { return this._dataStore.read(uuid); } - public streamContent(uuid: string): ReadableStream { + public streamContent(uuid: string): Readable { return this._dataStore.stream(uuid); } diff --git a/src/core/util/temp_mem_stores.ts b/src/core/util/temp_mem_stores.ts index 0a78510..02bad48 100644 --- a/src/core/util/temp_mem_stores.ts +++ b/src/core/util/temp_mem_stores.ts @@ -1,5 +1,7 @@ import type { Node } from "@core/filesystem/node"; import type { IDataStore, IMetaStore } from "@core/filesystem/vdisk"; +import type { Blob } from "node:buffer"; +import { Readable } from "node:stream"; export class MemMetaStore implements IMetaStore { private _data: Map = new Map(); @@ -33,11 +35,11 @@ export class MemDataStore implements IDataStore { return new Uint8Array(buff); } - public stream(uuid: string): ReadableStream { + public stream(uuid: string): Readable { const blob = this._data.get(uuid); if (!blob) throw new Error("Content missing - " + uuid); - return blob.stream(); + return Readable.from(blob.stream()); } public put(uuid: string, blob: Blob): Promise { diff --git a/test/core/filesystem/filesystem.test.ts b/test/core/filesystem/filesystem.test.ts index ae5f053..504fb78 100644 --- a/test/core/filesystem/filesystem.test.ts +++ b/test/core/filesystem/filesystem.test.ts @@ -1,7 +1,10 @@ import { FileSystem } from "@core/filesystem/filesystem"; -import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/node"; +import { NodeType, type FileNode } from "@core/filesystem/node"; +import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk"; import type { UserInfo } from "@core/session_manager"; import { createImmutableBuffer } from "@core/util/immutable_buffer"; +import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores"; +import { deserializeFileSystem } from "@shell/serializer"; import { expectAlreadyExistsError, expectDirectoryLoopError, @@ -9,83 +12,55 @@ import { expectNodeNotFoundError, expectNotADirectoryError, } from "@tests/helpers/error_matchers"; -import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture"; +import { buildTestFS } from "@tests/helpers/filesystems_fixture"; +import { Blob } from "node:buffer"; import { describe, expect, it } from "vitest"; -const testTree: FixtureNode = { - inode: 0, - name: "root", - type: "Dir", - children: [ - { - inode: 1, - name: "hello.txt", - type: "File", - content: "Goodbye World!", - children: [], - }, - { - inode: 2, - name: "sub", - type: "Dir", - children: [ - { - inode: 3, - name: "world.txt", - type: "File", - content: "Goodbye!", - children: [], - }, - { - inode: 4, - name: "bye", - type: "Dir", - children: [], - }, - ], - }, - ], -}; - -const fs = buildTestFS(testTree); +const fs = buildTestFS(); describe("FileSystem.init", () => { - const map: Map = new Map(); - const time = Date.now(); + const rawJson = `{"dev": [{"id": 0, "tree": {"type": "directory", "inode": 0, "mode": 443, "owner": 0, "group": 0, "parent": 0, "atime": 0, "mtime": 0, "ctime": 0, "children": []}}]}`; + const arr = deserializeFileSystem(rawJson); + const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = []; - const root: DirectoryNode = { - type: TreeNodeType.Directory, - inode: 0, - name: "root", - parent: 0, - mode: 0o555, - owner: 0, - group: 0, - mtime: time, - atime: time, - ctime: time, - children: {}, + const data = new MemDataStore(); + const meta = new MemMetaStore(); + const config: VDiskConfig = { + id: 0, + label: `disk-0`, + index: 0, + readonly: false, + size: 1024 * 1024 * 1024 * 8, //8 GiB + base: arr[0]!.map, + dataC: { + chunkSize: 1024 * 8, //8 KiB + cacheSize: 1024 * 1024 * 64, //64 MiB + cacheTTL: 1000 * 60, // 1 min + }, }; - map.set(0, root); - - const base: ReadonlyMap = map; - const overlay: ReadonlyMap = new Map(); + devices.push({ dstore: data, mstore: meta, config }); it("initializes correctly", () => { - const fs = FileSystem.init(base, overlay, 0); + const fs = FileSystem.init(devices); expect(fs).toBeInstanceOf(FileSystem); - expect(fs.INodeIndex).toBe(0); }); }); describe("createNode", () => { - const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum")); + const content = new Blob([new TextEncoder().encode("Lorem ipsum")]); it("creates a file and updates parent", () => { - const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content); + fs.createNode("0:0", "readme", { + type: NodeType.File, + mode: 0o644, + owner: 0, + group: 0, + content: content, + size: content.size, + }); - expect(newFS).toBeInstanceOf(FileSystem); - const newNode = newFS.getNode(5) as FileNode; + expect(fs).toBeInstanceOf(FileSystem); + const newNode = fs.getNode("0") as FileNode; expect(newNode.type).toBe(TreeNodeType.File); expect(newNode.name).toBe("readme"); diff --git a/test/helpers/filesystems_fixture.ts b/test/helpers/filesystems_fixture.ts index e4b3bff..718854b 100644 --- a/test/helpers/filesystems_fixture.ts +++ b/test/helpers/filesystems_fixture.ts @@ -1,71 +1,31 @@ import { FileSystem } from "@core/filesystem/filesystem"; -import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/node"; -import { createImmutableBuffer } from "@core/util/immutable_buffer"; +import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk"; +import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores"; +import { deserializeFileSystem } from "@shell/serializer"; -export interface FixtureNode { - inode: number; - name: string; - type: "File" | "Dir"; - children: FixtureNode[]; - content?: string; - mode?: number; - owner?: number; - group?: number; - mtime?: number; - ctime?: number; - atime?: number; -} +export function buildTestFS(): FileSystem { + const rawJson = `{"dev":[{"id":0,"tree":{"atime":1773013637,"children":[{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":1,"mode":493,"mtime":1757686809,"name":"run","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":2,"mode":493,"mtime":1757686772,"name":"dev","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":3,"mode":493,"mtime":1757686804,"name":"proc","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":4,"mode":493,"mtime":1757686785,"name":"lost+found","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":5,"mode":493,"mtime":1757686768,"name":"boot","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[{"atime":1788458583,"children":[{"atime":1788459229,"children":[],"content":"","ctime":1788459246,"group":1000,"inode":8,"mode":493,"mtime":1788459147,"name":"cat","owner":1000,"parent":7,"type":"regular"}],"content":null,"ctime":1788459229,"group":1000,"inode":7,"mode":493,"mtime":1788459229,"name":"bin","owner":1000,"parent":6,"type":"directory"}],"content":null,"ctime":1788458583,"group":1000,"inode":6,"mode":493,"mtime":1788458583,"name":"usr","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":9,"mode":493,"mtime":1757686775,"name":"etc","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":10,"mode":493,"mtime":1757686830,"name":"srv","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":11,"mode":493,"mtime":1757686781,"name":"lib64","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":12,"mode":493,"mtime":1757686689,"name":"root","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[{"atime":1773013637,"children":[{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":15,"mode":493,"mtime":1757766627,"name":".config","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":16,"mode":493,"mtime":1757766646,"name":".npm","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":17,"mode":493,"mtime":1757766597,"name":"Videos","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":18,"mode":493,"mtime":1757766631,"name":".java","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":19,"mode":493,"mtime":1757766614,"name":".cache","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":20,"mode":493,"mtime":1757766580,"name":"Downloads","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":21,"mode":493,"mtime":1757766577,"name":"Desktop","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":22,"mode":493,"mtime":1757766652,"name":".ssh","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":23,"mode":493,"mtime":1757766551,"name":"Documents","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":24,"mode":493,"mtime":1757766620,"name":".cargo","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[{"atime":1773013637,"children":[{"atime":1773013637,"children":[{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":28,"mode":493,"mtime":1759940170,"name":"about.md","owner":1000,"parent":27,"type":"regular"}],"content":null,"ctime":1773013637,"group":1000,"inode":27,"mode":493,"mtime":1759940182,"name":"This Website","owner":1000,"parent":26,"type":"directory"},{"atime":1773013637,"children":[{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":30,"mode":493,"mtime":1759940170,"name":"about.md","owner":1000,"parent":29,"type":"regular"}],"content":null,"ctime":1773013637,"group":1000,"inode":29,"mode":493,"mtime":1759942574,"name":"Sheepo","owner":1000,"parent":26,"type":"directory"},{"atime":1773013637,"children":[{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":32,"mode":493,"mtime":1759940170,"name":"about.md","owner":1000,"parent":31,"type":"regular"}],"content":null,"ctime":1773013637,"group":1000,"inode":31,"mode":493,"mtime":1759942576,"name":"SchoolProjectManager","owner":1000,"parent":26,"type":"directory"}],"content":null,"ctime":1773013637,"group":1000,"inode":26,"mode":493,"mtime":1759940133,"name":"Websites","owner":1000,"parent":25,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":33,"mode":493,"mtime":1759939881,"name":"C#","owner":1000,"parent":25,"type":"directory"}],"content":null,"ctime":1773013637,"group":1000,"inode":25,"mode":493,"mtime":1759939982,"name":"Projects","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":34,"mode":493,"mtime":1757766640,"name":".local","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":35,"mode":493,"mtime":1757766591,"name":"Pictures","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":36,"mode":493,"mtime":1757766660,"name":".var","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":38,"mode":493,"mtime":1757812927,"name":"nggyu.lrc","owner":1000,"parent":37,"type":"regular"}],"content":null,"ctime":1773013637,"group":1000,"inode":37,"mode":493,"mtime":1759939803,"name":"Music","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":39,"mode":493,"mtime":1757766636,"name":".dotnet","owner":1000,"parent":14,"type":"directory"},{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":40,"mode":493,"mtime":1757780849,"name":".bash_logout","owner":1000,"parent":14,"type":"regular"},{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":41,"mode":493,"mtime":1757780715,"name":".bashrc","owner":1000,"parent":14,"type":"regular"},{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":42,"mode":493,"mtime":1757780715,"name":".bash_profile","owner":1000,"parent":14,"type":"regular"},{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":43,"mode":493,"mtime":1757780889,"name":".xinitrc","owner":1000,"parent":14,"type":"regular"},{"atime":1773013637,"children":[],"content":"","ctime":1773013637,"group":1000,"inode":44,"mode":493,"mtime":1757780811,"name":".bash_history","owner":1000,"parent":14,"type":"regular"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":45,"mode":493,"mtime":1771137563,"name":"Test test","owner":1000,"parent":14,"type":"directory"}],"content":null,"ctime":1773013637,"group":1000,"inode":14,"mode":493,"mtime":1771137563,"name":"kamil","owner":1000,"parent":13,"type":"directory"}],"content":null,"ctime":1773013637,"group":1000,"inode":13,"mode":493,"mtime":1757766534,"name":"home","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":46,"mode":493,"mtime":1757686867,"name":"tmp","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":47,"mode":493,"mtime":1757686765,"name":"bin","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":48,"mode":493,"mtime":1757686874,"name":"var","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":49,"mode":493,"mtime":1757686823,"name":"sbin","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":50,"mode":493,"mtime":1757686863,"name":"sys","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":51,"mode":493,"mtime":1757686777,"name":"lib","owner":1000,"parent":0,"type":"directory"},{"atime":1773013637,"children":[],"content":null,"ctime":1773013637,"group":1000,"inode":52,"mode":493,"mtime":1757686791,"name":"opt","owner":1000,"parent":0,"type":"directory"}],"content":null,"ctime":1773013637,"group":1000,"inode":0,"mode":493,"mtime":1757686876,"name":"root_fs","owner":1000,"parent":0,"type":"directory"}},{"id":1,"tree":{}}]}`; + const arr = deserializeFileSystem(rawJson); + const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = []; -export function buildTestFS(tree: FixtureNode): FileSystem { - const map = new Map(); - - function parse(node: FixtureNode, parent: number): void { - if (node.type === "Dir") { - for (const child of node.children) { - parse(child, node.inode); - } - - const children: Record = {}; - for (const child of node.children) { - children[child.name] = child.inode; - } - - const directoryNode: DirectoryNode = { - type: TreeNodeType.Directory, - inode: node.inode, - name: node.name, - parent: parent, - mode: node.mode ?? 0o777, - owner: node.owner ?? 0, - group: node.group ?? 0, - mtime: node.mtime ?? Date.now(), - ctime: node.ctime ?? Date.now(), - atime: node.atime ?? Date.now(), - children: children, - }; - map.set(node.inode, Object.freeze(directoryNode)); - } else { - const fileNode: FileNode = { - type: TreeNodeType.File, - inode: node.inode, - name: node.name, - parent: parent, - mode: node.mode ?? 0o777, - owner: node.owner ?? 0, - group: node.group ?? 0, - mtime: node.mtime ?? Date.now(), - ctime: node.ctime ?? Date.now(), - atime: node.atime ?? Date.now(), - content: createImmutableBuffer(new TextEncoder().encode(node.content)), - }; - map.set(node.inode, Object.freeze(fileNode)); - } + for (const i of arr) { + const data = new MemDataStore(); + const meta = new MemMetaStore(); + const config: VDiskConfig = { + id: i.id, + label: `disk-${i.id}`, + index: 0, + readonly: false, + size: 1024 * 1024 * 1024 * 8, //8 GiB + base: i.map, + dataC: { + chunkSize: 1024 * 8, //8 KiB + cacheSize: 1024 * 1024 * 64, //64 MiB + cacheTTL: 1000 * 60, // 1 min + }, + }; + devices.push({ dstore: data, mstore: meta, config }); } - parse(tree, 0); - - const base: ReadonlyMap = map; - const overlay: ReadonlyMap = new Map(); - - return FileSystem.init(base, overlay, base.size - 1); + return FileSystem.init(devices); }