diff --git a/src/core/filesystem/errors.ts b/src/core/filesystem/errors.ts index 55e4b9f..a063f79 100644 --- a/src/core/filesystem/errors.ts +++ b/src/core/filesystem/errors.ts @@ -32,6 +32,13 @@ export interface NodeNotFoundError { timestamp: number; } +export interface DeviceNotFoundError { + type: "DeviceNotFound"; + message: string; + device_id: number; + timestamp: number; +} + export interface IllegalMutationError { type: "IllegalMutation"; message: string; diff --git a/src/core/filesystem/filesystem.ts b/src/core/filesystem/filesystem.ts index f3d1850..51dec8a 100644 --- a/src/core/filesystem/filesystem.ts +++ b/src/core/filesystem/filesystem.ts @@ -4,13 +4,23 @@ import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal"; import { VDiskManager } from "@core/util/vdisk_manager"; import type { AlreadyExistsError, + DeviceNotFoundError, DirectoryLoopError, FileSystemInitializationError, IllegalMutationError, NodeNotFoundError, NotADirectoryError, } from "./errors"; -import { NodeType, type DirectoryNode, type FileNode, type Node, type NodePointer } from "./node"; +import { + fromPtr, + NodeType, + toPtr, + type DirectoryNode, + type FileNode, + type Node, + type NodeEntry, + type NodePointer, +} from "./node"; import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk"; export interface FileSystemConfig { @@ -24,13 +34,12 @@ export class FileSystem { private readonly _maxPathLength: number; private readonly _maxSymlinkDepth: number; - private _SysPartitionID: number = 0; private _Mounts: Map = new Map(); private _VDiskManager: VDiskManager; private constructor( - config: FileSystemConfig, devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>, + config: FileSystemConfig, ) { this._maxNameLength = config.maxNameLength; this._maxPathLength = config.maxPathLength; @@ -43,19 +52,16 @@ export class FileSystem { } private resolveNode(ptr: NodePointer): Node | undefined { - const dev = this._VDiskManager.getVDisk(ptr[1]); + const fptr = fromPtr(ptr); + const dev = this._VDiskManager.getDisk(fptr.device_id); if (!dev) return undefined; - const node = dev.overlay.get(ptr[0]); - if (node) return node; - if (node === null) return undefined; - - return dev.base.get(ptr[0]); + return dev.getNode(fptr.inode); } public static init( - config: FileSystemConfig, devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>, + config: FileSystemConfig = { maxNameLength: 255, maxPathLength: Infinity, maxSymlinkDepth: 40 }, ): FileSystem { if (devices.length === 0) { throw { @@ -81,21 +87,24 @@ export class FileSystem { } satisfies FileSystemInitializationError; } - return new FileSystem(config, devices); + return new FileSystem(devices, config); } public getRoot(): DirectoryNode { - const node = this.resolveNode([0, this._SysPartitionID]); - if (node === undefined || node.type !== TreeNodeType.Directory) - throw new Error("System partitions root inode did not resolve properly."); - return node as DirectoryNode; + const dev = this._VDiskManager.getDisk(0); + if (!dev) throw new Error("Kernel Panic - Filesystem corrupted, system device not found!"); + + return dev.getNode(0) as DirectoryNode; } - public getSysPartitionID(): number { - return this._SysPartitionID; + public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined { + const fptr = fromPtr(ptr); + const dev = this._VDiskManager.getDisk(fptr.device_id)!; + + return dev.getIndex().get(ptr); } - public getNode(ptr: NodePointer): TreeNode | undefined { + public getNode(ptr: NodePointer): Node | undefined { return this.resolveNode(ptr); } @@ -104,52 +113,89 @@ export class FileSystem { } public isFile(ptr: NodePointer): boolean { + const fptr = fromPtr(ptr); const node = this.resolveNode(ptr); if (node === undefined) { throw { type: "NodeNotFound", - message: `Node under inode ${ptr[0]} not found`, + message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`, timestamp: Date.now(), - inode: ptr[0], + inode: fptr.inode, } satisfies NodeNotFoundError; } - return node.type === TreeNodeType.File ? true : false; + return node.type === NodeType.File ? true : false; } public isDirectory(ptr: NodePointer): boolean { + const fptr = fromPtr(ptr); const node = this.resolveNode(ptr); if (node === undefined) { throw { type: "NodeNotFound", - message: `Node under inode ${ptr[0]} not found`, + message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`, timestamp: Date.now(), - inode: ptr[0], + inode: fptr.inode, } satisfies NodeNotFoundError; } - return node.type === TreeNodeType.Directory ? true : false; + return node.type === NodeType.Directory ? true : false; } public isSymlink(ptr: NodePointer): boolean { + const fptr = fromPtr(ptr); const node = this.resolveNode(ptr); if (node === undefined) { throw { type: "NodeNotFound", - message: `Node under inode ${ptr[0]} not found`, + message: `Node under inode ${fptr.inode} not found under device ${fptr.device_id}`, timestamp: Date.now(), - inode: ptr[0], + inode: fptr.inode, } satisfies NodeNotFoundError; } - return node.type === TreeNodeType.Symlink ? true : false; + return node.type === NodeType.Symlink ? true : false; } public moveNode(source: NodePointer, destination: NodePointer, name?: string): FileSystem { - if (source[0] === 0) { + const sptr = fromPtr(source); + const dptr = fromPtr(destination); + const sdev = this._VDiskManager.getDisk(sptr.device_id); + const ddev = this._VDiskManager.getDisk(dptr.device_id); + const sourceNode = this.resolveNode(source); + const destinationNode = this.resolveNode(destination); + + if (!sdev) { + throw { + type: "DeviceNotFound", + message: `Device under id ${sptr.device_id} not found`, + timestamp: Date.now(), + device_id: sptr.device_id, + } satisfies DeviceNotFoundError; + } + + if (!ddev) { + throw { + type: "DeviceNotFound", + message: `Device under id ${dptr.device_id} not found`, + timestamp: Date.now(), + device_id: dptr.device_id, + } satisfies DeviceNotFoundError; + } + + if (sourceNode === undefined) { + throw { + type: "NodeNotFound", + message: `Source node under inode ${sptr.inode} not found`, + timestamp: Date.now(), + inode: sptr.inode, + } satisfies NodeNotFoundError; + } + + if (sourceNode.inode === 0) { throw { type: "IllegalMutation", message: `Attempt at illegal mutation of node under inode 0 (root)`, @@ -157,43 +203,41 @@ export class FileSystem { } satisfies IllegalMutationError; } - const sourceNode = this.resolveNode(source); - const destinationNode = this.resolveNode(destination); - - if (sourceNode === undefined) { - throw { - type: "NodeNotFound", - message: `Source node under inode ${source[0]} not found`, - timestamp: Date.now(), - inode: source[0], - } satisfies NodeNotFoundError; - } - if (destinationNode === undefined) { throw { type: "NodeNotFound", - message: `Destination node under inode ${destinationInode} not found`, + message: `Destination node under inode ${dptr.inode} not found`, timestamp: Date.now(), - inode: destinationInode, + inode: dptr.inode, } satisfies NodeNotFoundError; } - if (destinationNode.type !== TreeNodeType.Directory) { + if (destinationNode.type !== NodeType.Directory) { throw { type: "NotADirectory", - message: `Destination node under inode ${destinationInode} is not a directory`, - inode: destinationInode, + message: `Destination node under inode ${dptr.inode} is not a directory`, + inode: dptr.inode, timestamp: Date.now(), nodeType: destinationNode.type, } satisfies NotADirectoryError; } - if (destinationNode.children[nodeName] !== undefined) { + const sdevIndex = sdev.getIndex(); + const s_entry = sdevIndex.get(source); + if (!s_entry || !s_entry[0]) throw new Error("File System corruption, reverse index is missing a field"); + + const pinode = s_entry[0].pinode; + const p_entry = sdevIndex.get(toPtr(sdev.ID, pinode)); + if (!p_entry || !p_entry[0]) throw new Error("File System corruption, reverse index is missing a field"); + + const nodeName = name ?? s_entry[0].name; + + if (destinationNode.children.get(nodeName) !== undefined) { throw { type: "AlreadyExists", - message: `A node inside the destination inode ${destinationNode} under the name ${nodeName} already exists`, + message: `A node inside the destination inode ${destinationNode.inode} under the name ${nodeName} already exists`, timestamp: Date.now(), - parentInode: destinationInode, + parentInode: destinationNode.inode, name: nodeName, } satisfies AlreadyExistsError; } @@ -208,8 +252,6 @@ export class FileSystem { } satisfies DirectoryLoopError; } - const nodeName = name ?? sourceNode; - let newNode: TreeNode; if (sourceNode.type === TreeNodeType.File) { diff --git a/src/core/util/tree_traversal.ts b/src/core/util/tree_traversal.ts index 7bbf113..51e4b14 100644 --- a/src/core/util/tree_traversal.ts +++ b/src/core/util/tree_traversal.ts @@ -1,5 +1,5 @@ import type { FileSystem } from "@core/filesystem/filesystem"; -import { NodeType, type Node } from "@core/filesystem/node"; +import { fromPtr, NodeType, toPtr, type NodePointer } from "@core/filesystem/node"; export function dnTraverseTree(fs: FileSystem, root: number): number[] { if (fs.getNode(root) === undefined) return []; @@ -24,19 +24,26 @@ export function dnTraverseTree(fs: FileSystem, root: number): number[] { return result; } -export function upTraverseTree(fs: FileSystem, inode: number): number[] { - if (fs.getNode(inode) === undefined) return []; +export function upTraverseTree(fs: FileSystem, ptr: NodePointer): NodePointer[] { + if (fs.getNode(ptr) === undefined) return []; - const result: number[] = []; - result.push(inode); + const fptr = fromPtr(ptr); + const result: NodePointer[] = []; + result.push(ptr); - let currentInode = inode; - while (currentInode !== 0) { - const node = fs.getNode(currentInode) as Node; - if (!node) break; + let currentInode = fptr.inode; + let currentDev = fptr.device_id; + while (currentInode !== 0 && currentDev !== 0) { + const ptr = toPtr(currentDev, currentInode); - result.push(node.parent); - currentInode = node.parent; + const rindex = fs.getNodeEntries(ptr)!; + if (!rindex || rindex.length === 0) throw new Error("Fuck..."); + + const pptr = toPtr(rindex[0].pdevice_id, rindex[0].pinode); + + result.push(pptr); + currentInode = rindex[0].pinode; + currentDev = rindex[0].pdevice_id; } return result; diff --git a/src/dev.ts b/src/dev.ts index 32670ad..09ff668 100644 --- a/src/dev.ts +++ b/src/dev.ts @@ -11,24 +11,43 @@ namespace Termos { */ //import { FileSystem } from "@core/filesystem/filesystem"; -import type { FileSystemConfig } from "@core/filesystem/filesystem"; +import { FileSystem } from "@core/filesystem/filesystem"; import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk"; +import type { GroupInfo, UserInfo } from "@core/session_manager"; +import { createInitialShellState, type ShellState } from "@core/shell_state"; +import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores"; //import { getCurrentSession, type GroupInfo, type UserInfo } from "@core/session_manager"; //import { createInitialShellState, type ShellState } from "@core/shell_state"; //import { resolveInodeToHome } from "@core/util/path_resolver"; import { deserializeFileSystem } from "@shell/serializer"; +import { Shell } from "@shell/shell"; //import { deserializeFileSystem } from "@shell/serializer"; //import { Shell } from "@shell/shell"; 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 base = deserializeFileSystem(rawJson); +const arr = deserializeFileSystem(rawJson); +const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = []; -const fsConf: FileSystemConfig = {}; -const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[]; +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 }); +} -const fs = FileSystem.init(base, overlay, base.size - 1); - -//console.log(printTree(fs, 0)); +const fs = FileSystem.init(devices); const users: Map = new Map(); const groups: Map = new Map();