import type { UserInfo } from "@core/session_manager"; import { upTraverseTree } from "@core/util/tree_traversal"; import { VDiskManager } from "@core/util/vdisk_manager"; import type { AlreadyExistsError, DeviceNotFoundError, DirectoryLoopError, FileSystemCorruptionError, FileSystemInitializationError, IllegalMutationError, InvalidNodeNameError, NodeNotFoundError, NotADirectoryError, NotAFileError, } from "./errors"; import { NodePointer, NodeType, type DirectoryNode, type FileNode, type Node, type NodeEntry } from "./node"; import type { IDataStore, IMetaStore, VDiskConfig } from "./vdisk"; export interface FileSystemConfig { maxPathLength: number; maxSymlinkDepth: number; maxNameLength: number; } export class FileSystem { private _Mounts: Map = new Map(); private _RMounts: Map = new Map(); private _VDiskManager: VDiskManager; readonly maxNameLength: number; readonly maxPathLength: number; readonly maxSymlinkDepth: number; private constructor(manager: VDiskManager, config: FileSystemConfig) { this.maxNameLength = config.maxNameLength; this.maxPathLength = config.maxPathLength; this.maxSymlinkDepth = config.maxSymlinkDepth; this._VDiskManager = manager; } private resolveNode(ptr: NodePointer): Node | undefined { const dev = this._VDiskManager.getDisk(ptr.device); if (!dev) return undefined; return dev.getNode(ptr.inode); } // Make async public static async init( devices: Array<{ dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }>, config: FileSystemConfig = { maxNameLength: 255, maxPathLength: Infinity, maxSymlinkDepth: 40 }, ): Promise { if (devices.length === 0) { throw { type: "FileSystemInitializationError", message: "File System tried to initialized without any devices", timestamp: Date.now(), } satisfies FileSystemInitializationError; } if (!devices.find(d => d.config.id === 0)) { throw { type: "FileSystemInitializationError", message: "File System failed to initialize because no device with id 0 was present", timestamp: Date.now(), } satisfies FileSystemInitializationError; } if (devices[0]!.config.base.get(0)?.type !== NodeType.Directory) { throw { type: "FileSystemInitializationError", message: "File System failed to initialize because root node is not a directory", timestamp: Date.now(), } satisfies FileSystemInitializationError; } const manager = new VDiskManager(); for (const dev of devices) { await manager.registerDisk(dev.config, dev.dstore, dev.mstore); } return new FileSystem(manager, config); } public getRoot(): DirectoryNode { const dev = this._VDiskManager.getDisk(0); if (!dev) { throw { type: "FileSystemCorruption", message: "System device not registered. File System corrupted!", timestamp: Date.now(), } satisfies FileSystemCorruptionError; } return dev.getNode(0) as DirectoryNode; } public getNodeEntries(ptr: NodePointer): readonly NodeEntry[] | undefined { const dev = this._VDiskManager.getDisk(ptr.device); if (!dev) { throw { type: "DeviceNotFound", message: `Device under id ${ptr.device} not found`, timestamp: Date.now(), device_id: ptr.device, } satisfies DeviceNotFoundError; } return dev.getIndex().get(ptr); } public getNode(ptr: NodePointer): Node | undefined { return this.resolveNode(ptr); } public exists(ptr: NodePointer): boolean { return this.resolveNode(ptr) !== undefined; } public async read(ptr: NodePointer): Promise { 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 { type: "DeviceNotFound", message: `Device under id ${mount.device} not found`, timestamp: Date.now(), device_id: mount.device, } satisfies DeviceNotFoundError; } if (!this._VDiskManager.hasDisk(target.device)) { throw { type: "DeviceNotFound", message: `Device under id ${target.device} not found`, timestamp: Date.now(), device_id: target.device, } satisfies DeviceNotFoundError; } if (!this.getNode(mount)) { throw { type: "NodeNotFound", message: `Node under inode ${mount.inode} not found under device ${mount.device}`, timestamp: Date.now(), pointer: mount, } satisfies NodeNotFoundError; } if (!this.getNode(target)) { throw { type: "NodeNotFound", message: `Node under inode ${target.inode} not found under device ${target.device}`, timestamp: Date.now(), pointer: target, } satisfies NodeNotFoundError; } this._Mounts.set(mount, target); this._RMounts.set(target, mount); } public isMountPoint(ptr: NodePointer): boolean { return this._Mounts.has(ptr); } public isMountTarget(target: NodePointer): boolean { return this._RMounts.has(target); } public getMountTarget(ptr: NodePointer): NodePointer | undefined { return this._Mounts.get(ptr); } public getMountPoint(target: NodePointer): NodePointer | undefined { return this._RMounts.get(target); } public isFile(ptr: NodePointer): boolean { const node = this.resolveNode(ptr); if (!this._VDiskManager.hasDisk(ptr.device)) { throw { type: "DeviceNotFound", message: `Device under id ${ptr.device} not found`, timestamp: Date.now(), device_id: ptr.device, } satisfies DeviceNotFoundError; } if (node === undefined) { throw { type: "NodeNotFound", message: `Node under inode ${ptr.inode} not found under device ${ptr.device}`, timestamp: Date.now(), pointer: ptr, } satisfies NodeNotFoundError; } return node.type === NodeType.File ? true : false; } public isDirectory(ptr: NodePointer): boolean { const node = this.resolveNode(ptr); if (!this._VDiskManager.hasDisk(ptr.device)) { throw { type: "DeviceNotFound", message: `Device under id ${ptr.device} not found`, timestamp: Date.now(), device_id: ptr.device, } satisfies DeviceNotFoundError; } if (node === undefined) { throw { type: "NodeNotFound", message: `Node under inode ${ptr.inode} not found under device ${ptr.device}`, timestamp: Date.now(), pointer: ptr, } satisfies NodeNotFoundError; } return node.type === NodeType.Directory ? true : false; } public isSymlink(ptr: NodePointer): boolean { const node = this.resolveNode(ptr); if (!this._VDiskManager.hasDisk(ptr.device)) { throw { type: "DeviceNotFound", message: `Device under id ${ptr.device} not found`, timestamp: Date.now(), device_id: ptr.device, } satisfies DeviceNotFoundError; } if (node === undefined) { throw { type: "NodeNotFound", message: `Node under inode ${ptr.inode} not found under device ${ptr.device}`, timestamp: Date.now(), pointer: ptr, } satisfies NodeNotFoundError; } return node.type === NodeType.Symlink ? true : false; } public async moveNode(source: NodePointer, destination: NodePointer, name?: string) { const sdev = this._VDiskManager.getDisk(source.device); const ddev = this._VDiskManager.getDisk(destination.device); const sourceNode = this.resolveNode(source); const destinationNode = this.resolveNode(destination); if (!sdev) { throw { type: "DeviceNotFound", message: `Device under id ${source.device} not found`, timestamp: Date.now(), device_id: source.device, } satisfies DeviceNotFoundError; } if (!ddev) { throw { type: "DeviceNotFound", message: `Device under id ${destination.device} not found`, timestamp: Date.now(), device_id: destination.device, } satisfies DeviceNotFoundError; } if (sourceNode === undefined) { throw { type: "NodeNotFound", message: `Source node under ${source} not found`, timestamp: Date.now(), pointer: source, } satisfies NodeNotFoundError; } if (sourceNode.inode === 0) { throw { type: "IllegalMutation", message: `Attempt at illegal mutation of node under inode 0 (root)`, timestamp: Date.now(), } satisfies IllegalMutationError; } if (destinationNode === undefined) { throw { type: "NodeNotFound", message: `Destination node under ${destination} not found`, timestamp: Date.now(), pointer: destination, } satisfies NodeNotFoundError; } if (destinationNode.type !== NodeType.Directory) { throw { type: "NotADirectory", message: `Destination node under inode ${destination.inode} is not a directory`, inode: destination.inode, timestamp: Date.now(), nodeType: destinationNode.type, } satisfies NotADirectoryError; } const sdevIndex = sdev.getIndex(); const s_entry = sdevIndex.get(source); if (!s_entry || !s_entry[0]) { throw { type: "FileSystemCorruption", message: "Reverse index entry missing, FileSystem corrupted", timestamp: Date.now(), } satisfies FileSystemCorruptionError; } const pinode = s_entry[0].pinode; const nodeName = name ?? s_entry[0].name; if (destinationNode.children.has(nodeName)) { throw { type: "AlreadyExists", message: `A node inside the destination inode ${destinationNode.inode} under the name ${nodeName} already exists`, timestamp: Date.now(), parentInode: destinationNode.inode, name: nodeName, } 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", message: "Cannot move node to a descendant of itself", timestamp: Date.now(), descendantInode: destinationNode.inode, ancestorInode: sourceNode.inode, } satisfies DirectoryLoopError; } if (source.device === destination.device) { await ddev.moveNodeEntry(pinode, destination.inode, s_entry[0].name, name); } else { console.error("cross device node movement not yet implemented"); } } public async createNode( parent: NodePointer, name: string, data: | { type: NodeType.File; mode: number; owner: number; group: number; content: Blob | ReadableStream; size: number; } | { type: NodeType.Directory; mode: number; owner: number; group: number } | { type: NodeType.Symlink; mode: number; owner: number; group: number; target: string }, ): Promise { const pdev = this._VDiskManager.getDisk(parent.device); const parentNode = this.resolveNode(parent); if (!pdev) { throw { type: "DeviceNotFound", message: `Device under id ${parent.device} not found`, timestamp: Date.now(), device_id: parent.device, } satisfies DeviceNotFoundError; } if (!parentNode) { throw { type: "NodeNotFound", message: `Destination node under ${parent} not found`, timestamp: Date.now(), pointer: parent, } satisfies NodeNotFoundError; } if (parentNode.type !== NodeType.Directory) { throw { type: "NotADirectory", message: `Destination node under inode ${parent.inode} is not a directory`, inode: parent.inode, timestamp: Date.now(), nodeType: parentNode.type, } satisfies NotADirectoryError; } if (parentNode.children.has(name)) { throw { type: "AlreadyExists", message: `A node inside the destination inode ${parentNode.inode} under the name ${name} already exists`, timestamp: Date.now(), parentInode: parentNode.inode, name: name, } satisfies AlreadyExistsError; } if (name.length > this.maxNameLength) { throw { type: "InvalidNodeName", message: `Invalid name for the new node`, timestamp: Date.now(), pointer: parent, attemptedName: name, } satisfies InvalidNodeNameError; } return pdev.createNodeEntry(parent.inode, name, data); } public async deleteNode(target: NodePointer) { const tdev = this._VDiskManager.getDisk(target.device); if (target.inode === 0) { throw { type: "IllegalMutation", message: `Attempt at illegal mutation of node under inode 0 (root)`, timestamp: Date.now(), } satisfies IllegalMutationError; } if (!tdev) { throw { type: "DeviceNotFound", message: `Device under id ${target.device} not found`, timestamp: Date.now(), device_id: target.device, } satisfies DeviceNotFoundError; } const tdevIndex = tdev.getIndex(); const t_entry = tdevIndex.get(target); if (!t_entry || !t_entry[0]) { throw { type: "FileSystemCorruption", message: "Reverse index entry missing, FileSystem corrupted", timestamp: Date.now(), } satisfies FileSystemCorruptionError; } await tdev.unlinkNodeEntry(t_entry[0].pinode, t_entry[0].name); } public async writeContent(target: NodePointer, content: Blob) { const dev = this._VDiskManager.getDisk(target.device); const node = this.resolveNode(target); if (!dev) { throw { type: "DeviceNotFound", message: `Device under id ${target.device} not found`, timestamp: Date.now(), device_id: target.device, } 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 || node.type === NodeType.Symlink) { throw { type: "NotAFile", message: `Node is not a file`, inode: target.inode, timestamp: Date.now(), nodeType: node.type, } satisfies NotAFileError; } await dev.writeContent((node as FileNode).content, content); } public streamContent(target: NodePointer): ReadableStream { const dev = this._VDiskManager.getDisk(target.device); const node = this.resolveNode(target); if (!dev) { throw { type: "DeviceNotFound", message: `Device under id ${target.device} not found`, timestamp: Date.now(), device_id: target.device, } 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: target.inode, timestamp: Date.now(), nodeType: node.type, } satisfies NotAFileError; } return dev.streamContent((node as FileNode).content); } public async modifyNode(target: NodePointer, data: { mode?: number; owner?: number; group?: number }) { const dev = this._VDiskManager.getDisk(target.device); const node = this.resolveNode(target); if (!dev) { throw { type: "DeviceNotFound", message: `Device under id ${target.device} not found`, timestamp: Date.now(), device_id: target.device, } 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 || node.type === NodeType.Symlink) { throw { type: "NotAFile", message: `Node is not a file`, inode: target.device, timestamp: Date.now(), nodeType: node.type, } satisfies NotAFileError; } await dev.writeNodeEntry(node.inode, data); } public canRead(ptr: NodePointer, user: UserInfo): boolean { const node = this.resolveNode(ptr); if (!node) { throw { type: "NodeNotFound", message: `Node under ${ptr} not found`, timestamp: Date.now(), pointer: ptr, } satisfies NodeNotFoundError; } if (user.uid === 0) return true; if (user.uid === node.owner) { return (node.mode & 0o400) !== 0; } else if (user.gid === node.group || user.group.includes(node.group)) { return (node.mode & 0o040) !== 0; } else { return (node.mode & 0o004) !== 0; } } public canWrite(ptr: NodePointer, user: UserInfo): boolean { const node = this.resolveNode(ptr); if (!node) { throw { type: "NodeNotFound", message: `Node under ${ptr} not found`, timestamp: Date.now(), pointer: ptr, } satisfies NodeNotFoundError; } if (user.uid === 0) return true; if (user.uid === node.owner) { return (node.mode & 0o200) !== 0; } else if (user.gid === node.group || user.group.includes(node.group)) { return (node.mode & 0o020) !== 0; } else { return (node.mode & 0o002) !== 0; } } public canExecute(ptr: NodePointer, user: UserInfo): boolean { const node = this.resolveNode(ptr); if (!node) { throw { type: "NodeNotFound", message: `Node under ${ptr} not found`, timestamp: Date.now(), pointer: ptr, } satisfies NodeNotFoundError; } if (user.uid === 0) return true; if (user.uid === node.owner) { return (node.mode & 0o100) !== 0; } else if (user.gid === node.group || user.group.includes(node.group)) { return (node.mode & 0o010) !== 0; } else { return (node.mode & 0o001) !== 0; } } }